Creating and Executing Processes

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Exec Function calls Used to begin a processes execution. Accomplished by overwriting process imaged of caller with that of called. Several flavors, use.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Computer Studies (AL) Operating System Process Management - Process.
System calls for Process management
Operating Systems Processes 1.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Protection of System Resources
Unix Process Management
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
Processes in Unix, Linux, and Windows
Tutorial 3 Tutorial 3.
Process Programming Interface
Processes Prof. Ikjun Yeom TA – Mugyo
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Creating and Executing Processes Computer Systems II Creating and Executing Processes

Unix system calls fork( ) wait( ) exit( )

How To Create New 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 to wait for 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) Indeed in unix only one process is created at system initialization (the process is called init); all the others are children of this first process. Why have init? That is why not have all processes created via method 2? Ans: Because without init there would be no running process to create any others.

Fork System Call Current process split into 2 processes: parent, child Returns -1 if unsuccessful Returns 0 in the child Returns the child’s identifier in the parent Stack Stack ret = 0 Data Data Text ret = xxx Text

Fork System Call The child process inherits from parent identical copy of memory CPU registers all files that have been opened by the parent Execution proceeds concurrently with the instruction following the fork system call The execution context (PCB) for the child process is a copy of the parent’s context at the time of the call

How fork Works (1) PCB pid = 25 Data Resources Text Stack 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

How fork Works (2) PCB PCB pid = 25 Text Stack Data pid = 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); } ret = 0 Data Resources Text Stack 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); } ret = 26 UNIX

How fork Works (3) PCB PCB pid = 25 pid = 26 Data Resources Data Text Stack Stack PCB File PCB 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); } 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); } ret = 26 UNIX

How fork Works (4) PCB PCB pid = 25 pid = 26 Data Resources Data Text Stack Stack PCB File PCB 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); } 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); } ret = 26 UNIX

How fork Works (5) PCB PCB pid = 25 pid = 26 Data Resources Data Text Stack Stack PCB File PCB 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); } 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); } ret = 26 UNIX

How fork Works (6) pid = 25 Data Resources Text Stack Process Status 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); < … > ret = 26 UNIX

Orderly Termination: exit() To finish execution, a child may call exit(number) This system call: Saves result = argument of exit Closes all open files, connections Deallocates memory Checks if parent is alive If parent is alive, holds the result value until the parent requests it (with wait); in this case, the child process does not really die, but it enters a zombie/defunct state If parent is not alive, the child terminates (dies)

Waiting for the Child to Finish Parent may want to wait for children to finish Example: a shell waiting for operations to complete Waiting for any some child to terminate: wait() Blocks until some child terminates Returns the process ID of the child process Or returns -1 if no children exist (i.e., already exited) Waiting for a specific child to terminate: waitpid() Blocks till a child with particular process ID terminates #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);

State Transition on wait and exit Calls

Other useful system calls: getpid, getppid getpid returns the identifier of the calling process. Example call (pid is an integer): pid = getpid(); getppid returns the identifier of the parent. Note: Zombies can be noticed by running the 'ps' command (shows the process list); you will see the string "<defunct>" as their command name: ps -ef ps –ef | grep mdamian

Fork Example 1: What Output? int main() { pid_t pid; int x = 1; pid = fork(); if (pid != 0) { printf(“parent: x = %d\n”, --x); exit(0); } else { printf(“child: x = %d\n”, ++x); }

Fork Example 2 Key Points Both parent and child can continue forking void fork2() { printf("L0\n"); fork(); printf("L1\n"); printf("Bye\n"); } Bye L1 L0

Fork Example 3 void fork3() { printf("L0\n"); fork(); printf("L1\n"); printf("Bye\n"); } L1 L2 Bye L0

Fork Example 4 void fork4() { printf("L0\n"); if (fork() != 0) { } printf("Bye\n");

Fork Example 5 void fork5() { printf("L0\n"); if (fork() == 0) { } printf("Bye\n");

Summary Fork Exit Wait 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

Unix system calls execv execl

Unix’s 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

How execv Works (1) PCB PCB UNIX kernel pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB char * argv[ ] = {“/bin/ls”, 0}; <…> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid); char * argv[ ] = {“/bin/ls”, 0}; <…> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid); cpid = 26 cpid = 0 UNIX kernel /bin/ls

How execv Works (2) pid = 25 pid = 26 Data Resources Text Stack PCB File char * argv[ ] = {“/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 /bin/ls

How execv Works (3) PCB PCB <first line of ls> <…> pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB char * argv[ ] = {“/bin/ls”, 0}; <…> int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } <parent code> wait(&cpid); cpid = 26 <first line of ls> <…> exit(0); UNIX kernel /bin/ls

Example: A Simple Shell Shell is the parent process E.g., bash Parses command line E.g., “ls –l” Invokes child process Fork, execvp Waits for child Wait bash fork execvp wait ls

execv Example Note the NULL string at the end #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”); } else /* Parent executes here */ wait(&status); printf("Hello there! \n”); return 0; Note the NULL string at the end

execv Example – 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 drwxr-xr-x 2 mdamian faculty 512 Jan 28 15:02 notes Hello there!

Note the NULL string at the end 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 char * argv[] = {“/bin/ls”, “-l”, 0}; execv(argv[0], argv); execl is mainly used when the number of arguments is known in advance Note the NULL string at the end

General purpose process creation In the parent process: int childPid; char * const argv[ ] = {…}; main { childPid = fork(); if(childPid == 0) { // I am child ... // Do some cleaning, close files execv(argv[0], argv); } else // I am parent ... <code for parent process> wait(0);

Combined fork/exec/wait Common combination of operations Fork to create a new child process Exec to invoke new program in child process Wait in the parent process for the child to complete Single call that combines all three int system(const char *cmd); Example int main() { system(“echo Hello world”); }

Properties of fork / exec sequence In 99% of the time, we call execv(…) after fork() the memory copying during fork() is useless the child process will likely close open files and connections overhead is therefore high might as well combine both in one call (OS/2) 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

Variations of execv execv execvp execl execlp 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

Summary exec(v,vp,l,lp) system: 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