Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.

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.
CSCC69: Operating Systems
1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CSSE Operating Systems
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
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:
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Introduction to Processes CS Intoduction to Operating Systems.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
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
Creating and Executing Processes
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
CE Operating Systems Lecture 10 Processes and process management in Linux.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
System calls for Process management
Operating Systems Processes 1.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 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.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Process Management Azzam Mourad COEN 346.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor.
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.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Using Processes.
Unix Process Management
CSC 382: Computer Security
UNIX PROCESSES.
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.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Fork and Exec Unix Model
Lab 5 Process Control Operating System Lab.
Tutorial 3 Tutorial 3.
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
Lecture 6: Multiprogramming and Context Switching
CS510 Operating System Foundations
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 The Multi-Process Astronomy Server Consider the following problem: A server is made up of one “input process” and multiple “worker processes”. The input process performs a loop that reads multiple file names from disk. Each named file contains a very large scientific data set (with observation data on all stars in some galaxy). The process passes each file to a different newly forked child that opens the named file and performs some math on the data (to compute the trajectories of the observed stars). The results are written by the child to a results file for that galaxy. The child then exists with a code that indicates success or failure. The parent (input process) waits for the children. Each time a child exists, the parent checks the exit code and restarts the child if the code indicates that an error has occurred. What do you need to know to write this up?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Waiting for Children to Return The wait() Function wait function allows parent process to wait (block) until child finishes then returns the id and status of that child wait function causes the caller to suspend execution until child’s status is available waitpid function allows a parent to wait for a particular child

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Waiting for Children #include pid_t childpid; … childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Dead Children may become Zombies When a process terminates but its parent does not wait for it? Zombie (pid entry remains in system process table) Zombies unlike orphans do not consume many resources.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Zombie Removal Professional code installs signal handler for signal SIGCHLD … which issues a wait() call

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Orphans When the parent process terminates first? “Child is orphaned” Child is “re-parented” to init process Child continues to consume resources init process (id=1) waits for children

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Status Values for wait(int *stat_loc) The following macros are available for checking the return status of a child: #include WIFEXITED(int stat_val) WEXITSTATUS(int stat_val) WIFSIGNALED(int stat_val) WTERMSIG(int stat_val) WIFSTOPPED(int stat_val) WSTOPSIG(int stat_val) They are used in pairs. If WIFEXITED returns true, the child executed normally and the return status (at most 8 bits) can be gotten with WEXITSTATUS.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Simple Example int status; childpid = wait(&status); if (childpid == -1) perror("Failed to wait for child"); else if (WIFEXITED(status) && 0==WEXITSTATUS(status)) printf(“Child terminated normally”);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Typical Child Creation Code #include int main (void) { pid_t childpid; /* set up signal handlers here... */ childpid = fork(); if (childpid == -1) { perror("Failed to fork");return 1;} if (childpid == 0) fprintf(stderr, "I am child %ld\n", (long)getpid()); else if (wait(NULL) != childpid) fprintf(stderr, "A signal must have interrupted the wait!\n"); else fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(), (long)childpid); return 0; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 exec() Function Exec function allows child process to execute code that is different from that of parent Exec family of functions provides a facility for overlaying the process image of the calling process with a new image. Exec functions return -1 and sets errno if unsuccessful

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Exec Example #include int main (void) { pid_t childpid; childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) { /*child code*/ execl(“/bin/ls”, “ls”, “-l”, NULL); perror(“Child failed to exec ls”); return 1; } if (wait(NULL) != childpid) { /* parent code */ perror(“Parent failed to wait due to signal or error”); return 1; } return 0; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Background Processes and Daemons Shell – command interpreter creates background processes if line ends with & When shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting another command Ctrl-C does not terminate background process Daemon is a background process that normally runs indefinitely

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Processes and Threads

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Property Processes created with fork Threads of a processOrdinary function calls variables get copies of all variables share global variables IDsget new process IDs share the same process ID but have unique thread ID share the same process ID (and thread ID) Communicatio n Must explicitly communicate, e.g.pipes or use small integer return value May communicate with return value or shared variables if done carefully May communicate with return value or shared variables (don't have to be careful) Parallelism (one CPU) Concurrent Sequential Parallelism (multiple CPUs) May be executed simultaneously Kernel threads may be executed simultaneously Sequential Threads versus Processes

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Thread Components address space: code and global variables open files semaphores signals timers process ID A thread has its own program counter and stack, but shares a number of resources with its process and other threads of the process:

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Thread Components

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Creating a Thread When a new thread is created it runs concurrently with the creating process. When creating a thread you indicate which function the thread should execute.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Normal function call

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Threaded function call

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Pthread Operations POSIX functiondescription pthread_creat e create a thread pthread_detac h set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_kill send a signal to a thread pthread_join wait for a thread pthread_self find out own thread ID