Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.

Similar presentations


Presentation on theme: "Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2."— Presentation transcript:

1 Multiprogramming

2 Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2

3 Example r Let’s say that you had a program that initialized the value of x to 0 and then incremented x by 1 r What if you printed the value of x to the screen? r What if you printed the value of x to a file? r Does it make a big difference in time?

4 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 all its instructions finished before process q r Process p has an instruction that requires a read/write from/to disk (or terminal) r Reading from disk is slow r Why not have instructions from q execute while p is waiting?

5 Why Multiprogramming? r Take an editor such as Emacs or a word processor such as Word m These program often have to get more data from disk m Program is blocked until the data is retrieved m There is a gap between CPU speed and disk speed

6 Multiprogramming r Multiprogramming allows for the execution of multiple processes r But only one process active at any time

7 Why Multiprogramming? r Operating systems allow for interleaved execution m On a single-processor system, no more than one process ever runs. 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 in order to maximize CPU utilization.

8 Modeling Multiprogramming The more I/O the less CPU is utilized unless you have multiprogramming

9 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 This is referred to as context switching

10 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.

11 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 (for scheduling purposes) m etc

12 Process Control Block (PCB)

13 13 Context 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 …

14 14 Context 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

15 15 Context 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

16 16 Context 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 …

17 PCB Representation in Linux r The process control block is represented by the C structure task_struct r Some of the fields in task_struct include: pid_t pid; /* process identifier*/ long state; /*state of the process*/ unsigned int time_slice; /*scheduling information */ struct task_struct sibling; /*this process’s siblings */ struct list_head children; /* this process’s children */ struct files_struct *files; /*list of open files*/

18 Process Representation in Linux r All active processes are represented using a doubly linked list of task_struct r The kernel maintains a pointer, current, to the process currently executing on the system r Example of the kernel manipulating one the fields in the task_struct is this: current->state = new_state

19 Process Execution States r As a process executes, it changes execution state r The execution 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 execution 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

20 Process Execution 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 When the wait for I/O is over there is a return to the Ready state 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  P ready queue

21 Process Execution 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 When the wait for I/O is over there is a return to the Ready state 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  P ready queue

22 Question Why is there no arrow from waiting to running? When should a process be preempted?

23 Scheduling r The purpose of multiprogramming is to have a 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

24 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

25 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

26 Ready Queue and Various I/O Device Queues

27 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

28 Summary r Discussed the need for multiprogramming r Process representation


Download ppt "Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2."

Similar presentations


Ads by Google