You are on page 1of 3

LAB ASSIGNMENT- 3

SYSTEM CALLS IN LINUX USING FORK

NAME: SAHANA R
REG NO. : 22BPS1110
LAB SLOT: L23+24

AIM:

To understand how to create processes and to call processes using fork() System Call
and to verify the output.

Source Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid;
// Create a new process
pid = fork();
if (pid < 0) {
// Fork failed
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
// Child process
execl("/bin/ls", "ls", "-l", (char *)NULL);
} else {
// Parent process
wait(NULL);
printf("Child process completed\n");
}
return 0;
}
Output:

Result and Explanation of Output:

This program forks a new process and, in the child, executes the 'ls -l' command using
execl. The parent waits for the child to complete and prints a message. The output
typically displays the long format listing of the current directory, followed by the "Child
process completed" message in the parent process.

Source Code:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
//main function begins
int main(){
pid_t p= fork(); //calling of fork system call
int x=3;
if(p==0)
printf("In the child, the value of x is %d\n", ++x);
else
printf("In the parent, the value of x is %d\n", - - x);
return 0;
}
Output:

Result and Explanation of Output:

This program creates a child process using fork(). In the child, x is incremented and
printed. In the parent, x is decremented and printed. The output may vary depending on
the scheduling of the processes, but it generally shows interleaved messages from the
parent and child, reflecting the non-deterministic nature of process execution.

You might also like