0% found this document useful (0 votes)
65 views

Fork

The fork() system call creates a new process by duplicating the calling process. The child process is an exact copy of the parent process except that it has a unique process ID and parent process ID. fork() returns a process ID to the parent - the child process ID is 0 while the parent receives the child's process ID. The child and parent can then execute independently and the parent can wait for the child to exit using wait() or waitpid().

Uploaded by

PathfinderSrb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Fork

The fork() system call creates a new process by duplicating the calling process. The child process is an exact copy of the parent process except that it has a unique process ID and parent process ID. fork() returns a process ID to the parent - the child process ID is 0 while the parent receives the child's process ID. The child and parent can then execute independently and the parent can wait for the child to exit using wait() or waitpid().

Uploaded by

PathfinderSrb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

fork()

The fork() system call causes the creation of a new process. The new process
(the child process) is an exact copy of the calling process (the parent process).
SYNOPSIS
#include<unistd.h>
pid_tfork(void);
DESCRIPTION
Thechildprocessinheritsthefollowingattributesfromthe
parentprocess...(man)
Thechildprocessdiffersfromtheparentprocessinthe
followingways:
ThechildprocesshasauniqueprocessID.
ThechildprocessIDdoesnotmatchanyactiveprocessgroupID.
ThechildprocesshasadifferentparentprocessID(whichisthe
processIDoftheparentprocess).
Thesetofsignalspendingforthechildprocessisinitialized
totheemptyset.

fork()
pid_t is the generic process type. Under Unix, this is a short.
fork() can only return three things:
0 for a child process
1 no child was created
else PID of your child is returned to the parent

Child

Parent

exit()
When the child calls
exit(), the return value
passed will arrive at the
parent when it wait()s

wait()it waits for whichever one


happens to exit first
waitpid() specify exactly
which child to wait

fork()

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
intmain(void)
{
pid_tpid;
intrv;
switch(pid=fork()){
case1:
perror("fork");/*somethingwentwrong*/
exit(1);/*parentexits*/
case0:
printf("CHILD:Thisisthechildprocess!\n");
printf("CHILD:MyPIDis%d\n",getpid());
printf("CHILD:Myparent'sPIDis%d\n",getppid());
printf("CHILD:Entermyexitstatus(makeitsmall):");
scanf("%d",&rv);
printf("CHILD:I'mouttahere!\n");
exit(rv);

fork()
default:
printf("PARENT:Thisistheparentprocess!\n");
printf("PARENT:MyPIDis%d\n",getpid());
printf("PARENT:Mychild'sPIDis%d\n",pid);
printf("PARENT:I'mnowwaitingformychildtoexit()...\n");
wait(&rv);
printf("PARENT:Mychild'sexitstatusis:%d\n",WEXITSTATUS(rv));
printf("PARENT:I'mouttahere!\n");
}
return0;
}

fork()
if(!fork()){
printf("I'mthechild!\n");
exit(0);
}else{
printf("I'mtheparent!\n");
wait(NULL);
}
main()
{
signal(SIGCHLD,SIG_IGN);/*nowIdon'thavetowait()!*/
.
.
fork();fork();fork();/*Rabbits,rabbits,rabbits!*/
}

You might also like