Module 4: Processes

Download Report

Transcript Module 4: Processes

Processes
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Communication in Client-Server
Systems
Process Concept
 An operating system executes a variety of
programs:
Batch system – jobs
Time-shared systems – user programs or tasks
 Textbook uses the terms job and process almost
interchangeably.
 Process – a program in execution; process
 A process includes:
program counter
stack
data section
Stack
Temporary data:
Function parameter,
return addresses
and local variables
Memory: that is
dynamically allocated
during process run time
Heap
Data
A process is more than the
Text
program code –text section
Contains global variables
Process in memory
Contents of
processor’s registers
Value of Program counter
Relationship b/w process and program
 Is process same as a program?
No
 Program is sequence of statements that a
programmer writes.
 A single program may contain more than one
processes
Many processes may be derived from a single program
 When you perform “Copy” at the Command
Prompt of Windows XX, several processes are
derived from that.
Copy source_File destination_File
Multiprogramming VS Uniprogramming
 Some systems allow only one process called
Uni-programming Systems.
To invoke a new process other processes must be
finished.
 Most systems now a days allow more than one
process called Multiprogramming Systems.
 In case of uni-programming you don’t need to
store information about the process as much as
you would need to store information about a
process being executed in multiprogramming
system.
Process State
 In user view processes are running
simultaneously
 In computers view these processes are in
contention for the CPU and I/O
 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.
Diagram of Process State
The state of a process is defined in part by the current activity of that process
Process Control Block (PCB)
1/2
Saves Information associated with each
process.
Remember that we studied in interrupts that the process
state is saved in memory. PCB provides data structure
for saving process state
Process state
New, Wait, Ready etc.
Program counter
Next instruction to be executed.
CPU registers
EAX, EBX, SP etc.
Process Control Block (PCB)
CPU Switch From Process to
Process
Process Control Block (PCB)
 CPU scheduling information
Process priority, pointers to scheduling queues
 Memory-management information
Information about base and limit registers (remember
the Memory-protection chapter 2?), page tables etc.
 Accounting information
Amount of CPU time, Time limits (CPU protection),
process numbers etc
 I/O status information
List of I/O devices being used by the process
Processes
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Communication in Client-Server
Systems
Process Scheduling
Objective of multi-programming is to have
some processes running at all the time
Maximization of CPU utilization
It is achieved by time sharing
Process Scheduler is used to control the
“flow” of processes
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. –Linked List
Device queues – set of processes waiting
for an I/O device.—A device queue for each device
Monitor, HDD, Floppy and other I/O devices
have separate Queues.
Process migrates between the various
queues.
Ready Queue And Various I/O Device
Queues
Head is to tell
the start of the
queue where as
Tail is to
facilitate the
entry of new
Process in the
queue
Representation of Process
Queue
Scheduling
Resource
Schedulers
More processes are submitted than can be
executed immediately -- spooling
Long-term scheduler (or job scheduler) –
selects which processes should be brought
into the ready queue.
Might take Seconds to be activated
Short-term scheduler (or CPU scheduler) –
selects which process should be executed
next and allocates CPU.
Is happening in milliseconds
Long term and Short term Schedulers
 The long term scheduler may need to be invoked only when a
process leaves the system
 Degree of multiprogramming -- number of processes in memory
Long
Term
scheduler
New
process
Ready
Queue
Short
Term
Scheduler
CPU
Schedulers
cont.
I/O bound –Process that spends more of its time
doing I/O than it spends doing computation
CPU bound –Process that generates I/O requests
infrequently, using more of its time doing computations.
 Long term scheduler should select a good process mix
of I/O bound and CPU bound processes –balance in
queues.
 Long term scheduler may be absent or minimal on some
systems –(e.g. Time sharing systems (UNIX, Microsoft windows).
 Swapping – good to improve the process mix.
Addition of Medium Term Scheduling
Temporarily swaps out the process to reduce the degree of multiprogramming
Schedulers (Cont.)
 Short-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 of
multiprogramming.
Processes can be described as either:
 I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts.
 CPU-bound process – spends more time doing computations;
few very long CPU bursts.
Long-term scheduler must strike a balance between these
two types of processes
Current Trends…
Long-term scheduler is absent in almost
all the latest operating systems like
Windows and Unix ; 
Due to time-sharing we don’t need to have
long-term schedulers.
Degree of multiprogramming is in user’s hands
Greater the degree of multi-programming ,
slower the computer gets
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.
 The context is represented in the PCB of the
process
 Context-switch time is overhead; the system
does no useful work while switching.
Useful work? Useful with respect to users
point of view…
 Time dependent on hardware support.
Processes
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Communication in Client-Server
Systems
Process Creation
Parent process create children processes,
which, in turn create other processes,
forming a tree of processes.
Resource sharing in parent and child
processes ( 3 Types)
Parent and children share all resources.
Children share subset of parent’s resources.
Parent and child share no resources.
Execution
Parent and children execute concurrently.
Parent waits until children terminate.
Process Creation (Cont.)
 Address space
Child duplicate of parent. (Linux)
Child has a program loaded into it. (Windows)
 UNIX / Linux examples
fork system call creates new process
exec system call used after a fork to replace the
process’ memory space with a new program.
 Windows 2000/XP examples
CreateProcess system call
Several others also present to control the execution of
process.
Unix Process Implementation
Parent creates the process using Fork()
call
Child is created, and is exact copy of
parent
Child does not execute till it is issued
Exec() call
As parent and child are same the
distinction of being a parent or child is
made by ProcessID number
UNIX FORK and EXEC functions
#include <sys/types.h>
#include<stdio.h>
#include<unistd.h>
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) {
execlp(“/bin/ls”,”ls”);
} //ls is “Dir” equivalent of linux
Else {
wait(NULL); //wait till child process is complete
printf(“Child Complete”);
POSIX
Exit(0):
}
resumes
parent
wait
fork()
child
exit()
exec()
Win16 function WinExec()
(Now based on CreateProcess() function of Win32)
#include “windows.h”
#include “iostream.h”
int main(int argc, char* argv[])
{
WinExec("Calc",NULL);
return 0;
}
Documentation (ref. MSDN)
Creates a new process and runs it
in separate memory space
Processes are created in Win32 API using CreateProcess() function
CreateProcess() is similar to fork()
fork() is passed no parameters, CreateProcess() expects no fewer than ten
Address space difference --loading another program in child’s address space
Process Termination
 Process executes last statement and asks the
operating system to delete it (exit()).
Following steps are performed on exit
 Output data from child to parent (via wait).
 Process’ resources are deallocated by operating system.
 Parent may terminate execution of children
processes (abort ()).
Why would parent do that?
 Child has exceeded allocated resources.
 Task assigned to child is no longer required.
 Parent is exiting.
• Operating system does not allow child to continue if its parent
terminates.
• Cascading termination.
Interprocess Communication (IPC)
 Mechanism for processes to communicate and to
synchronize their actions.
 Message system – processes communicate with each
other without resorting to shared variables.
 IPC 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
Inter Process Communication
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 (breaking a task into subtasks)
Modularity (Divide the system functions into separate processes or threads)
Convenience (editing, printing and compiling at the same time)
Outline
 Process Concept
 Process Scheduling
 Operations on Processes
 Process Creation
 Process Termination
 Shared Memory Systems
 Message Passing Systems
 Naming
 Synchronization
 Buffering
 Communication in Client-Server Systems
 Sockets
 Remote Procedure Calls
 Remote Method Invocation
 Examples of IPC Systems
Shared memory systems
 Communicating processes need to establish a
region of shared memory.
 Shared memory region resides in the address
space of process creating SM seg.
 producer process produces information that is
consumed by a consumer process.
Compiler, assembler, loader
Producer is Server and a Consumer is client
 We must have a buffer of items that can be filled
by the producer and emptied by the consumer.
 Consumer shouldn’t try to consume that has not
yet been produced
Shared memory systems
(2)
 unbounded-buffer places no practical limit on the
size of the buffer.
 Consumer may have to wait; producer can always produce
 bounded-buffer assumes that there is a fixed
buffer size.
 Consumer must wait if the buffer is empty; producer must wait
if the buffer is full
 Shared buffer is implemented as a circular array
with two logical pointers in and out
 Buffer is empty when in==out
 Bufffer is full when ((in+1)%BUFFER_SIZE)==out
Shared memory systems
(3)
# define BUFFER_SIZE 10
item nextconsumed;
typedef struct {
while (true) {
…..
while(in==out)
}item;
;//do nothing
Item buffer[BUFFER_SIZE];
nextconsumed = buffer[out];
int in =0;
out=(out+1)%BUFFER_SIZE;
int out=0;
}
item nextproduced;
while (true){
while ((in +1)%BUFFER_SIZE)==out)
/*do nothing*/
buffer[in] = nextproduced;
in = (in +1)% BUFFER_SIZE;
}
Outline
 Process Concept
 Process Scheduling
 Operations on Processes
 Process Creation
 Process Termination
 Shared Memory Systems
 Message Passing Systems
 Naming
 Synchronization
 Buffering
 Communication in Client-Server Systems
 Sockets
 Remote Procedure Calls
 Remote Method Invocation
 Examples of IPC Systems
Message Passing Systems
 Mechanism for processes to communicate and to
synchronize their actions.
 Message system – processes communicate with each
other without resorting to shared variables.
 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
 Several methods for logically implementing send()/ receive()
operations:
 Direct & indirect communication
 Synchronous or asynchronous communication
 Automatic or explicit buffering
Lets do an example of
Direct message passing
#include <windows.h>
Its simple to execute this program:
Program asks for the window caption
#include <iostream.h>
Enter the caption e.g. Calculator
void main(void)
Calculator program must be running
{
This program will simply close the
char str[100];
program
cin >> str;
/*this sleep() is not required. Just wanted to tell you another function
that delays the execution. Basically this functions puts the current
thread to sleep for specified amount of time in milliseconds */
Sleep(3000); //wait for 3 seconds..
//Return the handler to the window with the specified caption
HWND hWnd = FindWindow(NULL, str);
//Send message to window with handle hWnd and ask it to close
SendMessage(hWnd,WM_CLOSE,0,0L);
//Guess what would happen if SetWindowText(hWnd,"Calc2000"); is
used //instead of SendMessage
}
Direct Communication
 Processes must name each other explicitly:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
 Properties of communication link
 Links are established automatically.
 A link is associated with exactly one pair of communicating processes.
 Between each pair there exists exactly one link.
 The link may be unidirectional, but is usually bi-directional.
 In MS Windows examples of such communication enabling
functions (system calls) are
 SendMessage(…) //Function used to send message to other window
 PostMessage(…) //posts a message to a queue
 Hard-Coding ----disadvantage
Indirect Communication
(1)
 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.
 Properties of communication link
Link established only if processes share a common
mailbox
A link may be associated with many processes.
Each pair of processes may share several communication
links.
Link may be unidirectional or bi-directional.
Indirect Communication (2)
 Operations
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 from
mailbox A
 POSIX message queues use an integer value to
identify a mailbox
Indirect Communication
(3)
 Mailbox sharing
P1, P2, and P3 share mailbox A.
P1, sends; P2 and P3 receive.
Who gets the message?
 Solutions
Allow 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
(4)
 Mailbox may be owned by a process or OS
 Owner – Process
 Owner –who can only receive messages through mailbox
 User –Who can only send messages to the mailbox
 No confusion about who should receive a message sent to this
mailbox
 Owner –OS
 mailbox is independent –not attached with any particular process
OS must provide a mechanism
 crate a new mailbox, send and receive messages, delete
 ownership and receiving privilege may be passed to other
processes –multiple receivers for each mailbox.
MS Windows API for Inter process
Indirect communications
 MailSlots
A mailslot is a mechanism for one-way interprocess
communications (IPC). A Win32-based application can store
messages in a mailslot. The owner of the mailslot can retrieve
messages that are stored there. Good for a process
broadcasting its message to many processes.
CreateMailSlot ( … )
ReadFile( … )
WriteFile ( … )
Must be synchronized to some extent.
 The creater can read or write the mail slot but the
client can only send data. Not like the description of
previous slide
void main(void)
{
Mailslot Server
HANDLE hMS; hf ; BOOL ret=1;
Process that receives messages
DWORD bWritten, b, c, d, e;
(Server.cpp)
char a[10];
//lets create mail slot (kind of mail server)
hMS=
CreateMailslot("\\\\.\\mailslot\\my_mailslot",0,MAILSLOT_WAIT_FOREVER,NULL);
if (hMS == INVALID_HANDLE_VALUE)
{
cout << "Failed to create mailslot" << endl;
}
/*open abstract file (this file is not in hard disk but in RAM). we will read or write to this file.
NOTE: Client cannot read this file, only thing client can do is to write to this file.. just like email..
sender cannot view email on your end */
hf = CreateFile("\\\\.\\mailslot\\my_mailslot",GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,NULL,NULL);
/*dummy wait for another process to send any message.
In real life a process might be involved with any other computations and still other process can
post mail message to it which this process can read upon return... when you will do some
visual programming (event driven) this will become more clear.*/
cin >> a;
//just to show you another API call
//Read the documentation to be clear about it! :)
GetMailslotInfo(hMS,&b,&c,&d,&e);
a[0] = '\0'; //initalize the array
ret = ReadFile(hMS,&a[0],c,&bRead,(LPOVERLAPPED)NULL); cout << a;
GetMailslotInfo(hMS,&b,&c,&d,&e);
ret = ReadFile(hMS,&a[0],c,&bRead,(LPOVERLAPPED)NULL); cout << a;
cin >> a; //dummy wait again!
}
Mailslot Client
#include <iostream.h>
Process that receives messages
#include <windows.h>
(Client.cpp)
void main(void)
{
HANDLE hfile;
hfile = CreateFile("\\\\.\\mailslot\\my_mailslot",GENERIC_READ |
GENERIC_WRITE, FILE_SHARE_READ |
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,NULL,NULL);
DWORD bWritten;
char a[100];
cout << endl <<"Enter message to send to other process: ";
cin >> a;
BOOL ret = WriteFile(hfile,a,strlen(a), &bWritten,NULL); //variable
length
ret = WriteFile(hfile,"OS!",3, &bWritten,NULL);//hardcoded
}
Create client.cpp and server.cpp. Compiler them and execute them separately
First execute server.exe. Don’t do any thing with this executable
Execute client.cpp and enter a message less than 10 chars
Then continue the server.exe my pressing enter key! 
MS Windows API for Inter process
Indirect communications
Named pipes
Protocol used to transfer data between
processes, usually across a network.
Typically, a process creates a named pipe
(conceptually, a conduit) with a well-known
name this process is the server process.
Processes that can get the name of the pipe
can open the other end of the pipe and can
exchange data by performing read and write
operations on the pipe.
Outline
 Process Concept
 Process Scheduling
 Operations on Processes
 Process Creation
 Process Termination
 Shared Memory Systems
 Message Passing Systems
 Naming
 Synchronization
 Buffering
 Communication in Client-Server Systems
 Sockets
 Remote Procedure Calls
 Remote Method Invocation
 Examples of IPC Systems
Synchronization
 Message passing may be either blocking or nonblocking.
 Blocking is considered synchronous
 Non-blocking is considered asynchronous
 Blocking send: The sender process is blocked
until the message is received by the receiving
process or by the mail box.
 Nonblocking send, Blocking receive,
Nonblocking receive
 Producer – consumer problem ?
Buffering
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.
Outline
 Process Concept
 Process Scheduling
 Operations on Processes
 Process Creation
 Process Termination
 Shared Memory Systems
 Message Passing Systems
 Naming
 Synchronization
 Buffering
 Communication in Client-Server Systems
 Sockets
 Remote Procedure Calls
 Remote Method Invocation
 Examples of IPC Systems --Self study
Client-Server Communication
Sockets
Remote Procedure Calls
Remote Method Invocation (Java)
Sockets
A socket is defined as an endpoint for
communication.
Processes communicating over a network
employ a pair of sockets.
Generally sockets use a client –server
architecture.
Server waits for the client request by
listening to a specified port
Well known ports (23, 21,80) <1024
Sockets
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.
All connection must be unique.
Java socket programming (connectionoriented TCP, connectionless UDP)
Socket communication is considered a
low-level form of communication
Socket Communication
Remote Procedure calls
High level method of communication
Messages are well structured
Messages are addressed to RPC daemon
listening to a port on the remote system.
Port is just a number included at the start
of a message packet.
One network address but many ports.
Different services –e.g. list users (3024)
Remote Procedure calls
 RPC hides details –stubs
 Separate stub exists for each separate remote
procedure.
 Marshalling parameters –pack the parameters
into a transmittable form.
 Difference in data representation on client and
server machines. {big-endian, little-endian}
 Convert the representation of data –machine
dependent to XDR (external data representation)
 Un-marshalling
RPC (little and big-endian)
 Endianness is the byte (and sometimes bit) ordering in
memory used to represent some kind of data.
 Big-endian and little-endian are terms that describe the
order in which a sequence of bytes are stored in
computer memory.
 Big-endian is an order in which the "big end" (most
significant value in the sequence) is stored first (at the
lowest storage address)
 Numbers are written in a big-endian order, with the
highest order numbers coming first (235 representation)
 Dates ordered in big-endian would be written yyyy-mmdd. The dd-mm-yyyy system would then be little endian
and mm-dd-yyyy middle endian.
RPC (little and big-endian)
Remote Procedure calls (contd)
 Failure of local procedure calls and RPC
 “exactly once” and “at most once” techniques
 “at most once”—time stamp, server keeps the
history, server can ignore the repeated
messages, client can send more than one time.
 “exactly once”– makes sure server receives the
request, ACK messages
 For RPC, OS should implement ??
Remote Procedure calls (contd)
Communication b/w server and client?
Binding takes place in standard procedure
calls
Procedure calls name is replaced by the
memory address of the procedure call
Possible approaches in RPC
1. fixed port addresses –not flexible
2. use a matchmaker
Remote Procedure calls (contd)
Execution of a RPC
Remote Method Invocation
Java feature similar to RPC
Remote objects: reside on different JVM
Differnce? RPC & RMI
RPC: supports procedural programming
Remote procedures & functions can be called
RMI: supports invocation of methods on remote
objects
Easy to develop a distributed java application
Implementation of stubs and skeleton
Remote Method Invocation
 When client invokes a remote method, the stub
for the remote object is called
Creating a parcel –name of the method to be invoked +
parameters to be marshalled.
 Skeleton for the remote object receives it –
unmarshalling the parameters!
Remote Method Invocation (contd..)
 Boolean val = server.someMethod (A, B)
 What if parameter A is local object and B is a remote
object?
 Pass by reference, by value
 RMI provides high level of abstraction and makes the
stubs and skeleton transparent
Stub
Skeleton
A, B, someMethod
Boolean return value
Outline
 Process Concept
 Process Scheduling
 Operations on Processes
 Process Creation
 Process Termination
 Shared Memory Systems
 Message Passing Systems
 Naming
 Synchronization
 Buffering
 Examples of IPC Systems
 Communication in Client-Server Systems
 Sockets
 Remote Procedure Calls
 Remote Method Invocation