Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition,
Processes
Modified by M.Rebaudengo - 2013
3.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Definitions
Algorithm: a logical procedure that solves a problem in a finite number ofstepsProgram:
formal expression of an algorithm by means of a programming languagea programming language needs a run-time support
Process: a sequence of operations performed by a program in executionon a given set of input data.
3.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Definitions of process
A program in executionOr, an instance of a program running on a computerOr, the context (the information/data) maintained for an executing program.Or, a unit of activity characterized by the execution of a sequence of instructions, a current state and an associated set of system resources
The terms job and process can be used almost interchangeably.
3.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Processes and programs
The program is a passive entity; whereas the process is the active entity
Two processes may be associated to the same programe.g., two users may be running two copies of the Web browser programeach of these is a separate processthe text sections are equivalentthe data, heap, stack sections vary.
3.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Management
Information maintained by OS for process managementprocess contextprocess control block
OS virtualization of CPU for each process.Context switchingDispatching loop.
3.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
User Level Context
User level context contains all states necessary to run a program (the information the process needs to do the job)
text section (program code)stack (temporary data: function parameters, return addresses and local variables)data section (global variables)heap (dynamically allocated memory).
3.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process in Memory: User Level Context
3.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
User level context
(b, *p) - main
(a) - foo
heap (array [1000])
data (aa, buf)
text (code) 0
MAX
Process memory
…int aa;char buf[1000];void foo() {int a;…
}main() {int b;char *p;p= malloc(1000);foo();
}
stack
3.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process context
Contains all states necessary to run a program
Is the user level context sufficient?
Only if the system runs through one program at a timeThe system typically needs to switch back and forth between programs.
R0 = 1
R2 = R0 + 1
R0 = 2
R2 = R0
P1 P2
• R2 in P1 is wrong. How to makeit correct?
• Save R0 in P1 before switching• Restore R0 in P1 when switching from P2 to P1.
• Registers should be a part of process context: the register context
3.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Context
Process context:User level context
Code, data, stack, heapRegister context (R0, R1,…, PC, stack pointer, PSW, etc).What else?
OS resources (e.g. open files, allocated resources, etc).
3.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Where is the process context stored?
User level context is in memory.Other context information is stored in a data structure called process control block.The OS has a process control block table. For each process, there is one entry in the table.
3.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Control Block (PCB)
The most important data structure in an OSInformation associated with each process:
Process identifier: unique numeric identifier of the processProcess state: new, ready, running, waiting, terminatedRegister Context:
Program counter: the address of the next instruction to be executedCPU registers.
3.13 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Control Block (PCB)CPU scheduling information: needed by the OS to perform its scheduling function
process priorityscheduling-related information (e.g., the amount of time that the process has been waiting and the amount of time that the process executed the last time it was running)pointers to scheduling queues
Memory-management information:mapping between virtual and physical memory locations.
Accounting information: used for billing purposes and performance measurements
the amount of CPU and real time usedmain memory storage occupancy
I/O status information: resources allocated to the processHW units: the list of I/O devices allocated to the processthe list of open files.
3.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Control Block (PCB)
3.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Table
One entry per process.
PCB PCB PCBPCB PCB
Pr 0 Pr 1 Pr 2 Pr 4 …..Pr N
3.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
OS CPU abstraction
Hardware reality:One CPU runs the fetch-execute algorithm
OS abstraction:Each process has one CPU, running the fetch-execute algorithm for the process.Each process executes within its context.
Load PC;IR = MEM[PC];While (IR != HALT) {
PC ++;Execute IR;IR = MEM[PC];
}
3.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
OS CPU abstraction
What does the OS have to do?Embed the process instruction sequence into hardware instruction sequence.
Process X instructions: x0, x1, x2, …Process Y instructions: y0, y1, y2, …Process Z instructions: z0, z1, z2, …
Hardware instructions? x0, x1, x2, y0, y1, y2, z0, z1, z2, x3, x4, x5, …
Does this embedding work? No!! Instructions in a process should onlybe executed within the process’s context to be correct.
3.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
OS CPU abstraction
Process X instructions: x0, x1, x2, …Process Y instructions: y0, y1, y2, …Process Z instructions: z0, z1, z2, …
x0, x1, x2, [store X’s context], [restore Y’s context] y0, y1, y2…
OS must do this to keep programs execute within its context: Context switching
3.19 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Context Switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switchContext of a process is represented in the PCBContext-switch time is overhead; the system does no useful work while switchingTime dependent on hardware support:
it depends on the memory speed, the number of registers that must be copied, and the existence of special instructions (e.g., a single instruction to load or store all registers)typical speeds are a few milliseconds.
3.20 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
CPU Switch From Process to Process
3.21 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Dispatching Loop
The hardware view of the system execution: dispatching loopLOOP
Run processSave process statesChoose a new process to runLoad states for the chosen process
Context Switch:Dispatcher
Scheduling
3.22 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
How Does the Dispatcher Regain Control?
Must change from user to system modeProblem: only one CPU, and CPU can only do one thing at a timeA user process running means the dispatcher isn’t
The dispatcher gets control back from the running process in two ways:1. Internal events (traps: events caused by process execution)
A process is waiting for I/OA process is waiting for some other processA process gives up CPU voluntarily
2. External events (hardware interrupts: events external to process)A completed I/O requestSignal from the keyboardTimer (alarm clock).
3.23 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process State
As a process executes, it changes statenew: The process is being createdrunning: Instructions are being executedwaiting: The process is waiting for some event to occurready: The process is waiting to be assigned to a processorterminated: The process has finished execution.
Only one process may be running on any processor at any instant.Many processes may be ready and waiting.
3.24 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Diagram of Process State5-state model
timeout
3.25 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
X
X X
3.26 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
How does the dispatcher choose the next process?
The dispatcher keeps a list of processes that are ready to runIf no processes are ready
Dispatcher just loopsOtherwise, the dispatcher uses a scheduling algorithm to find the next process.
3.27 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Operations on processes
Most systems provide a mechanism to create and terminate processes.
3.28 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Creation
A process may create several new processes, via a create-process system call, during the course of execution:
the creating process is called a parent processthe new processes are called the children processes of that parent.
Parent process create children processes, which, in turn create other processes, forming a tree of processesGenerally, process is identified and managed via a unique process identifier (pid), which is an integer number (a variable of type pid_t).
3.29 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
A tree of processes
There are some special processes: process ID 0 is usually the scheduler process and is often known as the swapper: it is part of the kernel and is known as a system process.Process ID 1 is usually the init process and is invoked by the kernel at the end of the bootstrap procedure.
3.30 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
Output data from child to parent (via wait)Process’ resources are deallocated by operating system
Parent may terminate execution of children processes (via kill).
3.31 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
UNIX examples
Process creation:fork system call creates new processexec system call used after a fork to replace the process’ memory space with a new program
3.32 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
fork()
int fork(void);
the return code is zero for the new (child) processthe nonzero and positive process identifier of the child is returned to the parent processa negative value represents an error in the process creation
The two processes share the same program code The new process consists of a copy of the address space of the original processBoth processes share the same value of the Program Counter Register:
Both processes continue the execution at the instruction after the fork()
3.33 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
fork() as a diagram
Parent
pid = fork()
Returns a new PID: e.g. pid == 5
Data
SharedProgram Data
Copied
Child
pid == 0
3.34 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exampleint main(){pid_t pid, fork_return;
/* fork another process */fork_return = fork();if (fork_return < 0) { /* error occurred */
printf("Fork Failed");}else if (fork_return == 0) { /* child process */
{pid = getpid();printf("I'm the children! pid: %d\n", pid);/* system call getpid() returns the pid of the calling process */}
}else { /* parent process */
printf("I'm the father! the child pid is %d\n", fork_return);}
}
3.35 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Identification
You can get the process ID of a process by calling getpid(). The function getppid() returns the process ID of the parent of the current process (this is also known as the parent process ID). Your program should include the header files ‘unistd.h’ and ‘sys/types.h’ to use these functions.
3.36 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 1Using the following program, identify the values of pid at line A, B, C and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively).
int main(){pid_t pid, pid1;
/* fork a child process */pid = fork();
if (pid < 0) { /* error occurred */printf("Fork Failed");return 1;
}else if (pid == 0) { /* child process */
{pid1 = getpid();printf("child: pid = %d\n", pid); /* A */printf("child: pid1 = %d\n", pid1); /* B */}
}else { /* parent process */
pid1 = getpid();printf("parent: pid = %d\n", pid); /* C */printf("parent: pid1 = %d\n", pid1); /* D */}
return 0;}
3.37 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 2Including the initial parent process, how many processes are created by the following program?
int main(){
/* fork a child process */fork();
/* fork another process */fork();
/* and fork another */fork();
}
3.38 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution
P
#2#1 #3
#3#2
#3
#3
3.39 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 3
Draw a diagram showing the parent/child relationships among the processes created by the following code fragment (showing which call to fork created each process).
pid = fork() /* call #1 */
if (pid != 0)
fork(); /* call #2 */
fork(); /* call #3 */
3.40 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution
P
#2#1 #3
#3#3
3.41 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 4
Draw a diagram showing the parent/child relationships among the processes created by the following code fragment (showing which call to fork created each process).
pid = fork() /* call #1 */
fork(); /* call #2 */
if (pid != 0)
fork(); /* call #3 */
3.42 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 5
Draw a diagram showing the parent/child relationships among the processes created by the following code fragment (showing which call to fork created each process).
#include <stdio.h>
int i:
int main ()
{
for (i=0 ; i < 2 ; i++)
{
printf("i: %d \n", i);
if (fork()) /* call #1 */
fork(); /* call #2 */
}
}
3.43 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
i=1i=0
i=1i=1
Solution
A
B C D E
F G H I
#1
#2
#1
#1
#2
#2#1 #2
3.44 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise 6Consider the following program which uses the fork() system call. What is the result written by the program to the standard output stream? What is the resulting process tree?
int split(int a, int b)
{
a++;
a = fork(); /* #4 */
if (a) {
a = b;
} else if (fork()) /* #5 */ {
a--;
b += a;
}
return a+b;
}
int main()
{
int a, b=5, c;
a = fork(); /* #1 */
if (a) {
a = b;
c = split(a, b++);
} else {
(void) fork(); /* #2 */
c = a++;
b += c;
}
if (b > c) {
(void) fork(); /* #3 */
}
printf("%3d", a+b+c);
return 0;
}
3.45 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution
A
B C
D E F G
IH
#1 #4
#5
#3
#3#3#2
#3
3.46 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution (cont.)
The output of the program is the following:
process a b c a+b+cA 5 6 10 21B 1 5 0 6C 5 6 3 14D 1 5 0 6E 1 5 0 6F 5 6 5 16G 5 6 3 14H 1 5 0 6I 5 6 5 16
3.47 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
ExerciseDraw a diagram showing the parent/child relationships among the processes created by the following code fragment (showing which call to fork created each process)
1> #include <stdlib.h>main()
{
pid1 = fork(); /* call #1 */
pid2 = fork(); /* call #2 */
if (pid1 != pid2)
fork(); /* call #3 */
}
2> #include <stdlib.h>
main()
{
pid = fork(); /* call #1 */
if (pid) fork(); /* call #2 */
if (pid) fork(); /* call #3 */
}
3.48 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise
Trace the following program segment and determine how many processes are created. Assume that no errors occur. Draw a graph that shows how the processes are related. In this graph each process will be represented by a small circle containing a number that represents which fork created the process. The original process will contain 0 and the process created by the first fork will contain 1, and so on.
c2 = 0;
c1 = fork(); /* fork number 1 */
if (c1 == 0)
c2 = fork(); /* fork number 2 */
fork(); /* fork number 3 */
if (c2 > 0)
fork(); /* fork number 4 */
3.49 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
exit()
exit() is a system call that terminates the process, asking the operating system to delete the processThe process may return an exit status value to its parent process (via the wait() system call)
All the resources of the process are deallocated by the operating system.Syntax:
void exit(int status);
the exit() function causes normal process termination and the value of status & 0377 is returned to the parent process (i.e., the lower 8 bits contain the status and the upper 8 bits contain 0)the exit() function does not return any value.
3.50 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
sleep()
A process may suspend for a period of time using the sleep function.
Syntax:unsigned int sleep (seconds)
The sleep() function shall cause the calling process to be suspended from execution until the number of realtime seconds specified by the argument seconds has elapsed
3.51 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Parent termination
In Unix, if the parent terminates, all its children (become orphan) have assigned as their new parent the init process (whose PID is 1)
Thus the children still have a parent to collect their status and execution statistics.
3.52 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Example#include <stdio.h>
main()
{
int pid ;
printf("I'am the original process with PID %d and PPID %d.\n", getpid(), getppid()) ;
pid = fork ( ) ; /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero, so I must be the parent*/
{
printf("I'am the parent with PID %d and PPID %d.\n", getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
}
else /* pid is zero, so I must be the child */
{
sleep(4); /* make sure that the parent terminates first */
printf("I'm the child with PID %d and PPID %d.\n", getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
}
The output is: I'am the original process with PID 5100 and PPID 5011.
I'am the parent process with PID 5100 and PPID 5011.
My child's PID is 5101
PID 5100 terminates. /* Parent dies */
I'am the child process with PID 5101 and PPID 1. /* Orphaned, whose parent process is “init” with pid 1 */
PID 5101 terminates.
3.53 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
wait()
wait() is a system call that suspends the execution of the parent process while the child executesWhen the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution.Syntax:
int wait (int *status);
status is the address of the variable containing the exit status of the child process the parameter could be NULL
the return value is the pid of the terminated process (or a negative number in case of error).
3.54 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
wait()
A process that calls wait()can:
suspend (block) if all of its children are still running, or
return immediately with the termination status of a child, or
return immediately with an error if there are no child processes.
3.55 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Note
The argument to wait()is the address on an integer variable or the NULL pointer
If it's not NULL, the system writes 16 bits of status information about the terminated child in the low-order 16 bits of that variable. Among these 16 bits, the higher 8 bits contain the lower 8 bits of the argument the child passed to exit(), while the lower 8 bits are all zero if the process exited correctly, and contain error information if not.
3.56 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
waitpid()
pid= waitpid(pid , &status);
wait() waits for any child; waitpid() waits for a specific child.
3.57 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
ExerciseUsing the following program, what is the output at line A and line B?
int value = 5;int main(){pid_t pid;int status;
pid = fork();
if (pid == 0) { /* child process */{ value += 15;
printf (“CHILD: value =%d\n”, value); /* LINE A */exit (0);
}else { /* parent process */
wait (&status);printf (“PARENT: value =%d\n”, value); /* LINE B */
}}
3.58 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Examplemain ( )
{
pid_t whichone, first, second ;
int howmany, status ;
if ( (first = fork ( ) ) == 0 ) /* Parent spawns 1st child */
{
printf ("Hi, I am the first child, and my ID is %d\n", getpid ( ) ) ;
sleep (10) ;
exit (0) ;
}
else if (first == -1)
{
printf("1st fork: something went wrong\n") ;
exit (1) ;
}
else if ( ( second = fork ( ) ) == 0 ) /* Parent spawns 2nd child */
{
printf ("Hi, I am the second child, and my ID is %d\n", getpid ( ) ) ;
sleep (15) ;
exit (0) ;
}
else if (second == -1)
{
printf("2nd fork: something went wrong\n") ;
exit (1) ;
}
3.59 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Example (cont.)printf ("This is parent\n") ;
howmany = 0 ;
while (howmany < 2) /* Wait Twice */
{
whichone = wait(&status) ;
howmany++ ;
if (whichone == first)
printf ("First child exited ") ;
else
printf ("Second child exited ") ;
if ( (status & 0xffff) == 0 )
printf ("correctly\n") ;
else
printf ("uncorrectly\n") ;
}
}
3.60 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise
Write a C program which uses Unix system calls to create a new process. The child process should create an empty file called ‘abc’ and then terminate. The parent process should wait for the child process to terminate and then output the process number of the child process. Don't forget to check for error conditions.
3.61 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution#include <stdio.h>
#include <sys/types.h>
main()
{
pid_t child;
int status;
FILE *fp;
child=fork();
if (child==-1)
printf("Error creating child process");
else
if (child==0) {
fp = fopen("abc", "w");
if (fp==NULL)
printf("Error creating file");
else
fclose(fp);
exit(0);
}
else {
child=wait(&status);
if (child==-1)
printf("Error waiting for child process");
else
printf("%ld\n", (long)child);
}
}
3.62 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
ExerciseWhat might be the output when the following program is run?
int main(int argc, char *argv[])
{
int x;
pid_t pid;
int status;
x = 13;
printf(“x is now %d\n”,x);
pid = fork();
if (pid == 0) {
x = x+4;
printf(“ Add 4, and x = %d\n”, x);
exit(0);
}
else
{
x = x*4;
printf(“ Multiply by 4, and x = %d\n”, x);
wait(&status);
}
printf(“In the end, x = %d\n”, x);
return 0;
}
3.63 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Zombie processes
A process that terminates cannot leave the system until its parent accepts its return code.
Note: the parent accepts the child’s return code either via a wait() or if it terminates
If its parent process is already dead, it’ll already have been adopted by the “init” process, which always accepts its children’s return codes. However, if a process’s parent is alive but never terminates, the process’s return code will never be accepted and the process will remain a zombie.
3.64 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Examplemain ( )
{
int pid ;
pid = fork(); /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero, so I must be the parent */
{
while (1) /* Never terminates */
sleep (100) ; /* stop executing for 100 seconds */
}
else /* pid is zero, so I must be the child */
{ exit (42) ; /* exit with any number */
}
}
The output is:
> a.out & execute the program in the background
[1] 5186
> ps obtain process status
PID TT STAT TIME COMMAND
5187 p0 Z 0:00 <exiting> the zombie child process
5149 p0 S 0:01 -csh (csh) the shell
5186 p0 S 0:00 a.out the parent process
5188 p0 R 0:00 ps
> kill 5186 kill the parent process
[1] Terminated a.out
> ps notice that the zombie is gone now
PID TT STAT TIME COMMAND
5149 p0 S 0:01 -csh (csh)
5189 p0 R 0:00 ps
3.65 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
exec()
Family of functions for replacing process’s program with the one inside the exec()call.
Typically the exec() system call is used after a fork() system call by one of the two processes to replace the process's memory space with a new program.The exec() system call loads a binary file into memory (destroying the memory image of the program containing the exec() system call) and starts its execution.As a new process is not created, the PID does not change across an exec(), but the data, heap and stack of the calling program are replaced by those of the new programThere is a family of different exec functions: there are 6 versions of the exec function, and they all do about the same thing: they replace the current program with the text of the new program. Main difference is how parameters are passed.
3.66 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Syntax:int execlp(char * path, char *arg0, char *arg1, ..., char *argN, (char *) 0);
The path argument specifies the path name of the file to execute as the new process image. Arguments arg0 through argn are a list of pointers to arguments to be passed to the new process image. The exec functions do not normally return to the calling process. If an exec function returns, an error occurred and the return value is –1.
3.67 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exampleint main(){pid_t pid;
/* fork another process */pid = fork();if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");exit(-1);
}else if (pid == 0) { /* child process */
execl("/bin/ls", "ls", "-l", NULL);printf("Failed Exec \n");exit(-1);
}else { /* parent process */
/* parent will wait for the child to complete */wait (NULL);printf ("Child Complete");exit(0);
}}
3.68 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
exec() family
There are 6 different ways of calling exec. Which one to use depends on three conditions:1. How arguments are passed (list of parameters or array)2. How the path is specified (pathname or filename)3. Whether a new environment is used
int execl(const char *pathname, const char *arg0, …);int execv(const char *pathname, char *const argv []);
int execlp(const char *filename, const char *arg0, . . .);int execvp(const char *filename, char *const argv[]);
int execle(const char *pathname, const char *arg0, … , char *const envp[] );int execve(const char *pathname, char *const argv[], char *const envp[]);
3.69 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
execl()
Creates a new program in the same environment requires a fully qualified pathexecl("/bin/ls", "ls", "-l", NULL);
Adding a “p” to an exec call tells the system will use the environment variable PATH to search for the fileexeclp("ls", "ls", "-l", NULL);
Same as execl, butArguments are passed as a null terminated array of character pointers
char *args[] = {“/bin/ls”, “-l”, NULL}
execv (args[0], args);
execlp()
execv() and execvp()
3.70 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
execle() and execve()
Adding an “e” to the execl and execv call allows the new program to run with a new environment Process arguments (in case of execve()) and the environment are passed as null terminated arrays of character pointers
char *env[] = {“USER=qos”,”PATH=/usr/bin:/bin”, NULL };char *args[] = {“/bin/someprog”, “-r”, “-verbose”, NULL};
execve(args[0], args, env);
3.71 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
After a fork()
Whenever a (parent) process calls fork(), a new child process is created with its own copies of the program text, data and stack segments.OS also creates new instances of the process descriptor (PCB).
Code
Data
Parent
Code
Data
Child
copy
copy
PCB PCB
3.72 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
After a exec()
Whenever a (child) process calls exec(), the new program is loaded in memory (with a new image of the process).
NewCode
NewData
PCB
3.73 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Process Creation using fork()
3.74 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Example: menu
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>
void main(){
char *cmd[] = {“who”, “ls”, “date”};int i;while( 1 )
{printf( 0=who 1=ls 2=date : “ );scanf( “%d”, &i );
:
continued
3.75 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
if(fork() == 0) { /* child */execlp( cmd[i], cmd[i], (char *)0 );printf( “execlp failed\n” );exit(1);}
else { /* parent */wait( (int *)0 );printf( “child finished\n” );}
} /* while */} /* main */
3.76 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Execution
menu
execlp()cmd[i]
child
wait()
fork()
3.77 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise
In the next code, assume thatall fork and execvp statements execute successfully,the program arguments of execvp do not spawn more processes or print out more charactersall pid variables are initialized to 0.
1. What is the total number of processes that will be created by the execution of this code?
2. How many of each character ‘A’ to ‘J’ will be printed out?
3.78 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exercise (cont.)void main()
{
...
pid1 = fork();
printf("A");
if (pid1) pid2 = fork();
if (!pid2) {
pid3 = fork();
printf("B");
}
else { execvp(...);
printf("C");
}
if (pid1 != pid3) {
printf("D");
if (pid1 && pid3) {
printf("E");
execvp(...);
}
pid4 = fork();
printf("F");
}
else {
printf("G");
pid5 = fork();
}
if (pid2) {
pid6 = fork();
pid7 = fork();
if (pid6) pid8 = fork();
printf("H");
}
if (!pid1 && !pid2 && !pid3 && !pid4 && !pid5) printf("I");
execvp(...);
printf("J");
}
3.79 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution
1
32
pid1 <> 0; pid2 <> 0pid1 = 0
pid2 = 0; pid3 <> 0
4 pid3 = 0; pid4 <> 0
5
‘A’
‘B D E’
‘B D F’
‘F’
6
7
8
pid3 <> 0pid4 <> 0
pid3 = 0pid5 <> 0
pid4 = 0
‘A B D F’
pid4 = 0pid3 = 0pid5 = 0
‘B G’
‘I’
‘F’
3.80 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Race Condition
A race condition occurs when multiple processes are trying to do something with shared data and the final outcome depends on the order in which the processes run.The fork function is a lively breeding ground for race conditions, if any of the logic after the fork depends on whether the parent or child runs first after the fork.In general we cannot predict which process runs first:
even if we knew which process would run first, what happens after that process starts running depends on the system load and the kernel’s scheduling algorithm.
3.81 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Exampleint main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {fprintf(stderr, "Fork Failed");exit(-1);
} else if (pid == 0) {
charatatime("output from child\n");
} else {
charatatime("output from parent\n");
}
exit(0);
}
void charatatime(char *str)
{
char *ptr;
int c;
setbuf(stdout, NULL); /* set unbuffered */
for (ptr = str; (c = *ptr++) != 0; )
putc(c, stdout);
}
3.82 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Output
$ ./a.outooutput from childutput from parent
3.83 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Another example of Race condition
File sharing: one characteristic of fork is that all descriptors that are open in the parent are duplicated in the childIf both parent and child write to the same descriptor, without any form of synchronization, their output will be intermixed:
this is not the normal mode of operation.
3.84 Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8th Edition
Solution
Synchronization among processes.