- Description
- Lecture 02: Linux Commands
- Lecture 03: IO System Calls
- Lecture 04: Process in OS
- References
This repository contains explanations, notes, and code examples from the Operating Systems Lab lectures. It covers key concepts such as process creation, process synchronization, system calls, and practical coding exercises. The repository is organized by lecture topics, with each section providing detailed explanations, example code, and execution instructions.
- Usage: Creates a new directory.
- Example:
mkdir mydir
- Create Multiple Directories:
mkdir dir1 dir2 dir3
- Usage: Creates nested directories (parent directories if they don’t exist).
- Example:
mkdir -p parent/child/grandchild
- Usage: Creates an empty file or updates the timestamp of an existing file.
- Example:
touch file.txt
- Usage: Writes text to a file.
- Overwrite File:
echo 'Hello' > file.txt
- Append to File:
echo 'World' >> file.txt
- Usage: Copies files or directories.
- Example:
cp file.txt copy.txt
- Copy Directory:
cp -r dir1 dir2
- Usage: Moves or renames files or directories.
- Example:
mv file.txt newfile.txt
- Usage: Removes files or directories.
- Example:
rm file.txt
- Remove Directory:
rm -r dir1
- Interactive Removal:
rm -ri dir1
- Usage: Removes an empty directory.
- Example:
rmdir emptydir
- Usage: Displays the contents of a file.
- Example:
cat file.txt
- Pipe with
tr
:cat file.txt | tr a-z A-Z > output.txt
- Usage: Counts lines, words, and characters in a file.
- Example:
wc file.txt
- Usage: Displays the first few lines of a file.
- Example:
head -3 file.txt
- Usage: Displays the last few lines of a file.
- Example:
tail -3 file.txt
- Usage: Views file content page by page.
- Example:
more file.txt
- Usage: Searches for a pattern in a file.
- Example:
grep 'hello' file.txt
- Usage: Opens the
vi
text editor. - Example:
vi file.txt
- Usage: Opens the
nano
text editor. - Example:
nano file.txt
- System Call:
creat()
- Usage: Creates a new file or truncates an existing file.
- Input:
filename
: Name of the file (e.g.,filename.txt
).mode
: File permissions (e.g.,S_READ | S_IWRITE
).
- Output:
fd
: File descriptor (a non-negative integer). Returns-1
if an error occurs.
Example Code:
#include <fcntl.h>
#include <sys/stat.h>
int main() {
int fd = creat("filename.txt", S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("Error creating file");
return 1;
}
printf("File created successfully with fd: %d\n", fd);
close(fd);
return 0;
}
- System Call:
open()
- Usage: Opens a file and returns a file descriptor.
- Input:
filename
: Name of the file (e.g.,filename.txt
).flags
: Mode of opening (e.g.,O_RDONLY | O_CREAT
).
- Output:
fd
: File descriptor (a non-negative integer). Returns-1
if an error occurs.
Example Code:
#include <fcntl.h>
int main() {
int fd = open("filename.txt", O_RDONLY | O_CREAT, 0644);
if (fd == -1) {
perror("Error opening file");
return 1;
}
printf("File opened successfully with fd: %d\n", fd);
close(fd);
return 0;
}
- System Call:
read()
- Usage: Reads data from an open file.
- Input:
fd
: File descriptor returned byopen()
.buffer
: Pointer to a buffer where the data will be stored.count
: Number of bytes to read.
- Output:
- Number of bytes read. Returns
-1
if an error occurs.
- Number of bytes read. Returns
Example Code:
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("filename.txt", O_RDONLY);
if (fd == -1) {
perror("Error opening file");
return 1;
}
char buffer[100];
ssize_t bytes_read = read(fd, buffer, 12);
if (bytes_read == -1) {
perror("Error reading file");
close(fd);
return 1;
}
printf("Read %zd bytes: %.*s\n", bytes_read, (int)bytes_read, buffer);
close(fd);
return 0;
}
- System Call:
write()
- Usage: Writes data to an open file.
- Input:
fd
: File descriptor returned byopen()
.buffer
: Pointer to the data to be written.count
: Number of bytes to write.
- Output:
- Number of bytes written. Returns
-1
if an error occurs.
- Number of bytes written. Returns
Example Code:
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("filename.txt", O_WRONLY | O_CREAT, 0644);
if (fd == -1) {
perror("Error opening file");
return 1;
}
const char *text = "Hello, World!";
ssize_t bytes_written = write(fd, text, 13);
if (bytes_written == -1) {
perror("Error writing to file");
close(fd);
return 1;
}
printf("Wrote %zd bytes\n", bytes_written);
close(fd);
return 0;
}
- System Call:
lseek()
- Usage: Moves the file pointer to a specific position.
- Input:
fd
: File descriptor returned byopen()
.offset
: Number of bytes to move.whence
: Starting point (SEEK_SET
,SEEK_CUR
, orSEEK_END
).
- Output:
- New file pointer position. Returns
-1
if an error occurs.
- New file pointer position. Returns
Example Code:
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("filename.txt", O_RDWR);
if (fd == -1) {
perror("Error opening file");
return 1;
}
off_t offset = lseek(fd, 0L, SEEK_SET);
if (offset == -1) {
perror("Error seeking file");
close(fd);
return 1;
}
printf("File pointer moved to offset: %ld\n", offset);
close(fd);
return 0;
}
- System Call:
stat()
- Usage: Retrieves information about a file.
- Input:
filename
: Name of the file (e.g.,file.txt
).stat_buff
: Pointer to astruct stat
object.
- Output:
0
on success,-1
on error.
Example Code:
#include <sys/stat.h>
#include <stdio.h>
int main() {
struct stat stat_buff;
if (stat("file.txt", &stat_buff) == -1) {
perror("Error getting file status");
return 1;
}
printf("File size: %ld bytes\n", stat_buff.st_size);
return 0;
}
- System Calls:
opendir()
,readdir()
,closedir()
- Usage: Opens, reads, and closes a directory.
- Input:
dirname
: Name of the directory.
- Output:
- Directory stream (
DIR *
).
- Directory stream (
Example Code:
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
int main() {
char dirname[100];
printf("Enter Directory Name: ");
scanf("%s", dirname);
DIR *dirp = opendir(dirname);
if (dirp == NULL) {
perror("Error opening directory");
return 1;
}
struct dirent *dptr;
while ((dptr = readdir(dirp)) != NULL) {
printf("%s\n", dptr->d_name);
}
closedir(dirp);
return 0;
}
- System Call:
fork()
- Usage: Creates a new process by duplicating the parent process.
- Output:
0
in the child process.> 0
(child's PID) in the parent process.-1
if an error occurs.
Example Code:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
printf("Child process (PID: %d)\n", getpid());
} else {
printf("Parent process (PID: %d, Child PID: %d)\n", getpid(), pid);
}
return 0;
}
- System Calls:
getpid()
,getppid()
- Usage: Retrieves the process ID of the current process and its parent.
- Output:
getpid()
: Current process ID.getppid()
: Parent process ID.
Example Code:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Current PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());
return 0;
}
- System Call:
exec()
- Usage: Replaces the current process image with a new one.
- Input:
path
: Path to the executable.args
: Command-line arguments.
- Output:
- Does not return on success. Returns
-1
if an error occurs.
- Does not return on success. Returns
Example Code:
#include <stdio.h>
#include <unistd.h>
int main() {
execl("/bin/ls", "ls", "-l", NULL);
perror("Exec failed");
return 1;
}
- System Call:
system()
- Usage: Executes a shell command.
- Input:
command
: Shell command (e.g.,"pwd"
).
- Output:
- Returns the exit status of the command.
Example Code:
#include <stdlib.h>
int main() {
int status = system("pwd");
if (status == -1) {
perror("System command failed");
return 1;
}
return 0;
}
-
Instructor's Lectures:
- Lecture #02: Linux Commands
- Lecture #03: IO System Calls
- Lecture #04: Process in OS
- These lectures are the primary source of the content and examples provided in this repository.
-
External Resources:
- Linux Programmer's Manual: Official documentation for Linux system calls and functions.
- Advanced Programming in the UNIX Environment by W. Richard Stevens: A comprehensive book on UNIX system programming.
- GNU C Library (glibc) Documentation: Official documentation for the GNU C Library.
- The Linux Kernel Documentation: Documentation for the Linux kernel, including system calls and process management.