1 / 27

System Structuring with Threads

System Structuring with Threads. Example: A Transcoding Web Proxy Appliance. “Proxy” Interposed between Web (HTTP) clients and servers. Masquerade as (represent) the server to the client. Masquerade as (represent) the client to the server. Cache

dvollmer
Download Presentation

System Structuring with Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Structuring with Threads

  2. Example: A Transcoding Web Proxy Appliance “Proxy” Interposed between Web (HTTP) clients and servers. Masquerade as (represent) the server to the client. Masquerade as (represent) the client to the server. Cache Store fetched objects (Web pages) on local disk. Reduce network overhead if objects are fetched again. Transcoding “Distill” images to size/resolution that’s right for client. Encrypt/decrypt as needed for security on the Internet. Appliance Serves one purpose only; no general-purpose OS. clients servers

  3. Using Threads to Structure the Proxy Server long-term periodic threads gather statistics “scrub” cache for expired (old) objects worker threads for specific objects distiller compresses/shrinks images encrypt/decrypt device controller threads logging thread one thread for each disk one thread for network interface server threads request handlers network driver HTTP request handler distill object cache manager scrubber stats encrypt logging disk driver

  4. Thread Family Tree for the Proxy Server network driver HTTP request handler distill file/cache manager scrubber stats encrypt logging disk driver main thread; waiting for child termination periodic threads; waiting for timer to fire server threads; waiting on queues of data messages or pending requests (e.g., device interrupts) worker threads; waiting for data to be produced/consumed

  5. Periodic Threads and Timers • The scrubber and stats-gathering threads must wake up periodically to do their work. • These “background” threads are often called daemons or sleepers. while (systemActive) { do my work; alarm->Pause(10000); } scrubber stats AlarmClock::Pause (int howlong); /* called by waiting threads */ Puts calling thread to sleep. Maintains a collection of threads waiting for time to pass. AlarmClock::Tick(); /* called by clock interrupt handler */ Wake up any waiting threads whose wait times have elapsed.

  6. TCP/IP protocol stack NIC device driver Interfacing with the Network sending receiving host memory buffer pool I/O Bus Network Interface Card NetTx NetRcv NetworkLink

  7. Network Reception HTTP request handler while (systemActive) { packetArrival->P(); disable interrupts; pkt = GetRcvPacket(); enable interrupts; HandleRcvPacket(pkt); } TCP/IP reception packetArrival->P() packetArrival->V() receive interrupt handler This example illustrates use of a semaphore by an interrupt handler to pass incoming data to waiting threads. interrupt

  8. Inter-Thread Messaging with Send/Receive get request for object from thread; while(more data in object) { read data from object; thread->send(data); } file/cache manager network receive • while (systemActive) { • object = GetNextClientRequest(); • find object in cache or Web server • while(more data in object) { • currentThread->receive(data); • transmit data to client; • } • } HTTP request handler network send This example illustrates use of blocking send/receive primitives to pass a stream of messages or commands to a specific thread, connection, or “port”.

  9. Request/Response with Send/Receive file/cache manager HTTP request handler Thread* cache; .... cache->send(request); response = currentThread->receive(); ... while(systemActive) { currentThread->receive(request); ... requester->send(response); }

  10. The Need for Multiple Service Threads Each new request will involve a stream of messages passing through dedicated server thread(s) in each service module. But what about new requests flowing into the system? A system with single-threaded service modules could only handle one request at a time, even if most time is spent waiting for slow devices. network HTTP request handler Solution: multi-threaded service modules. file/cache manager

  11. Using Ports for Multithreaded Servers file/cache manager HTTP request handler while(systemActive) { cachePort->receive(request); ... requester->send(response); } Port* cachePort .... cachePort->send(request); response = currentThread->receive(); ...

  12. Producer/Consumer Pipes char inbuffer[1024]; char outbuffer[1024]; while (inbytes != 0) { inbytes = input->read(inbuffer, 1024); outbytes = process data from inbuffer to outbuffer; output->write(outbuffer, outbytes); } file/cache manager network This example illustrates one important use of the producer/consumer bounded buffer in Lab #3.

  13. Forking and Joining Workers /* give workers their input */ distiller->Send(input); decrypter->Send(pipe); /* give workers their output */ distiller->Send(pipe); decrypter->Send(output); /* wait for workers to finish */ distiller->Join(); decrypter->Join(); HTTP handler input pipe output decrypter distiller distiller = new Thread(); distiller->Fork(Distill()); decrypter = new Thread(); decrypter->Fork(Decrypt()); pipe = new Pipe();

  14. A Serializer for Logging disk driver Multiple threads enqueue log records on a single queue without blocking for log write completion; a single logging thread writes the records into a stream, so log records are not interleaved.

  15. Summary of “Paradigms” for Using Threads • main thread or initiator • sleepers or daemons (background threads) • I/O service threads • listening on network or user interface • server threads or Work Crews • waiting for requests on a message queue, work queue, or port • filters or transformers • one stage of a pipeline processing a stream of bytes • serializers

  16. Threads vs. Events

  17. Review: Thread-Structured Proxy Server network driver HTTP request handler distill file/cache manager scrubber stats encrypt logging disk driver main thread; waiting for child termination periodic threads; waiting for timer to fire server threads; waiting on queues of data messages or pending requests (e.g., device interrupts) worker threads; waiting for data to be produced/consumed

  18. Summary of “Paradigms” for Using Threads • main thread or initiator • sleepers or daemons (background threads) • I/O service threads • listening on network or user interface • server threads or Work Crews • waiting for requests on a message queue, work queue, or port • filters or transformers • one stage of a pipeline processing a stream of bytes • serializers

  19. Thread Priority • Many systems allow assignment of priority values to threads. • Each job in the ready pool has an associated priority value;the scheduler favors jobs with higher priority values. • Assigned priorities reflect external preferences for particular users or tasks. • “All jobs are equal, but some jobs are more equal than others.” • Example: running user interface threads (interactive) at higher priority improves the responsiveness of the system. • Example: Unix nice system call to lower priority of a task. • Example: Urgent tasks in a real-time process control system.

  20. Keeping Your Priorities Straight • Priorities must be handled carefully when there are dependencies among tasks with different priorities. • A task with priority P should never impede the progress of a task with priority Q > P. • This is called priority inversion, and it is to be avoided. • The basic solution is some form of priority inheritance. • When a task with priority Q waits on some resource, the holder (with priority P) temporarily inherits priority Q if Q > P. • Inheritance may also be needed when tasks coordinate with IPC. • Inheritance is useful to meet deadlines and preserve low-jitter execution, as well as to honor priorities.

  21. Multithreading: Pros and Cons • Multithreaded structure has many advantages... • Express different activities cleanly as independent thread bodies, with appropriate priorities. • Activities succeed or fail independently. • It is easy to wait/sleep without affecting other activities: e.g., I/O operations may be blocking. • Extends easily to multiprocessors. • ...but it also has some disadvantages. • Requires support for threads or processes. • Requires more careful synchronization. • Imposes context-switching overhead. • May consume lots of space for stacks of blocked threads.

  22. Alternative: Event-Driven Systems Structure the code as a single thread that responds to a series of events, each of which carries enough state to determine what is needed and “pick up where we left off”. while (TRUE) { event = GetNextEvent(); switch (event) { case IncomingPacket: HandlePacket(); break; case DiskCompletion: HandleDiskCompletion(); break; case TimerExpired: RunPeriodicTasks(); etc. etc. etc. } The thread continuously polls for new events, whenever it completes a previous event. If handling some event requires waiting for I/O to complete, the thread arranges for another event to notify it of completion, and keeps right on going, e.g., asynchronous non-blocking I/O. Question: in what order should events be delivered?

  23. Example: Unix Select Syscall • A thread/process with multiple network connections or open files can initiate nonblocking I/O on all of them. • The Unix select system call supports such a polling model: • files are identified by file descriptors (open file numbers) • pass a bitmask for which descriptors to query for readiness • returns a bitmask of descriptors ready for reading/writing • reads and/or writes on these descriptors will not block Select has fundamental scaling limitations in storing, passing, and traversing the bitmaps.

  24. Event Notification with Upcalls • Problem: what if an event requires a more “immediate” notification? • What if a high-priority event occurs while we are executing the handler for a low-priority event? • What about exceptions relating to the handling of an event? • We need some way to preemptively “break in” to the execution of a thread and notify it of events. • upcalls • example: NT Asynchronous Procedure Calls (APCs) • example: Unix signals • Preemptive event handling raises synchronization issues similar to interrupt handling.

  25. Example: Unix Signals • Signals notify processes of internal or external events. • the Unix software equivalent of interrupts/exceptions • only way to do something to a process “from the outside” • Unix systems define a small set of signal types • Examples of signal generation: • keyboard ctrl-c and ctrl-z signal the foreground process • synchronous fault notifications, syscall errors • asynchronous notifications from other processes via kill • IPC events (SIGPIPE, SIGCHLD) • alarm notifications signal == “upcall”

  26. Handling Unix Signals • 1. Each signal type has a system-defined default action. • abort and dump core (SIGSEGV, SIGBUS, etc.) • ignore, stop, exit, continue • 2. A process may choose to block (inhibit) or ignore some signal types. • useful for synchronizing with signal handlers: inhibit signals before executing code shared with the signal handler • 3. The process may choose to catch some signal types by specifying a (user mode) handler procedure. • system passes interrupted context to handler • handler may munge and/or return to interrupted context

  27. Summary • 1. Threads are a useful tool for structuring complex systems. • Separate the code to handle concurrent activities that are logically separate, with easy handling of priority. • Interaction primitives integrate synchronization, data transfer, and possibly priority inheritance. • 2. Many systems include an event handling mechanism. • Useful in conjuction with threads, or may be viewed as an alternative to threads structuring concurrent systems. • Examples: Unix signals, NT APCs, GetNextEvent() • 3. Event-structured systems may require less direct handling of concurrency. • But must synchronize with handlers if they are preemptive.

More Related