chapter 4 processes -...

28
Contents Contents 1. 1. Introduction Introduction 2. 2. Computer Computer- System Structures System Structures 3. 3. Operating Operating- System Structures System Structures 4. 4. Processes Processes 5. 5. Threads Threads 6. 6. CPU Scheduling CPU Scheduling 7. 7. Process Synchronization Process Synchronization 8. 8. Deadlocks Deadlocks 9. 9. Memory Management Memory Management 10. 10. Virtual Memory Virtual Memory 11. 11. File Systems File Systems Chapter 4 Chapter 4 Processes Processes

Upload: others

Post on 18-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ContentsContents1.1. IntroductionIntroduction2.2. ComputerComputer--System StructuresSystem Structures3.3. OperatingOperating--System StructuresSystem Structures4.4. Processes Processes 5.5. Threads Threads 6.6. CPU Scheduling CPU Scheduling 7.7. Process Synchronization Process Synchronization 8.8. DeadlocksDeadlocks9.9. Memory ManagementMemory Management10.10. Virtual MemoryVirtual Memory11.11. File SystemsFile Systems

Chapter 4 Chapter 4 ProcessesProcesses

Page 2: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ProcessesProcesses

►►Objective:Objective:Process Concept & DefinitionsProcess Concept & Definitions

►►Process Classification:Process Classification:Operating system processes Operating system processes executing system codeexecuting system codeUser processes executing system User processes executing system codecodeUser processes executing user codeUser processes executing user code

ProcessesProcesses►►Example: Special Processes in UnixExample: Special Processes in Unix

PID 0 PID 0 –– SwapperSwapper (i.e., the scheduler)(i.e., the scheduler)►►Kernel processKernel process►►No program on disks correspond to this No program on disks correspond to this

processprocess

PID 1 PID 1 –– initinit responsible for bringing up a responsible for bringing up a Unix system after the kernel has been Unix system after the kernel has been bootstrapped. (/etc/bootstrapped. (/etc/rcrc* & init or /* & init or /sbin/rcsbin/rc* * & init)& init)►►User process with User process with superusersuperuser privilegesprivileges

Page 3: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ProcessesProcesses►►Example (continued): Example (continued):

Special Processes in UnixSpecial Processes in UnixPID 2 PID 2 -- pagedaemonpagedaemon responsible for responsible for pagingpaging►►Kernel processKernel process

ProcessesProcesses

►► ProcessProcessA Basic Unit of Work from the Viewpoint A Basic Unit of Work from the Viewpoint of OSof OSTypes:Types:►►Sequential processes: an activity resulted from Sequential processes: an activity resulted from

the execution of a program by a processorthe execution of a program by a processor►►MultiMulti--thread processesthread processes

An Active EntityAn Active Entity►►Program Code Program Code –– A Passive EntityA Passive Entity►►Stack and Data SegmentsStack and Data Segments

The Current ActivityThe Current Activity►►PC, RegistersPC, Registers , Contents in the Stack and Data , Contents in the Stack and Data

SegmentsSegments

Page 4: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ProcessesProcesses

►►Process StateProcess State

new

ready

waiting

terminated

running

admitted

interrupt

scheduled

exit

I/O or event wait

I/O or event completion

ProcessesProcesses

►►Process Control Block (PCB) Process Control Block (PCB) Process State Process State Program CounterProgram CounterCPU RegistersCPU RegistersCPU Scheduling InformationCPU Scheduling InformationMemory Management InformationMemory Management InformationAccounting InformationAccounting InformationI/O Status InformationI/O Status Information

Page 5: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ProcessesProcesses►►PCB: The repository for any PCB: The repository for any

information that may vary from information that may vary from process to process process to process

pointerprocess state

pcregister

012

PCB[]

NPROC-1

ProcessesProcesses

►►Process Control Block (PCB) Process Control Block (PCB) –– An Unix An Unix ExampleExample

proc[iproc[i]]►►Everything the system must know when the Everything the system must know when the

process is swapped out.process is swapped out.pidpid, priority, state, timer counters, etc., priority, state, timer counters, etc.

.u.u►►Things the system should know when process Things the system should know when process

is runningis runningsignal disposition, statistics accounting, files[], etc.signal disposition, statistics accounting, files[], etc.

Page 6: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

ProcessesProcesses

►►Example: 4.3BSDExample: 4.3BSD

text structure

proc[i]entry

pagetable Code Segment

Data Segment

PC

heap

user stackargv, argc,…

sp

.u

per-processkernel stack

p_textpx_caddr

p_p0br

u_proc

p_addr

RedZone

ProcessesProcesses

►►Example: 4.4BSDExample: 4.4BSD

proc[i]entry

process grp

file descrptors

VM space region lists

pagetable Code Segment

Data Segmentheap

user stackargv, argc,…

.u

per-processkernel stack

p_p0br

u_proc

p_addr

Page 7: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Process SchedulingProcess Scheduling

►►The goal of multiprogramming The goal of multiprogramming Maximize CPU/resource utilization!Maximize CPU/resource utilization!

►►The goal of time sharingThe goal of time sharingAllow each user to interact with his/her Allow each user to interact with his/her program!program! PCB1 PCB2

head

tail

head

tail

head

tail

PCB3

ready queue

disk unit 0

tape unit 1

Process Scheduling Process Scheduling –– A A QueueingQueueing DiagramDiagram

ready queuedispatch

CPU

I/O I/O queue I/O request

time slice expired

fork a child

wait for an interruptinterrupt occurs

child executeschild terminate

Page 8: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Process Scheduling Process Scheduling ––SchedulersSchedulers

►► LongLong--Term (/Job) SchedulerTerm (/Job) Scheduler

Goal: Select a good mix of I/OGoal: Select a good mix of I/O--bound bound and CPUand CPU--bound processbound process

RemarksRemarks::1.1. Control the degree of multiprogrammingControl the degree of multiprogramming2.2. Can take more time in selecting processes Can take more time in selecting processes

because of a longer interval between executionsbecause of a longer interval between executions3.3. May not exist physicallyMay not exist physically

CPU

MemoryJob pool

Process Scheduling Process Scheduling –– SchedulersSchedulers

►►ShortShort--Term (/CPU) SchedulerTerm (/CPU) SchedulerGoalGoal::Efficiently allocate the CPU to one of the Efficiently allocate the CPU to one of the ready processes according to some criteria.ready processes according to some criteria.

►►MidMid--Term SchedulerTerm SchedulerSwap processes in and out memory to control the Swap processes in and out memory to control the degree of multiprogrammingdegree of multiprogramming

Page 9: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Process Scheduling Process Scheduling –– Context Context SwitchesSwitches

►►Context Switch ~ Pure Context Switch ~ Pure OverheadsOverheads

Save the state of the old process and load Save the state of the old process and load the state of the newly scheduled process.the state of the newly scheduled process.►►The context of a process is usually reflected in The context of a process is usually reflected in

PCB and others, e.g., .u in Unix.PCB and others, e.g., .u in Unix.

Process Scheduling Process Scheduling –– Context Context SwitchesSwitches

►►IssuesIssues::The cost depends on hardware supportThe cost depends on hardware support►►e.g. processes with multiple register sets or e.g. processes with multiple register sets or

computers with advanced memory management.computers with advanced memory management.

Threads, i.e., lightThreads, i.e., light--weight process (LWP), weight process (LWP), are are

introduced to break this introduced to break this bottleneckbottleneck!!

Page 10: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Operations on ProcessesOperations on Processes►►Process Creation & Process Creation &

TerminationTerminationRestrictions on resource usageRestrictions on resource usagePassing of InformationPassing of InformationConcurrent executionConcurrent execution

root

pagedaemon swapper init

user1 user2 user3

Operations on ProcessesOperations on Processes►►Process DuplicationProcess Duplication

A copy of parent address space + A copy of parent address space + context is made for child, except the context is made for child, except the returned value from fork()returned value from fork()::►►Child returns with a value 0Child returns with a value 0►►Parent returns with process id of childParent returns with process id of child

No shared data structures between No shared data structures between parent and child communicate via parent and child communicate via shared files, pipes, etc.shared files, pipes, etc.Use Use execveexecve() to load a new program() to load a new programfork() fork() vsvs vforkvfork() () (Unix)(Unix)

Page 11: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Operations on ProcessesOperations on Processes►►Example:Example:

……if ( (if ( (pidpid = fork() ) == 0) {= fork() ) == 0) {

/* child process *//* child process */execlp(execlp(““/bin/ls/bin/ls””, , ““lsls””, NULL);, NULL);

} else if (} else if (pidpid < 0) {< 0) {fprintf(stderrfprintf(stderr, , ““Fork FailedFork Failed””););exit(exit(--1);1);

} else {} else {/* parent process */ /* parent process */ wait(NULL);wait(NULL);

}}

Operations on ProcessesOperations on Processes

►►Termination of Child ProcessesTermination of Child ProcessesReasons: Reasons: ►►Resource usages, needs, etc.Resource usages, needs, etc.

Kill, exit, wait, abort, signal, etc.Kill, exit, wait, abort, signal, etc.

►►Cascading TerminationCascading Termination

Page 12: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Cooperating ProcessesCooperating Processes

►►Cooperating processesCooperating processes can affect can affect or be affected by the other or be affected by the other processesprocesses

Independent ProcessesIndependent Processes

►►Reasons:Reasons:Information Sharing, e.g., filesInformation Sharing, e.g., filesComputation Speedup, e.g., Computation Speedup, e.g., parallelism.parallelism.Modularity, e.g., functionality Modularity, e.g., functionality dividingdividing

Cooperating ProcessesCooperating Processes

►►A ConsumerA Consumer--Producer Example:Producer Example:Bounded buffer or unbounded bufferBounded buffer or unbounded buffer►►Supported by interSupported by inter--process process

communication (IPC) or by hand codingcommunication (IPC) or by hand coding

z

0

12

n-1

n-2

in

out

buffer[0…n-1]

Initially,

in=out=0;

Page 13: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Cooperating ProcessesCooperating Processes

Producer:Producer:while (1) {while (1) {

/* produce an item /* produce an item nextpnextp */*/while (((in+1) % BUFFER_SIZE) == out)while (((in+1) % BUFFER_SIZE) == out)

; /* do nothing */; /* do nothing */buffer[ in ] = buffer[ in ] = nextpnextp;;in = (in+1) % BUFFER_SIZE;in = (in+1) % BUFFER_SIZE;

}}

Cooperating ProcessesCooperating Processes

Consumer:Consumer:while (1) {while (1) {

while (in == out) while (in == out) ; /* do nothing */; /* do nothing */

nextcnextc = buffer[ out ];= buffer[ out ];out = (out+1) % BUFFER_SIZE ; out = (out+1) % BUFFER_SIZE ; /* consume the item in /* consume the item in nextcnextc */*/

}}

Page 14: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication

►►Why InterWhy Inter--Process Communication (IPC)?Process Communication (IPC)?Exchanging of Data and Control Information!Exchanging of Data and Control Information!

►►Why Process Synchronization?Why Process Synchronization?Protect critical sections!Protect critical sections!Ensure the order of executions!Ensure the order of executions!

InterprocessInterprocess CommunicationCommunication►►IPCIPC

Shared MemoryShared MemoryMessage PassingMessage Passing

►►Logical Implementation of Logical Implementation of Message PassingMessage Passing

Fixed/variable Fixed/variable msgmsg size, size, symmetric/asymmetric symmetric/asymmetric communication, direct/indirect communication, direct/indirect communication, automatic/explicit communication, automatic/explicit buffering, send by copy or buffering, send by copy or reference, etc. reference, etc.

Page 15: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication

►►Classification of Communication by NamingClassification of Communication by NamingProcesses must have a way to refer to each Processes must have a way to refer to each other!other!

TypesTypes►►Direct CommunicationDirect Communication►►Indirect CommunicationIndirect Communication

InterprocessInterprocess CommunicationCommunication ––Direct CommunicationDirect Communication

►► Process must explicitly name the Process must explicitly name the recipient or sender of a recipient or sender of a communicationcommunication

Send(P, Send(P, msgmsg), Receive(Q, ), Receive(Q, msgmsg))

►► Properties of a Link:Properties of a Link:a.a. Communication links are established Communication links are established

automatically.automatically.b.b. Two processes per a linkTwo processes per a linkc.c. One link per pair of processesOne link per pair of processesd.d. Bidirectional or unidirectionalBidirectional or unidirectional

Page 16: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication ––Direct CommunicationDirect Communication

►►Issue in Addressing:Issue in Addressing:Symmetric or asymmetric addressingSymmetric or asymmetric addressing

Send(P, Send(P, msgmsg), Receive(id, ), Receive(id, msgmsg))

►►Difficulty:Difficulty:Process naming Process naming vsvs modularitymodularity

InterprocessInterprocess CommunicationCommunication ––Indirect CommunicationIndirect Communication►► Two processes can communicate Two processes can communicate

only if the process share a only if the process share a mailbox (or ports)mailbox (or ports)

AAsend(A, msg)=> =>receive(A, msg)

Page 17: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication ––Indirect CommunicationIndirect Communication►► Properties:Properties:

1.1. A link is established between a pair of A link is established between a pair of processes only if they share a mailbox.processes only if they share a mailbox.

2.2. nn processes per link for processes per link for nn >= 1.>= 1.3.3. nn links can exist for a pair of processes links can exist for a pair of processes

for for nn >=1>=1..4.4. Bidirectional or unidirectionalBidirectional or unidirectional

InterprocessInterprocess CommunicationCommunication ––Indirect CommunicationIndirect Communication►► Issues:Issues:

a.a. Who is the recipient of a message?Who is the recipient of a message?

b.b. Owners Owners vsvs UsersUsers►► Process Process owner as the sole recipient?owner as the sole recipient?►► OS OS Let the creator be the owner?Let the creator be the owner?

Privileges can be passed?Privileges can be passed?Garbage collection is needed?Garbage collection is needed?

P1msgs ?

P2

P3

Page 18: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication ––SynchronizationSynchronization

►►Blocking or Blocking or NonblockingNonblocking(Synchronous versus (Synchronous versus Asynchronous)Asynchronous)

Blocking sendBlocking sendNonblockingNonblocking sendsendBlocking receiveBlocking receiveNonblockingNonblocking receivereceive

►►Rendezvous Rendezvous –– blocking send & blocking send & receivereceive

InterprocessInterprocess CommunicationCommunication ––BufferingBuffering

►► The Capacity of a Link The Capacity of a Link = the # of = the # of messages could be held in the link.messages could be held in the link.

Zero capacity(no buffering)Zero capacity(no buffering)►► MsgMsg transfer must be synchronized transfer must be synchronized –– rendezvous! rendezvous!

Bounded capacityBounded capacity►► Sender can continue execution without waiting till the Sender can continue execution without waiting till the

link is fulllink is full

Unbounded capacityUnbounded capacity►► Sender is never delayed!Sender is never delayed!

►► The last two items are for asynchronous The last two items are for asynchronous communication and may need communication and may need acknowledgementacknowledgement

Page 19: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication ––BufferingBuffering

►► Special cases:Special cases:a.a. MsgsMsgs may be lost if the receiver can not catch may be lost if the receiver can not catch

up with up with msgmsg sending sending synchronizationsynchronizationb.b. Senders are blocked until the receivers have Senders are blocked until the receivers have

received received msgsmsgs and replied by reply and replied by reply msgsmsgsA Remote Procedure Call (RPC) frameworkA Remote Procedure Call (RPC) framework

InterprocessInterprocessCommunicationCommunication –– Exception Exception

ConditionsConditions►► Process terminationProcess termination

a.a. Sender TerminationSender Termination Notify or Notify or terminate the receiver!terminate the receiver!

b.b. Receiver TerminationReceiver Terminationa.a. No capacity No capacity server is blocked.server is blocked.b.b. BufferingBuffering messages are messages are

accumulated.accumulated.

Page 20: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

InterprocessInterprocess CommunicationCommunication ––Exception ConditionsException Conditions

►► Ways to Recover Lost Messages Ways to Recover Lost Messages (due to hardware or network failure):(due to hardware or network failure):

OS detects & resends messages.OS detects & resends messages.Sender detects & resends messages.Sender detects & resends messages.OS detects & notify the sender to handle it.OS detects & notify the sender to handle it.

►► Issues:Issues:a.a. Detecting methods, such as timeout!Detecting methods, such as timeout!b.b. Distinguish multiple copies if retransmitting is Distinguish multiple copies if retransmitting is

possiblepossible

InterprocessInterprocess CommunicationCommunication ––Exception ConditionsException Conditions

►► Scrambled Messages:Scrambled Messages:Usually OS adds checksums, such as CRC, Usually OS adds checksums, such as CRC, inside messages & resend them as necessary!inside messages & resend them as necessary!

Page 21: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Example Example -- MachMach►►Mach Mach –– A messageA message--based OS based OS

from the Carnegie Mellon from the Carnegie Mellon UniversityUniversity

When a task is created, two special When a task is created, two special mailboxes, called ports, are also mailboxes, called ports, are also created.created.►►The The KernelKernel mailbox is used by the mailbox is used by the

kernel to communicate with the taskskernel to communicate with the tasks►►The The NotifyNotify mailbox is used by the mailbox is used by the

kernel sends notification of event kernel sends notification of event occurrences.occurrences.

Example Example -- MachMach

►► Three system calls for message transfer:Three system calls for message transfer:msg_sendmsg_send::►► Options when mailbox is full:Options when mailbox is full:a.a. Wait indefinitelyWait indefinitelyb.b. Return immediatelyReturn immediatelyc.c. Wait at most for Wait at most for nn msmsd.d. Temporarily cache a message. Temporarily cache a message.

a.a. A cached message per mailboxA cached message per mailbox

* One task can either own or receive from a mailbox.

Page 22: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Example Example -- MachMach

►► msg_receivemsg_receiveTo receive from a mailbox or a set To receive from a mailbox or a set of mailboxes. of mailboxes. At any time, only one task can At any time, only one task can own or have a receiving privilege own or have a receiving privilege of itof it* options when mailbox is empty:* options when mailbox is empty:a.a. Wait indefinitelyWait indefinitelyb.b. Return immediately Return immediately c.c. Wait at most for Wait at most for nn msms

Example Example -- MachMach

►► msg_rpcmsg_rpcRemote Procedure CallsRemote Procedure Calls

Page 23: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Example Example -- MachMach►►port_allocate port_allocate

create a mailbox (owner)create a mailbox (owner)port_status ~ .e.g, # of port_status ~ .e.g, # of msgsmsgs in a linkin a link

►► All messages have the same priority and All messages have the same priority and are served in a FIFO fashion.are served in a FIFO fashion.

►► Message SizeMessage SizeA fixedA fixed--length head + a variablelength head + a variable--length data length data + two mailbox names+ two mailbox names

►► Message copying: message copying Message copying: message copying remapping of addressing spaceremapping of addressing space

►► System calls are carried out by messages.System calls are carried out by messages.

Example Example –– Windows 2000Windows 2000►► Local Procedure Call (LPC) Local Procedure Call (LPC) ––

Message Passing on the Same Message Passing on the Same ProcessorProcessor1.1. The client opens a handle to a The client opens a handle to a

subsystemsubsystem’’s s connection portconnection port object.object.2.2. The client sends a connection request.The client sends a connection request.3.3. The server creates two private The server creates two private

communication portscommunication ports, and returns the , and returns the handle to one of them to the client.handle to one of them to the client.

4.4. The client and server use the The client and server use the corresponding port handle to send corresponding port handle to send messages or callbacks and to listen messages or callbacks and to listen for replies.for replies.

Page 24: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Example Example –– Windows 2000Windows 2000

►►Three Types of Message Three Types of Message Passing TechniquesPassing Techniques

Small messagesSmall messages►►Message copyingMessage copying

Large messages Large messages –– section objectsection object►►To avoid memory copyTo avoid memory copy►►Sending and receiving of the pointer Sending and receiving of the pointer

and size information of the objectand size information of the object

A callback mechanismA callback mechanism►►When a response could not be made When a response could not be made

immediately.immediately.

Communication in ClientCommunication in Client--Server SystemsServer Systems

►►SocketSocketAn endpoint for communication An endpoint for communication identified by an IP address identified by an IP address concatenated with a port numberconcatenated with a port number►►A clientA client--server architectureserver architecture

Socket146.86.5.2:1652

Socket146.86.5.2:1652

Socket161.25.19.8:80

Socket161.25.19.8:80

Web server

Host X

* /etc/services: Port # under 1024 ~ 23-telnet, 21-ftp, 80-web server, etc.

Page 25: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Communication in ClientCommunication in Client--Server Server SystemsSystems

►►Three types of sockets in JavaThree types of sockets in JavaConnectionConnection--oriented (TCP) oriented (TCP) –– Socket classSocket classConnectionless (UDP) Connectionless (UDP) –– DatagramSocketDatagramSocketclassclassMulticastSocketMulticastSocket classclass

sock = new ServerSocket(5155);…client = sock.accept();pout = new PrintWriter(client.getOutputStream(),

true);…pout.close();client.close();

sock = new Socket(“127.0.0.1”,5155);…in = sock.getInputStream();bin = new BufferReader(new

InputStreamReader(in));…sock.close();

Server Client

Communication in ClientCommunication in Client--Server Server SystemsSystems

►►Remote Procedure Call (RPC)Remote Procedure Call (RPC)A way to abstract the procedureA way to abstract the procedure--call call mechanism for use between systems mechanism for use between systems with network connection.with network connection.Needs:Needs:►►Ports to listen from the RPC daemon site Ports to listen from the RPC daemon site

and to return results, identifiers of and to return results, identifiers of functions to call, parameters to pack, etc.functions to call, parameters to pack, etc.

►►Stubs at the client siteStubs at the client siteOne for each RPCOne for each RPCLocate the proper port and Locate the proper port and marshallmarshall parameters.parameters.

Page 26: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Communication in ClientCommunication in Client--Server Server SystemsSystems

Needs (continued)Needs (continued)►►Stubs at the server siteStubs at the server site

Receive the messageReceive the messageInvoke the procedure and return the results.Invoke the procedure and return the results.

Issues for RPCIssues for RPC►►Data representationData representation

External Data Representation (XDR)External Data Representation (XDR)►►Parameter marshallingParameter marshalling

►►Semantics of a callSemantics of a callHistory of all messages processedHistory of all messages processed

►►Binding of the client and server portBinding of the client and server portMatchmaker Matchmaker –– a rendezvous mechanisma rendezvous mechanism

Communication in ClientCommunication in Client--Server Server SystemsSystems

Client Messages Server

Call kernelto send RPCmsg to Procedure X

Kernel sendsmsg tomatchmaker

Kernel placesport P in usrRPC msg

Kernel sendsRPC

Kernel receivesreply and passesto user

Port: matchakerRe: addr. to X

Port: kernelRe: port P to X

Port: P<contents>

Port: kernel<output>

Matchmakerreceives msg

Matchmakerreplies to clientwith port P

Daemon listensto port P andreceives msg

Daemon processesrequest and sendsoutput

Page 27: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Communication in ClientCommunication in Client--Server Server SystemsSystems

►►An Example for RPCAn Example for RPCA Distributed File System (DFS)A Distributed File System (DFS)►►A set of RPC daemons and clientsA set of RPC daemons and clients►►DFS port on a server on which a file DFS port on a server on which a file

operation is to take place:operation is to take place:Disk operations: read, write, Disk operations: read, write, delete, status, etc delete, status, etc ––corresponding to usual system corresponding to usual system callscalls

Communication in ClientCommunication in Client--Server Server SystemsSystems

►►Remote Method Invocation (RMI)Remote Method Invocation (RMI)Allow a thread to invoke a method on Allow a thread to invoke a method on a remote object.a remote object.►►booleanboolean valval = = Server.someMethod(A,BServer.someMethod(A,B))

►►ImplementationImplementationStub Stub –– a proxy for the remote objecta proxy for the remote object►►Parcel Parcel –– a method name and its a method name and its

marshalledmarshalled parameters, etc.parameters, etc.

Skeleton Skeleton –– for the for the unmarshallingunmarshalling of of parameters and invocation of the parameters and invocation of the method and the sending of a parcel method and the sending of a parcel backback

Page 28: Chapter 4 Processes - cc.ee.ntu.edu.twcc.ee.ntu.edu.tw/~farn/courses/OS/OS2004/slide/Chapter04.pdf · Memory Management 10. Virtual Memory 11. File Systems Chapter 4 Processes. Processes

Communication in ClientCommunication in Client--Server Server SystemsSystems

►►Parameter PassingParameter PassingLocal (or Local (or NonremoteNonremote) Objects) Objects►►PassPass--byby--copy copy –– an object serializationan object serialization

Remote Objects Remote Objects –– Reside on a different Java Reside on a different Java virtual machine (JVM)virtual machine (JVM)►►PassPass--byby--referencereference

Implementation of the interface Implementation of the interface ––java.io.Serializablejava.io.Serializable