Lecture 3 Processes and Threads Outline Process Concept

  • Slides: 28
Download presentation
Lecture 3 Processes and Threads Outline: • Process Concept • Process Scheduling • Operations

Lecture 3 Processes and Threads Outline: • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Threads 1

Process Concept (1) • An operating system executes a variety of programs: - batch

Process Concept (1) • An operating system executes a variety of programs: - batch systems - jobs - time-shared systems - user programs or tasks - here job and program used interchangeably • Process - a program in execution - The execution of a Process must progress in a sequential fashion 2

Process Concept (2) • When a program is loaded into the memory and it

Process Concept (2) • When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data. 3

Process Concept (3) • The following image shows a simplified layout of a process

Process Concept (3) • The following image shows a simplified layout of a process inside main memory: 4

Process Concept (4) 5

Process Concept (4) 5

Processes The Process Model • Multiprogramming of four programs • Conceptual model of 4

Processes The Process Model • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant 6

Process =? Program 7

Process =? Program 7

Process =? Program • More to a process than just a program: - Program

Process =? Program • More to a process than just a program: - Program is just part of the process state - I run emacs on lectures. txt, you run it on homework. java – Same program, different processes • Less to a process than a program: - A program can invoke more than one process - cc starts up cpp, cc 1, cc 2, as, and ld 8

Process Creation Principal events that cause process creation: 1. System initialization. 2. Execution of

Process Creation Principal events that cause process creation: 1. System initialization. 2. Execution of a process creation system call by a running process. E. g. , data fetch over network 3. User request to create a new process. E. g, Type command 4. Initiation of a batch job. 9

Process Creation (2) • Resource sharing - Parent and children share all resources. -

Process Creation (2) • Resource sharing - Parent and children share all resources. - Children share subset of parent’s resources - prevents many processes from overloading the system. - Parent and children share no resources. • Execution - Parent and child execute concurrently. - Parent waits until child has terminated. • Address Space - Child process is duplicate of parent process. - Child process has a program loaded into it. 10

UNIX Process Creation • Fork system call creates new processes • execve system call

UNIX Process Creation • Fork system call creates new processes • execve system call is used after a fork to replace the processes memory space with a new program. Note: E. g. , when user type a command, sort, to the shell, the shell forks off a child process and the child executes sort. • On windows, Create. Process 11

Process Creation • Processes are created and deleted dynamically • Process which creates another

Process Creation • Processes are created and deleted dynamically • Process which creates another process is called a parent process; the created process is called a child process. • Result is a tree of processes - e. g. UNIX - processes have dependencies and form a hierarchy. • Resources required when creating process - CPU time, files, memory, I/O devices etc. 12

What does it take to create a process? • Must construct new PCB -

What does it take to create a process? • Must construct new PCB - Inexpensive • Must set up new page tables for address space - More expensive • Copy data from parent process? (Unix fork() ) - Semantics of Unix fork() are that the child process gets a complete copy of the parent memory and I/O state - Originally very expensive - Much less expensive with “copy on write” • Copy I/O state (file handles, etc. ) - Medium expense 13

Process Termination Conditions which terminate processes 1. 2. 3. 4. Normal exit (voluntary) Fatal

Process Termination Conditions which terminate processes 1. 2. 3. 4. Normal exit (voluntary) Fatal error (involuntary) Error exit (voluntary) Killed by another process (involuntary) Note: In UNIX this system call is kill, The corresponding WIN 32 function is Terminate. Process 14

Process Termination (2) • Most processes terminate because they have done their job. Example:

Process Termination (2) • Most processes terminate because they have done their job. Example: • When a compiler has compiled the program given to it, the compiler executes a system call to tell the OS that it is finished. - this call is exit in UNIX and Exit. Process in Windows. 15

Process Hierarchies • Parent creates a child process, child processes can create its own

Process Hierarchies • Parent creates a child process, child processes can create its own process • Forms a hierarchy – UNIX calls this a "process group“ • Windows has no concept of process hierarchy – all processes are created equal 16

UNIX Process Hierarchy 17

UNIX Process Hierarchy 17

Process Life Cycle • A process changes state as it executes. 18

Process Life Cycle • A process changes state as it executes. 18

Process State & Description • New: - The process is being created • Running:

Process State & Description • New: - The process is being created • Running: - Instructions are being executed • Waiting: - Waiting for some event to occur • Ready: - Waiting to be assigned to a processor • Terminated: - Process has finished execution 19

Process Control Block (1) Example 2: PCB 20

Process Control Block (1) Example 2: PCB 20

Example: PCB (Information and Description) • • • Process State: The current state of

Example: PCB (Information and Description) • • • Process State: The current state of the process i. e. , whether it is ready, running, waiting, or whatever. Process privileges: This is required to allow/disallow access to system resources. Process ID: Unique identification for each of the process in the operating system. Pointer: A pointer to parent process. Program Counter: Counter is a pointer to the address of the next instruction to be executed for this process. CPU registers: Various CPU registers where process need to be stored for execution for running state. CPU Scheduling Information: Process priority and other scheduling information which is required to schedule the process. Memory management information: This includes the information of page table, memory limits, Segment table depending on memory used by the operating system. Accounting information: This includes the amount of CPU used for process execution, time limits, execution ID etc. I/O status information: This includes a list of I/O devices allocated to the process. 21

Process Scheduling • Process (PCB) moves from queue to queue • When does it

Process Scheduling • Process (PCB) moves from queue to queue • When does it move? Where? A scheduling decision 22

Process Scheduling Queues • Job Queue - set of all processes in the system

Process Scheduling Queues • Job Queue - set of all processes in the system • Ready Queue - set of all processes residing in main memory, ready and waiting to execute. • Device Queues - set of processes waiting for an I/O device. • Process migration between the various queues. • Queue Structures - typically linked list, circular list etc. 23

Process Scheduling Queues 24

Process Scheduling Queues 24

Context Switch (1) • A context switch is the mechanism to store and restore

Context Switch (1) • A context switch is the mechanism to store and restore the state or context of a CPU in Process Control block so that a process execution can be resumed from the same point at a later time. • Using this technique, a context switcher enables multiple processes to share a single CPU. Context switching is an essential part of a multitasking operating system features. 25

Enabling Concurrency: Context Switch • Task that switches CPU from one process to another

Enabling Concurrency: Context Switch • Task that switches CPU from one process to another process - the CPU must save the PCB state of the old process and load the saved PCB state of the new process. • Context-switch time is overhead - System does no useful work while switching - Overhead sets minimum practical switching time; can become a bottleneck • Time for context switch is dependent on hardware support (1 - 1000 microseconds). 26

Context Switch (2) 27

Context Switch (2) 27

CPU Switch From Process to Process • Code executed in kernel above is overhead

CPU Switch From Process to Process • Code executed in kernel above is overhead - Overhead sets minimum practical switching time 28