Operating Systems Certificate Program in Software DevelopmentCSE-TC and CSIM, AITSeptember -- November, 2002 • Objectives • a review of OS processes and threads 4. Processes and Threads(Ch. 4 S&G) chs 4 and 5 in the 6th ed.
Overview 1. Processes 2. Process Scheduling 3. Operations on Processes 4. Cooperating Processes 5. Interprocess Communication (IPC) 6. Client/Server Communication continued
7. Threads 8. Thread Types 9. Multithreading Models 10. Pthreads
1. Processes • A process is an executing program. • Each process has its own program counter, stack pointer, address space. • Several processes may execute the same program, but they will have copies of the program data.
Process Control Block (PCB) Fig. 4.2., p.91 process state next previous Process ID (PID) program counter registers memory structure open file table etc
new terminated States of a Process Fig. 4.1, p.90 dispatch running exit ready interrupt eventcompleted waiting I/O eventor wait
2. Process Scheduling • Job scheduler (long-term scheduler) • select processes from storage to add to ready queue • CPU scheduler (short-term scheduler) • allocates a processor to a ready process
ready queue Queuing Diagram Fig. 4.5, p.95;VUW CS 305 cpu I/O I/O queue I/O request time slice expired child executes join fork a child wait queue resource queue resourcerequest
ready queue Medium-term Scheduling Fig. 4.6, p.96 swap in partially executedswapped out processes swap out end cpu I/O I/O queue I/O request
Context Switching • Switching from one process to another • often takes tens of microseconds • Save executing process in its PCB • registers, program counter, state, … • Load/reload a process from its PCB continued
3. Operations on Processes • Process creation (fork) • parent/child relationship • child is usually a copy of its parent • usually they continue in parallel • Process termination • exit, abort, or kill • Suspension • internal (wait) or external (resource wait)
4. Cooperating Processes • Cooperating processes can affect each other • compare to independent processes • Advantages of process cooperation: • information sharing, (possible) computation speed-up, modularity, convenience
Producer/Consumer • A producer process produces information that is consumed by a consumer process. • Many variations, e.g.: • unbounded-buffer • bounded-buffer shared memory buffer p c
5. Interprocess Communication (IPC) • IPC is allows processes to communicate and synchronize their actions. • A message-based approach: • processes communicate without shared vars • Two basic operations: • send(message) • receive(message)
Messaging Issues • Message format • Unidirectional, bi-directional? • One-to-one, broadcasting, multicasting? • Direct, indirect links? • names, locating, mailboxes • Blocking (synchronous), non-blocking (asynchronous)? • Sender/receiver buffering
6. Client/Server Communication • A server is a program (or collection of cooperating programs) that provides services and/or manages resources on the behalf of other programs (its clients).
Client/Server Environment clients LAN or WAN network Server Data
Example • The ATM network: • the clients are the ATM machines • user interfaces;some simple application processing • the server is at the bank • most application processing;very large database of customer accounts
Architectural Requirements • Reliable, robust communication between the clients and server. • Client/server cooperation • started by the client • Application processing is usually distributed between a client and the server. • Server controls services/data that the client accesses. • Server handles conflicting requests.
Communication Types • Sockets • (TCP) sockets are like telephones • the client and server sockets are the endpoints of their communications link • Remote Procedure Calls (RPC) • the client ‘calls’ a function in the server • Remote Method Invocation (RMI) • the Java version of RPC
7. Threads • A thread shares its data and OS resources (e.g. open files) with its peer threads • Sharing and smaller size makes threads less expensive to create & context switch • sometimes called light-weight processes (LWPs)
Threads Diagram Fig. 4.8, p.104 PC PC PC peer threads shared data
Typical Thread States JUST_CREATED READY RUNNING BLOCKED
Typical Thread Operations • fork() • creates a new thread sharing global structures • yield() • yields processor, enters ready queue • sleep() • yields processor, enters a wait queue • finish()
Threading Issues • Semantics of fork() and exec() calls • Thread cancellation • Signal handling • Thread pools • Thread specific data
8. Thread Types • Kernel threads • useful for parallelising system features • e.g. Windows 9x/NT/2000, Linux • User threads • scheduled within a user process • not seen by the kernel • e.g. POSIX Pthreads, Solaris threads
9. Multithreading Models • Many-to-One • One-to-One • Many-to-Many
9.1 Many-to-One • Many user-level threads are mapped to a single kernel thread (or process). • Often used on systems that do not support kernel threads.
9.2. One-to-One • Each user-level thread maps to a kernel thread. • Examples • Windows 9x/NT/2000 • OS/2
Windows 2000 • Each thread contains • a thread id • a register set • separate user and kernel stacks • a private data storage area
9.3. Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads. • Examples: • Solaris 2 • Windows NT/2000 with the ThreadFiberpackage
Solaris 2 Fig. 4.9, p.107 userthreads tasks lwp lwp lwp lwp lwp lwp kernelthreads kernel cpu cpu cpu
10. Pthreads • A POSIX standard API for thread creation and synchronization. • The API specifies the behavior of the thread library, but the implementation is up to the library developer. • Commonly found in UNIX, and variants.