policy vs mechanism
play

Policy vs. Mechanism Policy Decisions about what should be done. - PDF document

Processes and Threads Marie Roch Tanenbaum 2.1-2.2 Policy vs. Mechanism Policy Decisions about what should be done. Mechanism Algorithms and data structures that implement policy 2 1 Processes Process program in


  1. Processes and Threads Marie Roch Tanenbaum 2.1-2.2 Policy vs. Mechanism • Policy – Decisions about what should be done. • Mechanism – Algorithms and data structures that implement policy 2 1

  2. Processes • Process  program in execution • Processes – are assigned resources – run in user or kernel mode • Things we do with resources – share – acquire – release 3 Multiprogramming • Multiple processes on a single computer • Scheduling algorithm selects which processes are allocated CPU P 69 P 32 P 25 P 3 • Don’t know when scheduled next… avoid loops for timing 4 2

  3. Multiprogramming • How do we transition from one process to another? – cooperative multitasking processes explicitly yield virgin.com – preemptive multitasking forced release of processor resource based on external conditions 5 Some models use: initializing terminating 6 3

  4. Process life cycle • initialization – • termination created by another – Voluntary process • error • complete • running / blocked / – Involuntary ready – Process is • unhandled active exception • killed by another process 7 8 4

  5. Process Implementation • Process control block (PCB) – kernel data structure representing processes – frequently implemented as a fixed size array • Process control block contains – state – what else? 9 Typical PCB 10 5

  6. Process types • Background – no user supervision • Interactive – User I/O • Daemon – Special background processes that provide services 11 Program thread • Thread of execution – Stream of instructions being executed – CPU registers – Stack • current procedure calls • local variables • Can we have more than one thread? 12 6

  7. Multithreading • Multiple threads (aka lightweight processes) • Threads within process share resources: – files, heap, and any other allocated resources – and are allocated the CPU, much like processes • Thread control block – Keeps registers, PC, PSW and state 13 Multithreading dangers • Threads can – overwrite each others’ stacks – access data structures in transient states – change heap values – access resources in unexpected interleaving – … Okay, so why bother? 14 7

  8. Threads are convenient Figure 2-7. A word processor with three threads. Tanenbaum 2008 Threads are convenient A multithreaded Web server. Tanenbaum 2008 8

  9. Creating a thread • Implementation dependent • POSIX implementation (man pages/FAQ for details) – headers: <pthread.h> <sched.h> – pthread_create(…) – Create a thread – sched_yield(…) – Next thread runs – pthread_exit(…) – Terminate thread – pthread_join(…) – Wait for specific thread to exit – pthread_attr_init(…) – Initialize options structure to be 17 passed to pthread_create POSIX thread example 18 9

  10. POSIX thread example • Previous example needs headers • Compilation on a linux box gcc –o threadeg threadeg.c –l pthread –l rt Note : gcc puts library flags after list of files (rarely done) and the order of libraries is important. 19 Thread mechanisms • user-level threads – implemented via a user library – scheduling occurs in user code • kernel-level threads – part of OS implementation – data structures are maintained in kernel code 20 10

  11. User- vs. Kernel- mode threads Tanenbaum 2008 21 User- vs. Kernel- mode threads • Kernel-mode – Kernel schedules threads, not processes – Thread operations and switches require shift to kernel mode ($$$) • User-mode – Kernel unaware of user threads What happens when one thread blocks? – Very fast thread operations 22 11

  12. Hybrid thread design • User-threads mapped onto kernel-threads • Best of both worlds Tanenbaum 2008 23 Pop-up threads • Dynamic creation of threads to handle events Tanenbaum 2008 24 12

  13. Threads • Suppose a thread calls a function that sets a global return code (e.g. UNIX errno) • Can we run into problems? 25 13

Recommend


More recommend