Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Operating Systems - Fall 2009 Lecture 5 – January 26, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:

Similar presentations


Presentation on theme: "1 Advanced Operating Systems - Fall 2009 Lecture 5 – January 26, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:"— Presentation transcript:

1 1 Advanced Operating Systems - Fall 2009 Lecture 5 – January 26, 2009 Dan C. Marinescu Email: dcm@cs.ucf.edudcm@cs.ucf.edu Office: HEC 439 B. Office hours: M, Wd 3 – 4:30.

2 2 Last, Current, Next Lecture Last time: Butler Lampson’s hints for system design The complexity of computing and communication systems State Processes Today Process handling by the kernel Inter-process communication Threads Next time: Process synchronization

3 3 Process Control Block (PCB)

4 4 Process scheduling

5 5 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 Context-switch time is overhead; the system does no useful work while switching Context switch time dependent on hardware support

6 6 Inter-process communication The need to communicate: User process - kernel process  to invoke kernel functions Kernel process - kernel process  the kernel is not monolithic User process - user process  users have rights too!! Basic strategies Shared memory Message passing

7 7 Shared memory Producer – consumer (reader - writer) model. Shared buffer  buffer(BUFFER_SIZE) Unbounded: the produce could always write; the consumer may have to wait for new item Bounded: the produce may have to wait for buffer space the consumer may have to wait for new item Shared state between producer and consumer Count – the number of items written by the producer and yet to be read by the consumer Producer state: in  pointer to the next available location Consumer state: out  pointer to location of the next item

8 8 Producer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

9 9 Consumer while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }

10 10 Race conditions Compiler translates producer’s process P instruction count++ as: R1 = count R1 = R1 +1 count = R1 Compiler translates consumer’s process C instruction count-- as: R2 = count R2 = R2 - 1 count = R2 Initially count = 5, in =12, out=7 P is scheduled to run P executes R1 = count (R1 = 5) P : executes R1 = R1 + 1 (R1 = 6) An interrupt occurs and P is suspended and C is dispatched C executes R2 = count (R2 = 5) C executes R2 = R2-1 (R2 = 4) P executes count = R1 (count = 6) P writes the new item in buffer(12); in=13 P has finished and is suspended and C is dispatched C executes count = R2 r(count = 4} C read the new item from buffer(7); out=8

11 11 Message passing The entities involved: Sender – process P Receiver – process Q Logical communication channel Message Issues Identification of the communicating processes (direct/indirect) Type of communication (synchronous/asynchronous) Buffering (implict/explicit)

12 12 Direct process identification Name the destination/source explicitely Sender (P)  send(Q, message) Receiver (Q)  receive(P,message) Alternative: receiever accepts a message from any sender: Sender (P)  send(Q, message) Receiver (Q)  receive(id,message)

13 13 Indirect identification – Mailboxes/Ports A mailbox/port – shared among several processes Mailboxes in user or in system address space. One or more channel may be connected to a mailbox. Each channel may be shared by a number oif processes. Sender (P)  send(mailboxA, message) Receiver (Q)  receive(mailboxA,message) Q shares its inbox with one or more processes or may have one mailbox per communication partner.

14 14 Sockets – communication in the Internet Two transport protocols: TCP and UDP Sockets TCP – sockets UDP sockets To send a message to a process running on a host with IP address 173.25.19.1 at TCP port 80 you specify the IP anddess and the port: 173.25.19.1:80. Java sockets Connection-Oriented  TCP – sockets Socket class Connectionless  UDP sockets DatagramSocket class Multicast sockets MulticastSocket class

15 15 More about ports The port numbers are divided into three ranges: Well Known Ports from 0 through 1023, Registered Ports Dynamic and/or Private Ports Examples 20/tcp File Transfer [Default Data] 20/udp  File Transfer [Default Data] 21/tcp  File Transfer [Control] 21/udp  File Transfer [Control] 80/tcp  World Wide Web HTTP 80/udp  World Wide Web HTTP

16 16 Message passing in Mach (MAC OS X) At creation time each user process (task) is assigned two ports: Kernel  for communication with the Kernel Notify  to notify the process about the occurrence of events To create a new port a process uses: port_allocate(). The queue of messages at a port is owned by the process which created the port. To find the number of messages: port_status(). System calls for message passing: msg_send() msg_receive() msg_rpc() The system guarantees that the messages from the same sender are queued in a FIFO (First In First Out) manner. Messages from different senders could be queued in any order.

17 17 Synchronous/Asynchronous communication Synchronous or blocking send/receive Asynchronous (non-blocking) send/receive

18 18 Remote Procedure Call (RPC) Supports inter-process communication of remotely located processes and allows implementation of client-server systems (RFC 1831) Preserve the semantics of a local procedure call. To use an RPC a process may use a special service: PORTMAP or RPCBIND available at port 111. A new RPC service uses the portmapper to register. The portmapper also allows a service lookup. If the process knows the port number of the RPC it may call directly. RPC/TCP and also RPC/UDP Messages must be well-structured; contain the identification of the specific RPC are addressed to an RPC demon listening at an RPC port. A stub hides the details of the implementation of the RPC. A machine independent representation of data  external data representation standard (XDR).

19 19 RPC semanitcs At most once  a message is acted upon at most once. The server must keep a history of the time-stamps of all messages. Messages may arrive out of order….. What if the RPC fails? Exactly once  implement the at most once and request an acknowledgment from the server.

20 20 Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

21 21 Single and Multithreaded Processes

22 22 Benefits of multithreading Responsiveness Resource Sharing Economy Takes advantage of multi-processor and multi-core architectures

23 23 User Threads Thread management done by user-level threads library Thread libraries: UNIX, Linux  POSIX Pthreads Microsoft Windows  Win32 threads Java threads

24 24 Kernel Threads Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

25 25 Multithreading Models Many-to-One One-to-One Many-to-Many

26 26 Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

27 27 Many-to-One

28 28 One-to-One Each user-level thread maps to one kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

29 29 One-to-one

30 30 Many-to-Many Allows a user level thread to be mapped to several kernel threads the operating system to create a sufficient number of kernel threads Examples Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

31 31 Many-to-Many

32 32 Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

33 33 Two-level Model

34 34 Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

35 35 Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads?

36 36 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


Download ppt "1 Advanced Operating Systems - Fall 2009 Lecture 5 – January 26, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:"

Similar presentations


Ads by Google