Top Banner
CHAPTER 4 PROCESSES Operating Systems
69

CHAPTER 4 PROCESSES

Jan 04, 2016

Download

Documents

sean-fox

CHAPTER 4 PROCESSES. Operating Systems. Processes. Current-day computer systems allow multiple programs to be loaded into memory and to be executed concurrently. This resulted in process , which is a program in execution. Process Concept Process Scheduling Operation on Processes - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CHAPTER 4 PROCESSES

CHAPTER 4

PROCESSES

Operating Systems

Page 2: CHAPTER 4 PROCESSES

2

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

ProcessesCurrent-day computer systems allow multiple programs to be loaded into

memory and to be executed concurrently. This resulted in process, which is a

program in execution.

Process ConceptProcess Scheduling

Operation on ProcessesCooperating Processes

Interprocess Communication

Page 3: CHAPTER 4 PROCESSES

3

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process ConceptAn operating system executes a variety of programs:

Batch system – jobs

Time-shared systems – user programs or tasks

Even on a single-user system, such as Windows or Mac, a user may be able to run several programs at one time.

Textbook uses the terms job and process almost interchangeably.

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

§4.1

Page 4: CHAPTER 4 PROCESSES

4

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process ConceptA process is more than the program code (known as the text section), it also includes the current activity represented by:

program counter

Stack – contains temporary data (such as method parameters, return addresses, and local variables)

data section – contains global variables.

A program is not a process; a program is a passive entity, such as the contents of a file stored on disk, whereas a process is an active entity, with a program counter specifying the next instruction to execute.

§4.1.1

Page 5: CHAPTER 4 PROCESSES

5

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process Concept

Two processes associated with same program are considered two separate execution sequences.

Ex: several users running different copies of the mail program or same user may invoke many copies of the editor program.

Each of above is a separate process, and, although the text sections are equivalent, the data sections vary.

Page 6: CHAPTER 4 PROCESSES

6

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process State

As a process executes, it changes statenew: 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 process.terminated: The process has finished execution.

Only one process can be running on any processor at any instant. Many processes may be ready and waiting, however.

§4.1.2

Page 7: CHAPTER 4 PROCESSES

7

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Diagram of Process State

Page 8: CHAPTER 4 PROCESSES

8

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process Control Block (PCB)

Information associated with each process.

Process state

Program counter

CPU registers

CPU scheduling information

Memory-management information

Accounting information

I/O status information

§4.1.3

Page 9: CHAPTER 4 PROCESSES

9

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process Control Block (PCB)

Page 10: CHAPTER 4 PROCESSES

10

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

CPU Switch From Process to Process

Page 11: CHAPTER 4 PROCESSES

11

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process Scheduling Queues

Job queue – set of all processes in the system.Ready queue – set of all processes residing in main memory, ready and waiting to execute. This queue is generally stored as a linked list. Device queues – Since there are many processes in the system, the device may be busy with other processes’ requests. The list of processes waiting for a particular I/O device is called a device queue.

§4.2

Page 12: CHAPTER 4 PROCESSES

12

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Ready Queue and Various I/O Device Queues

Page 13: CHAPTER 4 PROCESSES

13

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process MigrationA common representation for process migration is a queueing diagram.

A new process is initially put in the ready queue. It waits in the ready queue until it is selected for execution or is dispatched. Once the process is allocated the CPU and is executing, one of several events could occur:

Issue an I/O request and be placed in an I/O queue.

Create a new subprocess and wait for its termination.

Be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.

Page 14: CHAPTER 4 PROCESSES

14

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Queueing Diagram

Page 15: CHAPTER 4 PROCESSES

15

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

SchedulersScheduler select processes from scheduling queues for their migration within the system. Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue (loaded into memory for execution).Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.Primary difference between them: the frequency of execution.

§4.2.2

Select frequently (miliseconds)

must be fast

Invoked very

infrequently (seconds,

minutes) may be slow

Page 16: CHAPTER 4 PROCESSES

16

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Long-term schedulerThe long-term scheduler controls the degree of multiprogramming (the number of processes in memory).It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound processes to reach the best performance.If all processes are I/O bound …If all processes are CPU bound …If no long-term scheduler… (some time-sharing system such as UNIX)

THINK

THINK

THINK

Page 17: CHAPTER 4 PROCESSES

17

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Medium-term schedulerSome OS may introduce an additional, intermediate level of scheduling.

Key idea: sometimes it can be advantageous to remove processes from memory (and from active contention for the CPU), and thus to reduce the degree of multiprogramming………swapping.

Swapping may be necessary to improve the process mix, or because a change in memory requirements has overcommitted available memory, requiring memory to be freed up.

Page 18: CHAPTER 4 PROCESSES

18

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Medium-term scheduler

Page 19: CHAPTER 4 PROCESSES

19

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Context SwitchWhen CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.The context of a process is represented in the PCB of a process.When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run.Context-switch time is overhead; the system does no useful work while switching.Time dependent on hardware support.

§4.2.3

Page 20: CHAPTER 4 PROCESSES

20

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process Creation

Parent process creates children processes, which, in turn create other processes, forming a tree of processes. (Fig. 4.7)

Resource sharingA subprocess may obtain its resources directly from the OS, or

may be constrained to a subset of the resources of the parent process.

§4.3.1

prevents any process from overloading the system by

creating too many subprocesses

Page 21: CHAPTER 4 PROCESSES

21

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

A Tree of Processes On a Typical UNIX System

Back

Page 22: CHAPTER 4 PROCESSES

22

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Process CreationExecution possibilities

Parent and children execute concurrently.Parent waits until some or all children have terminated.

Address spaceChild is a duplicate of the parent.Child has a program loaded into it.

UNIX examplesEach process is identified by its process identifier.A new process is created by the fork system call.Both the parent and the child continue execution at the instruction after the fork with different returning code: 0 : child, nonzero (process id): parentexeclp system call used after a fork to replace the process’ memory space with a new program.

Page 23: CHAPTER 4 PROCESSES

23

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

UNIX Example#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Page 24: CHAPTER 4 PROCESSES

24

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

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.

Parent may terminate execution of children processes (abort) for following reasons:

Child has exceeded allocated resources.

Task assigned to the child is no longer required.

Parent is exiting.• Operating system does not allow child to continue if its parent

terminates… called cascading termination.

§4.3.2

Page 25: CHAPTER 4 PROCESSES

25

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

Page 26: CHAPTER 4 PROCESSES

26

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

Page 27: CHAPTER 4 PROCESSES

27

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

Page 28: CHAPTER 4 PROCESSES

28

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

Page 29: CHAPTER 4 PROCESSES

29

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

Page 30: CHAPTER 4 PROCESSES

30

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

(pid > 0 ) (pid = 0)#include <stdio.h>Void main(int argc, char *argv[]){int pid;

pid = fork(); /* fork another process */if (pid < 0) { /* error occurred */

fprintf(stderr, “Fork Failed”);exit(-1);

}else if (pid == 0) { /* child process */

execlp(“/bin/ls”,”ls”,Null);}else { /* parent process wait for the child to complete*/

wait(NULL);printf(“Child Complete”);exit(0);

}}

Parent Child

/bin/ls

Page 31: CHAPTER 4 PROCESSES

31

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Cooperating ProcessesIndependent process cannot affect or be affected by the execution of another process.

Cooperating process can affect or be affected by the execution of another process

Advantages of process cooperationInformation sharing

Computation speed-up: if multiple processor exists.

Modularity: divide the system function into separate processes or threads.

Convenience: a user may have many tasks on which to work at one time.

§4.4

Page 32: CHAPTER 4 PROCESSES

32

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Producer-Consumer ProblemA common paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.

Require a buffer that can be filled by the producer and emptied by the consumer.

The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet been produced.

unbounded-buffer places no practical limit on the size of the buffer.

bounded-buffer assumes that there is a fixed buffer size.

Page 33: CHAPTER 4 PROCESSES

33

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Bounded-Buffer – Shared-Memory Solution

(Fig 4.9)Shared data #define BUFFER_SIZE 10 Typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;

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

Page 34: CHAPTER 4 PROCESSES

34

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Bounded-Buffer – Shared-Memory Solution

Bounded(Circular)

Buffer

Producer (Producer stores data in buffer(in). The value of in is increased by 1)

Consumer (Consumer reads contents of last full buffer. The value of out is incremented by 1.)

inout

Page 35: CHAPTER 4 PROCESSES

35

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Bounded-Buffer – Shared-Memory Solution

Producer process

item nextProduced;

while (1) {

while (((in + 1) % BUFFER_SIZE) == out)

; /* do nothing */

buffer[in] = nextProduced;

in = (in + 1) % BUFFER_SIZE;

}

Page 36: CHAPTER 4 PROCESSES

36

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Bounded-Buffer – Shared-Memory Solution

Consumer processitem nextConsumed;

while (1) {

while (in == out)

; /* do nothing */

nextConsumed = buffer[out];

out = (out + 1) % BUFFER_SIZE;

}

Page 37: CHAPTER 4 PROCESSES

37

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Interprocess Communication (IPC)Message-passing system for processes to communicate and to synchronize their actions without sharing the same address space. Useful in distributed environment. Ex: chat programIPC facility provides at least 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 themexchange messages via send/receive

Implementation of communication linkphysical (e.g., shared memory, hardware bus, network)logical (e.g., logical properties)

§4.5

Page 38: CHAPTER 4 PROCESSES

38

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S Communicating processes must have a way to refer to each other, directly or indirectly.

Direct communication

Indirect communication

§4.5.2Naming

Page 39: CHAPTER 4 PROCESSES

39

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Direct CommunicationProcesses must name each other explicitly:

send(P, message) – send a message to process Preceive(Q, message) – receive a message from process Q

Properties of communication linkLinks are established automatically. The processes need to know only each other’s identity to communicate.A link is associated with exactly two processes.Between each pair there exists exactly one link.

§4.5.2.1

Page 40: CHAPTER 4 PROCESSES

40

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Direct CommunicationA variant of symmetry in addressing is asymmetry addressing: only the sender names the recipient; the recipient is not required to name the sender.

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

receive(id, message) – receive a message from any process; id is set to the name the process with which communication has taken place.

Both symmetry and asymmetry communication has limited modularity of the resulting process definitions. Changing the name of a process may necessitate examining all other process definition.

Page 41: CHAPTER 4 PROCESSES

41

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Indirect CommunicationMessages are sent to and received from mailboxes (also referred to as ports). Each mailbox has a unique id. Processes can communicate only if they share a mailbox.

send(A, message) – send a message to mailbox Areceive(A, message) – receive a message from mailbox A

Properties of communication linkLink established only if processes share a common mailboxA link may be associated with many processes.Each pair of processes may share several communication links.

§4.5.2.2

Page 42: CHAPTER 4 PROCESSES

42

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S A mailbox may be owned either by a process or by the operating system.Mailbox sharing

P1, P2, and P3 share mailbox A.

P1, sends; P2 and P3 receive.Who gets the message?

SolutionsAllow a link to be associated with at most two processes.Allow only one process at a time to execute a receive operation.Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

Indirect Communication

Page 43: CHAPTER 4 PROCESSES

43

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

SynchronizationMessage passing may be either blocking or nonblocking – also known as synchronous and asynchronous.

Blocking send: The sending process is blocked until the message is received.

Nonblocking send: The sending process resumes operation.

Blocking receive: The receiver blocks until a message is available.

Nonblocking receive: The receiver retrieves either a valid message or a null.

When both the send and receive are blocking, we have a rendezvous between sender and receiver.

§4.5.3

Page 44: CHAPTER 4 PROCESSES

44

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Buffering

Message exchanged by communicating processes reside in a temporary queue implemented in one of three ways.1.Zero capacity – 0 messages

Sender must wait for receiver (rendezvous).

2.Bounded capacity – finite length of n messagesSender must wait if link full.

3.Unbounded capacity – infinite length Sender never waits.

§4.5.4

Page 45: CHAPTER 4 PROCESSES

45

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

An Example: Mach ( 略 )

Page 46: CHAPTER 4 PROCESSES

46

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Example: Windows 2000Windows 2000 supports multiple operating environments or subsystems, with which application programs (i.e. clients) communicate via a message-passing mechanism.

In Windows 2000, the message-passing between two processes that are on the same machine is called the local procedure-call facility (LPC).

NT uses a port object to establish and maintain a connection between two processes.

Page 47: CHAPTER 4 PROCESSES

47

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Example: Windows 2000

Communication works as follows:The client opens a handle to the subsystem’s connection port object.

The client sends a connection request.

The server creates two private communication ports, and returns the handle to one of them to the client

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

Page 48: CHAPTER 4 PROCESSES

48

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Example: Windows 2000NT uses three types of message-passing techniques over a port that the client specifies when it establishes the channel:

For small message, uses the port’s message queue as intermediate storage and copies the message from one process to the other.For larger message, it passes the message through a section object (shared memory).For better performance, in quick LPC, the server sets up a dedicated server thread to handle requests.

Page 49: CHAPTER 4 PROCESSES

49

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Communication in Client-Server Systems§4.6.1A Socket is defined as an endpoint for

communication.

A pair of processes (or threads) communicating over a network employs a pair of sockets – one for each process.

A socket is identified by an IP address concatenated with a port number.

Page 50: CHAPTER 4 PROCESSES

50

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Sockets

The server waits for incoming client requests by listening to a specified port. Once a request is received, the server accepts a connection from the client socket to complete the connection.

Servers implementing specific services by listening to well-known ports.

Telnet server listens to port 23.Ftp server listens to port 21.

Web server (http) listens to port 80.

All ports below 1024 are considered well known.

Page 51: CHAPTER 4 PROCESSES

51

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Sockets

When a client thread initiates a request for a connection, it is assigned an arbitrary port (> 1024) by the host computer.Example: a client on host X with IP address 146.86.5.20 wishes to establish a connection with a web server at 161.25.19.8

socket146.86.5.20/1625

socket161.25.19.8/80

host X(146.86.5.20)

web server(161.25.19.8)

Page 52: CHAPTER 4 PROCESSES

52

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Sockets

The packets traveling between the hosts are delivered to the appropriate process, based on the destination port number.

All connection must be unique. Therefore, if another process also on host X wished to establish another connection with the same web server, it would be assigned a port number > 1024 and ≠ 1625.

Page 53: CHAPTER 4 PROCESSES

53

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Java Sockets

Java provides three different types of sockets:

Connection-oriented (TCP) sockets(implemented with socket class)

Connectionless (UDP) sockets(use the DatagramSocket class)

MulticastSocket socket, a subclass of the DatagramSocket class, allows data to be sent to multiple recipients.

Page 54: CHAPTER 4 PROCESSES

54

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Java Sockets

Example: Multithreaded time-of-day server allows clients to request the time of day from the server.

When a connection is received, the server creates a new thread to serve the request. This new thread returns to the client the current time of day.

Page 55: CHAPTER 4 PROCESSES

55

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Time-of-day Serverimport java.net.*;import java.io.*;public class Server {

public static void main(String[] args) throws IOException {Socket client = null;PrintWriter pout = null;ServerSocket sock = null;try {

sock = new ServerSocket(5155);//now listen for connectionswhile (true) {

client = sock.accept();// we have a connectionpout = new PrintWriter(client.getOUtputStream(), true);// write the Date to the socketpout.println(new java.util.Date().toString());pout.close();client.close();

}}catch (IOException ioe) {

System.err.println(ioe);}finally {

if (client != null)client.close();

if (sock != null)sock.close();

}}

}

Creates a ServerSocket that specifies that it will listen

to port 5155

Listening to the port by blocking with accept() method and wait for a client to

request a connection.

Page 56: CHAPTER 4 PROCESSES

56

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Once the connection is made, the client can read from the socket

using normal file I/O statements.

The Clientimport java.net.*;import java.io.*;public class Client {

public static void main(String[] args) throws IOException {InputStream in = null;BufferedReader bin = null;Socket sock = null;try {

// make connection to socketsock = new Socket(“127.0.0.1”,5155);in = sock.getInputStream();bin = new BufferedReader(new InputStreamReader(in));String line;while ((line = bin.readLine()) != null)

System.out.println(line);}catch (IOException ioe) {

System.err.println(ioe);}finally {

if (sock != null)sock.close();

}}

}

create a socket and connecting to the port the server is listening on.

After it has received the time of day from the server, the client

closes the socket and exit.

Page 57: CHAPTER 4 PROCESSES

57

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Higher-Level Methods of Communication

Communication using sockets – although common and efficient – is considered a low-level form.Socket allow only unstructured stream of bytes to be exchanged. The client or server application need to impose a structure on the data.Two higher-level methods:

Remote Procedure CallsRemote Method Invocation

Page 58: CHAPTER 4 PROCESSES

58

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Remote Procedure Call

The semantics of an RPC system allows a client to invoke a procedure on a remote host as it would invoke a local procedure.

Advantage: Since the RPC system manages the communication channel, so application programs can be written such that the location of a procedure is transparent.

The process may be on the same computer, or on a

different computer connected by a network.

§4.6.2

Page 59: CHAPTER 4 PROCESSES

59

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

RPC vs. IPC

It is similar to the IPC mechanism and usually built on top of it.The messages exchanged for RPC are well structured and are thus no longer just packets of data.They are addressed to an RPC daemon listening to a port on the remote system, and contain in identifier of the function to execute and the parameters to pass to that function.The function is then executed as requested, and any output is sent back to the requester in a separate message.

Page 60: CHAPTER 4 PROCESSES

60

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Port

A port is simply a number included at the start of a message packet.

Whereas a system normally has one network address, it can have many ports within that address to differentiate the many network services it supports.

If a remote process needs a service, it addresses its messages to the proper port.

Example: a system allows other systems to be able to list its current user, it would have a daemon

supporting such an RPC attached to a port, say 3027. Other systems may now send an RPC message to port

3027

Page 61: CHAPTER 4 PROCESSES

61

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Stub

The RPC system use a stub to hide the necessary details allowing the communication to take place.

A separate stub exists for each separate remote procedure. When the client invokes a remote procedure, the RPC system calls the appropriate stub, passing it the parameters provided to the remote proceudre.

The stub locates the port on the server and packaging the parameters for transmitting over the network.

Page 62: CHAPTER 4 PROCESSES

62

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Stub

The stub then transmits a message to the server using message passing.

A similar stub on the server side receives this message and invokes the procedure on the server.

Page 63: CHAPTER 4 PROCESSES

63

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Timestamp

To avoid duplicating the RPC due to network errors, each message is attached a timestamp.

The server keep a history of all the timestamps of messages it has already processed.

Incoming messages that have a timestamp already in the history are ignored.

Page 64: CHAPTER 4 PROCESSES

64

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Port Binding

How does a client know the port numbers on the server?

1. The binding info may be predetermined, in the form of fixed port addresses. Once a program is compiled, the server cannot change the port number of the requested services.

2. Binding can be done dynamically by a rendezvous mechanism. OS provides a rendezvous daemon for matching the port numbers…overhead initially but more flexible.

Page 65: CHAPTER 4 PROCESSES

65

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Port Binding

Page 66: CHAPTER 4 PROCESSES

66

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Remote Method Invocation

RMI is a Java feature similar to RPCs, which allows a thread to invoke a method on a remote object.

§4.6.3

On remote JVM

Page 67: CHAPTER 4 PROCESSES

67

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

RPC vs. RMI

RPCs support procedural programming: only remote procedures and functions may be called.RMI is object-oriented: supports invocation of methods on remote objects.

RPC’s parameters to remote procedures are ordinary data structures. RMI is possible to pass objects as parameters.

Page 68: CHAPTER 4 PROCESSES

68

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Parameter MarshallingClient-side stub is responsible for creating a parcel consisting of the name of the method to be invoked on the server and the marshalled parameters for the method.The stub then sends this parcel to the server, where the skeleton for the remote object receives it.The skeleton is responsible for unmarshalling the parameters and invoking the desired method on the server.The skeleton then marshalls the return value into a parcel and returns this parcel to the client.The stub unmarshalls the return value and passes it to the client.

Page 69: CHAPTER 4 PROCESSES

69

CH

AP

TE

R F

OU

R, O

PE

RA

TIN

G

SY

ST

EM

S

Parameter Marshalling