CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Advertisements

Processes and Threads Prof. Sirer CS 4410 Cornell University.
CSE 451: Operating Systems Winter 2007 Module 4 Processes Ed Lazowska Allen Center 570.
IT344 – Operating Systems Winter 2011, Dale Rowe.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
1 Last Time: OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features.
1 Processes Professor Jennifer Rexford
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.
Chapter 4: Threads. 2 What’s in a process? A process consists of (at least): –an address space –the code for the running program –the data for the running.
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.
CSE451 Processes Spring 2001 Gary Kimura Lecture #4 April 2, 2001.
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.
CSE 451: Operating Systems Spring 2012 Module 4 Processes Ed Lazowska Allen Center 570.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Lecture Topics: 11/3 Address spaces Memory protection Creating a process –NT style –Unix style.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Multiprogramming CSE451 Andrew Whitaker. Overview Multiprogramming: Running multiple programs “at the same time”  Requires multiplexing (sharing) the.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
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,
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Computer Studies (AL) Operating System Process Management - Process.
Processes Questions answered in this lecture: What is a process? How does the dispatcher context-switch between processes? How does the OS create a new.
H.-H. S. Lee 1 ECE3055 Computer Architecture and Operating Systems Lecture 10 Process, Thread Prof. Hsien-Hsin Sean Lee School of Electrical and Computer.
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
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” –
Chapter 3: Process Concept
CSE 120 Principles of Operating
Process Management Presented By Aditya Gupta Assistant Professor
CSE 451: Operating Systems Autumn 2013 Module 4 Processes
Chapter 3: Processes.
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Processes in Unix, Linux, and Windows
IT 344: Operating Systems Module 4 Processes
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
CSE 153 Design of Operating Systems Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
Processes Hank Levy 1.
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
Tutorial: The Programming Interface
Jonathan Walpole Computer Science Portland State University
October 7, 2002 Gary Kimura Lecture #4 October 7, 2002
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
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Processes Hank Levy 1.
CSE 451: Operating Systems Autumn 2010 Module 4 Processes
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Presentation transcript:

CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads

2 Announcements l Homework 1 out (to be posted on ilearn) l Project 1 u Make sure to go over it so that you can ask the TAs in lab if anything is unclear u Both design document and code due before class on Feb. 3 rd l Read scheduling and synchronization in textbook u Don’t wait for these topics to be covered in class u You especially need to understand priority donation in project l Piazza enrollment u Some of you haven’t enrolled u You will miss announcements – especially about projects l All set with project groups? u your TA today if you have not notified group or if you are looking for a partner

3 Process Creation: Unix l In Unix, processes are created using fork() int fork() Usually combined with exec() fork() + exec() ~= CreateProcess() l fork() u Creates and initializes a new PCB u Creates a new address space u Initializes the address space with a copy of the entire contents of the address space of the parent u Initializes the kernel resources to point to the resources used by parent (e.g., open files) u Places the PCB on the ready queue l Fork returns twice u Returns the child’s PID to the parent, “0” to the child

4 fork() int main(int argc, char *argv[]) { char *name = argv[0]; int child_pid = fork(); if (child_pid == 0) { printf(“Child of %s is %d\n”, name, getpid()); return 0; } else { printf(“My child is %d\n”, child_pid); return 0; } What does this program print?

5 Example Output [well ~]$ gcc t.c [well ~]$./a.out My child is 486 Child of a.out is 486

6 Duplicating Address Spaces child_pid = fork(); if (child_pid == 0) { printf(“child”); } else { printf(“parent”); } ParentChild child_pid = fork(); if (child_pid == 0) { printf(“child”); } else { printf(“parent”); } PC child_pid = 486child_pid = 0 PC

7 Divergence child_pid = fork(); if (child_pid == 0) { printf(“child”); } else { printf(“parent”); } ParentChild child_pid = fork(); if (child_pid == 0) { printf(“child”); } else { printf(“parent”); } PC child_pid = 486child_pid = 0

8 Example Continued [well ~]$ gcc t.c [well ~]$./a.out My child is 486 Child of a.out is 486 [well ~]$./a.out Child of a.out is 498 My child is 498 Why is the output in a different order?

9 Why fork()? l Very useful when the child… u Is cooperating with the parent u Relies upon the parent’s data to accomplish its task l Example: Web server while (1) { int sock = accept(); if ((child_pid = fork()) == 0) { Handle client request } else { Close socket }

10 Process Creation: Unix (2) l Wait a second. How do we actually start a new program? int exec(char *prog, char *argv[]) l exec() u Stops the current process u Loads the program “prog” into the process’ address space u Initializes hardware context and args for the new program u Places the PCB onto the ready queue u Note: It does not create a new process l What does it mean for exec to return? l What does it mean for exec to return with an error?

11 Process Creation: Unix (3) l fork() is used to create a new process, exec is used to load a program into the address space l What happens if you run “exec csh” in your shell? l What happens if you run “exec ls” in your shell? Try it. l fork() can return an error. Why might this happen?

12 Process Termination l All good processes must come to an end. But how? u Unix: exit(int status), NT: ExitProcess(int status) l Essentially, free resources and terminate u Terminate all threads (coming up) u Close open files, network connections u Allocated memory (and VM pages out on disk) u Remove PCB from kernel data structures, delete l Note that a process does not need to clean up itself u OS will handle this on its behalf

13 wait() a second… l Often it is convenient to pause until a child process has finished u Think of executing commands in a shell l Use wait() (WaitForSingleObject) u Suspends the current process until a child process ends u waitpid() suspends until the specified child process ends l Wait has a return value…what is it? l Unix: Every process must be reaped by a parent u What happens if a parent process exits before a child? u What do you think is a “zombie” process?

14 Unix Shells while (1) { char *cmd = read_command(); int child_pid = fork(); if (child_pid == 0) { Manipulate STDIN/OUT/ERR file descriptors for pipes, redirection, etc. exec(cmd); panic(“exec failed”); } else { if (!(run_in_background)) waitpid(child_pid); }

15 Processes l Recall that … u A process includes many things: »An address space (all code and data pages) »OS resources (e.g., open files) and accounting info »Execution state (PC, SP, regs, etc.) u Processes are completely isolated from each other l Creating a new process is costly because of all of the data structures that must be allocated and initialized u Recall struct proc in Solaris u Expensive even with OS tricks l Communicating between processes is costly because most communication goes through the OS u Overhead of system calls and copying data P1 P2 OS

16 Parallel Programs l Also recall our Web server example that forks off copies of itself to handle multiple simultaneous requests u Or any parallel program that executes on a multiprocessor l To execute these programs we need to u Create several processes that execute in parallel u Cause each to map to the same address space to share data »They are all part of the same computation u Have the OS schedule these processes in parallel l This situation is very inefficient u Space: PCB, page tables, etc. u Time: create data structures, fork and copy addr space, etc.

17 Rethinking Processes l What is similar in these cooperating processes? u They all share the same code and data (address space) u They all share the same privileges u They all share the same resources (files, sockets, etc.) l What don’t they share? u Each has its own execution state: PC, SP, and registers l Key idea: Separate resources from execution state l Exec state also called thread of control, or thread

18 Recap: Process Components l A process is named using its process ID (PID) l A process contains all of the state for a program in execution u An address space u The code for the executing program u The data for the executing program u A set of operating system resources »Open files, network connections, etc. u An execution stack encapsulating the state of procedure calls u The program counter (PC) indicating the next instruction u A set of general-purpose registers with current values u Current execution state (Ready/Running/Waiting) Per- Process State Per- Thread State

19 Threads l Modern OSes (Mac OS, Windows, Linux) separate the concepts of processes and threads u The thread defines a sequential execution stream within a process (PC, SP, registers) u The process defines the address space and general process attributes (everything but threads of execution) l A thread is bound to a single process u Processes, however, can have multiple threads l Threads become the unit of scheduling u Processes are now the containers in which threads execute u Processes become static, threads are the dynamic entities

20 Recap: Process Address Space Stack 0x xFFFFFFFF Code (Text Segment) Static Data (Data Segment) Heap (Dynamic Memory Alloc) Address Space SP PC

21 Threads in a Process Stack (T1) Code Static Data Heap Stack (T2)Stack (T3) Thread 1 Thread 3 Thread 2 PC (T1) PC (T3) PC (T2)

22 Thread Design Space One Thread/Process Many Address Spaces (Early Unix) One Thread/Process One Address Space (MSDOS) Many Threads/Process Many Address Spaces (Mac OS, Unix, Windows) Many Threads/Process One Address Space (Pilot, Java) Address Space Thread

23