0% found this document useful (0 votes)
7 views1 page

Process Creation in C with Fork

The document is a C program that demonstrates the use of the fork() system call to create a child process. It prints the process IDs of both the parent and child processes, and after a sleep period, it shows that the parent process has finished, resulting in a different parent process ID for the child. The program also handles the case where the fork() call fails.

Uploaded by

Zaid Instead
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Process Creation in C with Fork

The document is a C program that demonstrates the use of the fork() system call to create a child process. It prints the process IDs of both the parent and child processes, and after a sleep period, it shows that the parent process has finished, resulting in a different parent process ID for the child. The program also handles the case where the fork() call fails.

Uploaded by

Zaid Instead
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
// fork() Create a child process

int pid = fork();


if (pid > 0)
{
//getpid() returns process id
// while getppid() will return parent process id
printf("Parent process\n");
printf("ID : %d\n\n",getpid());
}
else if (pid == 0)
{
printf("Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n",getpid());
// getppid() will return parent process id of child process
printf("Parent -ID: %d\n\n",getppid());

sleep(10);

// At this time parent process has finished.


// So if u will check parent process id
// it will show different process id
printf("\nChild process \n");
printf("ID: %d\n",getpid());
printf("Parent -ID: %d\n",getppid());
}
else
{
printf("Failed to create child process");
}

return 0;
}

You might also like