0% found this document useful (0 votes)
431 views3 pages

Practical 10 Process Creation

This C program uses fork() to create child processes and print their process IDs. The parent process creates one child process, prints their IDs, then creates another child and prints the IDs. If fork() fails, it returns -1. Otherwise, the child process ID is 0 and the parent's is the PID of the child. The program demonstrates how fork() creates child processes and they can access their own and parent's process IDs.

Uploaded by

Vishal Birajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
431 views3 pages

Practical 10 Process Creation

This C program uses fork() to create child processes and print their process IDs. The parent process creates one child process, prints their IDs, then creates another child and prints the IDs. If fork() fails, it returns -1. Otherwise, the child process ID is 0 and the parent's is the PID of the child. The program demonstrates how fork() creates child processes and they can access their own and parent's process IDs.

Uploaded by

Vishal Birajdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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