Advanced Operating Advanced Operating Advanced Operating Advanced Operating Systems Systems Systems Systems
Advanced Operating Advanced Operating Advanced Operating Advanced Operating Systems Systems Systems Systems
Advanced Operating
Systems
(CS G623)
by
8/16/2019 1
2
Parent process create children processes,
which, in turn create other processes,
forming a tree of processes
Generally, process identified and managed
via a process identifier (pid)
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
3
Address space
◦ Child duplicate of parent
◦ Child has a program loaded into it
UNIX examples
◦ fork system call creates new process
◦ exec system call used after a fork to replace the
process’ memory space with a new program
4
5
If fork() returns a negative value, the creation
of a child process was unsuccessful.
6
7
8
#include <stdio.h> void ChildProcess(void) void ParentProcess(void)
#include <sys/types.h> { {
void ChildProcess(void); int i; int i;
void ParentProcess(void);
for (i =1; i<= 10; i++) for (i=1; i<= 10; i++)
void main(void)
{ printf(“child, i=%d\n", i); printf(“parent, i= %d\n",
pid_t pid; printf(“child done \n"); i);
pid = fork(); } printf(“parent done\n");
if (pid == 0) }
ChildProcess();
else
ParentProcess();
}
9
10
11
12
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
int main(){
pid_t pid;
printf(“fork program starting \n”);
pid = fork();
if (pid < 0 ){ perror(“fork failed\n”); exit(1); }
else if (pid == 0){
printf(“ This is from child process My PID is %d and my Parent PID is
%d\n”,getpid(),getppid()); }
else { printf(“This is from parent process My PID is %d and my Child’s PID
is %d\n”,getpid(),pid); wait(NULL); } // common to parent and child
return 0;
}
13
Each process is allowed to have a unique number named process
identifier or PID
◦ usually between 2 and 32767 (/proc/sys/kernel/pid_max)
◦ 64 bit maximum PID number up to 4194303
Next unused number is chosen as process ID
Once upper limit of 32767 is reached then the numbers restart at
2 so that they wrap around.
PID 1 is init process, Process 0 is swapper or sched (Process 0 is
responsible for paging)
Process has its own code, data, stack, environment space,
program counter etc.
Process table contains information about all the processes that are
currently loaded with
14
fork
pid_t fork(void)
◦ Duplicates the current process, creating a new entry in the
process table with many of the same attributes as the parent
process.
15
The fork ( ) system call does the following in a UNIX
system
◦ Allocates slot in the process table for the new process
◦ Assigns a unique process id to the new process
◦ Make a copy of the process image of the parent, with the
exception of shared memory
◦ Increases counters for any files owned by the parent, to reflect
that an additional process now also owns these files.
◦ Assigns the child process a ready state
◦ Returns the Process ID number (PID) of the child to the parent
process and a 0 value to the child process.
16
All this work is done in Kernel space of parent process
After completing these functions, OS will do the following
operations as a part of dispatcher routine
Control returns to the user mode at the point of the fork
call of the parent
Transfer control to the child process. The child process
begins executing at the same point in the code as the
parent, namely at the return from the fork call
Transfer control to another process. Both child and parent
are left in the ready state
If fork system call fails, the return value to parent (no child
will be created) will be -1
17
#include <stdio.h> P1
#include <unistd.h>
#include <errno.h>
P2 P3 p4
#include <sys/types.h>
int main()
{
if(fork())
if(fork())
fork();
return 0;
}
18
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h> P1
int main()
{ P2 P3
printf(“fork program starting \n”);
if( fork( )) p
if(!fork( ) ) 4
fork( );
return 0;
}
19
#include <stdio.h> P1
#include <unistd.h>
#include <errno.h>
P2
#include <sys/types.h>
int main()
{ P3
if(!fork())
if(!fork())
fork(); p
4
return 0;
}
20
int x=0;
P1 X = 10
int main()
{ X = 10
for(i=0;i<2;i++)
{ P2 P3
fork();
x=x+5; X = 10
p4
} X = 10
return 0;
}
Number of new processes created is 3
Final value of x in P1 is 10
Final value of x in P2 is 10
Final value of x in P3 is 10
Final value of x in P4 is 10
21
The Unix process management model is split
into two distinct operations :
1. The creation of a process.
2. The running of a new program.
The creation of a new process is done using
the fork() system call.
A new program is run using the
exec(l,lp,le,v,vp)
exec family of system calls.
These are two separate functions which may
be used independently.
1. A call to fork() will create a completely
separate sub-process which will be exactly
the same as the parent.
2. The process that initiates the call to fork is
called the parent process.
3. The new process created by fork is called the
child process.
4. The child gets a copy of the parent's text and
memory space.
5. They do not share the same memory .
fork() system call returns an integer to both the
parent and child processes:
-1 this indicates an error with no child
process created.
A value of zero indicates that the child
process code is being executed.
Positive integers represent the child’s process
identifier (PID) and the code being executed is
in the parent’s process.
if ( (pid = fork()) == 0)
printf(“I am the child\n”);
else
printf(“I am the parent\n”);
Process 0 is created by the system boot code.
All other processes are created by the fork system
call.
After a fork, both parent and child processes
resume execution at the return of the fork
function.
The child process contains copies of the parent's
text, data, and stack segments. The child process
has file descriptors that contains references to the
same opened files as its parent, so they both share
the same file pointer to each opened file.
27
When the child process calls exec(), all data in the
original program is lost, and it is replaced with a
running copy of the new program.
32
execv – takes the path name of a binary
executable as its first argument, and an array
of arguments as its second argument.
execv("/bin/cp", args);
34
35
36
When a program wants to have another
program running in parallel, it will typically
first use fork,
fork then the child process will use
exec to actually run the desired program.
wait, waitpid - wait for a child process to stop or terminate
#include <sys/wait
<sys/wait.
wait.h>
pid_t wait(int
wait(int *status);
*status);
pid_t waitpid(
waitpid(pid_t pid,
pid, int *status, int options);
options);
It returns the PID of the child and the exit status gets placed
in status.
38
If a child has already exited by the time of the call, the
function returns immediately and system resources
used by the child are freed.
40
int main() {
int child_status, pid, pidwait;
if ((pid = fork()) == 0) {
printf(“This is the child!\n”);
}
else {
pidwait = wait(&child_status);
printf(“child %d has terminated\n”, pidwait);
}
return 0;
}
41
else if (pid == 0) {
#include <stdio.h>
printf(“ This is from child
#include <unistd.h>
process I am exiting\n”);
#include <errno.h>
exit(2);
#include <sys/types.h>
}
#include <sys/wait.h>
else {
int main()
printf(“This is from parent
{ process\n”);
pid_t pid; }
int status; wait(&status);
printf(“fork program starting \n”); // waitpid(pid, &status,0);
pid = fork(); // Child is no more and this part is
if (pid < 0 ){ only for Parent
perror(“fork failed\n”); printf(“Child exited already now
exit(1); with exit status %d\n”,status);
} return 0;
}
42
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{ printf("fork program starting with pid %d\n",getpid());
fork();
fork();
printf("My PID=%d My PPID = %d\n",getpid(),getppid());
wait(NULL);
wait(NULL);
return 0;
}
16 August 2019 43
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{ int i;
printf("fork program starting with pid %d\n",getpid());
for(i=0;i<2;i++)
fork();
printf("My PID=%d My PPID = %d\n",getpid(),getppid());
while(wait(NULL)!= -1);
return 0;
}
16 August 2019 44
System function exit allows termination of a
process.
Prototype :
#include <stdlib.h>
void exit(int status);
45
Process related Commands
46