Operating Systems Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) armahmood786@yahoo.com alphasecure@gmail.com alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com www.twitter.com/alphapeeler armahmood786@jabber.org www.facebook.com/alphapeeler alphapeeler@aim.com abdulmahmood-sss alphasecure mahmood_cubix 48660186 armahmood786@hotmail.com alphapeeler@icloud.com http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/ VC++, VB, ASP
Processes and Threads Processes have two characteristics: Resource Ownership Process includes a virtual address space to hold the process image the OS performs a protection function to prevent unwanted interference between processes with respect to resources Scheduling/Execution Follows an execution path that may be interleaved with other processes a process has an execution state (Running, Ready, etc.) and a dispatching priority and is scheduled and dispatched by the OS
Processes and Threads The unit of dispatching is referred to as a thread or lightweight process The unit of resource ownership is referred to as a process or task Multithreading - The ability of an OS to support multiple, concurrent paths of execution within a single process
Single Threaded Approaches A single thread of execution per process, in which the concept of a thread is not recognized, is referred to as a single-threaded approach MS-DOS some variants - UNIX JRE MS-DOS is an example Windows, Solaris, modern UNIX, Linux
Processes - Multithreaded environment In multi-threaded env. Process is defined as : unit of resource allocation and a unit of protection Associated items with processes: virtual address space that holds the process image Protected access to: processors other processes files I/O resources
One or More Threads in a Process Each thread has: an execution state (Running, Ready, etc.) saved thread context (program counter) when not running an execution stack some per-thread static storage for local variables access to the memory and resources of its process (all threads of a process share this)
Threads vs. Processes Thread control block: register values, priority, thread-related state info. All threads shares : PCB, process address space, access to the same data. What happens when one thread alters data in memory? What happens when one thread opens a file with read privileges?
Benefits of Threads Takes less time to create a new thread than a process Less time to terminate a thread than a process Switching between two threads takes less time than switching between processes Threads enhance efficiency in communication between programs Because threads within same process share memory and files, they can communicate with each other without invoking the kernel.
Uses of Thread in a Single-User Multiprocessing System Foreground and background work one thread could display menus and read user input, while another thread executes user commands and updates the spreadsheet. Asynchronous processing word processor to write its random access memory (RAM) buffer to disk once every minute. Speed of execution process can compute one batch of data while reading the next batch from a device. Modular program structure easier to design and implement using threads.
In an OS that supports threads, scheduling and dispatching is done on a thread basis Most of the state information dealing with execution is maintained in thread-level data structures suspending a process involves suspending all threads of the process termination of a process terminates all threads within the process
The key states for a thread are: Running Ready Blocked Thread operations associated with a change in thread state are: Spawn Block Unblock Finish
RPC Using Single Thread Scenario: program that performs two remote procedure calls (RPCs) 2 to two different hosts, using a single thread.
RPC Using One Thread per Server Scenario: program that performs two remote procedure calls (RPCs) 2 to two different hosts, using a multiple threads.
Multithreading on a Uniprocessor On a uniprocessor, multiprogramming enables the interleaving of multiple threads within multiple processes. Scenario: three threads in two processes are interleaved on the processor.
Thread Synchronization It is necessary to synchronize the activities of the various threads all threads of a process share the same address space and other resources any alteration of a resource by one thread affects the other threads in the same process
Types of Threads User Level Thread (ULT) Kernel level Thread (KLT)
User-Level Threads (ULTs) All thread management is done by the application The kernel is not aware of the existence of threads
Relationships Between ULT States and Process States Figure 4.6 Examples of the Relationships between User-Level Thread States and Process States
Scheduling can be application specific ULTs can run on any OS Thread switching does not require kernel mode privileges
Disadvantages of ULTs In a typical OS many system calls are blocking as a result, when a ULT executes a system call, not only is that thread blocked, but all of the threads within the process are blocked In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing (multi-processor or multi-core) Reason: A kernel assigns one process to only one processor at a time
Overcoming ULT Disadvantages (1) Jacketing -- converts a blocking system call into a non-blocking system call (2) Writing an application as multiple processes rather than multiple threads
Kernel-Level Threads (KLTs) Thread management is done by the kernel no thread management is done by the application API to kernel thread facility MS Windows is an example of this approach
Advantages of KLTs The kernel can simultaneously schedule multiple threads from the same process on multiple processors If one thread in a process is blocked, the kernel can schedule another thread of the same process Kernel routines can be multithreaded
Disadvantage of KLTs The transfer of control from one thread to another within the same process requires a mode switch to the kernel
Combined Approaches Thread creation is done in the user space Bulk of scheduling and synchronization of threads is by the application Solaris is an example
Multicore and Multithreding Performance of software on multicore: If only 10% of the code is inherently serial (f = 0.9), running the program on a multicore system with eight processors yields a performance gain of only a factor of 4.7
Applications That Benefit Multithreaded native applications characterized by having a small number of highly threaded processes Multiprocess applications characterized by the presence of many single-threaded processes Java applications Multiinstance applications multiple instances of the application in parallel
Fork() It is a system call that creates a new process under the UNIX operating system. It takes no arguments. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child.
Fork() returned value of fork(): * If fork() returns a negative value, the creation of a child process was unsuccessful. * fork() returns a zero to the newly created child process. * fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
Fork() Example: #include <stdio.h> #include <sys/types.h> int main() { printf( Before Forking\n ); fork(); printf( After Forking\n ); return 0; } =========================== > vi test.c > gcc test.c -o test.bin >./test.bin Before Forking After Forking After Forking