Download presentation
Presentation is loading. Please wait.
Published bySara Bennett Modified over 8 years ago
1
Multiprogramming
2
Readings r Chapter 2.1 of the textbook
3
Multiprogramming r Assume we have two programs P and Q. r Each is started up to make two processes p and q. r It is not the case that process p has have all its instructions finished before process q r Process p has an instruction that requires a read from disk r Reading from disk is slow r Why not have instructions from q execute while waiting r There are many issues to be addressed which we will discuss
4
Multiprogramming r Allow for the execution of multiple programs r Only one program active at any time r Example: Look at Microsoft Word m Microsoft Word has to go to the disk a lot m Program is often blocked until the data is retrieved
5
Multiprogramming r Allow for the execution of multiple programs r Only one program active at any time r Example: Look at Microsoft Word m Microsoft Word uses more disk space CPU m Often has to get more data from memory or disk m Program is often blocked until the data is retrieved
6
Why Multiprogramming? r Take an editor such as Emacs or a word processor such as Word m These programs make more use of disk space and memory than CPU cycles m They often have to get more data from memory or disk m Program is blocked until the data is retrieved m There is a gap between CPU speed and memory speed as well as between CPU speed and memory speed
7
Example r Let’s say that you had a program that computed the sum of the square roots of numbers from 0 to N e.g., for (i=0; i < num_iterations; i++) sum = sum + sqrt((double) i); r What if we periodically printed intermediate values of the variable sum to the terminal or to a file? r Does it make a big difference in time?
8
Why Multiprogramming? r Operating systems allow for interleaved execution m On a single-processor system, no more than one process ever runs at the same time. m However, one process’s instructions may be executed before the completion of the instructions from another process r The objective is to have some process running at all times, to maximize CPU utilization.
9
Modeling Multiprogramming
10
Process Switching r Current process executes an I/O operation r OS needs to be able to suspend current process so that another process can execute r Referred to as context switching
11
11 Process Switching r Program instructions operate on operands in memory and (temporarily) in registers Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog2 State Prog1 has CPU Prog2 is suspended Prog2 Code Prog2 Data Load A1, R1 Load A2, R2 Add R1, R2, R3 Store R3, A3 …
12
12 Process Switching r Saving all the information about a process allows a process to be temporarily suspended and later resumed from the same point Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State OS suspends Prog1 Prog2 Code Prog2 Data Prog2 State
13
13 Process Switching r Saving all the information about a process allows a process to be temporarily suspended and later resumed Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State OS resumes Prog2 Prog2 Code Prog2 Data Prog2 State
14
14 Process Switching r Program instructions operate on operands in memory and in registers Memory Prog1 Code Prog1 Data CPU ALU SPPC Prog1 State Prog2 has CPU Prog1 is suspended Prog2 Code Prog2 Data Load A1, R1 Load A2, R2 Sub R1, R2, R3 Store R3, A3 …
15
Process Switching r OS needs to be able to suspend current process r OS captures information about a process r Information captured must be sufficient to restore the hardware to the same configuration it was in when the process was switched out.
16
Characterizing a Process r Each process is represented in the OS by a process control block (PCB) which contains all the state for a program in execution including (but not limited to): m Pointer to text, data and stack segment information m An execution stack encapsulating the state of procedure calls m The program counter (PC) indicating the next instruction m Current values of the set of general-purpose registers m A set of operating system resources e.g., open files, network connections m Process identifier (PID) m Process priority m etc
17
Process States r As a process executes, it changes state r The state of a process is defined in part by the current activity of the process r A process may be in one of the following states: m New: The process is being created m Running: Instructions are being executed m Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of signal) m Ready: The process is waiting to be assigned to a processor m Exit: The process has finished executing r Only one process can be running on any processor at any instant r Many processes may be ready and waiting
18
Process States NewReadyRunningExit Waiting When you run a program, a new process is created If the system has sufficient memory, then the new process is loaded into memory and placed in ready queue CPU scheduler takes a process from the head of a ready queue to execute (Sometimes, there may be multiple ready queues.) Admit Dispatch Timeout Event Occurs Event Wait After finish execution, exit If processes are scheduled in a round robin manner, then when time quantum expires, the process is returned to ready queue If processes are scheduled in a round robin manner, then when time quantum expires, the process is returned to ready queue Process may be blocked by: - I/O (wait for I/O to complete) - semaphore wait - sleep - etc. When the waiting is over An interrupt is generated Process is then returned to ready queue
19
Question Why is there no arrow from waiting to running? When should a process be preempted?
20
Scheduling r The purpose of multiprogramming is to have some process running at all times r The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each process r The process scheduler selects an available process r There may be multiple processes to select from
21
Scheduling Queues r As processes enter the system, they are put into a job queue, which consists of all processes in the system r The processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue r Queues are implemented using linked list r A ready queue header contains pointers to the first and last final process control blocks in the list r Each PCB includes a pointer field that points to the next PCB in the ready queue
22
Process Control Block (PCB) Pointer (linked list) Process state Program counter CPU registers Memory limits Open files Accounting info (e.g., running time)
23
Other Queues r When a process is allocated the CPU, it executes for a while and eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the completion of an I/O request r The list of processes waiting for a particular I/O device is called a device queue
24
Question What happens to a process’s PCB when it is waiting in a device queue?
25
Issues Related to Multiprogramming r Shell m Creating new processes (already discussed) r Threads m A “lighter weight” process r Scheduling m Determining when a process should run r Booting m How do we get the first process
26
Summary r Discussed the need for multiprogramming r Process representation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.