cs5460 operating systems lecture 5 processes and threads
play

CS5460: Operating Systems Lecture 5: Processes and Threads - PowerPoint PPT Presentation

CS5460: Operating Systems Lecture 5: Processes and Threads (Chapters 3-4) CS 5460: Operating Systems Lecture 5 Context Switch Results lab2-15 3.8 us gamow 1.6 us home 1.0 us VirtualBox on lab2-25 170 us


  1. CS5460: Operating Systems Lecture 5: Processes and Threads (Chapters 3-4) CS 5460: Operating Systems Lecture 5

  2. Context Switch Results  lab2-15 3.8 us  gamow 1.6 us  home 1.0 us  VirtualBox on lab2-25 170 us  VirtualBox on gamow 4.6 us  VirtualBox on home 42 us CS 5460: Operating Systems Lecture 5

  3. Important From Last Time  Process state machine – Interaction with OS New Terminated invariants  Process control block create exit process process – Presence on OS queues schedule Running Ready  Process creation deschedule – UNIX vs. Windows style block on timer, I/O done I/O, page fault, …  Process termination Waiting CS 5460: Operating Systems Lecture 5

  4.  What does this program print? #include <stdio.h> #include <unistd.h> int main (void) { int x = 1000; fork(); printf ( “ %d\n ” , x++); printf ( “ %d\n ” , x++); return 0; } CS 5460: Operating Systems Lecture 5

  5. Termination #include <signal.h> #include <unistd.h> #include <stdio.h> int main(void) {  When process dies, int cid = fork(); OS reclaims its if (cid == 0) { sleep(10); resources printf( “ Child exiting!\n ” );  On Unix: exit(0); } else { – A process can terminate printf( “ Type to kill child\n ” ); itself using exit() system char answer[10]; call gets(answer); – A process can kill another if (!kill(cid,SIGKILL)) { process using the kill() printf( “ Child dead!\n ” ); system call } } } CS 5460: Operating Systems Lecture 5

  6. #include <signal.h> #include <unistd.h>  pid_t wait(int *status): #include <stdio.h> – Parent process use wait() to request notification int main(void) { when a child dies int ret, cid; – Returns PID of dead child; cid = fork(); sets status to be child ’ s if (cid == 0) { /* CHILD*/ exit code printf( “ Child exiting.\n ” ); – Works regardless of whether child dies before/ exit(100); after call }  What does this else { /* PARENT */ program do? wait(&ret); printf( “ Status: %d\n ” , ret); } What happens when a process dies? } Do we reclaim all resources? CS 5460: Operating Systems Lecture 5

  7.  Context switch: Switch from one process/thread running on CPU to another – When can it happen? – Which process/thread should run next? – How is it accomplished?  When? Preemptive vs non-preemptive scheduling – Can occur whenever the kernel (scheduler) gains control of CPU  Which? Scheduler decides based on its policies  How? – Save state of “ old ” process – Restore state of “ new ” process – Question: What constitutes the process’s “ state ” ? CS 5460: Operating Systems Lecture 5

  8. Timeline of a Context Switch P 1 P 1 Scheduler Scheduler (P 1 ) (P 1 ) Interrupt One instr or trap later! Save/restore Save/restore cpu state cpu state Interrupt int/trap or trap handler Return from trap/int (P2) Scheduler Scheduler (P N ) (P 2 ) P 2 P N int/trap handler Question: What parts of timeline execute in kernel mode? TIME CS 5460: Operating Systems Lecture 5

  9. Introducing Threads  Processes are “ heavyweight ” – Memory mappings: may be expensive to swap – Cache/TLB state: flushing expensive, especially side effects – Lots of kernel state – Context switching between processes is expensive  Threads are “ lightweight ” – Multiple threads share process state » Same address space » Same open file/socket tables – Goal: make context switching between threads cheap – Not all OSes support threads efficiently (e.g., older Unixes) » Had to fake it using things like select() and signal() CS 5460: Operating Systems Lecture 5

  10. What Are Threads Good For? 1. Making apps speed up on a multicore – (Bad) alternative to using threads: Use multiple single-threaded processes 2. Creating servers with a lot of internal concurrency 3. Letting programmers express somewhat independent computations within an address space 4. Letting programmers create the hardest debugging programs EVER – Race conditions – Deadlocks – Etc. CS 5460: Operating Systems Lecture 5

  11.  Threads separate process into two abstractions: – Shared resources: address space, kernel resources, privileges – Execution state: PC, SP, PSW, other registers, stack, … Address space Add multiprogramming Thread Add Older (unthreaded) UNIX DOS, small embedded systems Multi- threading Multihreaded OSes Real-time OSes, Java (Win7, OS X, Linux) CS 5460: Operating Systems Lecture 5

  12. Implementing Threads  Address space shared by threads – Code – Data and heap Stack: SP0  Thread private state: Stack: – Registers (inc. pc, sp, psw) SP1 – Stack  Key issue: HP Data: – How do you safely access “ shared ” state? » Read-only (e.g., code) à à easy Code: » Writable à à hard PC1 – Whole section of course dedicated to this PC0 » Synchronization » Concurrency control  Context switch between threads – Save/restore registers (tricky) CS 5460: Operating Systems Lecture 5

  13. Kernel Threads vs User Threads  Kernel threads: – OS supports threads directly – Each thread has separate OS state (ready, blocked, … ) – Kernel schedules threads rather than processes – Kernel context switches between threads (and processes) » Thread switch: save/restore registers (inc. PC and SP) » Process switch: above plus MMU/TLB state » Thread switches cheaper than process (~10us vs ~30us)  User threads: – OS does not directly support threads – User-level thread library implements threads – Thread “ context switch ” performed entirely within user space – Cannot get parallelism on a multicore!  Solaris operating system uses both CS 5460: Operating Systems Lecture 5

  14. Kernel Threads vs User Threads  Advantages of kernel threads – Threads can exploit multiprocessors – Blocking I/O does not stall non-blocked threads  Advantages of user threads – Faster to create, manipulate, and synchronize – In theory, can tailor scheduling policy to match application need  Alternative to threads à à event-based programming – See Ousterhout’s paper: “ Why Threads Are a Bad Idea ” – Getting concurrency right is hard à à events simplify model » Race conditions, deadlocks, livelocks, … – Downside: events are also difficult, and prevent running on multiple processors CS 5460: Operating Systems Lecture 5

  15. Cooperating Processes vs.Threads  Cooperating processes can provide benefits: – Improved performance by overlapping activities (parallelism) – Simpler program structure by separating activities (web server) – Fault isolation (e.g., shell, CGI subsystem of web server) » Example: Apache, Google Chrome  Problems arise: – New failure modes introduced à à concurrency control – Errors may be harder to debug – Cleanup complicated à à cannot just destroy single process  Questions to consider: – How do cooperating processes communicate? – Do the processes need to share the same {language, machine, OS}? CS 5460: Operating Systems Lecture 5

  16. IPC via Message Passing  Option 1: processes exchange “ messages ” – Need to be able to name other process (e.g., PID) – Some form of msgsnd() and msgrecv() supported by OS main() { consumer() { … while (1) { if (!fork()) producer(); msgrcv(nextc {, prodPID}); else consumer(); … } consume item nextc; … producer() { } while (1) { } … produce item nextp; … msgsnd(nextp, consPID); } } CS 5460: Operating Systems Lecture 5

  17. IPC via Shared Memory  Option 2: processes directly access same memory – Need mechanism to establish shared mappings (e.g., threads in same process, mmap() , shmget() ). main() { consumer() { … while (1) { buffer = shmget( … ); while (in == out); Shared also in = out = 0; nextc = buffer[out]; (not shown) if (!fork()) producer(); out = (out+1) mod N; else consumer(); … } consume item nextc; … producer() { } while (1) { } produce item nextp; …  Which is more efficient, shared while ((in+1)mod N)== out); memory or message passing? buffer[in] = nextp;  What are possible problems with this in = (in+1) mod N; solution? } } CS 5460: Operating Systems Lecture 5

  18. Important From Today  Processes and threads: – Encapsulate resources and accounting – Threads separate “ execution context ” from process abstraction – Typical address space layout – Context switch – Kernel threads versus User threads  How do threads communicate (within a process)?  How to cooperating processes communicate?  Synchronization is hard and important  Next up: Scheduling – How to decide what to run next CS 5460: Operating Systems Lecture 5

Recommend


More recommend