You are on page 1of 90

Silberschatz, Galvin and Gagne 2009

Operating System Concepts 8


th
Edition,
Processes
Modified by M.Rebaudengo - 2010
3.2
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Concept
! An Operating System executes a variety of programs:
" Batch system jobs
" Time-shared systems user programs or tasks.
3.3
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Definitions of process
! A program in execution
! An istance of a program running on a computer
! A unit of activity characterized by the execution of a sequence of
instructions, a current state and an associated set of system resources.

! The terms job and process can be used almost interchangeably


3.4
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
A process includes
! text section (program code)
! program counter
! stack (temporary data: function parameters, return addresses and
local variables)
! data section (global variables)
! heap (dynamically allocated memory).
3.5
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process in Memory
3.6
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Processes and programs
! The program is a passive entity; whereas the process is the active entity
! Two processes may be associated to the same programs
" e.g., two users may be running two copies of the Web browser program
" each of these is a separate process
" the text sections are equivalent
" the data, heap, stack sections vary.
3.7
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
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 nished execution.
! Only one process may be running on any processor at any instant.
! Many processes may be ready and waiting.
3.8
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Diagram of Process State
5-state model
timeout
3.9
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Control Block (PCB)
! The most important data structure in an OS
! Information associated with each process:
" Process identier: unique numeric identier of the
process
" Process state: new, ready, running, waiting,
terminated
" Program counter: the address of the next instruction
to be executed
" CPU registers: along with the program counter, this
state information must be saved when an interrupt
occurs
3.10
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Control Block (PCB)
" CPU scheduling information: needed by the OS to perform its
scheduling function
! process priority
! scheduling-related information (e.g., the amount of time that the
process has been waiting and the amount of time that the process
executed the last time it was running)
! pointers to scheduling queues
" Memory-management information:
! mapping between virtual and physical memory locations.
" Accounting information: used for billing purposes and performance
measurements
! the amount of CPU and real time used
! main memory storage occupancy
" I/O status information: resources allocated to the process
! HW units: the list of I/O devices allocated to the process
! the list of open les.
3.11
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Control Block (PCB)
3.12
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
CPU Switch From Process to Process
3.13
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Scheduling
! The objective of multiprogramming is to have some process
running at all times, to maximize the CPU utilization
! The objective of time sharing is to switch the CPU among
processes so frequently that users can interact with each
program while it is running
! To meet these objectives, the process scheduler selects an
available process for program execution on the CPU.
3.14
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
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
! Each device has its own device queue
! Processes migrate among the various queues.
3.15
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Scheduling Queues
! The queues are generally stored as linked lists
! The header contains pointers to the rst and the nal PCB in the list.
3.16
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Representation of Process Scheduling
! Queueing diagram:
" each rectangular box represents a queue
! two types of queues are present:
the ready queue
a set of device queues
" the circles represent the resources that serve the queues
" the arrows represent the ow of processes in the system.
3.17
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Representation of Process Scheduling
3.18
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Schedulers
! Long-term scheduler (or job scheduler) selects which processes should
be brought into the ready queue
" in a batch system active processes are spooled to a mass-storage
device, where they are kept for later execution
" the long-term scheduler selects processes from this pool and loads
them into memory for execution
! Short-term scheduler (or CPU scheduler) selects which process should
be executed next and allocates CPU:
" it selects from processes that are ready to execute.
3.19
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Schedulers (cont.)
! Frequency of execution:
" Short-term scheduler is invoked very frequently (once ever 100
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
(the number of processes in memory)
" The long-term scheduler may need to be invoked only when a
process leaves the system. Because of the longer interval
between executions, the long-term scheduler can afford to take
more time to decide which process should be selected for
execution.
3.20
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Schedulers (cont.)
! 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
! A system with the best performance needs to have a balanced
combination of I/O-bound and CPU-bound processes:
" if all processes are I/O-bound, the ready queue will almost
always be empty and the short-term scheduler will have little to
do
" if all processes are CPU-bound, the I/O queues will almost
always be empty.
3.21
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
OSs without long-term scheduler
! The long-term scheduler may be absent or minimal
" Time-sharing systems often have no long-term scheduler but simply put
every new process in memory for the short-term scheduler
" the stability of these systems depends either on a physical limitation
(the number of available terminals) or on the self-adjusting nature of
users (if the performance declines to unacceptable levels on a
multiusers system, some user will simply quit, or on a multitasking
system, some tasks will be killed).
3.22
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Addition of Medium Term Scheduling
! Some OSs may introduce an additional, intermediate level of scheduling: a
medium-term scheduler:
" it could be advantageous to remove processes from memory and thus
reduce the level of multiprogramming
" later, the process could be reintroduced in memory, and its execution
can be continued where it left off.
" This scheme is called swapping.
3.23
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Addition of Medium Term Scheduling
! A process is swapped by the medium-term scheduling
! Swapping may be necessary to improve the process mix of I/O-bound and
CPU-bound processes or because a change in memory requirements
causes that some memory has to be freed up.
3.24
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
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 via a context
switch
! Context of a process is represented in the PCB
! Context-switch time is overhead; the system does no useful work while
switching
! Time dependent on hardware support:
" it depends on the memory speed, the number of registers that must be
copied, and the existence of special instructions (e.g., a single
instruction to load or store all registers)
" typical speeds are a few milliseconds.
3.25
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Operations on processes
! Most systems provide a mechanism to create and terminate processes.
3.26
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Creation
! A process may create several new processes, via a create-process system
call, during the course of execution:
" the creating process is called a parent process
" the new processes are called the children processes of that parent.
! Parent process create children processes, which, in turn create other
processes, forming a tree of processes
! Generally, process is identied and managed via a unique process
identier (pid), which is an integer number.
3.27
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
A tree of processes on a typical Solaris
3.28
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Creation (cont.)
! When a process creates a new process the following possibilities
exist in terms of:
" Resource sharing
! Parent and children share all resources
! Children share subset of parents resources
! Parent and child share no resources
" Execution
! Parent and children execute concurrently
! Parent waits until all of its children have terminated.
3.29
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Termination
! Process 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)
" Child has exceeded allocated resources
" Task assigned to child is no longer required
" If parent is exiting
! Some operating system do not allow child to continue if its
parent terminates
All children terminated - cascading termination
3.30
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
UNIX examples
! Process creation:
" fork system call creates new process
" exec system call used after a fork to replace the process memory
space with a new program
3.31
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
fork()
! int fork(void);
" the return code is zero for the new (child) process
" the nonzero and positive process identier of the child is returned to the
parent process
" a negative value represents an error in the process creation
! The two processes share the same program code
! The new process consists of a copy of the address space of the original
process
! Both processes share the same value of the Program Counter Register:
" Both processes continue the execution at the instruction after the
fork()
3.32
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example
int main()
{
pid_t pid, fork_return;
/* fork another process */
fork_return = fork();
if (fork_return < 0) { /* error occurred */
printf("Fork Failed");
}
else if (fork_return == 0) { /* child process */
{
pid = getpid();
printf("I'm the children! pid: %d\n", pid);
/* system call getpid() returns the pid of the calling process */
}
}
else { /* parent process */
printf("I'm the father! the child pid is %d\n", fork_return);
}
}
3.33
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise
! Including the initial parent process, how many processes are created by the
following program?


int main()
{
/* fork a child process */
fork();

/* fork another process */
fork();

/* and fork another */
fork();

}

3.34
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise
! What is the output of this program?


int main()
{
pid_t pid;

/* fork a child process */
pid = fork();

if (pid==0)
while (1)
printf (1);
else
while (1)
printf (2);

}

3.35
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
exec()
! Typically the exec() system call is used after a fork() system call by one of the
two processes to replace the process's memory space with a new program.
! The exec() system call loads a binary le into memory (destroying the memory
image of the program containing the exec() system call) and starts its execution.
! As a new process is not created, the PID does not change across an exec(), but the
data, heap and stack of the calling program are replaced by those of the new
program
! There is a family of different exec functions
! Syntax:
" int execlp(char * path, char *arg0, char *arg1, ..., char
*argN, (char *) 0);
" The path argument species the path name of the le to execute as the new
process image.
" Arguments arg0 through argn are a list of pointers to arguments to be passed
to the new process image.
" The exec functions do not normally return to the calling process. If an exec
function returns, an error occurred and the return value is 1.
3.36
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
wait()
! wait() is a system call that suspends the execution of the parent process
while the child executes
! When the child process terminates, it returns an exit status to the operating
system, which is then returned to the waiting parent process. The parent
process then resumes execution.
! Syntax:
" int wait (int *status);
" status is the address of the variable containing the exit status of the
child process
" the return value is the pid of the terminated process (or a negative
number in case of error).
3.37
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Exercise
! Using the following program, what is the output at line A and line B?

int value = 5;
int main()
{
pid_t pid;

pid = fork();

if (pid == 0) { /* child process */
{ value += 15;
printf (CHILD: value =%d\n, value); /* LINE A */
return 0;
}
else { /* parent process */
wait (NULL);
printf (PARENT: value =%d\n, value); /* LINE B */
}
}

3.38
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
exit()
! exit() is a system call that terminates the process, asking the operating
system to delete the process
! The process may return an exit status value to its parent process (via the
wait system call)
! All the resources of the process are deallocated by the operating system.
! Syntax:
" void exit(int status);
" the exit() function causes normal process termination and the value
of status & 0377 is returned to the parent process
" the exit() function does not return.
3.39
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Process Creation using fork()
3.40
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
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", "-l", NULL);
printf("Failed Exec \n");
exit(-1);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
3.41
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Parent termination
! In Unix, if the parent terminates, all its children have assigned as their new
parent the init process.
! Thus the children still have a parent to collect their status and execution
statistics.
3.42
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Independent vs. cooperating processes
! A process is independent if it cannot be affected by the other processes
executing in the system
! A process is cooperating if it can affect or be affected by the other
processes executing in the systems
" any process that shares data with other processes is a cooperating
process.
3.43
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Cooperating processes
! Reasons for cooperating processes:
" Information sharing
" Computation speedup
" Modularity
" Convenience
3.44
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Interprocess Communication
! Cooperating processes need interprocess communication
(IPC) mechanism that will allow them to exchange data and
information
! Two models of IPC
" Shared memory:
! a region of memory that is shared by cooperating
processes is established
! processes can exchange information by reading and
writing data to the shared region
" Message passing:
! communication takes place by means of messages
exchanged between the cooperating processes.
3.45
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Communications Models
Shared memory Message passing
3.46
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Examples of IPC Systems - POSIX
! POSIX Shared Memory
" Process rst creates shared memory segment
id = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR);
" Process wanting access to that shared memory must attach to it
shared_memory = (char *) shmat(id, NULL, 0);
" Now the process could write to the shared memory
sprintf(shared_memory, "Writing to shared memory");
" When done a process can detach the shared memory from its address
space
shmdt(shared_memory);
" destroy a segment (only after that all the attached processes are
detached)
shmctl(id, IPC_RMID, 0);
3.47
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Shared memory and fork/exec/exit
! After a fork() the child inherits all the attached shared memory segments
! After an exec() all the attached shared memory segments are detached
(not destroyed)
! When exit() all attached shared memory are detached (not destroyed)
3.48
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example
#define SHMSZ 27
main ()
{
int shmid;
pid_t pid;

char c;
char *shm, *s;

if ( (shmid = shmget (IPC PRIVATE, SHMSZ, IPC_CREAT | 0666)) < 0)) exit (1);

if (( shm = shmat (shmid,NULL,0) ) == (char *) 1) exit (1) ;

/* fork another process */
pid = fork();
3.49
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Example (cont.)
if (pid < 0) exit(-1);

else if (pid == 0) { /* child process */
while (shm[SHMSZ-2] != 'z')
sleep(1);

for (s = shm; *s != NULL ; s++)
putchar(*s);
putchar('\n');

*shm = '*';
exit (0);
}
else { /* parent process */
s = shm;

for (c = 'a' ; c <= 'z' ; c++)
*s++ = c;

*s = NULL;

while (* shm != '*')
sleep(1);

shmdt(shm);
shmctl(shmid, IPC_RMID, 0);
exit(0);
}

}
3.50
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Shared memory vs. Message Passing
! Message Passing is useful for exchanging smaller amounts of data
! Message Passing is easier to implement for intercomputer communication
! Shared memory is faster than message passing, as ...
" message passing systems are typically implemented using system calls
and thus require the kernel intervention
" in shared-memory systems, systems calls are required only to establish
shared-memory regions and all accesses are treated as classical
memoy accesses and no assistance from the kernel is required.
3.51
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Producer-Consumer Problem
! Paradigm for cooperating processes
! producer process produces information that is consumed
by a consumer process later
! Since a producer and a consumer can work at different
speeds, a buffer is needed where the producer can
temporarily store data that can be retrieved by the consumer
at a more appropriate speed
buffer
Producer Consumer
3.52
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Producer-Consumer Problem
! Unbounded-buffer places no practical limit on the size of the
buffer
" the consumer may have to wait for new items,
" the producer can always produce new items
! Bounded-buffer assumes that there is a xed buffer size
" the consumer must wait if the buffer is empty
" the producer must wait if the buffer is full.
3.53
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Bounded-Buffer Shared-Memory Solution
! Data shared by the producer and the consumer
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;

item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
! Circular Array:
" in points to the next free position in the buffer
" out points to the rst full position in the buffer
" the buffer is empty if in == out
" the buffer is full if ((in+1)%BUFFER_SIZE) == out)
3.54
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Bounded-Buffer Producer
item nextProduced;
while (true) {
/* Produce an item */
while ((in + 1) % BUFFER_SIZE) == out)
; /* do nothing -- no free buffers */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER SIZE;
}

3.55
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Bounded Buffer Consumer
item nextConsumed;

while (true) {
while (in == out)
; /* do nothing -- nothing to consume */

/* remove an item from the buffer */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
return nextConsumed;
}
3.56
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Interprocess Communication Message Passing
! Message Passing provides a mechanism for processes to communicate
and to synchronize their actions without sharing the same address space
! IPC facility provides two operations:
" send(message)
" receive(message)
! If P and Q wish to communicate, they need to:
" establish a communication link between them
" exchange messages via send/receive
! Implementation of communication link
" physical (e.g., shared memory, hardware bus)
" logical (e.g., logical properties)
3.57
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Direct Communication
! Each process that wants to communicate must explicitly name the recipient
or sender of the communication:
" 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
3.58
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Indirect Communication
! Messages are directed and received from mailboxes (also known as
ports in Mach)
! A mailbox can be viewed abstractly as an object into which messages
can be placed by processes and from which messages can be removed
" 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
3.59
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Mailbox
! A mailbox can be owned either by a process or by the operating system.
3.60
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Mailbox owned by a process
! If the mailbox is owned by a process (that is the mailbox is part of the
address space of the process) then we distinguish between the owner
(which can only receive messages through this mailbox) and the user
(which can only send messages to the mailbox)
! Since each mailbox has a unique owner, there can be no confusion about
which process should receive a message sent to this mailbox
! When a process that owns a mailbox terminates, the mailbox dissapears
and any process that subsequently sends a message to this mailbox must
be notied that the mailbox no longer exists.

3.61
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Mailbox owned by the OS
! A mailbox that is owned by the operating system is independent and is not
attached to any particular process. The operaring system must provide a
mechanism that allows a process to do the following operations:
" create a new mailbox
" send and receive messages through mailbox
" destroy a mailbox.
! The process that creates a new mailbox is the initial owner of the mailbox
that can receive messages through this mailbox
! The ownership and receiving privilege may be passed to other processes
through appropriate system calls. This provision could result in multiple
receivers for each mailbox.
3.62
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Indirect Communication
! Primitives are dened as:
send(A, message) send a message to mailbox A
receive(A, message) receive a message from mailbox A
3.63
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Synchronization
! Message passing may be either blocking or non-blocking
! Blocking is considered synchronous
" Blocking send: the sender blocks until the message is
received
" Blocking receive: the receiver blocks until a message is
available
! Non-blocking is considered asynchronous
" Non-blocking send: the sender sends the message and
continues
" Non-blocking receive: the receiver receives a valid message
or null
3.64
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Possible combinations
! blocking send and blocking receive (rendez-vous)
! non blocking send and blocking receive
! non blockind send and non blocking receive.
3.65
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Buffering
! Queue of messages attached to the link; implemented in one of three
ways
1. Zero capacity 0 messages#
Sender must wait for receiver (rendez-vous)
2. Bounded capacity nite length of n messages#
Sender must wait if link full
3. Unbounded capacity innite length
Sender never waits
3.66
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Message queue in UNIX
! A message queue is an ordered list of messages held in the kernel, which
processes can access.
! A process can create a new message queue, or it can connect to an
existing one.
msgrcv()
msgsnd() 7 2 4
d
a
t
a

d
a
t
a

d
a
t
a

message queue
key = 1841
3.67
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Queue Properties
! A queue has a unique key, xed size, ownership, and access modes.

! It must be explicitly deleted.

! A message consists of a type (a positive integer), and data (characters).


3.68
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Create/access a queue
! A process can create a new Message Queue (MQ), or it can connect to an existing
one
! int msgget(key_t key, int msgflg)
" key:
! unique key (name) of the message queue. Every other process that wants to
connect to this queue will have to use the same key.
! IPC_PRIVATE: special value for key: it ignores msgg and returns ID of
message queue on success
" msgg:
! IPC_CREAT: create a new MQ unless it already exists
! IPC_EXCL: call of msgget() will fail if MQ already exists
" returns:
! ID of message queue (on success)
! -1 (otherwise)
3.69
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
msgsnd()
! To send a message
" int msgsnd(int msqid, const void *msgp, size_t msgsz,
int msgflg);
! Arguments
" msqid is the message queue identier returned by msgget()
" msgp is a pointer to the data to put on the queue
" msgsz is the size in bytes of the data to add to the queue
" msgflg set to IPC_WAIT to avoid blocking; 0 otherwise.
3.70
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
msgsnd(msqID, ptr, nbytes, flag);
message
queue ID
type: a
positive
integer
data (characters)
no. of bytes
of user data
set to IPC_WAIT
to avoid blocking;
0 otherwise
create struct before
calling msgsnd()
3.71
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Data Structure
! ptr must point to a struct of the form:

#define MSGLEN 100


/* any positive integer */

struct msg {
long mtype; /* positive message type */
char mtext[MSGLEN]; /* for message data */
};

! Cast it to struct msgbuf in msgsnd()
" struct msgbuf {
long mtype;
char mtext[1];
};
" msgsnd(msqID, (struct msgbuf *) ptr, nbytes, flag);
3.72
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Blocking
! When IPC_WAIT is not set.
! If the queue is full, then msgsnd() will block until:

1. the queue gets less full (i.e. a message is removed)

2. the queue is deleted

3. a signal is raised.
3.73
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Non-blocking
! IPC_WAIT is set

! If the queue is full then msgsnd() returns immediately


3.74
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Receiving from the Queue
! int msgrcv(int msqid, void *msgp, size_t msgsz,
long msgtyp, int msgflg);
" msgtyp corresponds to the mtype set in the msgsnd()
! msgtyp effect on msgrcv()
" Zero - Retrieve the next message on the queue, regardless of its
mtype.
" Positive - Get the next message with a mtype equal to the
specied msgtyp
" Negative - Retrieve the rst message on the queue whose
mtype eld is less than or equal to the absolute value of the
msgtyp argument.
3.75
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
sz= msgrcv(msqID, ptr, nbytes, type, flag);
message
queue ID
total no. of bytes
of message field
set to IPC_WAIT
to avoid blocking;
MSG_NOERROR
also possible
type
size of
actual data
type of
message wanted
Receiving from the Queue
3.76
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Message Data Length
! If the message data length > nbytes then:

" if MSG_NOERROR is set then the message is removed but


truncated.

" if MSG_NOERROR is not set then the message is left on the


queue
3.77
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
type Values
Type value Message returned#

0 rst message#

> 0 rst message whose type == type

< 0 rst message whose type is the


lowest value <= abs(type)
3.78
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Examples
! if type == 5, remove second message
! if type == -100 then three calls to msgrcv() remove messages 1, 5, 79
" can use this to code priorities
" 1 is highest, ... 100 is lowest
79 5 1
d
a
t
a

d
a
t
a

d
a
t
a

3.79
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Blocking
! When IPC_WAIT is not set.
! If the queue is empty, then msgrcv() will block until:

1. the queue gets some messages#

2. the queue is deleted

3. a signal is raised

3.80
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Non-blocking
! IPC_WAIT is set

! If the queue is empty then msgrcv() returns immediately, and errno is


assigned ENOMSG
3.81
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Destroying a message queue
! There are two ways of destroying a queue:
" Use the Unix command ipcs to get a list of dened message queues,
then use the command ipcrm to delete the queue.
" Write a program.
3.82
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
! Message Control Operation
! int msgctl(int msqid, int cmd, struct msqid_ds *buf);
! Arguments
" msqid is the queue identier obtained from msgget()
" cmd tells msgctl() how to behave: IPC_RMID is used to destroy the
message queue
" Buf argument can be set to NULL for the purposes of IPC_RMID.
msgctl()
3.83
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Communication in Client-Server systems
! Sockets
! RPC (Remote Procedure Calls)
3.84
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Sockets
! Two processes communicating over a network employ a pair of sockets.
! A socket is an IP ADDRSS + a PORT NUMBER
! They usually work as a client server connection: server listens for
connections from clients
! Specic services have specic ports (telnet: port 23, ftp: port 21, web: port
80)
! All ports below 1024 are used for standard services
3.85
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Sockets
! Client initiates a connection request on a known port (e.g. 80)
! The server assigns the client a port (> 1024)
3.86
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Socket Communication
3.87
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Remote Procedure Calls
! Remote procedure call (RPC) abstracts procedure calls between processes
on networked systems.
! Stubs client-side proxy for the actual procedure on the server.
! The client-side stub locates the server and marshalls the parameters.
! The server-side stub receives this message, unpacks the marshalled
parameters, and peforms the procedure on the server.
3.88
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Marshalling Parameters
Marshalling (similar to serialization) is the process of transforming the
memory representation of an object to a data format suitable for storage or
transmission.
3.89
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Execution of RPC
3.90
Silberschatz, Galvin and Gagne 2009
Operating System Concepts 8
th
Edition
Remote Method Invocation
! Remote Method Invocation (RMI) is a Java mechanism similar to RPCs.
! RMI allows a Java program on one machine to invoke a method on a
remote object.

You might also like