Inter Process
Communication
Mrs. Sowmya Jyothi, Faculty, Mangalore
References:
The Design of the UNIX Operating System
by Maurice J. Bach
•InterProcessCommunicationisa
mechanismwherebyoneprocess
communicateswithanotherprocess.
•Communicationmeansexchangeof
data.
•Thedirectionscanbeunidirectional,
bidirectionalormultidirectional.
Inter Process Communication
ProcessTracing:-
Theunixsystemprovidesaprimitiveformofinterprocess
communicationfortracingprocesses,usefulfordebugging.
Adebuggerprocesstakesasaninputaprocesstobetraced
andcontrolsitsexecutionwiththePTRACESYSTEM
CALL,settingandclearingbreakpoints,andreadingand
writingdatainitsvirtualaddressspace.
Processtracingthusconsistsofsynchronizationofthe
debuggerprocessandthetracedprocessandcontrolling
theexecutionofthetracedprocess.
Theptrace() system call provides tracing and debugging
facilities.
The syntax of the ptracesystem call is
ptrace(cmd, pid, addr, data)
where cmdspecifies various commandssuch as
reading data, writing data, resuming execution
and so on,
pidis the process ID of the traced process,
addris the Virtual address to be read or written
in the child process,
and data is an integer value to be written.
MESSAGES
There are four system calls for messages:
msggetreturns(andpossiblycreates)amessage
descriptor.
msgctlhasoptionstosetandreturnparameters
associatedwithamessagedescriptorandan
optiontoremovedescriptors.
msgsndsendsamessage.
msgrcvreceivesamessage.
MESSAGES:-
There are 4 system calls for messages.
1.msgget-Inordertouseamessagequeue,ithastobe
createdfirst.Inordertocreateanewmessagequeue,or
accessanexistingqueue,themsgget()systemcallisused.
Returnsamessagedescriptorthatdesignatesamessage
queueforuseinothersystemcalls.
Syntax:msgqid=msgget(key,flag)
Wheremsgqidisthedescriptorreturnedbythecall.
TheKernelstoresmessagesonalinkedlist(queue)per
descriptoranditusesmsgqidasanindexintoanarray
ofmessagequeueheaders.
The queue structure contains the following fields, in
addition to the common fields:
1.Pointers to the first and last messages on a linked
list.
2.The number of messages and total number of data
bytes on the linked list.
3.The maximum number of bytes of data that can be
on the linked list.
4.The process IDs of the last processes to send and
receive messages.
5.Time stamps of the lastmsgsnd,msgrcv,
andmsgctloperations.
2. Shared Memory
1.Allows two or more processes to share
some memory segments
2.With some control over read/write
permissions
2. Shared Memory:-
Processes can communicate directly with each other by
sharing parts of their virtual address space and then reading
and writing data stored in the shared memory.
Sharing the part of virtual memory and reading to and
writing from it, is another way for the processes to
communicate. The system calls are:
1.shmgetcreates a new region of shared memory or returns
an existing one.
2.shmatlogically attaches a regionto the virtual address
space of a process.
3.shmdtlogically detaches a region.
4.shmctlmanipulates the parameters associated with the
region.
1. The shmget system call creates a new region of shared
memory or returns an existing one.
Syntax: shmid=shmget(key, size, flag)
key: Identical to msgget
size: Size of the memory segment if a creating a new
segment, can use 0 if getting an existing segment. Is the
number of bytes in the region.
flags: Identical to msgget.
Just like msgget, shmget could:
1.Create a new memory segment.
2.Return the queue ID of an existing segment.
3.Signal an error.
2. A process attaches a shared memory region to its virtual
address space with the shmatsystem call.
Syntax: virtaddr= shmat(id, addr, flags)
Before the memory can be used, it must be attached by the
process and assigned a memory address.
1.shmid: id of shared memory segmentas returned by
shmget.
2.addr: If 0, address is selected by kernel (recommended).
Otherwise, segment is attached at supplied address.
3.flags: Control behavior of attachment. SHM_RDONLY makes
the segment read-only.
4.Returns a pointer to the shared memory segment.
3. Detaching Shared Memory Segments: Detaches
the shared memory segment located at the
address indicated by shmaddr.
TheSyntaxforshmdt:shmdt(addr);
whereaddristhevirtualaddressreturnedbya
priorshmatcall.
4. shmctlmanipulates the parameters associated
with the region.
Syntax ofshmctl
shmctl(id, cmd, shmstatbuf);
which is similar tomsgctl
3.Semaphores:-
Asemaphoreisaresourcethatcontainsanintegervalue,
andallowsprocessestosynchronizebytestingandsetting
thisvalueinasingleatomicoperation.
A SEMAPHORE is UNIX System V consists of the following
elements:
1.The valueof the semaphore.
2.The process ID of the last process to manipulate the
semaphore.
3.The number of processes waiting for semaphore value
to increase.
4.The number of processes waiting for the semaphore
value to equal 0.
The system calls are:
1.semgetto create and gain access to a set of
semaphores.
2.semctlto do various control operations on the set.
3.semopto manipulate the values of semaphores.
1.Creationofasemaphoreset-semgetcreatesanarray
ofsemaphores:
id = semget(key, count, flag);
Flag-usedtodefineaccesspermissionmodeandafewoptions
Thekernelallocatesanentrythatpointstoanarrayof
semaphorestructurewithcountelements
Sockets
Toprovidecommonmethodsforinterprocess
communicationandtoallowuseofsophisticated
networkprotocols,theBSDsystemprovidesa
mechanismknownassockets.
A socket is an IPC channel with generated
endpoints.
Intended as building block for communication
Endpoints established by the source and
destination processes
Thekernelstructureconsistsofthreeparts:
thesocketlayer,theprotocollayer,andthe
devicelayer,asshowninthefigure:
5.Thelistensystemcallspecifiesthe
maximumqueuelength:
listen(sd,qlength);
6.Theacceptcallreceivesincomingrequests
foraconnectiontoaserverprocess:
nsd=accept(sd,address,addrlen);
whereaddresspoints to a user data array that
the kernel fills with the return address of the
connecting client, and
addrlenindicates the size of the user array.
7. Thesendandrecvsystem calls transmit data over
a connected socket:
count = send(sd, msg, length, flags);
count = recv(sd, buf, length, flags);
The datagram versions of these system
calls,sendtoandrecvfromhave additional parameters
for addresses.
Processes can also usereadandwritesystem calls on
stream (virtual circuit) sockets after the connection is
set up.
8. Theshutdownsystem call closes a socket
connection:
shutdown(sd, mode);
wheremodeindicates whether the sending side, the
receiving side, or both sidesno longer allow data
transmission. After this call, the socket descriptors are
still intact.
9. Theclosesystem call frees the socket descriptor.
10.Thegetsocketnamesystemcallgetsthe
name ofasocketbound bya
previousbindcall:
getsocketname(sd, name, length);
Thegetsockoptandsetsockoptcalls retrieve
and set various options associated with the
socket, according to the communications domain
and protocol of the socket.