Process Management. Outline. Process vs. Program. Address Space. How do we run a program? What are steps to create a process?

Similar documents
CS3733: Operating Systems

CS 3733 Operating Systems

CS3733: Operating Systems

CHAPTER 2: PROCESS MANAGEMENT

Lecture 2 Process Management

CS 5523: Operating Systems

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Processes: Introduction. CS 241 February 13, 2012

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 3: Process Concept

Exceptional Control Flow Part I Oct. 28, 2009"

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

IPC and Unix Special Files

Exceptional Control Flow Part I September 22, 2008

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Exceptional Control Flow Part I Oct. 17, 2002

Process. Program Vs. process. During execution, the process may be in one of the following states

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

Exceptional Control Flow Part I

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Giving credit where credit is due

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Announcement (1) sys.skku.edu is now available

University of Washington What is a process?

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Chapter 3: Processes

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Processes. Process Concept

CHAPTER 3 - PROCESS CONCEPT

Processes and Threads

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Chapter 5 CPU scheduling

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

Exceptional Control Flow Part I

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

Processes. CSE 351 Autumn Instructor: Justin Hsia

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts 8th Edition,

Processes. CSE 351 Autumn Instructor: Justin Hsia

Operating Systems. Figure: Process States. 1 P a g e

Processes, Exceptional

Exceptional Control Flow: Exceptions and Processes

CS Lecture 2! Processes! George Mason University! Fall 2010!

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Chapter 5: CPU Scheduling. Operating System Concepts 8 th Edition,

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Part Two - Process Management. Chapter 3: Processes

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

Chapter 4: Processes. Process Concept

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Processes and Threads

Chapter 4: Processes

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

Operating System Design

Chapter 5: CPU Scheduling

OPERATING SYSTEMS: Lesson 4: Process Scheduling

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

W4118 Operating Systems. Junfeng Yang

Chapter 3: Processes. Operating System Concepts 9 th Edition

CPU Scheduling. CSE 2431: Introduction to Operating Systems Reading: Chapter 6, [OSC] (except Sections )

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

Mid-Semester Examination, September 2016

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

CSC 252: Computer Organization Spring 2018: Lecture 19

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

CSCE 313: Intro to Computer Systems

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CS370 Operating Systems

CSI Module 2: Processes

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Approaches to Concurrency

Killing Zombies, Working, Sleeping, and Spawning Children

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

The Big Picture So Far. Chapter 4: Processes

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

CS370 Operating Systems Midterm Review

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

SMD149 - Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Chapter 3: Processes. Operating System Concepts 8th Edition

The Big Picture So Far. Chapter 4: Processes

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Processes COMPSCI 386

CPU Scheduling: Objectives

CS370 Operating Systems

Operating System Structure

LECTURE 3:CPU SCHEDULING

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Diagram of Process State Process Control Block (PCB)

Transcription:

Outline Process Management Instructor: Dr. Tongping Liu Basic concepts of process Ø Address space and Process control block (PCB) Basic operations for process management Ø Process creation/termination Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication (IPC) Ø shared memory vs. message passing 1 2 Process vs. Program Program: a set of functions Ø a passive entity Ø stored as files on disk Process: a program in execution Ø Dynamic concept: running of a program Address Space An address space is a range of valid addresses in memory that are available for a program or process. That is, it is the virtual memory that a program or process can access. Typically, the largest address space for 32bit machine is 4G. Multiple processes may come from the same program How do we run a program? What are steps to create a process? 3 4 1

Address Space in Process Range of memory locations Ø code section Ø data section Ø Stack Ø Auxiliary: environment variables and command line arguments Ø Heap: memory for dynamically allocated data items How big is the address space? 2 n 0 Unused User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Auxiliary Process States 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" Ø ready: The process is waiting to be assigned to a processor" Ø terminated: The process has finished execution" 5 Running Context of A Process Process context is basically the process s current state (what is in its registers). Process Control Block (PCB) OS creates a PCB for each process OS manages the processes à a list of PCBs Special Registers, in addition to general registers Ø Program Counter (PC): contains the memory address of the next instruction to be executed. Ø Stack Pointer (SP): points to the top of the current stack in memory. The stack contains one frame for each procedure that has been entered but not yet exited. 7 8 2

Example: PCB in Linux (task_struct) PCB Used in Context Switches Process Management Registers Program Counter Stack Pointers Process State Priority Scheduling Parameters (slice) Process ID Parent process Process group Time when process started CPU time used Memory Management Pointer to text (code) segment Pointer to data segment Pointer to stack segment File Management Root directory Working directory User Id Group Id List of open files 9 Context Switch When CPU switches to another process, the system must save the states of the old process and load the saved states for the new process via a context switch! Context of a process represented in the PCB" Context-switch time is overhead" Ø 1 ~ 1000 ms" Hardware support" Ø Multiple set of registers" Other performance issues/problems" Ø Cache content: locality is lost" Ø TLB content: may need to flush" Outline Basic concepts of process Ø Address space and Process control block (PCB) Basic operations for process management Ø Process creation/termination Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication (IPC) Ø shared memory vs. message passing 12 3

Process Creation and Hierarchy How do we control processes? When to create processes? Ø System initialization Ø User request to create a new process Ø Running processes use system call to create new process (exec() family) Process hierarchy Ø Parent process creates child process(es) Process Tree in Solaris 13 performancemanagementcompanyblog.com Process Creation Address space" Ø Child duplicate its parent (data, program)" Ø Child has a program loaded into it (exec())" Execution" Ø Parent and children execute concurrently" Ø Parent waits until children terminate" Resource sharing: complicated" Ø Shared open files, but not descriptors" Ø Different page tables" Process Creation: System Call in LINUX Each process has a process identifier (pid) Parent process executes fork to spawn a child process The child process has a separate copy of the parent s address space. Both the parent and the child continue execution at the instruction following the fork system call Ø Return value of 0 à new (child) process continues Ø Return value >0, which is pid of child process à parent process continues Ø Return value is -1, fork() fails 16 4

When fork() may fail (man 2 fork)? An Example: fork( ) In UNIX EAGAIN Ø Cannot allocate memory to copy the parent's page tables and allocate a task structure for the child. Ø Encounter the limit of active process Ø In a scheduling state (SCHED_DEADLINE) ENOMEM Ø Failed to allocate the necessary kernel structures ENOSYS Ø fork() is not supported, e.g. Hardware has no MMU Text pid = 25 Data Stack Process Status < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); <parent code> wait(cpid); File File Resources Resources Text pid = 26 Data Stack Process Status COW cpid = 26 < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); <parent code> wait(cpid); cpid = 0 17 UNIX kernel 18 Load A Different Program Create & Terminate Processes Child process uses execlp to load a different program void main (){ int pid; pid = fork(); if (pid < 0) {error_msg else if (pid == 0) {/* child process */ execlp( /bin/ls, ls, NULL); else { /* parent process */ /* parent will wait for the child to complete */ wait(null); exit(0); 19 What does this print out? void main() { printf("l0\n"); fork(); printf("l1\n"); fork(); printf("\n"); L1 L0 L1 5

Create & Terminate Processes Create & Terminate Processes What does this print out? void main() { printf("l0\n"); fork(); printf("l1\n"); fork(); printf("l2\n"); fork(); printf("\n"); Does it always print in order? L2 L1 L2 L2 L0 L1 L2 What does this print out? void main() { printf("l0\n"); if (fork()!= 0) { printf("l1\n"); if (fork()!= 0) { printf("l2\n"); fork(); printf("\n"); 04_fork.c Create & Terminate Processes L0 L1 L2 05_fork.c 05_zombie.c What happens here? Linux void main() { if (fork() == 0) { /* Child */ printf("terminating Child, PID = %d\n", getpid()); exit(0); else { printf("running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ 05_zombie.c Zombie! 6

Create & Terminate Processes Create & Terminate Processes What happens here? What about this one? void main() { if (fork() == 0) { /* Child */ printf("terminating Child, PID = %d\n", getpid()); execv( /bin/ls, ls); else { printf("running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ 05_test.c void main() { if (fork() == 0) { /* Child */ printf("running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ else { printf("terminating Parent, PID = %d\n", getpid()); exit(0); Zombie! 06_zombie.c: Orphan process 06_zombie.c Zombie? Handle Zombie and Orphan Processes in Linux Zombies may cause resource leak, such as memory and process table entries (PIDs). Really hard to detect since the main process already exited! A orphan process is a process that is still executing, but whose parent has died. Ø Since there is no memory allocated to zombie processes the only system memory usage is for the process table entry itself the primary concern with many zombies is not running out of memory, but rather running out of process table entries Orphan processes will be adopted by init process (process PID 1), which waits on all of children 28 7

How do we control processes? How do we control processes? Need a way to kill the zombies! Need a way to kill the zombies! This is called reaping! Create & Terminate Processes Create & Terminate Processes So, how do we reap a child process programmatically? So, how do we reap a child process programmatically? wait() waitpid() Zombie? Zombie? 8

Create & Terminate Processes int wait(int* child_status) void main() { pid_t pid[n]; fork a child processes int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminated abnormally\n", wpid); Create & Terminate Processes int wait(int* child_status) void main() { pid_t pid[n]; wait for each to terminate int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminated abnormally\n", wpid); Create & Terminate Processes int wait(int* child_status) void main() { pid_t pid[n]; wait for each to terminate int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminated abnormally\n", wpid); Get Info on Status Create & Terminate Processes int waitpid(pid, &status, options) void main() { pid_t pid[n]; wait for PID to terminate int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = N-1; i >= 0; i--) { pid_t wpid = waitpid(pid[i], &child_status, 0); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminated abnormally\n", wpid); 9

Create & Terminate Processes int waitpid(-1, &status, 0) is the same as int wait(&status) Process Termination Voluntarily Ø process finishes and asks OS to delete it (exit). Involuntarily Ø parent terminate execution of children processes ( e.g. TerminateProcess() in Win32). Ø A process may also be terminated due to errors, segv After process terminate Ø Process resources are de-allocated by OS. Parent process is terminated (e.g., due to errors) Ø What will happen to the children process?! Orphan 38 Outline Basic concepts of process Ø Address space and Process control block (PCB) Basic operations for process management Ø Process creation/termination Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication (IPC) Ø shared memory vs. message passing Multiprogramming CPU-I/O Burst Cycle Multiprogramming is a form of parallel processing in which several programs are run at the same time on a uniprocessor. Ø Objective? Maximize CPU utilization. When a process wait for IO, all waiting time is wasted and no useful work is accomplished. 39 40 10

CPU Scheduler Selects one process that are ready to execute, and allocates the CPU to it (scheduler)" CPU scheduling decisions may take place when a process:" 1. "Switches from running to waiting state (e.g., I/O request)" 2. "Switches from running to ready state (e.g, quantum time passed)" 3. "Switches from waiting to ready (e.g., I/O is complete)" 4. Terminates" No choice under 1 and 4 " "scheduling is nonpreemptive! Under 2 and 3, scheduling " "is preemptive! Scheduling Goals Select a process that should be executed next " All systems" Ø Fairness: give each process a fair share of the CPU" Ø Balance: keep all parts of the system busy; CPU vs. I/O" Ø Enforcement: ensure that the stated policy is carried out" Batch systems" Ø Maximize Throughput: the number of jobs that are completed per unit time" Ø Minimize Turnaround time: the time from submission to completion" Ø CPU utilization: CPU time is precious à keep the CPU as busy as possible" " Scheduling Goals Interactive systems" Ø Response/wait time: respond quickly to users requests" Ø Proportionality: meet users expectations" " Real-time systems: correct and in time processing" Ø Meet deadlines: deadline miss à system failure!" Ø Hard real-time vs. soft real-time: aviation control system vs. DVD player" Ø Predictability: timing behaviors is predictable" Scheduling: Which Process to Run? How To pick the Lucky Process? What are the scheduling objectives? 44 11

CPU Scheduling on One Core Scheduling Queues (cont.) Execute only one process at a time Ø Other processes should wait for CPU Scheduling queues Ø Ready queue processes in main memory, ready and waiting to execute Ø Wait queue processes waiting for signals/interrupts Ø Device queues processes waiting for I/O operations Processes move between the various queues 45 46 Dispatcher Dispatcher gives control of CPU to the process selected by CPU scheduler; this involves:" Ø switching context" Ø switching to user mode" Ø jumping to the proper location in the user program to restart that program" Dispatch latency time it takes for the dispatcher to stop one process and start another running (overhead)" Performance Metrics for Scheduling CPU utilization " Ø What percent of the time the CPU is to run programs? " Ø util= (t total t idle t dispatch ) / t total Throughput " Ø Number of processes that complete their execution per time unit" Turnaround time " Ø Amount of time to execute a particular process" Waiting time" Ø Amount of time a process has been waiting in the ready queue" Response time " Ø Amount of time it takes from when a request was submitted until the first response is produced" Ø NOT possible to optimize for all metrics with a single scheduling algorithm 48 12

Calculate turnaround, wait, response times Given a process " Ø Arrival time: " "t a Ø First response time: "t r Ø Finish time: " "t f" Ø Total CPU burst time: t cpu Ø Total I/O time: " "t io Turnaround time: the process spent in the system" Ø T turn_arround = t f t a = t cpu + t io + t wait n Waiting time: the process spent in the ready queue" Ø t wait = (T turn_arround t cpu t io ) Response time: the process waited until the first response" Ø t response = t r t a Activities in Processes Bursts of CPU usage alternate with periods of I/O wait CPU-bound: high CPU utilization, interrupts are processed slowly I/O-bound: more time is spending on requesting data than processing it Process 1: CPU bound" Process 2:" I/O bound" Time" CPU bursts" Total CPU usage" I/O waits" Total CPU usage" 50 Classical Scheduling Algorithms FCFS: non-preemptive, based on arrival time Ø Long waiting time, e.g. long process before SSH console? SJF(shortest job first): preemptive & non-preemptive Ø Optimal in term of waiting time RR (Round-robin): preemptive Ø Processes take turns with fixed time quantum e.g., 10ms 51 First-Come, First-Served (FCFS) Suppose the following processes arrive at time t=0 in the given order " "Process "Burst Time "" " P 1 "24" " P 2 "3" " P 3! 3! The Gantt Chart for the schedule is: " P 1! P 2! P 3! " 0! 24! 27! 30! " Waiting time for P 1 = 0; P 2 = 24; P 3 = 27" Average waiting time: (0+24+27)/3=17" Problem: long jobs delay every job after them. Many processes may wait for a single long job. n CPU utilization : What percent of the time the CPU is n Throughput : Number of processes that complete their execution per time unit n Turnaround time : Amount of time to execute a particular process n Waiting time: Amount of time a process has been waiting in the ready queue n Response time : Amount of time it takes from when a request was submitted until the first response is produced, not output (for timesharing environment) 13

First-Come, First-Served (FCFS) Suppose that the processes arrive in the order:" " " P 2, P 3, P 1 " The Gantt chart for the schedule is: " P 2! P 3! P 1! 0! 3! 6! 30! Process Gantt Chart CPU Gantt Chart: show which process uses CPU at any time For each process, PGT show its state at any time For the example: P1: 24 (CPU burst time), P2: 3, P3: 3 Ø Process Gantt chart Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3" Average waiting time: (6 + 0 + 3)/3 = 3" " Convoy effect: short process behind long process" Ø Another presentation: R (running), r (ready), w(waiting) 54 Shortest Job First (SJF) Calculate turnaround, wait, response times 4" 3" 6" 3" A" B" C" D" SJF scheduler" Current job queue" Execution order" 3" 3" 4" 6" B" D" A" C" Shortest job goes first Ø Jobs are sorted in increasing order of execution time Optimal for wait time Ø Minimize average waiting time for a given set of processes" Ø Why?! Problem: how does the scheduler know how long a job will take? Given a process " Ø Arrival time: " "t a Ø First response time: "t r Ø Finish time: " "t f" Ø Total CPU burst time: t cpu Ø Total I/O time: " "t io Turnaround time: the process spent in the system" Ø T turn_arround = t f t a = t cpu + t io + t wait n Waiting time: the process spent in the ready queue" Ø t wait = (T turn_arround t cpu t io ) Response time: the process waited until the first response" Ø t response = t r t a 55 14

SJF vs. SRJF-Preemptive Round Robin (RR) scheduling A(4) and C(6) arrive at time 0 B(2) arrives at time 1 D(3) arrives at time 7 SJF schedule" 0" SRJF preemptive " A" B" C" D" 4" 6" 12" 15" A" B" A" C" D" C" 0" 1" 3" 6" 7" 10" 15" 4" 6" 2" 3" A" C" B" D" t wait = (T turn_arround t cpu t io ) [(4-4)+(6-1-2)+(12-0-6)+(15-7-3)]/4=14/4=3.5 Wait time: which one is better? [(6-0-4)+(3-1-2)+(15-0-6)+(10-7-3)]/4=11/4=2.75 [(6-4)+(3-3)+(15-6)+(10-10)]/ 4=11/4=2.75 By completion time Round Robin scheduling Ø Each process gets a fixed time allocation (quantum) Ø If not complete à go back to the end of ready queue, and RR scheduler picks the next ready process in queue What s a good quantum? Ø Too short: many context switches, hurt efficiency Ø Too long: poor response to interactive requests Ø Typical length: 10 50 ms A" B" C" D" E" t wait = [T f (T s + t cpu + t io )] 57 58 E" D" C" B" A" Time" Models/Assumptions for CPU Scheduling CPU model Ø By default, assume only a single CPU core Ø Exclusive use of CPU: only one process can use CPU I/O model Ø Multiple I/O devices Ø Processes can access different I/O devices concurrently If no one is using CPU, the process putted to the ready queue can run immediately 59 SJF SJF, SRJF, RR Example 3: SJF Ø Process Gantt chart SRJF RR AWT = (6 + 5)/2 = 5.5 P1: r r r r r r RRR r r RRRRRwwwwwwwRRR P2: RRRRRRwwwRR AWT = (8 + 0)/2 = 5 P1: RRRRr r r r RRRRwwwwwwwRRR P2: r r r r RRRRr r r r RR wwwrr AWT = (4 + 8)/2 = 6 60 15

CPU Scheduling for Multiprocessors Symmetric: all CPUs run OS with self scheduling Asymmetric: one CPU run scheduler (master) Ø Slave CPUs: execute assigned processes Process affinity: migration or not Load balancing: push/pull process from other CPUs Global queue vs. separate queues New architectures Ø Multicore: multiple CPUs (cores) on a single machine Ø Simultaneously multithreading (SMT): multiple function units and running context (registers), more inst./cycle CPU Scheduling for Multiprocessors (cont) Objectives Ø Minimal schedule length (given CPU numbers) Ø Minimal number of needed processors (other constrains) Priority of tasks: optimal is NP-hard Ø FIFO, or SJF Ø Longest Job First (LJF): e.g.: 7, 6, 3, 2, 2 on 2 cpus Scheduling algorithms Ø Worst-Fit à balance workload Ø Best-Fit or First-Fit à use fewer processors (for time constrained systems) 61 62 Outline Inter-Process Communication (IPC) Basic concepts of process Ø Address space and Process control block (PCB) Basic operations for process management Ø Process creation/termination States of process: different queues Ø ready, running, or wait etc Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication (IPC) Ø shared memory vs. message passing (SGG 3.4, 3.5, 3.6) 63 Processes within a system may be independent or cooperating! Cooperating process can affect or be affected by other processes" Reasons for cooperating processes:" Ø Information sharing, e.g., sharing a file" Ø Computation speedup, e.g., subtasks for parallelism" Ø Modularity & Convenience ( e.g., editing, printing in the same time)" Cooperating processes need inter-process communication (IPC)" Ø Shared memory" Ø Pipe and Named Pipe" Ø Message passing" " 16

Inter-Process Communication (IPC) POSIX Shared-Memory APIs Message Passing! Shared Memory! 65 shmget: allocate a shared memory segment Ø shm_id = shmget(ipc_private, size, S_IRUSR S_IWUSR); shmat: attach the segment with one address Ø shm_addr = (char*) shmat(shm_id, NULL, 0); shmdt: detach the shared memory segment Ø shmdt(shm_addr); shmctl: alter the permission of the shared segment Ø shmctl(shm_id, cmd, *buf); Ø cmd: IPC_STAT, IPC_SET, and IPC_RMID https://users.cs.cf.ac.uk/dave.marshall/c/node27.html 66 IPC with Shared Memory (POSIX/C) Communicate via Shared Memory #include <sys/ipc.h> #include <sys/shm.h> int main(int argc, char *argv[]) { int shmid; char *data; /* create the shared segment: */ shmid=shmget(ipc_private, 1024, 0644 IPC_CREAT)) /* attach it to a pointer */ data=shmat(shmid, (void *)0, 0); /* write some data */ sprintf(data, Hi, I am writing share memory ); shmdt(data); /* detach from the segment: */ /*remove the shared segment*/ shmctl(shmid, IPC_RMID, NULL); return 0; int main(int argc, char *argv[]) { int shmid; char * data; /* setup the shared segment: */ if ( (cpid = fork())==0 ){//child process sprintf(data, Child: using SM! ); sleep(1); //give parent a chance printf( %s\n, data); exit(0); else if (cpid >0){ //parent process sleep(1); //let child first sprintf(data, Parent: changing SM ); wait(cpid); //wait child to finish shmdt(data); shmctl(shmid, IPC_RMID, NULL); return 0; ipcs a: checking ipc information 67 68 What is output? 17

Shared Memory, Pros and Cons Ordinary Pipes Pros Ø Fast bidirectional communication among any number of processes Ø Saves Resources Cons Ø Needs concurrency control (leads to data inconsistencies like Lost update ) Ø Lack of data protection from Operating System (OS) Ordinary Pipes Ordinary Pipes allow communication in standard producer-consumer style Producer writes to one end (the write-end of the pipe) Consumer reads from the other end (the read-end of the pipe) Ordinary pipes are therefore unidirectional (one fd for one purpose) Require parent-child/sibling relationship between communicating processes Example of using Pipe int pfd[2]; char line[100]; main() { int pid; pipe(pfd); pid = fork(); if (pid == 0) { ChildFunc(); else { ParentFunc(); waitpid(null); 72 Void ParentFunc() { close(pfd[1]); read (pfd[0], line, sizeof(line)); printf("date from child is: %s\n", line); close(pfd[0]); Void ChildFunc() { close(pfd[0]); dup2(pfd[1], 1); close(pfd[1]); execl("/bin/date", "date", 0); Demo: pipe.c 18

Problems of Pipe Pros Ø simple Ø flexible Ø efficient communication Cons: Ø no way to open an already existing pipe. This makes it impossible for two arbitrary processes to share the same pipe, unless the pipe was created by a common ancestor process. Named Pipes (FIFO) Named Pipes are more powerful than ordinary pipes " Communication is bidirection (same fd for read and write) No parent-child/sibling relationship is necessary between the communicating processes " Several processes can use the named pipe for communication " Provided on both UNIX and Windows systems" 73 Named Pipes Named pipes also allow two unrelated processes to communicate with each other. They are also known as FIFOs (first-in, first-out) Named Pipes Cont. How does it work? Ø Uses an access point (A file on the file system) Ø Two unrelated processes opens this file to communicate Ø One process writes and the other reads, thus it is a half-duplex communication Used to establish a one-way (half-duplex) flow of data. reads Process 2 File System Named Pipe (FIFO) writes Process 1 file 19

Named Pipes - Properties Code Example of Half-Duplex Communication Named pipes are system-persistent objects. Ø i.e. They exist beyond the life of the process Ø In contrary, anonymous pipes are process-persistent objects Named Pipes must be explicitly deleted by one of the process by calling unlink By default, it supports blocked read and writes. Ø i.e. if a process opens the file for reading, it is blocked until another process opens the file for writing, and vice versa. Ø This can be overridden by calling O_NONBLOCK flag when opening them. Client Pipe Server A named pipe must be opened either read-only or write-only because it is half-duplex, that is, a one-way channel. How to obtain Full-Duplex using Named Pipes? Code Example of Full-Duplex Communication A full-duplex communication can be established using different named pipes so each named pipe provides the flow of data in one direction. Care should be taken to avoid deadlock. Client Pipe NP1 Server Pipe NP2 20

FIFOs: Named Pipes Create a FIFO #include <sys/stat.h> int mkfifo(const char *path, mode_t mode); *path specific the name of the pipe, which appears in the same directory as ordinary file Can be accessed by everybody Should be opened before read/write operations Remove a FIFO: same as remove a file Ø rm or unlink A named pipe is a special instance of a file that has no contents on the filesystem. 81 Parent/Child Processes with a FIFO Program 6.4 on page 194 [USP] Only the name of the pipe is passed to the parent and child routines. 82 Parent/Child Processes with a FIFO (cont.) Program 6.5 on page 195 [USP] Parent/Child Processes with a FIFO (cont.) Program 6.6 on page 196 [USP] 83 84 21

Using FIFO in Client-Server Model Client-Server Communication Ø Client processes request service from a server. Ø A server process waits for requests from clients. A process can act as both a client and a server When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the filesystem. Thus, it is much faster than sockets, and not using network resources. Message Passing A message-passing facility provides at least: Ø send(message) Ø receive(message) Ø Communication: direct or indirect Ø Operations: blocking (synchronous) or non-blocking (asynchronous) General communication mechanism Ø Processes running on different computers When to use shared memory vs. message passing? 85 86 Message Passing in Client-Server Systems Low-level communication" Ø Sockets - end-point for comm. (ip address + port number)" Ø Server creates a socket and accepts requests from clients." Ø Client makes connection request to the server socket, and read/write messages to the socket." High-level communication" Ø RPC " ü Invoke a procedure on a remote host" ü Hide communication details" Ø RMI" ü Java feature similar to RPC" ü Invoke a method on a remote object" Summary Basic concepts of process Ø Representation: process control block (PCB) Ø Execution environment: address space Basic operations for process management Ø Process creation/termination States of process: different queues Ø ready, running, or wait etc Ø Context switch: multiple hardware running contexts Scheduling of process: CPU scheduling Ø Basic scheduling algorithms: FIFO, SJF etc Inter process communication 88 22