97
1 CHAPTER 3 - PROCESS CONCEPT

CHAPTER 3 - PROCESS CONCEPT - SDUimada.sdu.dk/~jamik/dm510-17/material/Chapter3.pdfNULL, /* use parent’s environment block */ NULL, /* use parent’s existing directory */ 5.10 PROCESS

  • Upload
    vutram

  • View
    221

  • Download
    2

Embed Size (px)

Citation preview

1

CHAPTER 3 - PROCESSCONCEPT

OBJECTIVESIntroduce a process → a program in execution → basis ofall computation

Describe features of processes: scheduling, creation,termination, communication

Explore interprocess communication using sharedmemory and message passing

Describe communication in client-server systems

23 . 1

PROCESS CONCEPT

THE PROCESSBatch system –> jobs

Time-shared systems –> user programs or tasks

Textbook: uses job and process interchangeably

Process –> a program in execution

process execution must progress in sequential fashion

3 . 2

THE PROCESS - PARTSThe program code, also called text section

Current activity including program counter, processorregisters

Stack containing temporary data

Function parameters, return addresses, local variables

Data section containing global variables

Heap containing memory dynamically allocated duringrun time

3 . 3

PROGRAM VS PROCESSProgram is passive entity stored on disk (executable file),

process is active

Program becomes process when executable file loaded intomemory

Execution starts by GUI mouse clicks, command line entry ofits name, etc

One program can be several processes → Consider multipleusers executing the same program

3 . 4

PROCESS IN MEMORY

3 . 5

PROCESS STATEAs 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 aprocessor

terminated: The process has finished execution

3 . 6

DIAGRAM OF PROCESS STATE

3 . 7

PROCESS CONTROL BLOCKInformation associated with each process

PROCESS STATEProcess state: running, waiting, etc

Program counter: location of instruction to next execute

CPU registers: contents of all process-centric registers

CPU scheduling info: priorities, scheduling queuepointers

Memory-management info: memory allocated to process

Accounting info: CPU used, clock time elapsed since start,time limits

I/O status info: I/O devices allocated to process

3 . 83 . 9

CPU SWITCH FROM PROCESS TOPROCESS

3 . 10

THREADSSo far, process has a single thread of execution

Consider having multiple program counters per process

Multiple locations can execute at once

Multiple threads of control → threads

Must then have storage for thread details, multipleprogram counters in PCB

3 . 11

PROCESS REPRESENTATIONProcess Representation in Linux

Represented by the C structure task_struct

3 . 12

PROCESS REPRESENTATION INLINUX

task_structpid_t pid; /* process identifier */long state; /* state of the process */ unsigned int time slice /* scheduling information */ struct task_struct *parent; /* this process’s parent */ struct list_head children; /* this process’s children */ struct files_struct *files; /* list of open files */struct mm_struct *mm; /* address space of this process */

3 . 134 . 1

PROCESS SCHEDULING

PROCESS SCHEDULINGMaximize CPU use, quickly switch processes onto CPU for

time sharing

Process scheduler selects among available processes fornext execution on CPU

Maintains scheduling queues of processes

4 . 2

SCHEDULING QUEUSJob queue – set of all processes in the system

Ready queue – set of all processes residing in mainmemory, ready and waiting to execute

Device queues – set of processes waiting for an I/O device

Processes migrate among the various queues

4 . 3

SCHEDULING QUEUS

SCHEDULING QUEUESQueuing diagram represents queues, resources, flows

4 . 44 . 5

SCHEDULERSLong-term scheduler (or job scheduler) –> selects whichprocesses should be brought into the ready queue

Short-term scheduler (or CPU scheduler) –> selectswhich process should be executed next and allocates CPU

Sometimes the only scheduler in a system

4 . 6

SCHEDULERSShort-term scheduler is invoked very frequently(milliseconds) → must be fast

Long-term scheduler is invoked very infrequently(seconds, minutes) → may be slow

The long-term scheduler controls the degree ofmultiprogramming

4 . 7

SCHEDULERSProcesses can be described as either:

I/O-bound process – spends more time doing I/O thancomputations, many short CPU bursts

CPU-bound process – spends more time doingcomputations; few very long CPU bursts

Long-term scheduler strives for good process mix

4 . 84 . 9

MEDIUM TERM SCHEDULINGMedium-term scheduler can be added if degree of multiple

programming needs to decrease

Remove process from memory, store on disk, bring back infrom disk to continue execution: swapping

MEDIUM TERM SCHEDULING

4 . 104 . 11

MULTITASKING IN MOBILE SYSTEMSSome systems / early systems allow only one process to run,

others suspended

IOSDue to screen real estate, user interface limits iOS provides

for a

Single foreground process- controlled via user interface

Multiple background processes– in memory, running, butnot on the display, and with limits

Limits include single, short task, receiving notification ofevents, specific long-running tasks like audio playback

4 . 12

ANDROIDAndroid runs foreground and background, with fewer limits

Background process uses a service to perform tasks

Service can keep running even if background process issuspended

Service has no user interface, small memory use

4 . 134 . 14

CONTEXT SWITCHWhen CPU switches to another process, the system must

save the state of the old process and load the saved statefor the new process via a context switch

Context of a process represented in the PCB

CONTEXT SWITCHContext-switch time is overhead; the system does no useful

work while switching

The more complex the OS and the PCB → longer thecontext switch

Time dependent on hardware support

Some hardware provides multiple sets of registers perCPU → multiple contexts loaded at once

4 . 155 . 1

OPERATIONS ON PROCESSES

5 . 2

OPERATIONS ON PROCESSESSystem must provide mechanisms for process creation,

termination, and so on

5 . 3

PROCESS CREATIONParent process create children processes, which, in turn

create other processes, forming a tree of processes

Generally, process identified and managed via a processidentifier (pid)

5 . 4

RESOURCE SHARING OPTIONSParent and children share all resources

Children share subset of parent’s resources

Parent and child share no resources

5 . 5

EXECUTION OPTIONSParent and children execute concurrently

Parent waits until children terminate

5 . 6

ADDRESS SPACEChild duplicate of parent

Child has a program loaded into it

UNIX EXAMPLESfork() system call creates new process

exec() system call used after a fork() to replace theprocess’ memory space with a new program

5 . 7

A TREE OF PROCESSES IN LINUX

5 . 8

C PROGRAM FORKING SEPARATEPROCESS

# i n c l u d e < s y s / t y p e s . h > # i n c l u d e < s t d i o . h > # i n c l u d e < u n i s t d . h > i n t m a i n ( ) { p i d t p i d ; p i d = f o r k ( ) ; / * f o r k a c h i l d p r o c e s s * / i f ( p i d < 0 ) { / * e r r o r o c c u r r e d * / f p r i n t f ( s t d e r r , " F o r k F a i l e d " ) ; r e t u r n 1 ; } e l s e i f ( p i d = = 0 ) { / * c h i l d p r o c e s s * / e x e c l p ( " / b i n / l s " , " l s " , N U L L ) ; } e l s e { / * p a r e n t p r o c e s s * / w a i t ( N U L L ) ; / * p a r e n t w i l l w a i t f o r c h i l d t o c o m p l e t e * / p r i n t f ( " C h i l d C o m p l e t e " ) ; } }

5 . 9

CREATING - WINDOWS API# i n c l u d e < s t d i o . h > # i n c l u d e < w i n d o w s . h > i n t m a i n ( V O I D ) { S T A R T U P I N F O s i ; P R O C E S S _ I N F O R M A T I O N p i ; Z e r o M e m o r y ( & s i , s i z e o f ( s i ) ) ; / * a l l o c a t e m e m o r y * / s i . c b = s i z e o f ( s i ) ; Z e r o M e m o r y ( & p i , s i z e o f ( p i ) ) ; / * c r e a t e c h i l d p r o c e s s * / i f ( ! C r e a t e P r o c e s s ( N U L L , / * u s e c o m m a n d l i n e * / " C : \ \ W I N D O W S \ \ s y s t e m 3 2 \ \ m s p a i n t . e x e " , / * c o m m a n d * / N U L L , / * d o n ’ t i n h e r i t p r o c e s s h a n d l e * / N U L L , / * d o n ’ t i n h e r i t t h r e a d h a n d l e * / F A L S E , / * d i s a b l e h a n d l e i n h e r i t a n c e * / 0 , / * n o c r e a t i o n f l a g s * / N U L L , / * u s e p a r e n t ’ s e n v i r o n m e n t b l o c k * / N U L L , / * u s e p a r e n t ’ s e x i s t i n g d i r e c t o r y * /

5 . 10

PROCESS TERMINATIONProcess executes last statement and asks the operating

system to delete it (exit())

Output data from child to parent (via wait())

Process’ resources are deallocated by operating system

5 . 11

PROCESS TERMINATIONParent may terminate execution of children processes

(abort())

Child has exceeded allocated resources

Task assigned to child is no longer required

If parent is exiting

Some operating systems do not allow child to continueif its parent terminates

All children terminated → cascading termination

5 . 12

PROCESS TERMINATIONWait for termination, returning the pid:

pid_t pid; int status; pid = wait(&status);

If no parent waiting, then terminated process is a zombie

If parent terminated, processes are orphans

5 . 136 . 1

INTERPROCESS COMMUNICATION

INTERPROCESS COMMUNICATIONProcesses within a system may be independent or

cooperating

Independent process cannot affect or be affected by theexecution of another process

Cooperating process can affect or be affected by otherprocesses, including sharing data

6 . 2

INTERPROCESS COMMUNICATIONReasons for cooperating processes:

Information sharing

Computation speedup

Modularity

Convenience

6 . 3

INTERPROCESS COMMUNICATIONCooperating processes need interprocess communication(IPC)

Two models of IPC

Shared memory

Message passing

6 . 4

MULTIPROCESS ARCHITECTURE –CHROME BROWSER

Many web browsers ran as single process (some still do)

If one web site causes trouble, entire browser can hangor crash

6 . 5

MULTIPROCESS ARCHITECTURE –CHROME BROWSER

Google Chrome Browser is multiprocess with 3 categories

1. Browser process manages user interface, disk andnetwork I/O

2. Renderer process renders web pages, deals with HTML,Javascript, new one for each website opened

Runs in sandbox restricting disk and network I/O,minimizing effect of security exploits

3. Plug-in process for each type of plug-in

6 . 6

MULTIPROCESS ARCHITECTURE –CHROME BROWSER

6 . 7

COMMUNICATIONS MODELS

6 . 8

PRODUCER-CONSUMER PROBLEMParadigm for cooperating processes, producer processproduces information that is consumed by a consumer

process

unbounded-buffer places no practical limit on the size ofthe buffer

bounded-buffer assumes that there is a fixed buffer size

6 . 9

SHARED-MEMORY SYSTEMSShared data

# d e f i n e B U F F E R _ S I Z E 1 0 t y p e d e f s t r u c t { . . . } i t e m ;

i t e m b u f f e r [ B U F F E R _ S I Z E ] ; i n t i n = 0 ; i n t o u t = 0 ;

Solution is correct, but can only use BUFFER_SIZE-1elements

6 . 106 . 11

BOUNDED-BUFFER – PRODUCERw h i l e ( t r u e ) { / * p r o d u c e a n i t e m i n n e x t p r o d u c e d * / w h i l e ( ( ( i n + 1 ) % B U F F E R S I Z E ) = = o u t ) ; / * d o n o t h i n g * / b u f f e r [ i n ] = n e x t p r o d u c e d ; i n = ( i n + 1 ) % B U F F E R S I Z E ; }

6 . 12

BOUNDED-BUFFER – CONSUMERi t e m n e x t _ c o n s u m e d ; w h i l e ( t r u e ) { w h i l e ( i n = = o u t ) ; / * d o n o t h i n g * / n e x t c o n s u m e d = b u f f e r [ o u t ] ; o u t = ( o u t + 1 ) % B U F F E R S I Z E ; / * c o n s u m e t h e i t e m i n n e x t c o n s u m e d * / }

6 . 13

MESSAGE-PASSING SYSTEMSMechanism for processes to communicate and to

synchronize their actions

Message system – processes communicate with each otherwithout resorting to shared variable

INTERPROCESS COMMUNICATIONIPC facility provides two operations:

send(message) – message size fixed or variable

receive(message)

If P and Q wish to communicate, they need to:

establish a communication link between them

exchange messages via send/receive

6 . 14

INTERPROCESS COMMUNICATIONImplementation of communication link

physical (e.g., shared memory, hardware bus)

logical (e.g., direct or indirect, synchronous orasynchronous, automatic or explicit buffering)

6 . 15

IMPLEMENTATION QUESTIONSHow are links established?

Can a link be associated with more than two processes?

How many links can there be between every pair ofcommunicating processes?

What is the capacity of a link?

Is the size of a message that the link can accommodatefixed or variable?

Is a link unidirectional or bi-directional?

6 . 166 . 17

DIRECT COMMUNICATIONProcesses must name each other explicitly:

send(P, message) – send a message to process P

receive(Q, message) – receive a message fromprocess Q

DIRECT COMMUNICATIONProperties of communication link

Links are established automatically

A link is associated with exactly one pair ofcommunicating processes

Between each pair there exists exactly one link

The link may be unidirectional, but is usually bi-directional

6 . 18

INDIRECT COMMUNICATIONMessages are directed and received from mailboxes (also

referred to as ports)

Each mailbox has a unique id

Processes can communicate only if they share a mailbox

6 . 19

INDIRECT COMMUNICATIONProperties of communication link

Link established only if processes share a commonmailbox

A link may be associated with many processes

Each pair of processes may share several communicationlinks

Link may be unidirectional or bi-directional

6 . 20

INDIRECT COMMUNICATIONOperations

create a new mailbox

send and receive messages through mailbox

destroy a mailbox

Primitives are defined as:

send(A, message) – send a message to mailbox A

receive(A, message) – receive a message frommailbox A

6 . 21

INDIRECT COMMUNICATIONMailbox sharing

P1 , P2, and P3 share mailbox A

P1, sends; P2 and P3 receive

Who gets the message?

6 . 22

INDIRECT COMMUNICATIONSolutions

Allow a link to be associated with at most two processes

Allow only one process at a time to execute a receiveoperation

Allow the system to select arbitrarily the receiver. Senderis notified who the receiver was.

6 . 23

SYNCHRONIZATIONMessage passing may be either blocking or non-blocking

Blocking is considered synchronous

Blocking send has the sender block until the message isreceived

Blocking receive has the receiver block until a message isavailable

6 . 24

SYNCHRONIZATIONNon-blocking is considered asynchronous

Non-blocking send has the sender send the message andcontinue

Non-blocking receive has the receiver receive a validmessage or null

6 . 25

SYNCHRONIZATIONDifferent combinations possible

If both send and receive are blocking, we have a rendezvous

Producer-consumer becomes trivialm e s s a g e n e x t _ p r o d u c e d ; w h i l e ( t r u e ) { / * p r o d u c e a n i t e m i n n e x t p r o d u c e d * / s e n d ( n e x t p r o d u c e d ) ; }

m e s s a g e n e x t _ c o n s u m e d ; w h i l e ( t r u e ) { r e c e i v e ( n e x t c o n s u m e d ) ; / * c o n s u m e t h e i t e m i n n e x t c o n s u m e d * / }

6 . 26

BUFFERINGQueue of messages attached to the link; implemented in

one of three ways

1. Zero capacity – 0 messages → Sender must wait forreceiver (rendezvous)

2. Bounded capacity – finite length of n messages → Sendermust wait if link full

3. Unbounded capacity – infinite length → Sender neverwaits

6 . 277 . 1

EXAMPLES OF IPC SYSTEMS

POSIX SHARED MEMORYProcess first creates shared memory segment

s h m _ f d = s h m _ o p e n ( n a m e , O C R E A T | O R D R W , 0 6 6 6 ) ;

Also used to open an existing segment to share it

Set the size of the object

f t r u n c a t e ( s h m f d , 4 0 9 6 ) ;

Now the process could write to the shared memory

s p r i n t f ( s h a r e d m e m o r y , " W r i t i n g t o S M " ) ;

7 . 2

POSIX PRODUCER

7 . 3

POSIX CONSUMER

7 . 4

MACH (1)Mach communication is message based

Even system calls are messages

Each task gets two mailboxes at creation- Kernel andNotify

Only three system calls needed for message transfermsg_send(), msg_receive(), msg_rpc()

Mailboxes needed for commuication, created viaport_allocate()

7 . 5

MACH (2)Send and receive are flexible, for example four options ifmailbox full:

1. Wait indefinitely

2. Wait at most n milliseconds

3. Return immediately

4. Temporarily cache a message

7 . 6

WINDOWS (!)Message-passing centric via advanced local procedure call

(LPC) facility

Only works between processes on the same system

Uses ports (like mailboxes) to establish and maintaincommunication channels

7 . 7

WINDOWS (2)Communication works as follows:

The client opens a handle to the subsystem’sconnection port object.

The client sends a connection request.

The server creates two private communication portsand returns the handle to one of them to the client.

The client and server use the corresponding porthandle to send messages or callbacks and to listen forreplies.

7 . 8

LOCAL PROCEDURE CALLS IN WIN XP

7 . 98 . 1

COMMUNICATION IN CLIENT –SERVER SYSTEMS

COMMUNICATION IN CLIENT –SERVER SYSTEMS

Sockets

Remote Procedure Calls

Pipes

Remote Method Invocation (Java)

8 . 2

SOCKETSA socket is defined as an endpoint for communication

Concatenation of IP address and port – a numberincluded to differentiate network services on host

The socket 161.25.19.8:1625 refers to port 1625 on host161.25.19.8

Communication consists between a pair of sockets

All ports below 1024 are well known, used for standardservices

Special IP address 127.0.0.1 (loopback) to refer to systemon which process is running

8 . 3

SOCKET COMMUNICATION

8 . 4

SOCKETS IN JAVAThree types of sockets

Connection-oriented (TCP)

Connectionless (UDP)

MulticastSocket class – data can be sent to multiplerecipients

8 . 5

SOCKETS IN JAVA

8 . 6

REMOTE PROCEDURE CALLSRemote procedure call (RPC) abstracts procedure calls

between processes on networked systems

Again uses ports for service differentiation

Stubs – client-side proxy for the actual procedure on theserver

The client-side stub locates the server and marshalls theparameters

The server-side stub receives this message, unpacks themarshalled parameters, and performs the procedure onthe server

8 . 7

REMOTE PROCEDURE CALLSOn Windows, stub code compile from specificationwritten in Microsoft Interface Definition Language (MIDL)

Data representation handled via External DataRepresentation (XDL) format to account for differentarchitectures → Big-endian and little-endian

Remote comm. → more failure scenarios than local

Messages can be delivered exactly once rather than atmost once

OS typically provides a rendezvous/matchmaker serviceto connect client and server

8 . 8

EXECUTION OF RPC

PIPESActs as a conduit allowing two processes to communicate

Issues

Is communication unidirectional or bidirectional?

In the case of two-way communication, is it half or full-duplex?

Must there exist a relationship (i.e. parent-child)between the communicating processes?

Can the pipes be used over a network?

8 . 98 . 10

ORDINARY PIPESOrdinary Pipes allow communication in standardproducer-consumer style

Producer writes to one end (the write-end of the pipe)

Consumer reads from the other end (the read-end of thepipe)

Ordinary pipes are therefore unidirectional

8 . 11

ORDINARY PIPESRequire parent-child relationship betweencommunicating processes

Windows calls these anonymous pipes

8 . 12

NAMED PIPESNamed Pipes are more powerful than ordinary pipes

Communication is bidirectional

No parent-child relationship is necessary between thecommunicating processes

Several processes can use the named pipe forcommunication

Provided on both UNIX and Windows systems

8 . 139 . 1

QUESTIONS

9 . 2

BONUSExam question number 2: Process Concept andMultithreaded Programming