0% found this document useful (0 votes)
72 views46 pages

Advanced Operating Advanced Operating Advanced Operating Advanced Operating Systems Systems Systems Systems

1. The document discusses process management in Unix/Linux operating systems using the fork() and exec() system calls. 2. fork() is used to create a new process that is a duplicate of the parent process. exec() replaces the current process with a new executable, keeping the same process ID. 3. Together, fork() and exec() allow new programs to be run within existing processes, providing the basis for things like shells and command interpreters.

Uploaded by

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

Advanced Operating Advanced Operating Advanced Operating Advanced Operating Systems Systems Systems Systems

1. The document discusses process management in Unix/Linux operating systems using the fork() and exec() system calls. 2. fork() is used to create a new process that is a duplicate of the parent process. exec() replaces the current process with a new executable, keeping the same process ID. 3. Together, fork() and exec() allow new programs to be run within existing processes, providing the basis for things like shells and command interpreters.

Uploaded by

Darshan Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Advanced Operating
Systems
(CS G623)
by

Dr. Mrs. Shubhangi Gawali


Dept. of CS and IS

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.

 fork() returns a zero to the newly created


child process.

 fork() returns a positive value, the process


ID of the child process, to the parent.

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.

◦ The new process is almost identical to the original, executing


the same code but with its own data space, environment and file
descriptor.

◦ Uses copy on write technique

◦ Kernel internally invokes do_fork() function (in fork.c)

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.

Calling one of the exec() family will terminate the


currently running program and starts executing a new
one which is specified in the parameters of exec in the
context of the existing process.

The process id is not changed.

 If the exec command fails, then it returns 1.


Using exec, an executable binary file (eg: a.out)
can be converted into a process.

An example of using exec is implementing a


shell program or a command interpreter.

A shell program takes user commands and


executes them.
 int execl(
execl const char *path, const char *arg, ...);
 int execlp(
execlp const char *file, const char *arg, ...);
 int execle(
execle const char *path, const char *arg ,
..., char * const envp[]);
 int execv(
execv const char *path, char *const argv[]);
 int execvp(
execvp const char *file, char *const argv[]);
The first difference in these functions is that the first
four take a pathname argument while the last two
take a filename argument. When a filename argument
is specified:

• if filename contains a slash, it is taken as a


pathname.
• otherwise the executable file is searched for in the
directories specified by the PATH environment
variable.
 execl-
execl- takes the path name of a binary
executable as its first argument, the rest of
the arguments are the command line
arguments ending with a NULL.

 Example: execl("./a.out", NULL)

32
 execv – takes the path name of a binary
executable as its first argument, and an array
of arguments as its second argument.

 Example: static char* args[] = {“ “, "cat.txt",


"test1.txt", NULL};

 execv("/bin/cp", args);

 The empty string “ “ can be replaced by the


command itself (eg “ls”) or leave it as “ “.
33
 execlp - same as execl except that we don’t
have to give the full path name of the
command.

 Example: execlp("ls", NULL)

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 pause / suspends the parent process until one of its child


processes is exited or until a signal is delivered whose action
is to terminate the parent or child process or to call a signal
handling function.

 It returns the PID of the child and the exit status gets placed
in status.

 To prevent the child becoming a zombie the parent should


call wait on its children, either periodically or upon receiving
the SIGCHLD signal, which indicates a child process has
terminated.

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.

 The call returns the PID of the child process (when


child process terminates) on success.

 The status information allows the parent process to


determine the exit status of the child process. i.e the
value returned from main or passed by exit.

 If status is not a null pointer, the status information will


be written to the location to which it points.
39
In waitpid() the value of pid can be
 < -1 which means to wait for any child process whose
process group ID is equal to the absolute value of pid.
 -1 which means to wait for any child process;
this is the same behavior which wait exhibits.
 0 which means to wait for any child process
whose process group ID is equal to that of the calling
process.
 > 0 which means to wait for the child whose
process ID is equal to the value of pid.

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

 The value of the status may be


EXIT_SUCCESS(0), EXIT_FAILURE(1) or any
other value that is available to a parent
process.

45
Process related Commands

 The process related system calls in UNIX


include fork( ), exec( ) [many variations of
this],
this] wait( ) and exit( ) system calls.
calls.

 Using exec, an executable binary file (eg:


(eg:
a.out) can be converted into a process.
process.

46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy