Operating Systems
Processes
Objectives
1. What’s process?
2. What’s the difference between program and process?
3. What about process’s lifecycle?
(a) Multiprogramming of four programs. (b) Conceptual model of four independent, sequential
processes.(c) Only one program is active at once.
The Process Model
Process• What’s process?
– A process is an instance of a computer program that is being executed. It contains the program code and its current activity.
• What is the difference between program and process?– Program is just the static text .– Process is an dynamic entity being executed and has
lifecycle.
• A process consists of three parts:– Process Control Block ( Process Table Entry)– Program (Text)– Data
Some of the fields of a typical process table entry.
Processes Control Block
Process Hierarchies
• Parent creates a child process, child processes can create its own process
• Forms a hierarchy– UNIX calls this a "process
group"
• Windows has no concept of process hierarchy– all processes are created
equal
Events which cause process creation:
• System initialization.• Execution of a process creation system call by a
running process.• A user request to create a new process.• Initiation of a batch job.
Process Creation
forkmain(){
pid_t val;printf("PID before fork(): %d \n",(int)getpid());val=fork();if ( val > 0) {
printf("Parent PID: %d\n",(int)getpid());} else if (val == 0) {
printf("Child PID: %d\n",(int)getpid());} else {
printf(“Fork failed!”); exit(1);}
}
bash fork()
exec() ls -l exit()
Shell and its children
• All processes are started by other processes– Parent/Child relationship
$ ls –l
• A process can be terminated for two reasons:– The process terminates itself when done.– The process is terminated by a signal from another process.
Code
pid_t val;int exit_code = 0;
val=fork();if (val > 0) {
int stat_val;pid_t child_pid;child_pid = waitpid(val,&stat_val,0);printf("Child has finished: PID = %d\n", child_pid);if(WIFEXITED(stat_val))
printf("Child exited with code %d\n", WEXITSTATUS(stat_val));else
printf("Child terminated abnormally\n");exit(exit_code);
} else if (val == 0) {execlp("ls","ls","-l",NULL);
}
The life of a process
A process can be in running, blocked, or ready state. Transitions between these states are as shown.
Process States
The lowest layer of a process-structured operating system handles interrupts and scheduling. Above that layer are
sequential processes.
Scheduling
Events which cause process termination:
• Normal exit (voluntary).• Error exit (voluntary).• Fatal error (involuntary).• Killed by another process (involuntary).
Process Termination
Skeleton of what the lowest level of the operating system does when an interrupt occurs.
Implementation of Processes (3)
CPU utilization as a function of the number of processes in memory.
Modeling Multiprogramming
Summary
1. Pseudo-parallelism vs. multiprocessor
2. Program vs. process
3. Process Hierarchies
4. Process lifecycle
5. Process states
6. System Calls:• fork() vfork()
• execl() execv() execle() execve() execlp() execvp()
• wait() waitpid()
• exit()
• system()