Unix System Calls Fork Wait Exit
Unix System Calls Fork Wait Exit
Unix System Calls Fork Wait Exit
fork( )
wait( )
exit( )
1
Process Creation
Process Creation
A process ID number names each process.
A unique process ID is allocated to each process when it is
created.
The lifetime of a process ends when its termination is reported to
its parent process; at that time, all of the process resources,
including its process ID, are freed.
Processes are created with the fork() system call (so the operation
of creating a new process is sometimes called forking a process).
The child process created by fork is a copy of the original parent
process, except that it has its own process ID.
Underlying mechanism
- A process runs fork to create a child process
- Parent and children execute concurrently
- Child process is a duplicate of the parent process
parent
fork()
child
After a fork, both parent and child keep running, and each can fork
off other processes.
Bootstrapping
Returns -1 if unsuccessful
fork()
Stack
Stack
Data
Data
Text
Text
Data
Resources
Stack
PCB
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
File
UNIX
pid = 26
Data
Resources
Stack
PCB
ret = 26
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
Text
Data
Stack
PCB
File
UNIX
ret = 0
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
1
pid = 26
Data
Resources
Stack
PCB
ret = 26
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
Text
Data
Stack
PCB
File
UNIX
ret = 0
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
1
pid = 26
Data
Resources
Stack
PCB
ret = 26
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
Text
Data
Stack
PCB
File
UNIX
ret = 0
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
1
pid = 26
Data
Resources
Stack
PCB
ret = 26
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
Text
Data
Stack
PCB
File
UNIX
ret = 0
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
}
1
Data
Resources
Stack
Process Status
ret = 26
ret = fork();
switch(ret)
{
case -1:
perror(fork);
exit(1);
case 0: // I am the child
<code for child >
exit(0);
default: // I am parent ...
<code for parent >
wait(0);
<>
File
UNIX
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
void main()
{ int pid,n; //pid_t pid;
printf("Enter the number\n");
scanf("%d",&n);
pid=fork();
if(pid<0)
{ printf(Error);
exit(1);
}
else if(pid==0)
{
printf("child process:\n");
}else
{ wait(1);
printf("parent process:\n");
}
}
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
1
getppid
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
void main()
{ int pid,n; //pid_t pid;
printf("Enter the number\n");
scanf("%d",&n);
pid=fork();
printf("out pid:%d\n",pid);
if(pid<0)
{
printf("ERr");
exit(1);
}
2
Cont..
if(pid==0)
{
printf("child process: %d \n",++n);
printf("ch pid =%d\n",pid);
printf("ch:fn pid %d\n",getpid());
printf("ch:fn ppid%d\n",getppid());
exit(0);
}
else { wait(1);
printf("par: pid%d\n",getppid());
printf("par: pid var%d\n",pid);
printf("parent process:%d \n",n);
printf("par: pid%d\n",getpid());
}
}
Wait()
- for a child process to terminate or stop, and determine its status.
- will force a parent process to wait for a child process to stop or
terminate.
- return the pid of the child or -1 for an error.
Exit()
- exit() terminates the process which calls this function and returns
the exit status value.
- By convention, a status of 0 means normal termination.
- Any other value indicates an error or unusual occurrence.
Sleep ()
- A process may suspend for a period of time using the sleep
command
2
Summary
Fork
- Creates a duplicate of the calling process
- The result is two processes: parent and child
- Both continue executing from the same point on
Exit
- Orderly program termination
- Unblocks waiting parent
Wait
- Used by parent
- Waits for child to finish execution
2