You are on page 1of 3

Practical : 10

AIM:
To write a C program to perform process creation using fork() system call .
OPERATING SYSTEM (OS) LAB IN LINUX ENVIRONMENT
ALGORITHM:·
Start program.
· Assign fork() system call to pid.
· if pid is equal to -1, child process not created.
· if pid is equal to 0, child process will be created.
· Print the id of parent process and child process.
· Create another one child process in same loop.
· Print id of parent process and the child process.
· Print grand parent id.
· Stop the program.

Code :
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();
if (pid < 0) {
perror("fork() failure\n");
return 1;
}

// Child process
if (pid == 0) {
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
} else { // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}

Output:
Conclusion: In this way ,We studied about parent and child process creation.

You might also like