Module 4: Processes

Download Report

Transcript Module 4: Processes

Chapter 3: Processes
Process Concept
 Process – a program in execution; process
execution must progress in sequential
fashion
 A process includes:
Pseudo
Code
Executable
Code
(text) for OS Simulator
//~ load pc into mar
 Stack
increment pc
 Data//~
section
 Heap
//~ check to ensure that pc is legal


//~ load mdr based upon mar
Review //~ move mdr to ir
 MAR//~
memory
address
execute
ir register

MDR memory data register

IR current instruction

PC next instruction
Operating System Concepts - 7th Edition, Feb 7, 2006
3.2
Silberschatz, Galvin and Gagne ©2005
Process State

As 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 a processor

terminated: The process has finished execution
Operating System Concepts - 7th Edition, Feb 7, 2006
3.3
Silberschatz, Galvin and Gagne ©2005
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
Operating System Concepts - 7th Edition, Feb 7, 2006
3.4
Silberschatz, Galvin and Gagne ©2005
Context Switch
 When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new
process
 Context-switch time is overhead; the system does no useful work
while switching
 Time dependent on hardware support
Operating System Concepts - 7th Edition, Feb 7, 2006
3.5
Silberschatz, Galvin and Gagne ©2005
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
 Device queues – set of
processes waiting for an I/O
device
 Processes migrate among the
various queues
Operating System Concepts - 7th Edition, Feb 7, 2006
3.6
Silberschatz, Galvin and Gagne ©2005
Schedulers
 Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
 Medium-term scheduler– selects which process should
be removed from ready Q
 Short-term scheduler (or CPU scheduler) – selects
which process should be executed next and allocates
CPU
 There is an entire chapter on scheduling
Operating System Concepts - 7th Edition, Feb 7, 2006
3.7
Silberschatz, Galvin and Gagne ©2005
Ability to spawn a new process
 Why might an operating system want to be able to do this?
 Why might an operating system want to be able to support/provide
this service?
Process A
Spawn
New process
Operating System Concepts - 7th Edition, Feb 7, 2006
3.8
Silberschatz, Galvin and Gagne ©2005
Process Creation
 Parent process create children processes, which, in turn create
other processes, forming a tree of processes
 Execution

Parent and children execute concurrently

Parent waits until children terminate
Operating System Concepts - 7th Edition, Feb 7, 2006
3.9
Silberschatz, Galvin and Gagne ©2005
C Program Forking Separate Process
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
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 */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
Operating System Concepts - 7th Edition, Feb 7, 2006
3.10
Silberschatz, Galvin and Gagne ©2005
Cooperating Processes
 Independent 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 cooperation

Information sharing

Computation speed-up

Modularity

Convenience
Operating System Concepts - 7th Edition, Feb 7, 2006
3.11
Silberschatz, Galvin and Gagne ©2005
Interprocess Communication (IPC)
 Why might an operating system want to provide this service?
 Mechanism for processes to communicate and to synchronize their
actions
 Shared memory
 Message-passing
Process A
Operating System Concepts - 7th Edition, Feb 7, 2006
Process B
3.12
Silberschatz, Galvin and Gagne ©2005
Shared Memory Models ProducerConsumer Problem
 Shared memory models

Must keep from stepping on one another

Don’t write to a location that has not been read yet
 Known as the producer-consumer problem

producer process produces information that is
consumed by a consumer process
Operating System Concepts - 7th Edition, Feb 7, 2006
3.13
Silberschatz, Galvin and Gagne ©2005
A Solution
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
 in points to the next free position
 out points to the first full position
 Empty when in == out
 Both producer and consumer can access in, out, and buffer
Operating System Concepts - 7th Edition, Feb 7, 2006
3.14
Silberschatz, Galvin and Gagne ©2005
Producer
while (true) {
/* Produce an item */
while (( (in + 1) % BUFFER_SIZE)
;
== out)
/* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Operating System Concepts - 7th Edition, Feb 7, 2006
3.15
Silberschatz, Galvin and Gagne ©2005
Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
// remove an item from the buffer
item = buffer[out];
// could set buffer[out] to some val
// indicating that it is empty
out = (out + 1) % BUFFER SIZE;
// consume (do something with) item;
}
Operating System Concepts - 7th Edition, Feb 7, 2006
3.16
Silberschatz, Galvin and Gagne ©2005
Messaging-passing
 Direct

Process A
while (TRUE) {
produce an item
send ( B, item )
}
Process B
while (TRUE) {
receive ( A, item )
consume item
}
 Indirect

Messages 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
Operating System Concepts - 7th Edition, Feb 7, 2006
3.17
Silberschatz, Galvin and Gagne ©2005
Synchronization When Communicating


Blocking is considered synchronous

Blocking send has the sender block until the message is
received

Blocking receive has the receiver block until a message is
available
Non-blocking is considered asynchronous

Non-blocking send has the sender send the message and
continue

Non-blocking receive has the receiver receive a valid
message or null
 Example: checking an indirect message-passing mailbox to see if
any messages; if none, continue execution

Blocking or non-blocking?
Operating System Concepts - 7th Edition, Feb 7, 2006
3.18
Silberschatz, Galvin and Gagne ©2005
Remote Procedure Calls
 Message-passing
 Blocking
 Similar to local procedure call

To the calling process it appears as if the process “blocks”
while waiting for the procedure to complete
Call
Process B
Process A
Result
Operating System Concepts - 7th Edition, Feb 7, 2006
3.19
Silberschatz, Galvin and Gagne ©2005
Buffering
 Allows for speed mismatch
 Queue of messages attached to the link; implemented in one of
three ways
1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits
Process A
Operating System Concepts - 7th Edition, Feb 7, 2006
Buffer
3.20
Process B
Silberschatz, Galvin and Gagne ©2005
Long distance IPC
 Sockets TCP/IP

A socket is defined as an endpoint for communication

Concatenation of IP address and port

The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8

Communication consists between a pair of sockets
Operating System Concepts - 7th Edition, Feb 7, 2006
3.21
Silberschatz, Galvin and Gagne ©2005
End of Chapter 3