4.1 Operating Systems Lecture 9 Fork and Exec Read Ch 4.4 - 4.5.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Operating Systems Lecture 7.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
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.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CSSE Operating Systems
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Phones OFF Please Processes Parminder Singh Kang Home:
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
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.
Simple Shell Part 1 Due date (75%): April, 2002 Part 2 Due date (25%): Apr 5, 2002.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CE Operating Systems Lecture 10 Processes and process management in Linux.
System calls for Process management
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
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,
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.
CSCI 330 UNIX and Network Programming
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Using Processes.
Unix Process Management
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
System Structure and Process Model
Lab 5 Process Control Operating System Lab.
Tutorial 3 Tutorial 3.
Process Programming Interface
Simple Shell Due date: March 27, 2002.
Processes Prof. Ikjun Yeom TA – Mugyo
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
Process Description and Control in Unix
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
System Programming: Process Management
Presentation transcript:

4.1 Operating Systems Lecture 9 Fork and Exec Read Ch

4.2 Process Creation A parent process creates child process(es). Parent Child

4.3 The fork( ) command In UNIX, a child process is created with the fork( ) command. cpid = fork( ); This creates a copy of the process that executed the fork( ) command.  Address spaces for the two processes are (almost) identical.  Both processes have identical open files  Both processes have the program counter (PC) pointing to the statement after the fork( ) command.  The ID of the child process is returned to the parent by fork( ).  Zero is returned to the child process.

4.4 Using fork( ) Example: int main( void) { int cpid; cpid = fork( ); cout << "Hello there." << endl; } Question: What is the output? Demo 1

4.5 Diagram of fork( ) We will draw this in class. Question: What is the difference between the two processes?

4.6 Distinguishing Parent and Child Typical code to distinguish parent and child: cpid = fork( ); if (cpid == 0) { //child code } else { //parent code } Demo 2

4.7 Multiple forks What is the output of the following code? int main (void) { int cpid, cpid2; cpid = fork( ); cpid2 = fork( ); if (cpid == 0) { cout << "I am the first child." << endl; } else if (cpid2 == 0 ) { cout << "I am the second child." << endl; } else { cout << "I am the parent." << endl; } return 0; } Demo 3

4.8 Diagram of two process code We will diagram this in class.

4.9 Distinguishing the grandchild from the child Question: How do you distinguish the grandchild from the child? We will write this code in class.

4.10 Multiple forks from the parent only Question: How do you get the first child not to fork( )? We will write this code in class.

4.11 The exec( ) function There are many versions of the exec( ) function: execv( ), execl( ), execlp( ), execv( ), execvp( ), etc. We will use execlp( ) and execvp( ) execlp( "cmd", arg0, arg1, arg2..., NULL); execvp( "cmd", argv);

4.12 Properties of the exec( ) call The process that calls exec( ) is demolished.  exec( ) overlays (most of) the address space of the caller with the executable file, cmd.  exec( ) overlays the memory image of the process.  The parameters are passed appropriately. Note: Open files are inherited. If you want both child1 and child2 to read/write to the same file, the parent should open it.

4.13 Example with execlp( ) int main(void) { int cpid; cpid = fork( ); if (cpid == 0) {//child execlp( "ls", "ls", "-l", NULL); } else { cout << "I am the parent." << endl; } return 0; } Demo 4

4.14 Process synchronization The parent can wait for a child to terminate using the wait( ) function. int cpid_done, status; cpid_done = wait(&status); The caller sleeps until any child terminates. cpid_done gets the pid of the child that terminated. status gets the exit status of the terminated process. int cpid_done, status, options = 0; cpid_done = waitpid(cpid, &status, options); caller sleeps until child with cpid terminates. returns the pid of the terminating child stores the status of the terminating child in status.

4.15 Process Termination A process terminates with the exit (status) call This terminates the process. It closes all open files associated with the process. Destroys the image of the memory image.

4.16 Typical code using fork, exec and wait int main( void ) int cpid, cpid_done, status; cpid = fork( ); if (cpid == 0) {//child // call exec function with desired cmd } else { cpid = wait(&status); //rest of parent code } return 0; } Demo 5

4.17 Basic Shell code Pseudo-code fpr basic shell (not real C++ code): while (not EOF) { parse_command_line; (get command, args, redirect, etc.) p = fork( ); if (p == 0) { //redirect I/O (takes a few steps to do) // exec (cmd, args) } else { if (command doesn't end with &) wait( ); }