CS703 – Advanced Operating Systems

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
Advertisements

University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
University of Washington Today Finish up cache-aware programming example Processes  So we can move on to virtual memory 1.
University of Washington Today Move on to … 1. University of Washington Hmm… Lets look at this again Disk Main Memory L2 unified cache L1 I-cache L1 D-cache.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
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.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class14.ppt CS 123.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 Program and OS interactions: Exceptions and Processes Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
Fabián E. Bustamante, Spring 2007 Exceptional Control Flow Part I Today Exceptions Process context switches Creating and destroying processes Next time.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Exceptional Control Flow Part I March 4, 2004 Topics Exceptions Process context switches Creating and destroying processes class16.ppt “The course.
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
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes : Introduction to Computer Systems 13 th Lecture, Oct. 7, 2014 Instructors:
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
University of Amsterdam Computer Systems – Processes Arnoud Visser 1 Computer Systems Processes.
Program Translation and Execution II: Processes Oct 1, 1998 Topics User-level view of processes Implementation of processes setjmp/longjmp class12.ppt.
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes class16.ppt.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Operating Systems Process Creation
PROCESS AND THREADS. Three ways for supporting concurrency with socket programming  I/O multiplexing  Select  Epoll: man epoll  Libevent 
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Exceptional Control Flow I March 12, 2002
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
Carnegie Mellon Introduction to Computer Systems /18-243, spring th Lecture, Mar. 3 rd Instructors: Gregory Kesden and Markus Püschel.
Carnegie Mellon Exceptions and Processes Slides adapted from: Gregory Kesden and Markus Püschel of Carnegie Mellon University.
Overview of today’s lecture Re-view of the previous lecture Process management models and state machines (cont’d from the previous lecture) What is in.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
CS 3214 Introduction to Computer Systems
Section 8: Processes What is a process Creating processes Fork-Exec
Exceptional Control Flow & Processes
Instructor: Randy Bryant
Exceptional Control Flow Part I
Instructors: Anthony Rowe, Seth Goldstein and Gregory Kesden
Processes in Unix, Linux, and Windows
Chapter 4: Threads.
Processes in Unix, Linux, and Windows
Processes CSE 351 Autumn 2016 Instructor: Justin Hsia
Processes in Unix, Linux, and Windows
CS 105 “Tour of the Black Holes of Computing!”
Exceptional Control Flow Part I Mar. 11, 2003
Exceptional Control Flow Ⅱ
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Exceptional Control Flow Part I
CSE 451: Operating Systems Spring 2008 Module 5 Threads
Process Programming Interface
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
Jonathan Walpole Computer Science Portland State University
CS 105 “Tour of the Black Holes of Computing!”
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
CS 105 “Tour of the Black Holes of Computing!”
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Exceptional Control Flow & Processes October 2, 2008
Chapter 3 Process Description and Control
Presentation transcript:

CS703 – Advanced Operating Systems By Mr. Farhan Zaidi

Lecture No. 6

Overview of today’s lecture Fork examples (cont’d from previous lecture) Zombies and the concept of Reaping Wait and waitpid system calls in Linux Concurrency—The need for threads within processes Threads—Introduction Re-cap of lecture

Fork Example #3 Both parent and child can continue forking Key Points void fork3() { printf("L0\n"); fork(); printf("L1\n"); printf("L2\n"); printf("Bye\n"); } L1 L2 Bye L0

Fork Example #4 Both parent and child can continue forking Key Points void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); printf("L2\n"); fork(); } printf("Bye\n"); L1 Bye L2 Bye Bye L0

Fork Example #5 Both parent and child can continue forking Key Points void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); printf("L2\n"); fork(); } printf("Bye\n"); Bye Bye L2 Bye L1 L0

exit: Destroying Process void exit(int status) exits a process Normally return with status 0 atexit() registers functions to be executed upon exit void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0);

Zombies When process terminates, still consumes system resources Idea When process terminates, still consumes system resources Various tables maintained by OS Called a “zombie” Living corpse, half alive and half dead Reaping Performed by parent on terminated child Parent is given exit status information Kernel discards process What if Parent Doesn’t Reap? If any parent terminates without reaping a child, then child will be reaped by init process Only need explicit reaping for long-running processes E.g., shells and servers

Zombie Example ps shows child process as “defunct” void fork7() { if (fork() == 0) { /* Child */ printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", while (1) ; /* Infinite loop */ } linux> ./forks 7 & [1] 6639 Running Parent, PID = 6639 Terminating Child, PID = 6640 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00 forks <defunct> 6641 ttyp9 00:00:00 ps linux> kill 6639 [1] Terminated 6642 ttyp9 00:00:00 ps ps shows child process as “defunct” Killing parent allows child to be reaped

Non-terminating Child Example void fork8() { if (fork() == 0) { /* Child */ printf("Running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } else { printf("Terminating Parent, PID = %d\n", getpid()); exit(0); } Non-terminating Child Example linux> ./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> kill 6676 6678 ttyp9 00:00:00 ps Child process still active even though parent has terminated Must kill explicitly, or else will keep running indefinitely

wait: Synchronizing with children int wait(int *child_status) suspends current process until one of its children terminates return value is the pid of the child process that terminated if child_status != NULL, then the object it points to will be set to a status indicating why the child process terminated

wait: Synchronizing with children void fork9() { int child_status; if (fork() == 0) { printf("HC: hello from child\n"); } else { printf("HP: hello from parent\n"); wait(&child_status); printf("CT: child has terminated\n"); printf("Bye\n"); exit(); HP HC Bye CT Bye

Wait Example If multiple children completed, will take in arbitrary order Can use macros WIFEXITED and WEXITSTATUS to get information about exit status void fork10() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminate abnormally\n", wpid); }

Waitpid waitpid(pid, &status, options) Can wait for specific process Various options void fork11() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = waitpid(pid[i], &child_status, 0); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); }

exec: Running new programs int execl(char *path, char *arg0, char *arg1, …, 0) loads and runs executable at path with args arg0, arg1, … path is the complete path of an executable arg0 becomes the name of the process typically arg0 is either identical to path, or else it contains only the executable filename from path “real” arguments to the executable start with arg1, etc. list of args is terminated by a (char *)0 argument returns -1 if error, otherwise doesn’t return! main() { if (fork() == 0) { execl("/usr/bin/cp", "cp", "foo", "bar", 0); } wait(NULL); printf("copy completed\n"); exit();

Summarizing Exceptions Processes Events that require nonstandard control flow Generated externally (interrupts) or internally (traps and faults) Processes At any given time, system has multiple active processes Only one can execute at a time, though Each process appears to have total control of processor + private memory space

Summarizing (cont.) Call to fork One call, two returns Call exit Spawning Processes Call to fork One call, two returns Terminating Processes Call exit One call, no return Reaping Processes Call wait or waitpid Replacing Program Executed by Process Call execl (or variant) One call, (normally) no return

Concurrency Imagine a web server, which might like to handle multiple requests concurrently While waiting for the credit card server to approve a purchase for one client, it could be retrieving the data requested by another client from disk, and assembling the response for a third client from cached information Imagine a web client (browser), which might like to initiate multiple requests concurrently Imagine a parallel program running on a multiprocessor, which might like to employ “physical concurrency” For example, multiplying a large matrix – split the output matrix into k regions and compute the entries in each region concurrently using k processors

What’s in a process? an address space the code for the running program A process consists of (at least): an address space the code for the running program the data for the running program an execution stack and stack pointer (SP) traces state of procedure calls made the program counter (PC), indicating the next instruction a set of general-purpose processor registers and their values a set of OS resources open files, network connections, sound channels, … That’s a lot of concepts bundled together! decompose … threads of control (other resources…)

What’s needed? Everybody wants to run the same code In each of these examples of concurrency (web server, web client, parallel program): Everybody wants to run the same code Everybody wants to access the same data Everybody has the same privileges Everybody uses the same resources (open files, network connections, etc.) But you’d like to have multiple hardware execution states: an execution stack and stack pointer (SP) traces state of procedure calls made the program counter (PC), indicating the next instruction a set of general-purpose processor registers and their values

How could we achieve this? Given the process abstraction as we know it: fork several processes cause each to map to the same physical memory to share data It’s really inefficient space: PCB, page tables, etc. time: creating OS structures, fork and copy addr space, etc.

Can we do better? Key idea: separate the concept of a process (address space, etc.) …from that of a minimal “thread of control” (execution state: PC, etc.) This execution state is usually called a thread, or sometimes, a lightweight process

Threads and processes Most modern OS’s (Mach, Chorus, NT, modern UNIX) therefore support two entities: the process, which defines the address space and general process attributes (such as open files, etc.) the thread, which defines a sequential execution stream within a process A thread is bound to a single process / address space address spaces, however, can have multiple threads executing within them sharing data between threads is cheap: all see the same address space creating threads is cheap too! Threads become the unit of scheduling processes / address spaces are just containers in which threads execute