Tags:

Transcription

Operating System:Chap3 Processes ConceptNational Tsing-Hua University2016, Fall Semester

OutlineProcess Concept Process Scheduling Operations on Processes Interprocess Communication Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab2

Process ConceptChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab3

Process Concept An operating system concurrently executes a varietyof programs (e.g Web browser, text editor, etc) entity: binary stored in diskProgram — passiveactive entity: a program in execution in memoryProcess —A process includes: Code segment (text section)Data section— global variablesStack —temporary local variables and functionsHeap —dynamic allocated variables or classesCurrent activity (program counter, register contents)A set of associated resources (e.g. open file handlers)Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab4

Process in Memorytemporary data (e.g.function parameters,return addresses,local variables)dynamic allocation(e.g. class object,pointer object)global variablescodeChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab5

Threads A.k.a lightweight process:basic unit of CPU utilization All threads belonging to thesame process share code section, data section,and OS resources (e.g. openfiles and signals) But each thread has its own thread ID, program counter,register set, and a stackChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab6

Process State States New: the process is being created Ready: the process is in the memory waiting to beassigned to a processor Running: instructions are being executed by CPU Waiting: the process is waiting for events to occur Terminated: the process has finished executionChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab7

Diagram of Process StateNewexitadmittedTerminatedscheduler dispatchReadyI/O or event completionRunninginterruptI/O or event waitWaiting Only one process is running on any processor at any instantHowever, many processes may be ready or waitingChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab8

Process Control Block (PCB)To next PCBInfo. associated with each process Process state Program counter CPU registers CPU scheduling information(e.g. priority) Memory-management information(e.g. base/limit register) I/O status information Accounting informationChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab9

Context Switchprocess P0executingoperating systeminterrupt or system callprocess P1save state into PCB0.idleidlereload state from PCB1interrupt or system callexecutingsave state into PCB1.executingChapter3 Processes Conceptreload state from PCB0Operating System Concepts – NTHU LSA Labidle10

Context Switch Context Switch: Kernel saves the state of the oldprocess and loads the saved state for the new processContext-switch time is purely overheadSwitch time (about 1 1000 ms) depends on memory speednumber of registersexistence of special instructionsa single instruction to save/load all registershardware supportmultiple sets of registers (Sun UltraSPARC – a contextswitch means changing register file pointer)Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab11

Review Slides (1)What’s the definition of a process? What’s the difference between process andthread? What’s PCB? its contents? Process state Program counter CPU registers The kinds of process state? New, Ready, Running, Waiting, Terminated What’s context switch?Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab12

Process SchedulingChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab13

Process Scheduling Multiprogramming: CPU runs process at alltimes to maximize CPU utilization Time sharing: switch CPU frequently such thatusers can interact with each program while itis running Processes will have to wait until the CPU isfree and can be re-scheduledChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab14

Process Scheduling Queues Processes migrate between the variousqueues (i.e. switch among states) Job queue (New State) – set of all processesin the system Ready queue (Ready State) – set of allprocesses residing in main memory, readyand waiting to execute Device queue (Wait State)– set of processeswaiting for an I/O deviceChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab15

Process Scheduling QueuesI/O queueChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab16

Process Scheduling Diagramready queueI/OCPUI/O queueI/O requesttime sliceexpiredchildterminatesChapter3 Processes Conceptchildexecutesfork a childINToccurswait for aninterruptOperating System Concepts – NTHU LSA Lab17

Schedulers Short-term scheduler (CPU scheduler)– selects which processshould be executed and allocated CPU (Ready state Run state)Long-term scheduler (job scheduler) – selects which processesshould be loaded into memory and brought into the ready queue(New state Ready state)Medium-term scheduler – selects which processes should beswapped in/out memory (Ready state Wait state)CPU SchedulingCPUChapter3 Processes ConceptOperating SystemJob1Job2Job3Job4MemoryJob SchedulingJob swappingOperating System Concepts – NTHU LSA LabDiskJob pool18

Long-Term Scheduler Control degree of multiprogramming Execute less frequently (e.g. invoked only when aprocess leaves the system or once several minutes) Select a good mix of CPU-bound & I/O-boundprocesses to increase system overall performance UNIX/NT: no long-term scheduler Created process placed in memory for short-term schedulerMultiprogramming degree is bounded by hardwarelimitation (e.g., # of terminals) or on the self-adjustingnature of usersChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab19

Short-Term SchedulerExecute quite frequently (e.g. once per 100ms) Must be efficient: if 10 ms for picking a job, 100 ms for such a pick, overhead 10 / 110 9%long-termready queueI/OChapter3 Processes Conceptshort-termI/O queueCPUI/O requestOperating System Concepts – NTHU LSA Lab20

Medium-Term Scheduler swap out: removing processes from memory to reducethe degree of multiprogrammingswap in: reintroducing swap-out processes into memoryPurpose: improve,process mix free up memoryMost modern OS doesn’t have medium-term schedulerbecause having sufficient physical memory or usingvirtual memoryChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab21

Operations on ProcessesChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab22

Tree of Processes Each process is identified by a uniqueprocessor identifier (pid)schedparent of p3pid 0child of p0pageoutpid 2UNIX: “ps -ael” will list completeinfo of all active processesChapter3 Processes Conceptinitpid 1fsflushpid 3intdpid 140dtloginpid 251Cshpid 7778Cshpid 1400Netscapepid 7785Operating System Concepts – NTHU LSA Lablspid 2123catpid 253623

Process Creation Resource sharing Two possibilities of execution Parent and child processes share all resourcesChild process shares subset of parent’s resourcesParent and child share no resourcesParent and children execute concurrentlyParent waits until children terminateTwo possibilities of address space Child duplicate of parent, communication via sharing variablesChild has a program loaded into it, communication viamessage passingChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab24

UNIX/Linux Process Creation fork system call execlp system call Create a new (child) processThe new process duplicates the address space of its parentChild & Parent execute concurrently after forkChild: return value of fork is 0Parent: return value of fork is PID of the child processLoad a new binary file into memory – destroying the old codewait system call The parent waits for one of its child processes to completeChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab25

UNIX/Linux Process Creation Memory space of fork(): Old implementation: A’s child is an exact copy of parentCurrent implementation: use copy-on-write technique tostore differences in A’s child address spacefree memoryfree memoryfree memoryCA’s childBBBfree memoryfree memoryfree memoryAkernelOriginallyAkernelAfter A doesan forkAkernelAfter the childdoes an execlpChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab26

UNIX/Linux Example#include stdio.h void main( ){int A;/* fork another process */A fork( );if (A 0) { /* child process */printf(“this is from child process\n”);execlp(“/bin/ls”, “ls”, NULL);Output:this is from child processthis is from parent processa.out hello.c readme.txtChild 32185 completesprocess ends 32185} else { /* parent process */printf(“this is from parent process\n”);int pid wait(&status);printf(“Child %d completes”, pid);}printf(“process ends %d\n”, A);}Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab27

Example Quiz: How many processes are created?#include stdio.h #include unistd.h int main() {for (int i 0; i 3; i ){fork();}P0return 0;P0}Chapter3 Processes ConceptP0P1P0P1P2P4P2P5Operating System Concepts – NTHU LSA LabP1P3P6P3P728

Process Termination Terminate when the last statement is executed orexit() is called Parent may terminate execution of childrenprocesses by specifying its PID (abort) All resources of the process, including physical & virtualmemory, open files, I/O buffers, are deallocated by the OSChild has exceeded allocated resourcesTask assigned to child is no longer requiredCascading termination: killing (exiting) parent killing (exiting) all its childrenChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab29

Review Slides (2)What’s long-term scheduler? features? What’s short-term scheduler? features? What’s medium-term scheduler? features? What’s the different between duplicateaddress space and load program? Theircommands? Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab30

InterprocessCommunication (IPC)Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab31

Interprocess Communication IPC: a set of methods for the exchange of dataamong multiple threads in one or more processesIndependent process: cannot affect or be affected byother processesCooperating process: otherwisePurposes information sharingcomputation speedup (not always true )convenience (performs several tasks at one time)modularityChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab32

Communication Methods Shared memory: Require more careful usersynchronizationImplemented by memoryaccess: faster speedUse memory address toaccess dataMsg PassingShared MemoryMessage passing: No conflict: more efficientfor small dataUse send/recv messageImplemented by system call:slower speedChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab33

Communication Methods Sockets: Client(146.86.5.20)A network connectionidentified by IP & portExchange unstructuredstream of )Remote Procedure Calls: Cause a procedure toexecute in anotheraddress spaceParameters and returnvalues are passed bymessageServer(161.25.19.8)Clientval server.method(A,B)Serverbool method(A,B){ .}A, B, methodBoolean return valueChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab34

Interprocess Communication Shared Memory Message SocketPassing Remote Procedure CallsChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab35

Shared Memory Processes are responsible for Establishing a region of shared memoryTypically, a shared-memory region resides in the addressspace of the process creating the shared-memory segmentParticipating processes must agree to remove memoryaccess constraint from OS Determining the form of the data and the location Ensuring data are not written simultaneously byprocessesChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab36

Consumer & Producer Problem Producer process produces information that isconsumed by a Consumer process Buffer as a circular array with size B next free: infirst available: outempty: in outfull: (in 1) % B outoutinThe solution allows at most (B-1) item in the buffer Otherwise, cannot tell the buffer is fall or emptyChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab37

Shared-Memory Solution/*producer*/while (1) {while (((in 1) % BUFFER SIZE) out)in; //wait if buffer is fulloutbuffer[in] nextProduced;/* global data structure */in (in 1) % BUFFER SIZE;#define BUFSIZE 10“in” only modified by producer}item buffer[BUFSIZE];/*consumer*/int in out 0;while (1) {while (in out); //wait if buffer is emptynextConsumed buffer[out];out (out 1) % BUFFER SIZE;outin“out”onlymodifiedbyconsumer}Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab38

Interprocess Communication Shared Memory Message SocketPassing Remote Procedure CallsChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab39

Message-Passing System Mechanism for processes to communicate andsynchronize their actionsIPC facility provides two operations: Send(message) – message size fixed or variableReceive(message)Message system – processes communicate withoutresorting to shared variablesTo communicate, processes need to Establish a communication link Exchange a message via send/receiveChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab40

Message-Passing System Implementation of communication link physical (e.g., shared memory, HW bus, or network) logical (e.g., logical properties)Direct or indirect communicationSymmetric or asymmetric communicationBlocking or non-blockingAutomatic or explicit bufferingSend by copy or send by referenceFixed-sized or variable-sized messagesChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab41

Direct communication Processes must name each other explicitly: Send (P, message) – send a message to proc P Receive (Q, message) – receive a message fromprocess Q Properties of communication link Links are established automatically One-to-One relationship between links and processes The link may be unidirectional, but is usually bi-directionalChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab42

Direct communication Solution for producer-consumer problem:/*producer*/while (1) {send (consumer, nextProduced);}/*consumer*/while (1) {receive (producer, nextConsumed);}limited modularity: if the name of a process ischanged, all old names should be foundChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab43

Indirect communication Messages are directed and received from mailboxes(also referred to as ports) Each mailbox has a unique IDProcesses can communicate if they share a mailboxSend (A, message) – send a message to mailbox AReceive (A, message) – receive a message from mailbox AProperties of communication link Link established only if processes share a common mailboxMany-to-Many relationship between links and processesLink may be unidirectional or bi-directionalMailbox can be owned either by OS or processesChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab44

Indirect Communication Mailbox sharingP1send Solutions P2recv?P3recv?MailboxAllow a link to be associated with at most two processesAllow only one process at a time to execute a receiveoperationAllow the system to select arbitrarily a single receiver.Sender is notified who the receiver wasChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab45

Synchronization Message passing may be either blocking (synchronous)or non-blocking (asynchronous) Blocking send: sender is blocked until the message is receivedby receiver or by the mailboxNonblocking send: sender sends the message and resumesoperationBlocking receive: receiver is blocked until the message isavailableNonblocking receive: receiver receives a valid message or anullsender Buffer implementation receiverbufferZero capacity: blocking send/receiveBounded capacity: if full, sender will be blockedUnbounded capacity: sender never blocksChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab46

Interprocess Communication Shared Memory Message SocketPassing Remote Procedure CallsChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab47

Sockets ServerA socket is identified by a(161.25.19.8)concatenation of IP addresssocket()and port numberWell-known portbind()Communication consists161.25.19.8:80between a pair of sockets Clientlisten()Use 127.0.0.1 to refer itself sign port146.86.5.20:1625 r3 Processes ConceptOperating System Concepts – NTHU LSA LabBlock until client requestsData req.Data replyread()write()close()48

Sockets Considered as a low-level form of communicationunstructured stream of bytes to be exchangedData parsing responsibility falls upon the server andthe client applicationsHTTP example:ClientSocket(146.86.5.20:1625)GET /index.html HTTP/1.1Host: www.example.comChapter3 Processes ConceptHTTP/1.1 200 OKDate: Mon, 23 May 2005 22:38:34 GMTServer: Apache/1.3.3.7Last-Modified: Wed, 08 Jan 2003 23:11:55 GMTServerSocket(161.25.19.8:80)Operating System Concepts – NTHU LSA Lab49

Remote Procedure Calls: RPC Remote procedure call (RPC) abstracts procedurecalls between processes on networked systems allows programs to call procedures located on othermachines (and other processes)Stubs – client-side proxy for the actual procedure onthe serverChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab50

Client and Server StubsClient stub: Packs parameters into a message (i.e. parameter marshaling) Calls OS to send directly to the server Waits for result-return from the serverServer stub: Receives a call from a client Calls the corresponding procedure Returns results to the caller Unpacks the parametersChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab51

Review Slides (3)Shared memory vs. Message-passing system? Direct vs. Indirect message-passing system? Blocking vs. Non-Blocking? Socket vs. RPC? Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab52

Reading Material & HWChap 3 HW (Problem set) 3.1 3.2 3.5 3.7 3.10Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab53

BackupChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab54

{}Example: POSIX Shared Memory/* allocate a R/W shared memory segment */ size R/W modechar* segment id shmget(IPC PRIVATE, 4096, S IRUSR S IWUSR);/* attach the shared memory segment */ mem. location R/W modechar* shared memory (char*) shmat(segment id, NULL, 0);/* write a message to the shared memory segment */sprintf(shared memory, “Write to shared memory”);/* print out the string from the shared memory segment */printf(“%s\n”, shared memory);/* detach the shared memory segment */shmdt(shared memory);/* remove the shared memory segment */shmctl(shared memory, IPC RMID, NULL);Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab55

Example: Mach Message Passing Mach operating system developed at CMU microkernel design most communications are carried out bymessages and mailboxes (aka ports) Problem: performance (data coping) When each task (process) is created kernel & notify mailboxes also created kernel mailbox: channel between OS & task notify mailbox: OS sends event notification toChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab56

Mach Mailbox port-allocate: system call to create a mailbox default buffer size: 8 messagesFIFO queueinga message: one fixed-size header variable-length dataportionimplementing both blocking- & non-blocking send/receiveMsgtofromtype size value type sizelen mailbox mailboxheaderChapter3 Processes Conceptvalue.dataOperating System Concepts – NTHU LSA Lab57

RPC Problems Data representations integer, floating?Different address spaces pointer?Communication error duplicate or missing callsChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab58

RPC Problems: Data Representation Issue Problem IBM mainframes use EBCDIC char code and IBM PC usesASCII codeInteger: one’s complement and 2’s complementFloating-point numbersLittle endian and big endianSolution External data representation (XDR)Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab59

RPC Problems: Address Space IssueA pointer is only meaningful in its addressspace Solutions No pointer usage in RPC calls Copy the entire pointed area (such as arrays orstrings)Only suitable for bounded and known areasChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab60

RPC Problems: Communication Issue RPCs may fail, or be duplicated and execute more thanonce, as a result of common network errorsat most once: prevent duplicate calls Implemented by attaching a timestamp to each messageThe server must keep a history large enough to ensure thatrepeated messages are detectedexact once: prevent missing calls The server must acknowledge to the client that the RPC callwas received and executedThe client must resend each RPC call periodically until theserver receives the ACKChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab61

Pipes One of the 1st IPC mechanism in early UNIX systemsPipe is a special type of fileIssues in implementing uni- or bi-directional?half or full duplex? (travel in both directionssimultaneously)Must a relationship (parent – child) exist?Over a network, or reside on the same machine?write-endRead-endChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab62

Ordinary Pipes Also called anonymous pipes in WindowsRequires a parent-child relationship between thecommunicating processes Implemented as a special file on Unix (via fork(), a childprocess inherits open files from its parent)Can only be used between processes on the same machineUnidirectional: two pipes must be used for two–waycommunicationUNIX:int fd[2];pipe(fd);Chapter3 Processes ConceptWindows:CreatePipe(&ReadHandle, &WriteHandle, &sa, 0)Operating System Concepts – NTHU LSA Lab63

Named Pipes No parent-child relationship is requiredSeveral processes can use it for communications Continue to exist after communicating processes exitIn Unix: It may have several writersAlso called FIFOCommunicating processes have to be on the same machineIn Windows: bi-directionalCommunicating processes can be on different machineChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab64

UNIX/Linux: Fork Inherited from the parent:process credentialsenvironmentstackmemoryopen file descriptorssignal handling settingsscheduler classprocess group IDsession IDcurrent working directoryroot directoryfile mode creation mask(umask) resource limits controlling terminal Chapter3 Processes Concept Unique to the child: process IDdifferent parent process IDOwn copy of file descriptors anddirectory streams.process, text, data and othermemory locks are NOT inherited.process times, in the tms structresource utilizations are set to 0pending signals initialized to theempty settimers created by timer createnot inheritedasynchronous input or outputoperations not inheritedOperating System Concepts – NTHU LSA Lab65

Remote Method Invocation RMI is a Java mechanism similar to RPCRMI allows a Java program on one machine to invokea method on a remote object instead of a functionChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab66

Distributed Objects2-16A remote object with client-side proxyChapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab67

Static & Dynamic RMI RMI Remote Method Invocation Invoke an object’s method through proxy Static invocation objectname.methodname(para) If interfaces change, apps must be recompiled Dynamic invocation invoke(object, method, inpars, outpars)Chapter3 Processes ConceptOperating System Concepts – NTHU LSA Lab68

Chapter3 Processes Concept Operating System Concepts - NTHU LSA Lab . 15 . Process Scheduling Queues Processes migrate between the various queues (i.e. switch among states) Job queue (New State) - set of all processes in the system Ready queue (Ready State) - set of all processes residing in main memory, ready and waiting to execute