CS 3214 Computer Systems Lecture 13 Godmar Back.

Slides:



Advertisements
Similar presentations
Operating Systems Lecture 7.
Advertisements

University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
CSE 451: Operating Systems Winter 2007 Module 4 Processes Ed Lazowska Allen Center 570.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Process Control in Unix Operating Systems Hebrew University Spring 2004.
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.
CSSE Operating Systems
Exceptional Control Flow Part I Topics Exceptions Process context switches Creating and destroying processes.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
BINA RAMAMURTHY UNIVERSITY AT BUFFALO System Structure and Process Model 5/30/2013 Amrita-UB-MSES
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 Program and OS interactions: Exceptions and Processes Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
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.
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,
CS 3214 Computer Systems Godmar Back Lecture 12. Announcements Exercise 6 coming up Project 3 milestone due Oct 8 CS 3214 Fall 2010.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Creating and Executing Processes
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
System calls for Process management
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Exceptional Control Flow I March 12, 2002
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 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
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.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
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.
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” –
CS 3214 Introduction to Computer Systems
CS 3214 Computer Systems Lecture 9 Godmar Back.
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Exceptional Control Flow & 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.
CS703 – Advanced Operating Systems
Tarek Abdelzaher Vikram Adve Marco Caccamo
System Structure and Process Model
System Structure and Process Model
Processes in Unix, Linux, and Windows
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
CSE 451: Operating Systems Winter 2007 Module 4 Processes
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
Presentation transcript:

CS 3214 Computer Systems Lecture 13 Godmar Back

Announcements Project 3 milestone due Oct 8 Have still not heard from everybody regarding SVN Exercise 6 handed out CS 3214 Fall 2010

Part 1 Threads and Processes CS 3214 Fall 2010

A Context Switch Scenario Timer interrupt: P1 is preempted, context switch to P2 I/O device interrupt: P2’s I/O complete switch back to P2 Process 1 Process 2 user mode kernel mode System call: (trap): P2 starts I/O operation, blocks context switch to process 1 Timer interrupt: P2 still has time left, no context switch Kernel CS 3214 Fall 2010

Bottom Up View: Exceptions An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS event current exception next exception processing by exception handler exception return (optional) CS 3214 Fall 2010

Reasoning about Processes: Process States RUNNING READY BLOCKED Process must wait for event Event arrived Scheduler picks process preempted Only 1 process (per CPU) can be in RUNNING state CS 3214 Fall 2010

User View If process’s lifetimes overlap, they are said to execute concurrently Else they are sequential Default assumption is concurrently Exact execution order is unpredictable Programmer should never make any assumptions about it Any interaction between processes must be carefully synchronized CS 3214 Fall 2010

fork() #include <unistd.h> #include <stdio.h> int main() { int x = 1; if (fork() == 0) { // only child executes this printf("Child, x = %d\n", ++x); } else { // only parent executes this printf("Parent, x = %d\n", --x); } // parent and child execute this printf("Exiting with x = %d\n", x); return 0; fork() Child, x = 2 Exiting with x = 2 Parent, x = 0 Exiting with x = 0 CS 3214 Fall 2010

The fork()/join() paradigm After fork(), parent & child execute in parallel Unlike a fork in the road, here we take both roads Used in many contexts In Unix, ‘join()’ is called wait() Purpose: Launch activity that can be done in parallel & wait for its completion Or simply: launch another program and wait for its completion (shell does that) Parent: fork() Parent process executes Child process executes Child process exits Parent: join() OS notifies CS 3214 Fall 2010

fork() #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(int ac, char *av[]) { pid_t child = fork(); if (child < 0) perror(“fork”), exit(-1); if (child != 0) { printf ("I'm the parent %d, my child is %d\n", getpid(), child); wait(NULL); /* wait for child (“join”) */ } else { printf ("I'm the child %d, my parent is %d\n", getpid(), getppid()); execl("/bin/echo", "echo", "Hello, World", NULL); } CS 3214 Fall 2010

fork() vs. exec() fork(): exec(): Clone most state of parent, including memory Inherit some state, e.g. file descriptors Keeps program, changes process Called once, returns twice exec(): Overlays current process with new executable Keeps process, changes program Called once, does not return (if successful) CS 3214 Fall 2010

exit(3) vs. _exit(2) exit(3) destroys current processes OS will free resources associated with it E.g., closes file descriptors, etc. etc. Can have atexit() handlers _exit(2) skips them Exit status is stored and can be retrieved by parent Single integer Convention: exit(EXIT_SUCCESS) signals successful execution, where EXIT_SUCCESS is 0 CS 3214 Fall 2010

wait() vs waitpid() int wait(int *status) Blocks until any child exits If status != NULL, will contain value child passed to exit() Return value is the child pid Can also tell if child was abnormally terminated int waitpid(pid_t pid, int *status, int options) Can say which child to wait for CS 3214 Fall 2010

If multiple children completed, wait() returns them 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); } Wait Example CS 3214 Fall 2010

Observations on fork/exit/wait Process can have many children at any point in time Establishes a parent/child relationship Resulting in a process tree Zombies: processes that have exited, but their parent hasn’t waited for them “Reaping a child process” – call wait() so that zombie’s resources can be destroyed Orphans: processes that are still alive, but whose parent has already exited (without waiting for them) Become the child of a dedicated process (“init”) who will reap them when they exit “Run Away” processes: processes that (unintentionally) execute an infinite loop and thus don’t call exit() or wait() CS 3214 Fall 2010

Unix File Descriptors Unix provides a file descriptor abstraction File descriptors are Small integers that have a local meaning within one process Can be obtained from kernel Several functions create them, e.g. open() Can refer to various kernel objects (not just files) Can be passed to a standard set of functions: read, write, close, lseek, (and more) Can be inherited when a process forks a child CS 3214 Fall 2010

Examples 0-2 are initially assigned int fd = open(“file”, O_RDONLY); 0 – stdin 1 – stdout 2 – stderr But this assignment is not fixed – process can change it via syscalls int fd = open(“file”, O_RDONLY); int fd = creat(“file”, 0600); CS 3214 Fall 2010

Implementing I/O Redirection dup and dup2() system call pipes: pipe(2) CS 3214 Fall 2010

dup2 #include <stdio.h> #include <stdlib.h> // redirect stdout to a file int main(int ac, char *av[]) { int c; int fd = creat(av[1], 0600); if (fd == -1) perror("creat"), exit(-1); if (dup2(fd, 1) == -1) perror("dup2"), exit(-1); while ((c = fgetc(stdin)) != EOF) fputc(c, stdout); } CS 3214 Fall 2010

The Big Picture user view kernel view Process 1 1 2 Terminal Device 3 1 2 Terminal Device 3 Open File File Descriptor x open(“x”) 4 File Descriptor open(“x”) close(4) Process 2 1 2 3 dup2(3,0) CS 3214 Fall 2010

Reference Counting Multiple file descriptors may refer to same open file Within the same process: fd = open(“file”); fd2 = dup(fd); Across anchestor processes: fd = open(“file”); fork(); But can also open a file multiple times: fd = open(“file”); fd2 = open(“file”); In this case, fd and fd2 have different read/write offsets In both cases, closing fd does not affect fd2 Reference Counting at 2 Levels: Kernel keeps track of how many processes refer to a file descriptor –fork() and dup() may add refs And keeps track of how many file descriptors refer to open file close(fd) removes reference in current process CS 3214 Fall 2010

Practical Implications Number of simultaneously open file descriptors per process is limited 1024 on current Linux, for instance Must make sure fd’s are closed Else ‘open()’ may fail Number space is reused “double-close” error may inadvertently close a new file descriptor assigned the same number CS 3214 Fall 2010

IPC via “pipes” Fixed Capacity Buffer write() read() A bounded buffer providing a stream of bytes flowing through Properties Writer() can put data in pipe as long as there is space If pipe() is full, writer blocks until reader reads() Reader() drains pipe() If pipe() is empty, readers blocks until writer writes Classic abstraction Decouples reader & writer Safe – no race conditions Automatically controls relative progress – if writer produces data faster than reader can read it, it blocks – and OS will likely make CPU time available to reader() to catch up. And vice versa. CS 3214 Fall 2010

pipe Note: there is no race condition in int main() { int pipe_ends[2]; if (pipe(pipe_ends) == -1) perror("pipe"), exit(-1); int child = fork(); if (child == -1) perror("fork"), exit(-1); if (child == 0) { char msg[] = { "Hi" }; close(pipe_ends[0]); write(pipe_ends[1], msg, sizeof msg); } else { char bread, pipe_buf[128]; close(pipe_ends[1]); printf("Child said "); fflush(stdout); while ((bread = read(pipe_ends[0], pipe_buf, sizeof pipe_buf)) > 0) write(1, pipe_buf, bread); } pipe Note: there is no race condition in this code. No matter what the scheduling order is, the message sent by the child will reach the parent. CS 3214 Fall 2010

esh – extensible shell Open-ended assignment Encourage collaborative learning Run each other’s plug-ins Does not mean collaboration on your implementation Secondary goals: Exposure to yacc/lex and exposure to OO-style programming in C CS 3214 Fall 2010

Using the list implementation list_entry(e, struct esh_command, elem) struct esh_pipeline: …. struct list commands struct esh_command: …. struct list_elem elem; struct list_elem *next; struct list_elem *prev; struct esh_command: …. struct list_elem elem; struct list_elem *next; struct list_elem *prev; struct list_elem head struct list_elem tail struct list_elem *next; struct list_elem *prev; struct list_elem *next; struct list_elem *prev; Key features: “list cell” – here call ‘list_elem’ is embedded in each object being kept in list Means you need 1 list_elem per list you want to keep an object in CS 3214 Fall 2010