• 沒有找到結果。

2. Computer-System Structures 3. Operating-System Structures 4. Processes

N/A
N/A
Protected

Academic year: 2022

Share "2. Computer-System Structures 3. Operating-System Structures 4. Processes "

Copied!
25
0
0

加載中.... (立即查看全文)

全文

(1)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Contents

1. Introduction

2. Computer-System Structures 3. Operating-System Structures 4. Processes

5. Threads

6. CPU Scheduling

7. Process Synchronization 8. Deadlocks

9. Memory Management 10. Virtual Memory 11. File Systems

Chapter 4 Processes

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Processes

ƒ Objective:

ƒ Process Concept & Definitions

ƒ Process Classification:

ƒ Operating system processes executing system code

ƒ User processes executing system code

ƒ User processes executing user code

Processes

ƒ Example: Special Processes in Unix

ƒ PID 0 – Swapper (i.e., the scheduler)

ƒ Kernel process

ƒ No program on disks correspond to this process

ƒ PID 1 – init responsible for bringing up a Unix system after the kernel has been

bootstrapped. (/etc/rc* & init or /sbin/rc* & init)

ƒ User process with superuser privileges

ƒ PID 2 - pagedaemon responsible for paging

ƒ Kernel process

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Processes

ƒ Process

ƒ A Basic Unit of Work from the Viewpoint of OS

ƒ Types:

ƒ Sequential processes: an activity resulted from the execution of a program by a processor

ƒ Multi-thread processes

ƒ An Active Entity

ƒ Program Code – A Passive Entity

ƒ Stack and Data Segments

ƒ The Current Activity

ƒ PC, Registers , Contents in the Stack and Data Segments

Processes

ƒ Process State

new

ready

waiting

terminated

running admitted

interrupt

scheduled

exit

I/O or event wait

I/O or event completion

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Processes

ƒ Process Control Block (PCB)

ƒ Process State

ƒ Program Counter

ƒ CPU Registers

ƒ CPU Scheduling Information

ƒ Memory Management Information

ƒ Accounting Information

ƒ I/O Status Information

Processes

ƒ PCB: The repository for any information that may vary from process to process

pointer process state

pc register 0

1 2 PCB[]

NPROC-1

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Processes

ƒ Process Control Block (PCB) – An Unix Example

ƒ proc[i]

ƒ Everything the system must know when the process is swapped out.

ƒ pid, priority, state, timer counters, etc.

ƒ .u

ƒ Things the system should know when process is running

ƒ signal disposition, statistics accounting, files[], etc.

Processes

ƒ Example: 4.3BSD

text structure

proc[i]

entry

page

table Code Segment

Data Segment

PC heap

user stack argv, argc,…

sp .u

per-process kernel stack

p_textp x_caddr

p_p0br

u_proc p_addr

Red

Zone

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Processes

ƒ Example: 4.4BSD

proc[i]

entry

process grp

file descrptors

VM space region lists

page

table Code Segment

Data Segment heap user stack argv, argc,…

.u

per-process kernel stack

p_p0br

u_proc p_addr

Process Scheduling

ƒ The goal of multiprogramming

ƒ Maximize CPU/resource utilization!

ƒ The goal of time sharing

ƒ Allow each user to interact with his/her program!

PCB1 PCB2

head tail head

tail head

tail

PCB3 ready

queue

disk

unit 0

tape

unit 1

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Process Scheduling – A Queueing Diagram

ready queue dispatch

CPU

I/O I/O queue I/O request

time slice expired

fork a child

wait for an interrupt interrupt occurs

child executes child terminate

Process Scheduling – Schedulers

ƒ Long-Term (/Job) Scheduler

ƒ Goal: Select a good mix of I/O-bound and CPU-bound process

ƒ Remarks:

1. Control the degree of multiprogramming

2. Can take more time in selecting process because of a longer interval between executions

3. May not exist physically CPU

Memory

Job pool

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Process Scheduling – Schedulers

ƒ Short-Term (/CPU) Acheduler

ƒ Goal:Efficiently allocate the CPU to one of the ready processes

according to some criteria.

ƒ Mid-Term Scheduler

ƒ Swap processes in and out memory to control the degree of multiprogramming

Process Scheduling – Context Switches

ƒ Context Switch ~ Pure Overheads

ƒ Save the state of the old process and load the state of the newly scheduled process.

ƒ The context of a process is usually reflected in PCB and others, e.g., .u in Unix.

ƒ Issues:

ƒ The cost depends on hardware support

ƒ e.g. processes with multiple register sets or computers with advanced memory management.

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

introduced to break this bottleneck!

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Operations on Processes

ƒ Process Creation & Termination

ƒ Restrictions on resource usage

ƒ Concurrent execution

root

pagedaemon swapper init

user1 user2 user3

Operations on Processes

ƒ Process Duplication

ƒ A copy of parent address space + context is made for child, except the returned value from fork():

ƒ Child returns with a value 0

ƒ Parent returns with process id of child

ƒ No shared data structures between parent and child communicate via shared files, pipes, etc.

ƒ Use execve() to load a new program

ƒ fork() vs vfork() (Unix)

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Operations on Processes

ƒ Example:

if ( pid = fork() ) == 0) { /* child process */

execlp(“/bin/ls”, “ls”, NULL);

} else if (pid < 0) {

fprintf(stderr, “Fork Failed”);

exit(-1);

} else {

/* parent process */

wait(NULL);

}

Operations on Processes

ƒ Termination of Child Processes

ƒ Reasons:

ƒ Resource usages, needs, etc.

ƒ Cascading Termination

(11)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Cooperating Processes

ƒ Cooperating processes can affect or be affected by the other processes

ƒ Independent Processes

ƒ Reasons:

ƒ Information Sharing

ƒ Computation Speedup

ƒ Modularity

ƒ Convenience

Cooperating Processes

ƒ A Consumer-Producer Example:

ƒ Bounded buffer or unbounded buffer

ƒ Supported by inter-process

communication (IPC) or by hand coding

z 0

1 2

n-1 n-2

in out

buffer[0…n-1]

Initially,

in=out=0;

(12)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Cooperating Processes

Producer:

while (1) {

/* produce an item nextp */

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

; /* do nothing */

buffer[ in ] = nextp;

in = (in+1) % BUFFER_SIZE;

}

Cooperating Processes

Consumer:

while (1) {

while (in = out)

; /* do nothing */

nextc = buffer[ out ];

out = (out+1) % BUFFER_SIZE ; /* consume the item in nextc */

}

(13)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication

ƒ Why Inter-Process Communication (IPC)?

ƒ Exchanging of Data and Control Information!

ƒ Why Process Synchronization?

ƒ Protect critical sections!

ƒ Ensure the order of executions!

Interprocess Communication

ƒ IPC

ƒ Shared Memory

ƒ Message Passing

ƒ Logical Implementation of Message Passing

ƒ Fixed/variable msg size, direct/indirect communication,

automatic/explicit buffering, send by

copy or reference, etc.

(14)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication

ƒ Classification of Communication by Naming

ƒ Processes must have a way to refer to each other!

ƒ Types

ƒ Direct Communication

ƒ Indirect Communication

Interprocess Communication

Direct Communication

ƒ Process must explicit name the

recipient or sender of a communication

ƒ Send(P, msg), Receive(Q, msg)

ƒ Properties of a Link:

a. Communication links are established automatically.

b. Two processes per a link

c. One link per a pair of processes

d. Bidirectional or unidirectional

(15)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication

Direct Communication

ƒ Issue in Addressing:

ƒ Symmetric or asymmetric addressing receive(id, msg)

ƒ Difficulty:

Process naming vs modularity

Interprocess Communication – Indirect Communication

ƒ Two processes can communicate only if the process share a mailbox (or ports)

ƒ Properties:

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

2. n processes per link for n >= 1.

3. n links can exist for a pair of processes for n >=1 .

4. Bidirectional or unidirectional A A

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

(16)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication – Indirect Communication

ƒ Issues:

a. Who is the recipient of a msg?

b. Owners vs Users

ƒ Process Æ owner as the sole recipient?

ƒ OS Æ Let the creator be the owner?

Privileges can be passed?

Garbage collection is needed?

P1

mgs ?

P2 P3

Interprocess Communication – Synchronization

ƒ Blocking or Nonblocking

(Synchronous versus Asynchronous)

ƒ Blocking send

ƒ Nonblocking send

ƒ Blocking receive

ƒ Nonblocking receive

ƒ Rendezvous – blocking send &

receive

(17)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication – Buffering

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

ƒ Zero capacity(no buffering)

ƒ Msg transfer must be synchronized – rendezvous!

ƒ Bounded capacity

ƒ Sender can continue execution without waiting till the link is full

ƒ Unbounded capacity

ƒ Sender is never delayed!

ƒ The last two items are for asynchronous

communication and may need acknowledgement

Interprocess Communication

Buffering

ƒ Special cases:

a. Msgs may be lost if the receiver can not catch up with msg sending Æ synchronization

b. Senders are blocked until the receivers have received msgs and replied by reply msgs

Æ A Remote Procedure Call (RPC)

framework

(18)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Interprocess Communication – Exception Conditions

ƒ Process termination

a. Sender TerminationÆ Notify or terminate the receiver!

b. Receiver Termination

a. No capacity Æ server is blocked b. BufferingÆ msgs are accumulated

Interprocess Communication – Exception Conditions

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

ƒ OS detects & resends msgs

ƒ Sender detects & resends msgs

ƒ OS detects & notify sender to handle it.

ƒ Issues:

a. Detecting methods, such as timeout!

b. Distinguish multiple copies if retransmitting is possible

ƒ Scrambled Messages:

ƒ Usually OS adds checksums, such as CRC, inside

messages & resend them as necessary!

(19)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Example - Mach

ƒ Mach – A message-based OS from the Carnegie Mellon University

ƒ When a task is created, two special mailboxes, called ports, are also created.

ƒ The kernel mailbox is used by the kernel to communication with the tasks

ƒ The Notify mailbox is used by the kernel sends notification of event occurrences.

Example - Mach

ƒ Three system calls for message transfer:

ƒ msg_send:

ƒ Options when mailbox is full:

a. Wait indefinitely b. Return immediately c. Wait at most for n ms d. Temporarily cache a mag.

a. A cached message per mailbox

(20)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Example - Mach

ƒ msg_receive

ƒ To receive from a mailbox or a set of mailboxs. Only one task can own &

have a receiving privilege of it

* options when mailbox is empty:

a. Wait indefinitely b. Return immediately c. Wait at most for n ms

ƒ msg_rpc

ƒ Remote Procedure Calls

Example - Mach

ƒ port_allocate

ƒ create a mailbox (owner)

ƒ port_status ~ .e.g, # of msgs in a link

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

ƒ Message Size

ƒ A fixed-length head + a variable-length data + two mailbox names

ƒ Message copying: message copying Æ remapping of addressing space

ƒ System calls are carried out by messages.

(21)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Example – Windows 2000

ƒ Local Procedure Call (LPC) – Message Passing on the Same Processor

1. The client opens a handle to a subsystem’s connection port object.

2. The client sends a connection request.

3. The server creates two private

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

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

Example – Windows 2000

ƒ Three Types of Message Passing Techniques

ƒ Small messages

ƒ Large messages – section object

ƒ To avoid memory copy

ƒ For sending and receiving

ƒ A callback mechanism

(22)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Communication in Client- Server Systems

ƒ Socket

ƒ An endpoint for communication identified by an IP address concatenated with a port number

ƒ A client-server architecture

Socket

146.86.5.2:1652

Socket

146.86.5.2:1652

Socket

161.25.19.8:80

Socket

161.25.19.8:80

Web server Host X

Communication in Client- Server Systems

ƒ Three types of sockets in Java

ƒ Connection-oriented (TCP) – Socket class

ƒ Connectionless (UDP) – DatagramSocket class

ƒ MulticastSocket class

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

(23)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Communication in Client- Server Systems

ƒ Remote Procedure Call (RPC)

ƒ A way to abstract the procedure-call mechanism for use between systems with network connection.

ƒ Needs:

ƒ Ports to listen from the RPC daemon site and to return results, identifiers of functions to call, parameters to pack, etc.

ƒ Stubs at the client site

ƒ One for each RPC

ƒ Locate the proper port and marshall parameters.

Communication in Client- Server Systems

ƒ Needs (continued)

ƒ Stubs at the server site

ƒ Receive the message

ƒ Invoke the procedure and return the results.

ƒ Issues for RPC

ƒ Data representation

ƒ External Data Representation (XDR)

ƒ Parameter marshalling

ƒ Semantics of a call

ƒ History of all messages processed

ƒ Binding of the client and server port

ƒ Matchmaker – a rendezvous mechanism

(24)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Communication in Client- Server Systems

Client Messages Server

Call kernel to send RPC msg to Procedure X

Kernel sends msg to matchmaker

Kernel places port P in usr RPC msg

Kernel sends RPC

Kernel receives reply and passes to user

Port: matchaker Re: addr. to X

Port: kernel Re: port P to X

Port: P

<contents>

Port: kernel

<output>

matchmaker receives msg

matchmaker replies to client with port P

daemon listen to port P and receives msg

daemon processes request and sends output

Communication in Client- Server Systems

ƒ An Example for RPC

ƒ A Distributed File System

ƒ A set of RPC daemons and clients

ƒ DFS port on a server on which a fiel operation is to take place

ƒ Disk operations: read, write, delete,

status, etc

(25)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Communication in Client- Server Systems

ƒ Remote Method Invocation (RMI)

ƒ Allow a thread to invoke a method on a remote object.

ƒ boolean val = Server.someMethod(A,B)

ƒ Implementation

ƒ Stub – a proxy for the remote object

ƒ Parcel – method name and marshalled parameters, etc.

ƒ Skeleton – for the unmarshalling of parameters and invocation of the method.

Communication in Client- Server Systems

ƒ Parameter Passing

ƒ Local (or nonremote) objects

ƒ Pass-by-copy and an object serialization

ƒ Remote Objects – Reside on a different Java virtual machine (JVM)

ƒ Pass-by-reference

ƒ Implementation of the interface –

java.io.Serializable

參考文獻

相關文件

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

4 理律法律事務所 © 2020 All rights

All rights reserved.... All

2 Department of Materials Science and Engineering, National Chung Hsing University, Taichung, Taiwan.. 3 Department of Materials Science and Engineering, National Tsing Hua

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005..

* All rights reserved, Tei-Wei Kuo, National Taiwan University,