CE 466
System Programming
Process Management
Computer Engineering Department
Yarmouk University
10/19/2008
Objectives
• Understand the UNIX process model
• Learn how to create a process
• Experiment with fork and exec
• Discuss the implications of process inheritance
• Use wait for cleanup
1
Processes in UNIX
• A process is a program in execution
• It is the basic active entity in an OS
• The compiler:
¾Translates the source file into object modules (OM)
¾Links the different OM with the necessary libraries
to produce executable module (EM)
Processes in UNIX
• Process creation:
¾OS copies the EM into a program image in memory
¾OS allocates proper resources and adds appropriate
info to the kernel data structures.
A process has an ID and a state (parts of a PCB)
• A process has:
¾Address space
¾At least one flow of control
¾A program counter
2
Process States
state meaning
new being created
running instructions are being executed
blocked waiting for an event such as I/O
ready waiting to be assigned to a
processor
done finished
Waiting
Program image
3
Process Termination
• On termination (normal or abnormal) the OS:
¾ Deallocates process resources (VM, locks, files,..)
¾ Updates appropriate statistics
¾ Notifies other processes (parent)
• A process does not completely releases its resources
until the parent waits for it.
• If no parent waits for the child process:
¾ Process is zombie (inactive process whose resources are
deleted later)
¾ Process is adapted by a special process (init process id=1)
• If a parent process terminates without waiting for a
child, the child becomes an orphan.
Process Manipulation
• A process is identified by its ID
• A new process can be created using the system
call fork()
• Fork() returns two processes:
¾Parent (original)
¾Child (new)
• fork() makes a copy of the parent address space
• Both processes continue execution after fork()
statement
4
Process Creation
#include <unistd.h>
pid_t fork(void);// pid_t: unsigned int
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int x;
x = 0;
fork();
x++;
Printf("I am process %d and my x value is %d\n“,
getpid(), x);
return 0;
}
Process Creation
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int x;
PC x = 0;
PC fork(); #include <stdio.h>
PC x++; #include <unistd.h>
Printf("I am process %d and my x int main(void)
{
value is %d\n“, getpid(), x);
int x;
return 0; x = 0;
} fork();
PC x++;
PC Printf("I am process %d and my x
value is %d\n“, getpid(), x);
return 0;
}
Time
5
Process Creation ???
• Because both may perform different tasks, we
should be able to identify them
• The return value is:
¾Child’s process ID (in parent process)
¾Zero (in the child process)
• Child and parent processes execute different
parts of the same program
Forking a Child Process
#include <stdio.h>
#include <stdlib.h>
main() {
int i;
printf("before forking.\n");
if (fork()){
for (i=0;i<100;i++) {
printf(“ \t \t \t Parent process %d \n", i );
}
}
else{
for (i=0;i<100;i++) {
printf(“Child process %d \n", i );
}
}
}
6
Basic Control (wait(), exit())
• exit(exit_status)
¾Used to terminate calling process.
exit_status = 0 (success)
exit_status !=0 (failure)
• Closes all of the of its opened file descriptors,
directory streams,….
• If the parent is waiting for a child, it gets
notified and its exit status is made available to
the parent.
Basic Control (wait(), exit())
• wait(&status)
¾ Allows the parent process to suspend its activities until one
of its child processes either stopped or terminated.
¾ Returns
Child’s exit status to the parent process
Process ID of child whose termination caused the wait() to wake
up.
¾ Returns immediately with a value of -1, if the calling
process does not have any children associated with it.
¾ Check the man pages for wait(), waitid(),
waitpid(), wait3()
7
Synchronization between parent and child
wait(0) exit()
• Used by the parent • Used by the child
to wait for the child upon completion
to die
Concurrent Execution of Parent and Child
Shell
Parent fork() Child
exec()
wait()
exit
continue
8
Child Execute a Separate Program
• Use exec() system call:
¾Causes the process to replace its execution image
with that of a new program.
¾Context of the old program is lost
Code space, data space, stack, heap
¾Never returns except on failure.
¾Retains process ID, parent, children, and open file
descriptors.
exec()
• Six different flavors, based on:
¾Program path name (relative or absolute)
¾Environment variables (inherited or explicit)
¾Command line arguments (explicit or vector)
• Easiest :
¾ execlp(char *filename, char *arg0, char arg1,..... ,
char *argn, (char *) 0);
¾ e.g. execlp( “sort”, “sort”, “-n”, “foo”, 0);
9
exec() Example
arg[1] arg[2]
execlp(“sort”, ”sort”, ”-n”, “foo”, 0);
mark end of list
What the file to receive as its arguments
The program will receive as arg[0]
Name of file to execute,
Execute the sort program with a passing arguments ‘-n’, ‘foo’.
exec() Flavors
• int execl(char *pathname, char *arg0, char arg1,..... ,
char *argn, (char *) 0);
• int execlp(char *filename, char *arg0, char arg1,..... ,
char *argn, (char *) 0);
• int execle(char *pathname, char *arg0, char arg1,..... ,
char *argn, (char *) 0, char *envp[]);
• int execv(char *pathname, char **argv,);
• int execvp(char *filename, char **argv);
• int execve(char *pathname, char **argv, char **envp);
10
exec() Flavors
Library Call Name Argument List Pass Current Environment Variables Search PATH automatic
execl list yes no
execv array yes no
execle list no no
execve array no no
execlp list yes yes
execvp array yes yes
exec() Flavors
Explicit list of arguments terminated by NULL pointer
• int execl(char *pathname, char *arg0, char arg1,..... ,
char *argn, (char *) 0);
Explicit pathname is provided
Environment variables are inherited from calling process
• int execlp(char *filename, char *arg0, char arg1,..... ,
char *argn, (char *) 0);
Filename has a ‘/’ -> execl
Filename does not have a ‘/’ -> PATH is searched
Environment variables are inherited from calling process
• int execle(char *pathname, char *arg0, char arg1,..... ,
char *argn, (char *) 0, char *envp[]);
Explicit pathname is provided
Environment is passed as a parameter
11
exec() Flavors
Passes the command-line arguments in an argument array
• int execv(char *pathname, char **argv,);
Explicit pathname is provided
Enviornment variables are inherited from calling process
• int execvp(char *filename, char **argv);
Filename has a ‘/’ -> execl
Filename does not have a ‘/’ -> PATH is searched
Environment variables are inherited from calling process
• int execve(char *pathname, char **argv, char
**envp);
Explicit pathname is provided
Environment is passed as a parameter
exec() Flavors
• The execl (execl, execlp and execle) functions
pass the command-line arguments in an
explicit list and are useful if you know the
number of command-line arguments at
compile time.
• The execv (execv, execvp and execve)
functions pass the command-line arguments in
an argument array.
12
Example
/* tinymenue.c*/
#include ,stdio.h.
main() {
char *cmd[]={“who”, “ls”, “date”};
int i;
printf(“0=who, 1=ls, 2=date :) ;
scanf(“%d”, &i);
execlp(cmd[i], cmd[i], 0);
printf(“command not found\n”); /*exec failed*/
}
- execlp() call never returns unless execlp() fails.
- If execlp() succeeds, control is transferred to the new program - All
context within the old program is lost; there is no way back
- This limitation can be solved using the idea of fork() with child parent
synchronization.
Example
main() {
char *cmd[]={“who”, “ls”, “date”};
int i;
while(1){
printf(“0=who, 1=ls, 2=date :) ;
scanf(“%d”, &i);
if (fork() == 0) {
execlp(cmd[i], cmd[i],0);
printf(“command not found\n”); /*exec failed*/
exit(1);
}
else {
wait(0);
}
}
}
13
Example
main() {
char *cmd[]={“who”, “ls”, “date”};
int i;
while(1){
printf(“0=who, 1=ls, 2=date :) ;
scanf(“%d”, &i);
Parent Child
else { if (fork() == 0) {
wait(0); execlp(cmd[i], cmd[i],0);
} printf(“command not
} found\n”); /*exec
} failed*/
exit(1);
}
Inheritance of Process Attributes
Attribute Inherited by child? Retained on exec?
Process ID No Yes
Static data Copied No
Stack Copied No
Heap Copied No
Code Shared No
Open file descriptors Copied Usually
Environment Yes Depends of version
Current directory Yes Yes
14