Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 6 Chapter 4: Threads.

Similar presentations


Presentation on theme: "Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 6 Chapter 4: Threads."— Presentation transcript:

1 Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 6 Chapter 4: Threads

2 2 CS 446/646 Principles of Computer Operating Systems Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server Systems

3 3 CS 446/646 Principles of Computer Operating Systems Process State As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

4 4 CS 446/646 Principles of Computer Operating Systems Process Control Block (PCB) Information associated with each process Process state Program counter CPU registers (accumulators, index registers, stack pointers, general purpose registers, etc.) CPU scheduling information (priority, pointers to scheduling queues, etc.) Memory-management information (base and limit registers, page/segment tables, etc.) Accounting information (account numbers, job/process number, time limits, etc.) I/O status information (allocated I/O devices, open files, etc.)

5 5 CS 446/646 Principles of Computer Operating Systems Process Scheduling Queues Multiprogramming: Have some process running at all times to maximize CPU utilization Time-sharing: Switch CPU among processes so frequently that users can interact with each program Process scheduler selects an available process Processes migrate among the various queues Job queue – set of all processes in the system Ready queue – set of all processes residing in main memory, ready and waiting to execute Device queues – set of processes waiting for an I/O device Processes can be described as either: I/O-bound process – spends more time doing I/O than computations, many short CPU bursts CPU-bound process – spends more time doing computations; few very long CPU bursts

6 6 CS 446/646 Principles of Computer Operating Systems Context Switch When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch Context of a process represented in the PCB Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support Multiple register sets

7 7 CS 446/646 Principles of Computer Operating Systems Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes Resource sharing Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate Address space Child duplicate of parent Child has a program loaded into it

8 8 CS 446/646 Principles of Computer Operating Systems Process Termination Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process’ resources are deallocated by operating system Parent may terminate execution of children processes (abort) Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting  Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination  In UNIX, init process inherits childrens of an exiting process

9 9 CS 446/646 Principles of Computer Operating Systems Interprocess Communication Processes within a system may be independent or cooperating Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by other processes, including sharing data Cooperating processes need interprocess communication (IPC) Two models of IPC Message passing Shared memory

10 10 CS 446/646 Principles of Computer Operating Systems Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size

11 11 CS 446/646 Principles of Computer Operating Systems Interprocess Communication – Message Passing Mechanism for processes to communicate and to synchronize their actions Message system – processes communicate with each other without resorting to shared variables IPC facility provides two operations: send(message) – message size fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Implementation of communication link physical (e.g., shared memory, hardware bus) logical (e.g., logical properties)

12 12 CS 446/646 Principles of Computer Operating Systems Direct Communication Processes must name each other explicitly: send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Properties of communication link Links are established automatically A link is associated with exactly one pair of communicating processes Between each pair there exists exactly one link The link may be unidirectional, but is usually bi-directional

13 13 CS 446/646 Principles of Computer Operating Systems Indirect Communication Messages are directed and received from mailboxes (also referred to as ports) Each mailbox has a unique id Processes can communicate only if they share a mailbox Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A Properties of communication link Link established only if processes share a common mailbox A link may be associated with many processes Each pair of processes may share several communication links Link may be unidirectional or bi-directional

14 14 CS 446/646 Principles of Computer Operating Systems Indirect Communication Operations create a new mailbox send and receive messages through mailbox destroy a mailbox Mailbox sharing P 1, P 2, and P 3 share mailbox A P 1, sends; P 2 and P 3 receive Who gets the message? Solutions Allow a link to be associated with at most two processes Allow only one process at a time to execute a receive operation Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

15 15 CS 446/646 Principles of Computer Operating Systems Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null

16 16 CS 446/646 Principles of Computer Operating Systems Buffering Queue of messages attached to the link; implemented in one of three ways 1.Zero capacity – 0 messages Sender must wait for receiver (rendezvous) 2.Bounded capacity – finite length of n messages Sender must wait if link full 3.Unbounded capacity – infinite length Sender never waits

17 17 CS 446/646 Principles of Computer Operating Systems Examples of IPC Systems - POSIX POSIX Shared Memory Process first creates shared memory segment segment id = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR); Process wanting access to that shared memory must attach to it shared memory = (char *) shmat(id, NULL, 0); Now the process could write to the shared memory sprintf(shared memory, "Writing to shared memory"); When done a process can detach the shared memory from its address space shmdt(shared memory);

18 18 CS 446/646 Principles of Computer Operating Systems Communications in Client-Server Systems Sockets A socket is defined as an endpoint for communication Concatenation of IP address and port Remote Procedure Calls abstracts procedure calls between processes on networked systems Stubs – client-side proxy for the actual procedure on the server The client-side stub locates the server and marshalls the parameters The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the server Remote Method Invocation (Java) RMI allows a Java program on one machine to invoke a method on a remote object Pipes A pipe acts as a conduit allowing two processes to communicate.

19 19 CS 446/646 Principles of Computer Operating Systems Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

20 20 CS 446/646 Principles of Computer Operating Systems Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads, Win32, and Java thread libraries To examine issues related to multithreaded programming

21 21 CS 446/646 Principles of Computer Operating Systems Single and Multithreaded Processes Multi-threads

22 22 CS 446/646 Principles of Computer Operating Systems Multithreaded Server Architecture

23 23 CS 446/646 Principles of Computer Operating Systems Benefits Responsiveness: Continue even if part of an application is blocked due to I/O Allow user interaction in one thread while image is loading in another. Resource Sharing: Memory / resource sharing is by default with threads. Several different threads of activity within the same address space. Economy: It is more economical to create and context-switch threads. In general, creating a process is 30 times slower, context-switching a process is 5 times slower. Scalability: Multiprocessor architectures A single-threaded process can only run on one processor.

24 24 CS 446/646 Principles of Computer Operating Systems Multicore Scalability Parallel Execution on a Multicore System Concurrent Execution on a Single-core System

25 25 CS 446/646 Principles of Computer Operating Systems Multicore Programming Multicore systems putting pressure on programmers, challenges include Dividing activities: Finding areas of code that can run in parallel as concurrent tasks. Balance: Tasks should perform equal work of equal value. Data splitting: Data manipulated by the tasks must be divided to run on separate cores. Data dependency: Ensure the execution of the tasks is synchronized to accommodate data dependencies between tasks. Testing and debugging: Testing a program with multiple execution paths is inherently more challenging.

26 26 CS 446/646 Principles of Computer Operating Systems User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

27 27 CS 446/646 Principles of Computer Operating Systems Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

28 28 CS 446/646 Principles of Computer Operating Systems Multithreading Models Many-to-One One-to-One Many-to-Many Two-level

29 29 CS 446/646 Principles of Computer Operating Systems Many-to-One Model

30 30 CS 446/646 Principles of Computer Operating Systems Many-to-One Many user-level threads mapped to single kernel thread. Thread management is done by the thread library in user space, Is efficient, But, entire process will block is a thread makes a blocking system call. Only one thread can access the kernel at a time, Multiple threads can not run in parallel on multiprocessors. Examples: Solaris Green Threads GNU Portable Threads

31 31 CS 446/646 Principles of Computer Operating Systems One-to-one Model

32 32 CS 446/646 Principles of Computer Operating Systems One-to-One Each user-level thread maps to kernel thread. Provides more concurrency. No blocking due to another thread. Threads may run in parallel on multiprocessors. Creating a user thread requires creating the corresponding kernel thread. Hence, OSes limit the number of threads. Examples Windows NT/XP/2000 Linux Solaris 9 and later

33 33 CS 446/646 Principles of Computer Operating Systems Many-to-Many Model

34 34 CS 446/646 Principles of Computer Operating Systems Many-to-Many Model Many user level threads are mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Suffers from neither of shortcomings of many-to-one or one-to-one. Examples Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

35 35 CS 446/646 Principles of Computer Operating Systems Two-level Model

36 36 CS 446/646 Principles of Computer Operating Systems Two-level Model Similar to many-to-many, except that it allows a user thread to be bound to kernel thread. Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

37 37 CS 446/646 Principles of Computer Operating Systems

38 38 CS 446/646 Principles of Computer Operating Systems Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS

39 39 CS 446/646 Principles of Computer Operating Systems Pthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)

40 40 CS 446/646 Principles of Computer Operating Systems Java Threads Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface

41 41 CS 446/646 Principles of Computer Operating Systems Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

42 42 CS 446/646 Principles of Computer Operating Systems Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads?

43 43 CS 446/646 Principles of Computer Operating Systems Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled

44 44 CS 446/646 Principles of Computer Operating Systems Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals  Signal is generated by particular event  Signal is delivered to a process  Signal is handled Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific threa to receive all signals for the process

45 45 CS 446/646 Principles of Computer Operating Systems Thread Pools Create a number of threads in a pool where they await work Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool

46 46 CS 446/646 Principles of Computer Operating Systems Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

47 47 CS 446/646 Principles of Computer Operating Systems Scheduler Activations Both many-to-many and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads

48 48 CS 446/646 Principles of Computer Operating Systems Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

49 49 CS 446/646 Principles of Computer Operating Systems Windows XP Threads

50 50 CS 446/646 Principles of Computer Operating Systems Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)

51 Modified from Silberschatz, Galvin and Gagne ©2009 End of Chapter 4


Download ppt "Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 6 Chapter 4: Threads."

Similar presentations


Ads by Google