1 Processes Professor Jennifer Rexford

Slides:



Advertisements
Similar presentations
CSE 451: Operating Systems Winter 2007 Module 4 Processes Ed Lazowska Allen Center 570.
Advertisements

CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
1 Operating Systems and Protection Professor Jennifer Rexford CS 217.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
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.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Lecture 5 Process, Thread and Task September 22, 2015 Kyu Ho Park.
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.
Chapter 3 Process Description and Control
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CE Operating Systems Lecture 10 Processes and process management in Linux.
8-Sep Operating Systems Yasir Kiani. 8-Sep Agenda for Today Review of previous lecture Process scheduling concepts Process creation and termination.
Processes – Part I Processes – Part I. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Review on OSs Upon brief introduction of OSs,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Processes Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
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.
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.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
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.
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.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
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” –
Lecture 3 Process.
Processes and threads.
Chapter 3: Process Concept
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Operating Systems Lecture 6.
Process & its States Lecture 5.
Chapter 3: Processes.
I/O and Process Management
Lecture 6: Multiprogramming and Context Switching
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
IT 344: Operating Systems Winter 2008 Module 4 Processes
CSE 451: Operating Systems Winter 2007 Module 4 Processes
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

1 Processes Professor Jennifer Rexford

2 Goals of Today’s Lecture Processes  Process vs. program  Context switching Creating a new process  Fork: process creates a new child process  Wait: parent waits for child process to complete  Exec: child starts running a new program  System: combines fork, wait, and exec all in one Communication between processes  Pipe between two processes  Redirecting stdin and stdout

3 Processes

4 Program vs. Process Program  Executable code, no dynamic state Process  An instance of a program in execution, with its own –Address space (illusion of a memory) Text, RoData, BSS, heap, stack –Processor state (illusion of a processor) EIP, EFLAGS, registers –Open file descriptors (illusion of a disk)  Either running, waiting, or ready… Can run multiple instances of the same program  Each as its own process, with its own process ID

5 Life Cycle of a Process Running: instructions are being executed Waiting: waiting for some event (e.g., I/O finish) Ready: ready to be assigned to a processor CreateReadyRunningTermination Waiting

6 OS Supports Process Abstraction Supporting the abstraction  Multiple processes share resources  Protected from each other Main memory  Swapping pages to/from disk  Virtual memory Processor  Switching which process gets to run on the CPU  Saving per-process state on a “context switch” Hardware Operating System User Process User Process

7 When to Change Which Process is Running? When a process is stalled waiting for I/O  Better utilize the CPU, e.g., while waiting for disk access When a process has been running for a while  Sharing on a fine time scale to give each process the illusion of running on its own machine  Trade-off efficiency for a finer granularity of fairness CPU I/O 1: CPU I/O 2:

8 Switching Between Processes Context  State the OS needs to restart a preempted process Context switch  Saving the context of current process  Restoring the saved context of some previously preempted process  Passing control to this newly restored process Running Save context Load context Save context Load context RunningWaiting Process 1 Process 2

9 Context: What the OS Needs to Save Process state  New, ready, waiting, halted CPU registers  EIP, EFLAGS, EAX, EBX, … I/O status information  Open files, I/O requests, … Memory management information  Page tables Accounting information  Time limits, group ID,... CPU scheduling information  Priority, queues

10 Creating a New Process

11 Why Start a New Process? Run a new program  E.g., shell executing a program entered at command line  Or, even running an entire pipeline of commands  Such as “ wc –l * | sort | uniq -c | sort –nr” Run a new thread of control for the same program  E.g., a Web server handling a new Web request  While continuing to allow more requests to arrive  Essentially time sharing the computer Underlying mechanism  A process runs “fork” to create a child process  (Optionally) child process does “exec” of a new program

12 Creating a New Process Cloning an existing process  Parent process creates a new child process  The two processes then run concurrently Child process inherits state from parent  Identical (but separate) copy of virtual address space  Copy of the parent’s open file descriptors  Parent and child share access to open files Child then runs independently  Executing independently, including invoking a new program  Reading and writing its own address space parent child

13 Fork System Call Fork is called once  But returns twice, once in each process Telling which process is which  Parent: fork() returns the child’s process ID  Child: fork() returns a 0 pid = fork(); if (pid != 0) { /* in parent */ … } else { /* in child */ … }

14 Fork and Process State Inherited  User and group IDs  Signal handling settings  Stdio  File pointers  Root directory  File mode creation mask  Resource limits  Controlling terminal  All machine register states  Control register(s)  … Separate in child  Process ID  Address space (memory)  File descriptors  Parent process ID  Pending signals  Time signal reset times  …

15 Example: 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); exit(0); }

16 Executing a New Program Fork copies the state of the parent process  Child continues running the parent program  … with a copy of the process memory and registers Need a way to invoke a new program  In the context of the newly-created child process Example execlp(“ls”, “ls”, “-l”, NULL); fprintf(stderr, “exec failed\n”); exit(1); program null-terminated list of arguments (to become “argv[]”)

17 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 pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);

18 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 fork ls waitexecvp bash

19 Example: A Simple Shell … parse command line … pid = fork(); if (pid == -1) fprintf(stderr, “fork failed\n”); else if (pid == 0) { /* in child */ execvp(file, argv); fprintf(stderr, “exec failed\n”); } else { /* in parent */ pid = wait(&status); } … return to top of loop

20 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”); }

21 Communication Between Processes

22 Communication Between Processes different machines same machine

23 Interprocess Communication Pipes  Processes on the same machine  One process spawns the other  Used mostly for a pipeline of filters Sockets  Processes on any machines  Processes created independently  Used for client/server communication (e.g., Web) Both provide abstraction of an “ordered stream of bytes.”

24 Pipes

25 Example Use of Pipes Compute a histogram of content types in my  Many messages, consisting of many lines  Lines like “Content-Type: image/jpeg” indicate the type Pipeline of UNIX commands  Identifying content type: grep -i Content-Type *  Extracting just the type: cut -d" " -f2  Sorting the list of types: sort  Counting the unique types: uniq -c  Sorting the counts: sort –nr Simply running this at the shell prompt:  grep -i Content-Type * | cut -d" " -f2 | sort | uniq -c | sort –nr

26 Creating a Pipe

27 Pipe Example child parent

28 Dup a.out < foo

29 Dup2

30 Pipes and Stdio child makes stdin (0) the read side of the pipe parent makes stdout (1) the write side of the pipe

31 Pipes and Exec child process invokes a new program

32 The Beginnings of a UNIX Shell A shell is mostly a big loop  Parse command line from stdin  Expand wildcards (‘*’)  Interpret redirections (‘|’, ‘ ’)  Pipe, fork, dup, exec, and wait, as necessary Start from the code in earlier slides  And edit till it becomes a UNIX shell  This is the heart of the last programming assignment

33 Conclusion Processes  An instance of a program in execution  Shares CPU with other processes  May also communicate with other processes System calls for creating processes  Fork: process creates a new child process  Wait: parent waits for child process to complete  Exec: child starts running a new program  System: combines fork, wait, and exec all in one System calls for inter-process communication  Pipe: create a pipe with a write end and a read end  Open/close: to open or close a file  Dup2: to duplicate a file descriptor