Tarek Abdelzaher Vikram Adve Marco Caccamo

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

CSCC69: Operating Systems
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Introduction to Processes
The Process Model.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Unix Processes.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
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
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
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.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
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.
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)
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Chapter 3: Processes.
Protection of System Resources
Using Processes.
Unix Process Management
Chapter 3: Processes.
Processes A process is a running program.
Processes in Unix, Linux, and Windows
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.
System Structure and Process Model
System Structure and Process Model
Structure of Processes
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
System Structure and Process Model
Operating Systems Lecture 6.
Process Creation Process Termination
Process Description and Control
Processes Prof. Ikjun Yeom TA – Mugyo
Process Description and Control
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CS510 Operating System Foundations
Process Description and Control in Unix
Process Description and Control in Unix
Chapter 3 Process Description and Control
Presentation transcript:

Tarek Abdelzaher Vikram Adve Marco Caccamo Processes Tarek Abdelzaher Vikram Adve Marco Caccamo Copyright ©: Nahrstedt, Angrave, Abdelzaher

Users, Programs, Processes Copyright ©: Nahrstedt, Angrave, Abdelzaher Users, Programs, Processes Users have accounts on the system Users launch programs Many users may launch same program One user may launch many instances of the same program Processes: an executing program (the unit of work of a modern computer)

Copyright ©: Nahrstedt, Angrave, Abdelzaher Process States Possible process states (simplified view): Running (occupy CPU) Blocked Ready (does not occupy CPU)

Linux: 5 State Process Model Copyright ©: Nahrstedt, Angrave, Abdelzaher Linux: 5 State Process Model Add states for creating and deleting process Add transitions Timeout, Dispatch, Event Occurs

How to List all Processes? Copyright ©: Nahrstedt, Angrave, Abdelzaher How to List all Processes? On Windows: run Windows task manager Hit Control+ALT+delete Click on the “processes” tab On UNIX $ ps –e Try “man ps” to understand more about its usage.

Process Identification Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Identification UNIX identifies processes via a unique Process ID Each process also knows its parent process ID since each process is created from a parent process. Root process is the ‘init’ process ‘getpid’ and ‘getppid’ – functions to return process ID (PID) and parent process ID (PPID) Example 1 #include <stdio.h> #include <unistd.h> int main (void) { printf(“I am process %ld\n”, (long)getpid()); printf(“My parent id is %ld\n”, (long)getppid()); return 0; }

Creating a Process – fork() Copyright ©: Nahrstedt, Angrave, Abdelzaher Creating a Process – fork() pid_t fork() creates a duplicate of the calling process: Both processes continue with return from fork() Child gets exact copy of code, stack, file descriptors, heap, globals, and program counter fork() returns -1 if fork fails 0 in child process child’s PID in parent process

Creating a child Process – fork() Copyright ©: Nahrstedt, Angrave, Abdelzaher Creating a child Process – fork() fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points: The child has its own unique process ID The child does not inherit its parent’s memory locks (e.g., mlock(2)). Process resource utilizations and CPU time counters are reset to zero in the child. The child’s set of pending signals is initially empty. The child does not inherit record locks from its parent (fcntl(2)). The child does not inherit timers from its parent. The termination signal of the child (sent to the parent) is always SIGCHLD. 8 8

Creating a child Process – fork() Copyright ©: Nahrstedt, Angrave, Abdelzaher Creating a child Process – fork() The entire virtual address space of the parent is replicated in the child, including the states of mutexes, etc. The child inherits copies of the parent’s set of open file descriptors. Each file descriptor in the child refers to the same open file description (see open(2)) as the corresponding file descriptor in the parent. This means that the two descriptors share open file status flags, and current file offset. 9 9

Creating a Process in Unix Copyright ©: Nahrstedt, Angrave, Abdelzaher Creating a Process in Unix Example 2 #include <stdio.h> #include <unistd.h> int main(void) { pid_t x; x = fork(); if (x == 0) printf(“In child: fork() returned %ld\n”, (long) x); else printf(“In parent: fork() returned %ld\n", (long) x); } What does this print?

Copyright ©: Nahrstedt, Angrave, Abdelzaher Chain and Fan What is the difference between these two segments of code? pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) break; pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <=0) break;

Copyright ©: Nahrstedt, Angrave, Abdelzaher Chain and Fan Fan Chain pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <=0) break; pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) break; Parent Parent Child Child Child Child … … 12 12

Process Operations: Creation Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Operations: Creation A new process needs resources such as CPU, memory, file descriptors Child can get resources from OS or from parent Child address space is a duplicate of parent process Child process is given a subset of parent resources Prevents too many processes from overloading system Execution possibilities are Parent continues concurrently with child Parent waits until child has terminated

Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Termination Normal exit (voluntary) Returning zero from main() exit(0) Error exit (voluntary) exit(1) Fatal error (involuntary) Divide by 0, seg fault, exceeded resources Killed by another process (involuntary) Signal: kill(procID)

Process Operations: Termination Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Operations: Termination When a child process terminates: Open files are flushed and closed tmp files are deleted Child’s resources are de-allocated File descriptors, memory, semaphores, file locks, … Parent process is notified via signal SIGCHLD Exit status is available to parent via wait()

Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Hierarchies Parent creates a child process, a child process can create its own child processes Forms a hierarchy UNIX calls this a "process group" Windows has no concept of process hierarchy all processes are created equal

wait(), waitpid() System Calls Copyright ©: Nahrstedt, Angrave, Abdelzaher wait(), waitpid() System Calls pid_t wait(int *status); wait() causes parent process to wait (block) until some child finishes wait() returns child’s pid and exit status to parent waitpid() waits for a specific child errno Cause ECHILD Caller has no unwaited-for children EINTR Function was interrupted by signal EINVAL Options parameter of waitpid was invalid

Copyright ©: Nahrstedt, Angrave, Abdelzaher wait(), waitpid() System Calls pid_t wait(int *status); In the case of a terminated child, performing a wait allows the system to release the resources associated with the child; if a wait is not performed, then the terminated child remains in a "zombie“ state. If a child has already terminated, then the call returns immediately. Otherwise it blocks until either a child terminates or a signal handler interrupts the call. A child that has terminated and which has not yet been waited upon by this system call (or waitpid) is termed waitable. 18 18

Copyright ©: Nahrstedt, Angrave, Abdelzaher wait(), waitpid() System Calls If status is not NULL, wait() stores status information in the int to which it points. This integer can be inspected with specific macros (see man pages): WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit, or by returning from main(). WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true. 19 19

Copyright ©: Nahrstedt, Angrave, Abdelzaher wait() & “zombie” A child that terminates, but has not been waited for becomes a "zombie". The kernel maintains a minimal set of information about the zombie process (PID, termination status, resource usage information) in order to allow the parent to later perform a wait to obtain information about the child. As long as a zombie is not removed from the system via a wait, it will consume a slot in the kernel process table, and if this table fills, it will not be possible to create further processes. If a parent process terminates, then its "zombie" children (if any) are adopted by init(8), which automatically performs a wait to remove the zombies. 20 20

Waiting for a child to finish (Try “man –s 2 wait”) Copyright ©: Nahrstedt, Angrave, Abdelzaher Waiting for a child to finish (Try “man –s 2 wait”) #include <errno.h> #include <sys/wait.h> pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);