Operating Systems: CS5523 Spring 2009 Lecture Notes on Processes and Scheduling - Prof. Da, Study notes of Operating Systems

An outline and slides from the cs5523 operating systems course taught at carnegie mellon university during spring 2009. The content covers the basics of processes, process control blocks, execution environments, process management, process states, context switching, scheduling algorithms, and inter-process communication.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-cjm
koofers-user-cjm 🇺🇸

9 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Spring 2009 CS5523: Operating System: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 1
CS 5523 Operating Systems
Topics:
Process Management
CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 2
Spring 2009
Outline
aBasic concepts of process
¾Representation: process control block (PCB)
¾Execution environment: address space
aBasic operations for process management
¾Process creation/termination
aStates of process: different queues
¾ready, running, or wait etc
¾Context switch: multiple hardware running contexts
aScheduling of process: CPU scheduling
¾Basic scheduling algorithms: FIFO, SJF etc
aInter process communication
¾shared memory and message
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Operating Systems: CS5523 Spring 2009 Lecture Notes on Processes and Scheduling - Prof. Da and more Study notes Operating Systems in PDF only on Docsity!

Spring 2009 CS5523: Operating System: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 1

CS 5523 Operating Systems

Topics:

Process Management

Outline

a Basic concepts of process ¾ Representation: process control block (PCB) ¾ Execution environment: address space a Basic operations for process management ¾ Process creation/termination a States of process: different queues ¾ ready, running, or wait etc ¾ Context switch: multiple hardware running contexts a Scheduling of process: CPU scheduling ¾ Basic scheduling algorithms: FIFO, SJF etc a Inter process communication ¾ shared memory and message

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 3

Process vs. Program

a Program : one (or a set of) functions

¾ stored in files ¾ a passive entity

aProcess: a program in execution

¾ Running of a program ¾ A program counter: what the next instruction is ¾ A set of associated resources (memory, open file handles) ¾ Dynamic concept

How/when do we start a process?

Process Control Block (PCB)

aOS creates a PCB for each process

aOS manages the process table Æ a link of PCB

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 7

Address Space

aRange of memory locations

¾ code section (text segment), ¾ data section (global variables), ¾ stack (temporary data Æ local variables and return address etc) ¾ Auxiliary : environment variables and command line arguments ¾ Heap: memory for dynamically allocated data items

Stack

Text

Heap

Auxiliary regions

0

2 N

How big is the address space?

Base and Limit Registers

a Special CPU registers ¾ Base register: start of the process’s memory partition ¾ Limit register: length of the process’s memory partition ¾ Access limited to system mode a Address generation ¾ Logical address: location from the process’s point of view ¾ Physical address: location in actual memory ¾ Physical = base + logical address ¾ Logical address larger than limit Æ error

P

OS 0

0xFFFF

Limit

Base

0x

0x

Logical address: 0x Physical address: 0x1204+0x9000 = 0xa

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 9

Multiprogramming: More Processes

a Each process has its own base and limit registers

a Memory allocation: no overlap for protection

P

OS 0

0xFFFF

Limit

Base

0x

0x

P

Limit

Base

0x

0xC

How to keep track free memory?

How to allocate memory?

Virtual Æ physical address translation?

Process Creation and Problems

aPrincipal events that cause process creation

¾ System initialization ¾ User request to create a new process (run a program) ¾ A running process use system call for process creation

aParent process creates child processes

¾ Hierarchy of processes

aProblems and Issues

¾ Will the parent and child execute ”concurrently”? ¾ How their address spaces are related? ¾ Will/should the parent and child share some resources?

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 13

Unix fork( )

pid = 26

UNIX kernel

Text

Process Status

Stack

Data

FileFile

Stack

ResourcesResources Data

<…> int cpid = fork( ); if (cpid = = 0) { exit(0); } wait(cpid);

Text

<…> int cpid = fork( ); if (cpid = = 0) { exit(0); } wait(cpid);

Process Status

pid = 25

cpid = 26 (^) cpid = 0

Load A Different Program after fork

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); } }

a Child process uses execlp to load different program

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 15

Activities in Processes

a Bursts of CPU usage alternate with periods of I/O wait a Some processes are CPU-bound : they don’t make many I/O requests a Other processes are I/O-bound and make many kernel requests

CPU bound

I/O bound

CPU bursts I/O waits

Total I/O usage

Total CPU usage

Time

Process States and Transition

Ready (^) Running

Waiting

New Terminated

Event wait

Event occurs

Scheduler Exit Dispatch

Timeout

Admit

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 19

Context Switch (cont.)

CPU Scheduling

aOne CPU Æ execute only one process at a time

aMore processes Æ wait for CPU accordingly

aOS is responsible for the scheduling activities

aScheduling 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

aProcesses move between the various queues

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 21

Scheduling Queues

When are processes scheduled?

aAt the time they enter the system

¾ Common in batch systems ¾ Two types of batch scheduling 9 Submission of a new job causes the scheduler to run 9 Scheduling only done when a job voluntarily gives up the CPU ( i.e. , while waiting for an I/O request)

aAt relatively fixed intervals (clock interrupts)

¾ Necessary for interactive systems ¾ May also be used for batch systems ¾ Scheduling algorithms at each interrupt, and picks the next process from the pool of “ready” processes

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 25

Scheduling Algorithms

aFIFO: non-preemptive, based on arrival time

¾ Long computation process proceeds SSH console?

aSJF(shortest job first): preemptive & non-preemptive

¾ Optimal in term of waiting time

aRR (Round-robin): preemptive

¾ Processes take turns, e.g., 10ms

aPriority-based scheduling

¾ Real-time systems: earliest deadline first (EDF)

aMulti-level queue (priority classes)

¾ System processes >faculty processes >student processes

aMulti-level feedback queues: short Æ long quantum

First Come, First Served (FCFS)

a Goal: do jobs in the order they arrive ¾ Fair in the same way a bank teller line is fair a Simple algorithm! a Problem: long jobs delay every job after them ¾ Many processes may wait for a single long job

A B C D

4 3 6 3

Current job queue

Execution order

FCFS scheduler

A B C D

4 3 6 3

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 27

Shortest Job First (SJF)

a Goal: do the shortest job first ¾ Short jobs complete first ¾ Long jobs delay every job after them a Jobs sorted in increasing order of execution time ¾ Ordering of ties doesn’t matter a Shortest Remaining Time First (SRTF): preemptive form of SJF a Problem: how does the scheduler know how long a job will take?

A B C D

4 3 6 3

B D A C

3 3 4 6

Current job queue

Execution order

SJF scheduler

Round Robin (RR) scheduling

a Round Robin scheduling ¾ Give each process a fixed time slot ( quantum ) ¾ Rotate through “ready” processes ¾ Each process makes some progress a What’s a good quantum? ¾ Too short: many process switches hurt efficiency ¾ Too long: poor response to interactive requests ¾ Typical length: 10–50 ms

A B C D E

Time

A

B

C

D

E

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 31

Three-level scheduling

CPU

Main memory

CPU scheduler

Memory scheduler

Job scheduler Input queue

Arriving jobs

a Jobs held in input queue until moved into memory ¾ Pick “complementary jobs”: small & large, CPU- & I/O-intensive ¾ Jobs move into memory when admitted a CPU scheduler picks next job to run a Memory scheduler picks some jobs from main memory and moves them to disk if insufficient memory space

Scheduling Policy Vs. Mechanism

aSeparate what may be done from how it is done

¾ Mechanism allows 9 Priorities to be assigned to processes 9 CPU to select processes with high priorities ¾ Policy set by what priorities are assigned to processes

aScheduling algorithm parameterized

¾ Mechanism in the kernel ¾ Priorities assigned in the kernel or by users

aParameters may be set by user processes

¾ Don’t allow a user process to take over the system! ¾ Allow a user process to voluntarily lower its own priority ¾ Allow a user process to assign priority to its threads

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 33

CPU Scheduling for Multiprocessors

aSymmetric: all CPUs run OS with self scheduling

aAsymmetric: one CPU run scheduler (master)

¾ Slave CPUs: execute assigned processes

aProcess affinity: migration or not

aLoad balancing: push/pull process from other CPUs

aGlobal queue vs. separate queues

aNew architectures

¾ Simultaneously multithreading (SMT): multiple function units and running context (registers), more inst./cycle ¾ Chip-Multiprocessors (CMP): multiple simple CPUs (cores) on single chip

Inter-Process Communication (IPC)

Message Passing (^) Shared Memory

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 37

Access Shared Memory in two Processes

*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: chaning SM”); wait(cpid); //wait child to finish } shmdt(data); shmctl(shmid, IPC_RMID, NULL); return 0; }

Message Passing (a general mechanism)

a Message system – processes communicate with each other without resorting to shared variables.

a A message-passing facility provides at least two operations: ¾ send ( message ) ¾ receive ( message ) ¾ These operations can be either blocking (synchronous) or non- blocking (asynchronous).

a What should we do for processes running on different computers (e.g., in distributed system)?

Shared memory vs. message passing?

Spring 2009 CS5523: Operating Systems: slides incorporate materials kindly provided by Prof. Kay A. Robbins. 39

IPC with Message Passing (socket)

a C/C++ (sys/socket.h, netinet/in.h)

¾ Server 9 Create a socket with the socket() 9 Bind the socket to an address using the bind() 9 Listen for connections with the listen() 9 Accept a connection with the accept() system call. ¾ Client 9 Create a socket with the socket() system call 9 Connect to server using the connect() system call 9 read() and write()

a Java

¾ Server: ServerSocket ¾ Client: Socket

Summary

aBasic concepts of process

¾ Representation: process control block (PCB) ¾ Execution environment: address space

aBasic operations for process management

¾ Process creation/termination

aStates of process: different queues

¾ ready, running, or wait etc ¾ Context switch: multiple hardware running contexts

aScheduling of process: CPU scheduling

¾ Basic scheduling algorithms: FIFO, SJF etc

aInter process communication