SYSTEM CALLS
ASSIGNMENT 4
1. Write a program to implement fork () system call.
The fork system call is used to create a new process, or child process, by duplicating an existing
process, or parent process, in Unix-based operating systems. The new process is an exact copy of the
parent process, with its own memory and address space.
Here are some things to know about the fork system call:
Return values: The fork system call returns a value to indicate the success or failure of the
operation:
o Negative value: The child process creation was unsuccessful
o Zero: A new child process was created
o Positive value: The process ID of the child process
#include <stdio.h> #used for input and output function like printf scanf etc
#include <sys/types.h> #used for the declaration of fork
#include <unistd.h> #defines data-types used in system calls
int main() { #its entry point of the program
int pid = fork(); #fork() system call is used to create a new process
if (pid < 0) {
printf("Fork failed.\n");
} else if (pid == 0) {
printf("In Child process");
} else {
printf("In Parent process");
}
}
2. Write a program to implement wait () and exit () System Calls
The wait system call is used by a parent process to wait for the termination of a child process and
retrieve its exit status.
The exit system call is used by a process to terminate its execution and return an exit status to its
parent process.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main () {
int pid = fork(); #fork() creates a new child process.
if (pid < 0) {
printf("Fork failed.\n");
} else if (pid == 0) {
printf("Child process, PID: %d\n", getpid());
printf("Child is exiting.\n");
exit(0);
} else {
printf("Parent process, Child PID: %d\n", pid);
printf("Parent is waiting for the child to exit...\n");
wait(NULL); #waits for child process to terminate.
printf("Parent's wait is done.\n"); } }
3. Write a program to implement execv() system call
The execv system call is used to execute a new program in the current process, replacing the current
program with a new one.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> #For process waiting functions
int main() {
pid_t pid; #Declare a variable to hold the process ID
pid = fork(); #Create a new process
if (pid < 0) {
perror("fork"); # Print an error message if fork fails
exit(1); #Exit the program with an error code
} else if (pid == 0) { #this block is executed by child process
char *argv[] = {"/bin/ls", "-l", NULL}; #Defines an array of strings argv to pass as arguments to
the execv() system call. The first element is the path to the ls command, the second element is the -
l option, and the third element is a null pointer to indicate the end of the argument list.
execv(argv[0], argv); #Replace the child process with the 'ls -l' command
perror("execv"); #Print an error message if execv fails
exit(1);
} else { #this block is executed by parent process
int status; #Variable to hold the exit status of the child
waitpid(pid, &status, 0); #Wait for the child process to finish
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
#Extracts the exit status of a child process from the status argument.
}
return 0; #successful completion.
}
4. Write a program to implement the system calls open (), read (), write
() & close ().
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/watt.h>
#include<unistd.h>
int main(){
int fd;
char buffer [80];
static char message[]="hello";
fd=open("[Link]", O_RDWR | O_CREAT);
if(fd!=-1){
printf("[Link] opened with read/write access\n");
write(fd, message, sizeof(message));
lseek(fd,0,SEEK_SET);
read(fd, buffer, sizeof(message));
printf("%s was written to [Link]\n", buffer);
close(fd);}
return 0; }