Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes...
-
Upload
lydia-carroll -
Category
Documents
-
view
233 -
download
2
Transcript of Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes...
Processes
Process ConceptProcess SchedulingOperations on ProcessesCooperating ProcessesInterprocess CommunicationCommunication 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
Data
Text
Heap
Stack
Process in memory Value of Program counter
Contents of processor’s registers
Temporary data:Function parameter,
return addresses and local variables
Contains global variables
Memory: that is dynamically allocated
during process run time
A process is more than the program code –text section
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/2Saves 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 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 ConceptProcess SchedulingOperations on ProcessesCooperating ProcessesInterprocess CommunicationCommunication 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 QueuesJob 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 tofacilitate the entry of new Process in the queue
SchedulersMore processes are submitted than can be
executed immediately -- spoolingLong-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 Long TermTerm
schedulerscheduler
ReadyReadyQueueQueue
ShortShortTermTerm
SchedulerSchedulerCPU
Newprocess
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 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 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 ConceptProcess SchedulingOperations on ProcessesCooperating ProcessesInterprocess CommunicationCommunication in Client-Server
Systems
Process CreationParent 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 processpid = 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 completeprintf(“Child Complete”);
Exit(0):}
POSIX
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 CommunicationIndependent process cannot affect or be
affected by the execution of another process.Cooperating process can affect or be
affected by the execution of another processAdvantages 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
typedef struct {
…..
}item;
Item buffer[BUFFER_SIZE];
int in =0;
int out=0;
item nextproduced;
while (true){
while ((in +1)%BUFFER_SIZE)==out)
/*do nothing*/
buffer[in] = nextproduced;
in = (in +1)% BUFFER_SIZE;
}
item nextconsumed;
while (true) {
while(in==out)
;//do nothing
nextconsumed = buffer[out];
out=(out+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>#include <iostream.h>void main(void){
char str[100];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 closeSendMessage(hWnd,WM_CLOSE,0,0L);//Guess what would happen if SetWindowText(hWnd,"Calc2000"); is used //instead of SendMessage
}
Its simple to execute this program:Program asks for the window caption
Enter the caption e.g. CalculatorCalculator program must be runningThis program will simply close the
program
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){
HANDLE hMS; hf ; BOOL ret=1;DWORD bWritten, b, c, d, e;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 arrayret = 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 ServerProcess that receives messages
(Server.cpp)
#include <iostream.h>#include <windows.h>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 lengthret = WriteFile(hfile,"OS!",3, &bWritten,NULL);//hardcoded
}
Mailslot ClientProcess that receives messages
(Client.cpp)
Create client.cpp and server.cpp. Compiler them and execute them separatelyFirst execute server.exe. Don’t do any thing with this executableExecute client.cpp and enter a message less than 10 charsThen 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 non-
blocking. 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 messagesSender 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
SocketsBICSE (not to be covered as it is part of your C language course)
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. No programming practice for sockets because
you will do it in either C or in Advanced Java
Following code creates a process (e.g. Calculator) and executes it as a child process.
WaitForSingleObject() halts the execution of parent process till the time child process is finished.
#include <iostream.h>#include <windows.h>void main(void){ STARTUPINFO si; PROCESS_INFORMATION pi; //create process wants this structure to be initilized ZeroMemory( &si, sizeof(si) ); //fills the process with all zero’s si.cb = sizeof(si); // Start the child process. Returns false if child process failed to
execute if( !CreateProcess( NULL, // No module name (use command line). "calc", // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { cout << "CreateProcess failed." ; }
cout << "Child is running with parent on timeshared bases" << endl; cout <<"Now i am going to block the parent till child exits" << endl; // Wait till child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );cout << "Child process exited" << endl;
}
How to create child process in Win32 using CreateProcess()Reference MSDN
#include <Windows.h>#include <iostream.h>int main(void){ DWORD dwThreadId, dwThrdParam = 1; HANDLE hThread; hThread = CreateThread( NULL, // no security attributes 0, // use default stack size ThreadFunc, // thread function – The function that executes as a
thread &dwThrdParam, // argument to thread function 0, // use default creation flags &dwThreadId); // returns the thread identifier // Check the return value for success. if (hThread == NULL) {
cout << " Thread creation failed" << endl;return 0;
}int x=0;while ( x < 50){
cout << "i am parent ---" << x << endl;x ++;
} CloseHandle( hThread );}
Creating a Thread inMS Platforms
Thread executes as aSeparate process
DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
char szMsg[80];
unsigned int x; x = 0;
while (x < 50)
{
cout << x << " --- Thread is being executed" << endl;
x++;
}
return 0;
} This function runs as a
concurrent thread.
int main(){//fork another processInt pid = fork();If (pid < 0) { //error occurred
fprintf(stderr, “Fork failed”);exit(-1); }
else (if pid ==0) {execlp(“/bin/ls”,”ls”); }
Else {wait(NULL); //wait till child process is
//completeprintf(“Child Complete”);
Exit(0):}
int main(){//fork another processInt pid = fork();If (pid < 0) {
fprintf(stderr, “Fork failed”);exit(-1); }
else (if pid ==0) {execlp(“/bin/ls”,”ls”); }
Else {wait(NULL);printf(“Child Complete”);
Exit(0):}
PARENT CHILD
NOTE: Same code runs for both child and parent (Identified by pid returned by fork()
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
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
Lets do an example of Direct message passing#include <windows.h>#include <iostream.h>void main(void){
char str[100];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 closeSendMessage(hWnd,WM_CLOSE,0,0L);//Guess what would happen if SetWindowText(hWnd,"Calc2000"); is used //instead of SendMessage
}
Its simple to execute this program:Program asks for the window caption
Enter the caption e.g. CalculatorCalculator program must be runningThis program will simply close the
program
Interesting Program
#include <windows.h>
#include <iostream.h>
void main(void)
{
FILETIME a,b,c,d;
HANDLE h =OpenProcess(PROCESS_ALL_ACCESS,TRUE,1920);
GetProcessTimes(h,&a,&b,&c,&d); //READ THE DOCUMENTATION
SYSTEMTIME aa,bb,cc,dd;
FileTimeToSystemTime(&c,&cc);
cout << cc.wMilliseconds <<endl;
}
Read the documentation carefully (ref msdn) and guess what is this program doing!A QUIZ MIGHT BE COMING YOUR WAY!!!!!!!!!!!!!!!!!!
Indirect Communication 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 CommunicationOperations
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
Indirect Communication 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.