Computer System Lecture Notes Class 3 Hour 1 -- by Guanghao Ding(ding.g@husky.neu.edu) ============================================================== == Tonight's Topics * Threads / Threads Scheduling * Scheduling Issues * Syncronization ************************************** * Threads / Threads Scheduling * ************************************** == Motivation What's the limitation of processes, i.e. why threads are useful? * context switching is expensive * sharing data is cumbersome * applications can only do one thing at a time * high memory overhead == What we have to copy during context switching 1. stack, heap are not necessary 2. registers So we can have a solution like this .............................. ............................... | text data I/O | | text data I/O | |----------------------------| |-----------------------------| | register stack | |registers|registers|registers| |----------------------------| ==> |---------|---------|---------| | | | stack | stack | stack | | | |---------|---------|---------| | threads | | thread | thread | thread | | | | | | | `````````````````````````````` ``````````````````````````````` : it helps with "high memory overhead", the context switching is a little bit cheaper, but doesn't help data sharing. == Benefits 1. Responsiveness e.g. email client: one thread waiting for server, another thread waiting for input and so forth. 2. Resource Sharing == Challenges : sometimes it's hard to divide the task into multi parts, not that simple to be managed. e.g. How to make maximum flow problem multi-thread? The simplest answer is that write separate thread to find path, when the sub-threads get back, the main thread decides which path to remove. == Implementing Threads - Multithreading Models * One-to-One Model AD(advantage): * Many-to-One Model : kernel doesn't know the multi-threading in user space, only one application ADVANTAGE: we can build a better scheduler since user know what the threads are doing, and context switching is cheap since kernel space switching is avoided. DISADVANTAGE: poor blocking behavior, one block, all blocked. * Many-to-Many Model : get the benefits of the above two, can be operated as many-to-one and one-to-one == Implementing Threads - Basic Functions * create * cancel * join == Threading Issues * fork() * SIGNAL : notify a process that a particular event has occurred. if no one interests in the SIGNAL, just ignore or pick one randomly * Threads Pool e.g. Web Server. If the server create a new thread for every request, the server can be easily brought down by sending huge amount of requests. Threads pool are useful in this case.