CS 3214 Computer Systems Godmar Back Lecture 12. Announcements Exercise 6 coming up Project 3 milestone due Oct 8 CS 3214 Fall 2010.

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
CS 4284 Systems Capstone Godmar Back Processes and Threads.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Carnegie Mellon 1 Exceptional Control Flow: Exceptions and Processes /18-243: Introduction to Computer Systems 13 th Lecture, June 15, 2011 Instructors:
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
University of Washington Today Midterm grades posted  76. HW 3 due Lab 4 fun! 1.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
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.
Exceptional Control Flow Part I September 22, 2008 Topics Exceptions Process context switches Creating and destroying processes class11.ppt ,
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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.
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.
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.
CS 3214 Computer Systems Lecture 13 Godmar Back.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
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!”
CE Operating Systems Lecture 10 Processes and process management in Linux.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
System calls for Process management
Operating Systems Processes 1.
Operating Systems Process Creation
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
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.
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.
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.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
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.
CS 3214 Introduction to Computer Systems
CS 3214 Computer Systems Lecture 9 Godmar Back.
Exceptional Control Flow & Processes
Overview of today’s lecture
Exceptional Control Flow Part I
Processes in Unix, Linux, and Windows
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
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Exceptional Control Flow Part I
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
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
Presentation transcript:

CS 3214 Computer Systems Godmar Back Lecture 12

Announcements Exercise 6 coming up Project 3 milestone due Oct 8 CS 3214 Fall 2010

THREADS AND PROCESSES Part 1 CS 3214 Fall 2010

Processes Def: An instance of a program in execution OS provides each process with key abstractions –Logical control flow 1 flow – single-threaded process Multiple flows – multi-threaded process –Private address space –Abstracted resources: e.g., stdout/stdin file descriptors These abstractions create the illusion that each process has access to its own –CPU (or CPUs for multi-threaded processes) –Memory –Devices: e.g., terminal CS 3214 Fall 2010

Context Switching Historical motivation for processes was introduction of multi-programming: –Load multiple processes into memory, and switch to another process if current process is (momentarily) blocked –This required protection and isolation between these processes, implemented by a privileged kernel Time-sharing: switch to another process periodically to make sure all processes make equal progress Switch between processes is called a context switch CS 3214 Fall 2010

Dual-Mode Operation Two fundamental modes: –“kernel mode” – privileged aka system, supervisor or monitor mode Intel calls its PL0, Privilege Level 0 on x86 –“user mode” – non-privileged PL3 on x86 Bit in CPU – controls operation of CPU –Privileged operations can only be performed in kernel mode. Example: hlt –Must carefully control transition between user & kernel mode int main() { asm(“hlt”); } int main() { asm(“hlt”); }

Mode Switching User  Kernel mode –For reasons external or internal to CPU External (aka hardware) interrupt: –timer/clock chip, I/O device, network card, keyboard, mouse –asynchronous (with respect to the executing program) Internal interrupt (aka software interrupt, trap, or exception) –are synchronous –can be intended (“trap”): for system call (process wants to enter kernel to obtain services) –or unintended (usually): (“fault/exception”) (division by zero, attempt to execute privileged instruction in user mode, memory access violation, invalid instruction, alignment error, etc.) Kernel  User mode switch on iret instruction CS 3214 Fall 2010

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

CS 3214 Fall 2010 Context Switching, Details Process 1 Process 2 Kernel user mode kernel mode intr_entry: (saves entire CPU state) (switches to kernel stack) intr_entry: (saves entire CPU state) (switches to kernel stack) intr_exit: (restore entire CPU state) (switch back to user stack) iret intr_exit: (restore entire CPU state) (switch back to user stack) iret switch_threads: (in) (saves caller’s state) switch_threads: (in) (saves caller’s state) switch_threads: (out) (restores caller’s state) switch_threads: (out) (restores caller’s state) (kernel stack switch)

CS 3214 Fall 2010 System Calls Process 1 Kernel user mode kernel mode User processes access kernel services by trapping into the kernel, executing kernel code to perform the service, then returning – very much like a library call. Unless the system call cannot complete immediately, this does not involve a context switch. Kernel’s System Call Implementation

Syscall example: write(2) CS 3214 Fall 2010 /* gcc -static -O -g -Wall write.c -o write */ #include int main() { const char msg[] = "Hello, World\n"; return write(1, msg, sizeof msg); } /* gcc -static -O -g -Wall write.c -o write */ #include int main() { const char msg[] = "Hello, World\n"; return write(1, msg, sizeof msg); } a : a: 53 push %ebx b: 8b mov 0x10(%esp),%edx #arg f: 8b 4c 24 0c mov 0xc(%esp),%ecx # arg : 8b 5c mov 0x8(%esp),%ebx # arg : b mov $0x4,%eax # syscall no c: cd 80 int $0x e: 5b pop %ebx f: 3d 01 f0 ff ff cmp $0xfffff001,%eax : 0f e jae 8051ed a: c3 ret a : a: 53 push %ebx b: 8b mov 0x10(%esp),%edx #arg f: 8b 4c 24 0c mov 0xc(%esp),%ecx # arg : 8b 5c mov 0x8(%esp),%ebx # arg : b mov $0x4,%eax # syscall no c: cd 80 int $0x e: 5b pop %ebx f: 3d 01 f0 ff ff cmp $0xfffff001,%eax : 0f e jae 8051ed a: c3 ret /usr/include/asm/unistd.h: …. #define __NR_write 4 ….

CS 3214 Fall 2010 Kernel Threads Process 1 Process 2 Kernel user mode kernel mode Most OS support kernel threads that never run in user mode – these threads typically perform book keeping or other supporting tasks. They do not service system calls or faults. Most OS support kernel threads that never run in user mode – these threads typically perform book keeping or other supporting tasks. They do not service system calls or faults. Kernel Thread Careful: “kernel thread” not the same as kernel-level thread (KLT) – more on KLT later

Context vs Mode Switching Mode switch guarantees kernel gains control when needed –To react to external events –To handle error situations –Entry into kernel is controlled Not all mode switches lead to context switches –Kernel decides when – subject of scheduling policies Mode switch does not change the identity of current process/thread –See blue/yellow colors in slide on ctxt switch details Hardware knows about modes, does not (typically) know about contexts CS 3214 Fall 2010

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

Process States RUNNING: –Process is on CPU, its instructions are executed READY: –Process could make progress if a CPU were available BLOCKED: –Process cannot make progress even if a CPU were available because it’s waiting for something (e.g., a resource, a signal, a point in time, …) Model is simplified –OS have between 5 and 10 states typically Terminology not consistent across OS: –E.g., Linux calls BLOCKED “SLEEPING” and READY “RUNNING” 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

Process Creation Two common paradigms: –Cloning vs. spawning Cloning: (Unix) –“fork()” clones current process –child process then loads new program Spawning: (Windows) –“exec()” spawns a new process with new program Difference is whether creation of new process also involves a change in program

CS 3214 Fall 2010 fork() #include 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; } #include 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; } 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: fork() Parent: join() Parent process executes Parent process executes Child process executes Child process executes Child process exits Child process exits OS notifies

CS 3214 Fall 2010 fork() #include 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); } #include 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); }

fork() vs. exec() fork(): –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, 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); } 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