![]() |
Scheduling and Threads
The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently that it appears to the user that all programs are running concurrently. In uni-processor only one process is running.
Scheduling QueuesAs process enter the system, they are put into a job queue. The figure below is showing processes in main memory are ready and waiting to execute. These are kept in ready queue. List of processes waiting for I/O device is called a device queue. http://imgs.g4estatic.com/operating-system/OS10.jpg In the figure above rectangular box represents a queue such as ready and device queues. Circles represent resources that serve the queues. Arrows represent flow of processes. Events
Schedulers A process migrates between the various scheduling queues throughout its lifetime, such as longterm scheduling, medium-term scheduling and short-term scheduling.The details of the scheduling concepts will be discussed in the next article. Co-Operating ProcessesA process is independent when it can't be affect or be affected by the execution of another process. When the process shares data with other processes, it is called co-operating processes.A cooperating process can affect or be affected by the execution of another process. Reasons for Co-operation
One of the best example of such process is Producer-Consumer Problem. Producer-Consumer ProblemProducer process produces information and consumer process consumes information, by using buffer. That is buffer can be filled by the producer and emptied by the consumer. Consumer must wait until an item is to be produced.Synchronization is necessary so that consumer cannot consume an item which is not produced.Similarly producer should also wait if the buffer is full, Conditions
Coding part
Producer Process Code:
repeatCode:
repeatInter Process Communication (IPC)Inter process communication facility in operating system provide the means for co-operating processes to communicate and to synchronize their actions. That is to communicate with each other via IPC. Inter process communication is best provided by a message system. Many tasks can be accomplished in isolated processes, but many other tasks require Inter Process Communication. Basic Structure The function of a message system is to allow processes to communicate with each other. Inter process communication provides two operations: send and receive message. Messages can be of either fixed or variable size. Communication link properties to implement
Several methods for logically implementing a link
Naming In direct communication, name the recipient or sender of the communication, by using send and receive primitives. Send and receive primitives are:
You may come up with a very common question Which process will receive the message sent by P1? Solution to this question is:
If each mailbox has a unique owner, there can be no confusion about who should receive a message sent to this mailbox. Operating system provides a mechanism that allows a process
Buffering A link can determine the number of messages that can reside in it temporarily. Messages queue can be implemented in three different ways:
Exception Conditions Message system is very much useful in a distributed environment, where processes may reside at different sites i.e., machines. In such cases there is a probability that an error will occur during communication and processing. Whenever a failure is to occur in either a centralized or distributed system, some error recovery, that is exception-condition handling must take place. Process Terminates In this process, either a sender or a receiver may terminate before a message is processed. Lost Messages Messages may be lost due to hardware or communication line failure. Methods for dealing this situation are:
Scrambled Messages Message may be delivered to its destination, but scrambled on the way due to noise in the communication channel. For detecting such errors use error checking codes like checksums, parity, CRC, etc. ThreadsThread is also called as Lightweight process (LWP). Thread is a basic unit of CPU utilization and consists of :
A thread share with its peer threads the code section,data section and resources like open files. A traditional or heavy weight process is equal to a task with one thread. A task does not do anything if no threads are in it, and a thread must be in exactly one task.Threads operate, in many respects same as processes. Thread states are ready, waiting, running and terminated. Like processes, threads share CPU and only one thread at a time is active. A thread within a process executes sequentially, and each thread has its own stack and program counter. Having multiple threads running in parallel in one process is analogous as to having multiple processes running in parallel in one computer. Producer and consumer can be threads in a task. In multiprocessor system, producer and consumer threads can execute in parallel. Threads can be supported by the operating system kernel. This is shown in the figure below: http://imgs.g4estatic.com/operating-system/OS11.jpg E.g., consider two processes, one with one thread (process ‘a’) and the other with 50 threads (process ‘b’). In general process ‘a’ and process ‘b’ receives the same number of time slices, so the thread in process ‘a’ runs 50 times as fast as a thread in process ‘b’. If each thread is scheduled independently, then process ‘b’ can receive 50 times the CPU time that process ‘a’ receives. Why Threads? Lets take an example. In modern GUI user can select an action to be performed at almost anytime i.e. an event can occur at any time. The program must be able to respond to a variety of different events at unknown times and in an unknown order of request. Such user driven events are usually too small to justify the creation of new process.Instead the action is implemented as a thread. Thread can be executed independently without the overhead of a process.The primary requirement for thread is an area to store the program counter and registers associated with that thread. Multithreading It describes a situation of allowing multiple threads executing in the sane process.Modern Operating Systems supprt multiple threads of execution within a single process. MS DOS supports single thread model. Unix supports multiple User but supports only one thread per process. Windows 2000,Solaris,Linux support multple threads. Advantages Of Threads
Disadvantages Of Threads
|
Re: Scheduling and Threads
Process scheduling is a technique which is used when there are limited resources and many processes.And only one process can be run at a time.So which process will be execute first is decided by process scheduling.If the system has more than one processor, then it is possible to execute more than one process at the same time.
Operating systems may have up to three distinct types of schedulers: long-term scheduler or high-level scheduler,medium-term scheduler and a short-term scheduler, The names suggest the relative frequency in which they are performed. |
Re: Scheduling and Threads
Nominate this article for Article of the month - Jun 2010
|
Re: Scheduling and Threads
Different people use different techniques for this.I can not recommend any one.
|
Re: Scheduling and Threads
Quote:
|
| All times are GMT +5.5. The time now is 00:34. |