Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Processes.

Similar presentations


Presentation on theme: "Chapter 3: Processes."— Presentation transcript:

1 Chapter 3: Processes

2 Prerequisite Test Results
80% of students are familiar with C/C++ Total score is 120 Above 80: 0 70-79: 3 60-69: 6 50-59: 6 40-49: 6 Below 40: 11

3 Objectives To introduce the notion of a process
To describe process scheduling, creation, termination, and communication To explore interprocess communication using shared memory and message passing To describe communication in client-server systems

4 OS Major Functions Interleave the execution of multiple processes, to maximize processor utilization while providing reasonable response time Allocate resources to processes Support interprocess communication and user creation of processes

5 Process A program in execution
An instance of a program running on a computer The entity that can be assigned to and executed on a processor A unit of activity characterized by the execution of a sequence of instructions a current state an associated set of system resources

6 Process Concept Multiple parts
The program code, also called text section Current activity context including program counter, processor registers Stack containing temporary data Function parameters, return addresses, local variables Data section containing global variables Heap containing memory dynamically allocated during run time

7 Process Concept (Cont.)
Program is passive entity stored on disk (executable file), process is active Program becomes process when executable file loaded into memory How to execute a program? Execution of a program starts via GUI mouse click, command line entry of its name, etc. Can we have one program in several processes? Consider multiple users executing the same program on cse.unl.edu server

8 Process in Memory

9 Process Stack Parameter passing in a read procedure call:
Figure (a) The stack before the call to read. (b) The stack while the called procedure is active.

10 Process State As a process executes, it changes state
new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur e.g., a completion of an I/O operation or a semaphore signal from another process ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

11 Diagram of Process State

12 How to describe a process? What elements are included in it?
Process Elements (I) Identifier State Priority Program counter Memory pointers: to code and data How to describe a process? What elements are included in it?

13 Process Elements (II) Context data: value of CPU registers
I/O status information Outstanding I/O requests Assigned I/O devices and used files Accounting information Amount of processor time & clock time used Time limits

14 Process Control Block (PCB)
Each process is represented by a PCB Created and managed by the operating system Contains the process elements

15 Process Control Block Allows support for multiple processes

16 OS Control Tables Tables are constructed for each entity (i.e., process, resource) the operating system manages

17 Process Tables Manage processes
A process is composed of program, data, heap, stack, and attributes process image User Data The modifiable part of the user space, including data section and heap. User Program The program to be executed. Stack Each process has one or more last-in-first-out (LIFO) stacks associated with it. A stack is used to store parameters and returning addresses for procedure and system calls. Process Control Block Data needed by the OS to control the process

18 Process Location Where are the processes located?
To manage and execute a process, at least a small portion of its image must be maintained in main memory OS must know the location of each page of each process image, achieved by process tables

19 Process Images

20 PCB: Process Control Information
Data structuring: a process may be linked to other process in a queue, ring, or some other structure. For example, all ready processes for a particular priority level may be linked in a queue. A process may exhibit a parent-child (creator-created) relationship with another process. The process control block may contain pointers to other processes to support these structures.

21 Process Creation Assign a unique process identifier
Allocate space for the process Initialize process control block Set up appropriate linkages Create or expand other data structures

22 When to Switch Process Clock interrupt I/O interrupt Memory fault
process has executed for the maximum allowable time slice I/O interrupt Memory fault memory address is in disk so it must be brought into main memory

23 When to Switch Process Trap System Call error or exception occurred
may cause process to be moved to Exit state System Call Inter-process communication I/O operation such as file open Lead to a transfer to an OS routine

24 How to Switch Process?

25 CPU Switch From Process to Process

26 Process (Context) Switch
When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch Context of a process represented in the PCB Context-switch time is overhead; the system does no useful work while switching The more complex the OS and the PCB  the longer the context switch Time dependent on hardware support Some hardware provides multiple sets of registers per CPU  multiple contexts loaded at once

27 Process (Context) Switch
Save context of processor including program counter and other registers Update the process control block of the process that is currently in the Running state Move process control block to appropriate queue – e.g., ready; blocked Select another process for execution Update the process control block of the process selected Restore context of the selected process Update memory-management data structures (page tables, TLB, etc.)

28 Execution of the OS Funtions
Process Switch vs. Mode Switch

29 Execution of the Operating System
Process-based operating system Implement the OS as a collection of system processes Process switch for OS service

30 Execution of the Operating System
Execution Within User Processes Operating system software within context of a user process Mode switch for OS service

31 OS Executes in User Space
Mode switch for OS service

32 Process vs. Mode Switch Save context of processor including program counter and other registers Update the process control block of the process that is currently in the Running state Move process control block to appropriate queue – ready; blocked; ready/suspend Select another process for execution Update the process control block of the process selected Restore context of the selected process Update memory-management data structures

33 Mode Switch Save context of processor including program counter and other registers Update the process control block of the running process Mode changes to kernel mode, finishes the OS routine Mode changes back, restore the context, and continue the running process in user mode A mode switch may occur without changing the state of the process that is currently in the Running state. Therefore, the context saving and subsequent restoration involve little overhead.

34 Process Scheduling (I)
What does process scheduling do? What is the objective of doing process scheduling?

35 Process Scheduling (II)
To maximize CPU use and for time sharing, quickly switch processes onto CPU Process scheduler selects among available processes for next execution on CPU OS maintains scheduling queues of processes Ready queues – set of all processes residing in main memory, ready and waiting to execute Device queues – set of processes waiting for an I/O device Processes migrate among the various queues

36 Process Scheduling (III)
Three different categories of process scheduler Short-term scheduler Long-term scheduler Medium-term scheduler

37 Short-term Scheduler Short-term scheduler (or CPU scheduler or Dispatcher) – selects which process should be executed next and allocates CPU Sometimes the only scheduler in a system Short-term scheduler is invoked frequently (milliseconds)  (must be fast) Trace of the Process Sequence of instructions that execute for a process Dispatcher switches the processor from one process to another  Interleaving process traces

38 Example Execution

39 Trace of Process

40 Combined Trace of Process

41 Long-term Scheduler Long-term scheduler (or job scheduler) – selects which processes should be admitted (i.e., brought into ready queue) The long-term scheduler controls the (maximum) degree of multiprogramming Long-term scheduler is invoked infrequently (seconds, minutes)  (may be slow) Processes can be described as either: I/O-bound process – spends more time doing I/O than computations, many short CPU bursts CPU-bound process – spends more time doing computations; few very long CPU bursts Long-term scheduler strives for good process mix

42 Addition of Medium Term Scheduling
Medium-term scheduler can be added if (actual) degree of multiprogramming needs to change Remove process from memory, store on disk, bring back in from disk to continue execution: swapping Present in all systems with virtual memory (Chap 9)  Virtual Memory is controlled by the Medium term scheduler of an OS

43 Suspended Processes Processor is faster than I/O so all executable processes could be waiting for I/O, while there are some new processes waiting to be admitted Swap these processes to disk to free up more memory to admit new processes Blocked state becomes suspend state when swapped to disk Two new states Blocked/Suspend Ready/Suspend

44 Two Suspend States

45 Operations on Processes
System must provide mechanisms for: process creation, process termination, and so on as detailed next

46 Process Creation Parent process creates children processes, which, in turn create other processes, forming a tree of processes Generally, process identified and managed via a process identifier (pid) Resource sharing options Parent and children share all resources Children share subset of parent’s resources Parent and children share no resources For example, rfork in FreeBSD permits fine-grained sharing of resources between parent and child processes Execution options Parent and children execute concurrently (e.g., fork) Parent waits until children terminate (e.g., vfork)

47 Process Creation (Cont.)
Address space Child duplicate of parent UNIX example fork() system call creates new process

48 fork() Process creation in Unix could be made by means of the kernel system call, fork() When a process issues a fork request, the OS: Allocates a slot in the process table for the new process; Assigns a unique process ID to the child process; Makes a copy of the process image of the parent, with the exception of any shared memory; Increments counters for any files owned by the parent, to reflect that an additional process now also owns those files; Assigns the child process to the Ready to Run state; Returns the ID number of the child to the parent process, and a 0 value to the child process.

49

50 Process Creation (Cont.)
Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork() system call creates new process exec() system call used after a fork() to replace the process’ memory space with a new program

51 C Program Forking Separate Process

52 A Tree of Processes in Linux

53 Process Termination Process executes last statement and then asks the operating system to delete it using the exit() system call Returns status data from child to parent (via wait()) Process’ resources are deallocated by operating system Parent may terminate the execution of children processes using the abort() system call.

54 Process Termination Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all its children must also be terminated cascading termination: all children, grandchildren, etc. are terminated. The termination is initiated by the operating system. The parent process may wait for termination of a child process by using the wait()system call. The call returns status information and the pid of the terminated process pid = wait(&status);

55 Process Termination If no parent waiting (did not invoke wait()), the terminated child process is a zombie, exit status kept in process table. Some operating systems allow child to exists if its parent has terminated. If parent terminated without invoking wait , process is an orphan, Linux and UNIX assign the init process as the new parent to orphaned processes, who invokes wait periodically to collect the exit status of any orphaned process and release its pid and process-table entry.

56 Multiprocess Application
What are the reasons that we sometimes want to develop multiprocess applications?

57 Cooperating Processes
Processes within a system may be independent or cooperating Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing: video surveillance Computation speed-up: merge sort Modularity multiprocess browser; concurrent server system Convenience: a user’s multiple processes

58 Multiprocess Architecture – Chrome Browser
Google Chrome Browser is multiprocess with 3 different types of processes: Browser process manages user interface, disk and network I/O Renderer process renders web pages, deals with HTML, Javascript. A new renderer created for each website opened Runs in sandbox restricting disk and network I/O, minimizing effect of security exploits Plug-in process for each type of plug-in

59 Example: Concurrent Server
Request dispatched to a worker process Dispatcher process Server Worker process

60 Interprocess Communication
Cooperating processes need interprocess communication (IPC) Two models of IPC Shared memory Message passing

61 Communications Models
(a) Message Passing (b) Shared Memory

62 Producer-Consumer Problem
Paradigm for cooperating processes: producer process produces information that is consumed by a consumer process

63 Interprocess Communication – Shared Memory
An area of memory shared among the processes that wish to communicate The communication is under the control of the user processes, not the operating system Major issue is to provide mechanism that will allow the user processes to synchronize their actions when they access shared memory Synchronization is discussed in great detail in Chapter 5

64 Examples Linux Shared Memory Linux Mapped Memory
shmget(), shmat(), shmdt() etc. Linux Mapped Memory shm_open(), mmap(), shm_unlink() etc.

65 Examples of IPC Systems - POSIX
POSIX Shared Memory Process first creates shared memory segment shm_fd = shm_open(name, O CREAT | O RDWR, 0666); Also used to open an existing segment to share it Set the size of the object ftruncate(shm_fd, 4096); Now the process could write to the shared memory sprintf(shared memory, "Writing to shared memory");

66 IPC POSIX Producer

67 IPC POSIX Consumer

68 Interprocess Communication – Message Passing
Mechanism for processes to communicate and to synchronize their actions IPC facility provides operations for sending and receiving messages The message size is either fixed or variable Self-read Chap 3.4 on message passing and understand how to solve the producer and consumer problem using message passing

69 Examples Linux IPC Pipes pipe() FIFOs (named pipes) makefifo()

70 Reading Assignment By this Wednesday, finish reading Chap 4


Download ppt "Chapter 3: Processes."

Similar presentations


Ads by Google