Nothing Special   »   [go: up one dir, main page]

Unix System Calls Fork Wait Exit

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Unix system calls

fork( )
wait( )
exit( )
1

Process Creation

Processes are the primitive units for allocation of system


resources.
Each process has its own address space and (usually) one thread
of control.
A process executes a program; you can have multiple processes
executing the same program, but each process has its own copy of
the program within its own address space and executes it
independently of the other copies.
Processes are organized hierarchically. Each process has a
parent process, which explicitly arranged to create it.
The processes created by a given parent are called its child
processes.
A child inherits many of its attributes from the parent process.
2

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.

How To Create New Processes?

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

When a computer is switched on or reset, there must be an


initial program that gets the system running
This is the bootstrap program
- Initialize CPU registers, device controllers, memory
- Load the OS into memory
- Start the OS running

OS starts the first process (such as init)

OS waits for some event to occur


- Hardware interrupts or software interrupts (traps)

Fork System Call

Current process split into 2 processes: parent, child

Returns -1 if unsuccessful

Returns 0 in the child

Returns the childs


identifier in the parent

fork()

Stack

Stack

Data

Data

Text

Text

Fork System Call

The child process inherits from parent


- identical copy of memory
- CPU registers
- all files that have been opened by the parent

Execution proceeds concurrently with the instruction following


the fork system call
The execution context (PCB) for the child process is a copy of the
parents context at the time of the call

Process Description: Process Control Block (PCB)

Information associated with each process


Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
A process is named using its process ID (PID)
Data is stored in a process control block (PCB)

How fork Works (1)


pid = 25
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

How fork Works (2)


pid = 25
Text

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

How fork Works (3)


pid = 25
Text

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

How fork Works (4)


pid = 25
Text

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

How fork Works (5)


pid = 25
Text

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

How fork Works (6)


pid = 25
Text

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");
}
}

Orderly Termination: exit()

To finish execution, a child may call exit(number)

This system call:


-

Saves result = argument of exit


Closes all open files, connections
Deallocates memory
Checks if parent is alive
If parent is alive, holds the result value until the parent requests
it (with wait); in this case, the child process does not really die,
but it enters a zombie/defunct state
- If parent is not alive, the child terminates (dies)

Waiting for the Child to Finish

Parent may want to wait for children to finish


- Example: a shell waiting for operations to complete

Waiting for any some child to terminate: wait()


- Blocks until some child terminates
- Returns the process ID of the child process
- Or returns -1 if no children exist (i.e., already exited)

#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
1

State Transition on wait and exit Calls

Other useful system calls: getpid,

getppid

getpid returns the identifier of the calling process. Example


call (pid is an integer):
pid = getpid();

getppid returns the identifier of the parent.

#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(), Exit() & Sleep()

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

You might also like