Computer Systems II: Creating and Executing Processes
Computer Systems II: Creating and Executing 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
Process Creation
After a fork, both parent and child keep running, and each can fork off other processes. A process tree results. The root of the tree is a special process created by the OS during startup.
A process can choose h t wait to it for f children to terminate. For example, if C issued a wait() system call, it would block until G finished.
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)
Data Text
ret = xxx
Data Text
E Execution ti proceeds d concurrently tl with ith the th instruction i t ti following f ll i 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
PCB
File
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); }
UNIX
PCB
PCB
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); } 9
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); }
UNIX
PCB
PCB
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); } 10
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); }
UNIX
PCB
PCB
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); } 11
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); }
UNIX
PCB
PCB
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); } 12
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); }
UNIX
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); <>
UNIX
13
14
16
getppid
getpid returns the identifier of the calling process. Example call (pid is an integer): pid = getpid();
Fork Example 2
Key Points
- Both parent and child can continue forking
L0
L1
19
Fork Example 3
L2
L1
L2 L2
L0
L1
L2
20
Fork Example 4
void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); printf( L2\n ); fork(); } } printf("Bye\n"); }
21
Fork Example 5
void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } } printf("Bye\n"); }
22
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
23
24
Unixs execv
The system call execv executes a file, transforming the calling process into a new process. After a successful execv, there is no return to the calling process.
execv(const char * path, char * const argv[])
path is the full path for the file to be executed argv is the array of arguments for the program to execute
each argument is a null-terminated string the first argument is the name of the program the last entry in argv is NULL
25
PCB
char * argv[ ] = { {/bin/ls, /bin/ls , 0}; <> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid);
PCB
char * argv[ ] = { {/bin/ls, /bin/ls , 0}; <> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid);
cpid = 26
cpid = 0
/bin/ls
UNIX kernel
26
PCB
File
char * argv[ ] = {/bin/ls { /bin/ls , 0}; <> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid);
cpid = 26
Exec destroys the process image of the calling process. A new process image is constructed from the executable file (ls).
UNIX kernel
27
/bin/ls
PCB
PCB
char * argv[ ] = {/bin/ls { /bin/ls , 0}; <> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid);
cpid = 26
/bin/ls
UNIX kernel
28
wait
execv Example
#include <stdio.h> #include <unistd.h> char * argv[] = {/bin/ls, -l, 0}; int main() { int pid, status;
if ( (pid = fork() ) < 0 ) { printf(Fork error \n); exit(1); } if(pid == 0) { /* Child executes here */ execv(argv[0], argv); printf(Exec error \n); exit(1); } else /* Parent executes here */ wait(&status); printf("Hello there! \n); return 0; }
30
10
Sample output:
total 282 drwxr-xr-x 2 mdamian faculty 512 Jan 29 14:02 assignments -rw-r--r-- 1 mdamian faculty 3404 Jan 29 14:05 index.html y 512 Jan 28 15:02 notes drwxr-xr-x 2 mdamian faculty Hello there!
31
execl
Same as execv, but takes the arguments of the new program as a list, not a vector: Example:
execl(/bin/ls, /bin/ls, -l, 0);
Is equivalent to
11
Combined fork/exec/wait
Example
vfork()
- a system call that creates a process without creating an identical memory image - sometimes called lightweight fork - child process is understood to call execv() almost immediately
35
Variations of execv
execv
- Program arguments passed as an array of strings
execvp
- Extension of execv - Searches for the program name in the PATH environment
execl
- Program arguments passed directly as a list
execlp
- Extension of execv - Searches for the program name in the PATH environment
36
12
Summary
exec(v,vp,l,lp)
Does NOT create a new process Loads a new program in the image of the calling process The first argument is the program name (or full path) Program arguments are passed as a vector (v,vp) or list (l,lp) Commonly called by a forked child
system:
- combines fork, wait, and exec all in one
37
13