260 likes | 274 Views
Operating System Concepts and Techniques Lecture 2. Multiprogramming, Multithreading, Multiprocessing, and Multitasking M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011.
E N D
Operating System Concepts and Techniques Lecture 2 Multiprogramming, Multithreading, Multiprocessing, and Multitasking M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011. To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com
Definitions • Program: a set of instructions which is prepared to perform a specific assignment if executed by a computer. • Program need not be connected; it could be stored on a flash memory and placed in one’s pocket. • Program is not an active entity. It is completely passive.
Definitions… • Process: is created by operating system from a program • To do so, it has to perform many activities, like assigning a name, allocating space, (partially) loading the corresponding program, etc. • Roughly speaking, A process is an active program • A process is created to run a program by using computer facilities such as CPU; like a human who is born to live his life
Single programming • Single-programming: there exist at the most one user process at any given time in the computer; thus there is usually one ongoing activity at a time • That is, from many devices within the computer often one device is active at any given time • If a process has asked for a data from the user, the system has to wait until this data is entered before being able to proceed • If this data entry takes one second the CPU could have done millions of instructions if it did not have to wait • With single-programming the computer resources are not used efficiently
Process States in Single-Programming • With single-programming, right after a process is born the system starts running it • It continues until the process has to wait for data, output results, or wait for an event • There are special purpose processors called Input/Output (I/O) processors for transferring data from input devices to main memory and from main memory to output devices • It is understandable that such a processor will perform the specific task better than a general-purpose processor, i.e., CPU • While an I/O operation is in progress, the CPU has to wait and do nothing. After the I/O operation is completed, the CPU will resume the execution of the program • This cycle, of going through process states of running and input/output, may be repeated over and over, until the job is completed or, for some reason, the process is aborted • The life cycle of a process in a single-programming environment is shown in Figure 1
Blocked for I/O Process birth Running Input/Output I/O completed Process Termination Process’s life cycle Figure 1: The life cycle of a process in single-programming environments
Processor wait ratio • If the average execution time of a program with single-programming is e and the average I/O time is b, then the following ratio is the CPU wait fraction(w). It is actually the fraction of the time the CPU is idle. • For example, if execution time of programs is 10, of which 9 seconds is spent on I/O, then w = 9/10 = 0.9. This means, on the average, 90% of the CPU time is wasted.
The Multiprogramming Concept • Multiprogramming: is a technique that allows more than one program to be ready for execution (process) and provides the ability to switch from one process to another, even if the former is not completed • Of course, sometimes in the future we will have to switch back to the first process and resume (not restart) its computation • This technique works for both single-processor (like some personal computers) and multiprocessor (such as large main frame) computers. • Multiprogramming is mainly accomplished by the operating system. The hardware provides some specific circuitry that may be used by the operating system in the course of facilitating multiprogramming
Multiprogramming and PCs • Do we need multiprogramming for PCs? Yes. All PC users like to run many applications simultaneously • Nobody runs for example an Internet explorer looking for an information while staring at the monitor for the results for a long time. • In this era of computer usage, every general-purpose operating system must have the following capabilities: • It must provide an environment to run processes in a multiprogramming fashion. • It must act as a service provider for all common services that are usually needed by computer users, such as copying files, making new folders, compressing information, sending and receiving messages from/to other computers in the network, etc. • Its user interface must be easy to use and pleasant to work with.
Multiprogramming productivity • Multiprogramming increases productivity • If CPU wait time is represented by w in single-programming environment, the CPU wait time decreases to approximately wn for a system running n processes simultaneously. • Example: If w = .9 then wn =0.59 ; meaning that if we have five processes running simultaneously, the CPU utilization is increased by (0.41-.10)/0.1*100 = 310%. • By increasing the CPU utilization other device’s utilization is also increased.
Process State Transition Diagram • The life cycle of a process in multi-programming has three states • Ready: ready to use CPU, however CPU is busy • Running: Uses CPU • Wait/Blocked • Wait: process is waiting for a device or an event • Blocked: process is waiting for its I/O to be completed by an I/O processor • May switch between states many times during its life
Process Termination Running A process is picked to run Preempted for the interest of others Needs I/O or circumstance Process birth Wait/Blocked Ready Running obstacle is vanished Process’s life cycle Figure 2: Basic process state transition diagram in multiprogramming
Requirements of Multiprogramming • Process Switching possibility: the system must be able to safely switch from one process to another and be able to return in the future • This is called context switching. • Direct Memory Access: I/O processors must be able to directly access main memory without interference and conflictions • The Interrupt System: I/O processors and monitoring devices must be able to safely communicate with the CPU to report or to get new assignments
To context switch • Save enough information from current process to be able to return to it and continue • Save all temporary storage information such as registers and flags • Load/initialize the information of new process • Change PC • Need some help from Hardware to make the switching process fast
Interrupt • Interrupt: a signal that is sent to the CPU to capture its attention. It may be issued by different sources such as: • By a fault detecting hardware that is trying to informs the CPU of a circuitry malfunction • By a monitoring circuit within the ALU that controls the acceptability of the data size for the operation being performed and, consequently, the operation result size • By a user that may press a special key like “break” at any time • By an slave processor that wants to inform the CPU of the completion of the assigned task • By a timer etc.
Interrupt masking • Some interrupts are maskable by user programs • Masked interrupts are ignored by the CPU • This is analogous to ignoring a door knock • Some interrupts are not user-maskable • It is not reasonable to let the computer continue in the presence of a hardware failure. • However, all interrupts are maskable within the kernel of an operating system.
Interrupt Categorization • Interrupts are categorized into few classes • Different classes of interrupts require different handling consideration • Each category of interrupts is represented by a bit in interrupt vector within the CPU • Each interrupt category has a priority • When handling a higher priority interrupt handling any lower priority interrupt is postponed
Program execution suspension • A non-masked interrupt causes the suspension of the currently executing program. • Usually possible to resume the execution of the program after the interrupt is handled. • Interrupt signals reach the CPU and are stored in the interrupt vector. The interrupt(s) is ignored until the currently executing machine instruction is completed. • There are very few exceptions. One example might be the “move” instruction in some computers that is suppose to move a big chunk, say 256 bytes, of data from one location of main memory to another. • The CPU always looks for interrupt signals just before fetching a new instruction to execute. If there are any interrupt signals, the system handles them in the order of priorities and urgencies.
Fetch cycle Handle all non-masked interrupts Execute cycle Move the data upon which the instruction has to be executed from main memory to the CPU. Read the instruction pointed out by the Program Counter (PC) register from main memory and move it to the CPU Execute the instruction, i.e., perform what is requested by the instruction. Perhaps this may readjust the PC. Find out what this instruction is Adjust PC for the next instruction Fetch-Execute Cycle with interrupt Figure 4: A simplified functional model of a computer with interrupt
Privileged instructions • Privileged instructions are those which are not usable by assembly application programmers • Privileged instructions are executable only within the operating system kernel • Other parts of the OS and application programmers may execute privileged instruction by special means that are provided by the operating system • Disable interrupt which disables all interrupts is a sample privileged instruction
Multiprocessing • If you think clearly, you will notice that we should have used multiprocessing instead of multiprogramming. This is true. Unfortunately, the term “multiprogramming” is recognized for this technique of the operating system and we will stick to it • On the other hand, “multiprocessing” is used for systems with more than one processor. Processors, in such a system, can collectively run many tasks simultaneously
Multitasking • Computer users like to have many application programs simultaneously operational. This is necessary because some application programs require long processing times before the desired results can be produced. • It is true that by having more than one application program operational, the time that it takes for each process to complete its task increases. However, the overall system productivity • These simultaneously executing programs are called tasks. Therefore, a system with the capability of multitasking allows users to activate more than one task, or application program, at a time. An Internet browser that searches for some information and A word-processing software that is activated to perform the word-processing task are applications. • The operating system will switch between tasks based on the tasks current states and their requirements and priorities. • Multitasking is only possible when multiprogramming is the fundamental capability of simultaneously executing pieces of software. • Most modern operating systems, like UNIX, Linux, and Windows, support multitasking.
Multithreading • A multithreading operating system is one that is capable of handling processes and threads at the same time and in which from each process the system is able to generate more than one thread. • In such an operating system, there must be facilities for thread creation, deletion, switching, etc. • Such an operating system allows users to generate more than one request to a process at a time. For example, a browser can be made to search simultaneously for more than one topic, even though there is only one copy of the “browser program” in main memory. • The multiprogramming methodology and technique are essential in the implementation of multithreading. In this new environment, a thread becomes the smallest functional object to which CPU (or a PU) is assigned. • Details of thread methodology and technique is discussed in upcoming lectures.
Summary • The ultimate goal in the design and implementation of an operating system is to produce a handy software program that manages computer resources in the most efficient way so as to serve computer users correctly, reliably and fairly. • This is not achievable in single-programming environments. • Modern operating systems are built with the capabilities of multiprogramming, multitasking, and multithreading. • Providing these capabilities requires many hardware and software methodologies and techniques. • A good understanding of process creation, life cycle, and termination, along with its state transition conditions is most essential in elucidating the needs of different mechanisms within the operating system. • Some of these mechanisms, namely process switching, interrupt system and handling, and direct memory access, are briefly explained in this chapter. • Multithreading, as an offspring of multiprogramming, has become an essential part of all modern operating systems.
Find out • How many interrupt classes your computer’s processor has • Some privileged instructions and the reasons for being privileged • How are interrupts actually masked • The exact differences of multiprogramming and multiprocessing • What an ultra DMA is • The exact differences between program and process