Process Management (Unix)
Process Management (Unix)
Process Management (Unix)
System Programming
Process Management
Objectives
1
Processes in UNIX
• 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
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 ???
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.
• 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()
Shell
exec()
wait()
exit
continue
8
Child Execute a Separate Program
exec()
• 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]
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[]);
10
exec() Flavors
Library Call Name Argument List Pass Current Environment Variables Search PATH automatic
execle list no no
execve array no no
exec() Flavors
Explicit list of arguments terminated by NULL pointer
11
exec() Flavors
Passes the command-line arguments in an argument array
exec() Flavors
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*/
}
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);
}
14