Inter Process Communication PPT

sowmyajyothi16 130 views 39 slides Feb 01, 2021
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

Inter Process Communication by
Mrs. Sowmya Jyothi


Slide Content

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.

Thedebuggertakesasaninputachildprocess,
whichinvolvestheptracesystemcallandasa
result,thekernelsetsatracebitinthechild
processtableentry.Thechildnowexecsthe
programbeingtraced.
Thetracebitissetinitsprocesstableentry,the
childawakenstheparentfromitssleepinthewait
systemcall,entersaspecialtracestatesimilarto
thesleepstateanddoesacontextswitch.
Whenthetracedprocessawakensthedebugger,
thedebuggerreturnsfromwait,readsuserinput
commands,andconvertsthemtoaseriesofptrace
callstocontrolthechild(traced)process.

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.

Unix Versions

SystemVIPC:-
TheUnixsystemIPCpackageconsistsof3
mechanisms.
1.Messagesallowprocessestosendformatted
datastreamstoarbitraryprocesses.
2.Sharedmemoryallowprocessestoshareparts
oftheirvirtualaddressspace
3.Semaphoresallowprocessestosynchronize
execution.

Commonpropertiessharedbythemechanismsare:
1.Eachmechanismcontainsatablewhoseentries
describeallinstancesofthemechanism.
2.Eachentrycontainsanumerickey.
3.Eachmechanismcontainsa“get”systemcalltocreatea
newentryortoretrieveanexistingoneandparameters
tothecallsincludeakeyandflags..
4.ForeachIPCmechanism,thekernelusesthe
followingformulatofindtheindexintothetableof
datastructuresfromthedescriptor:
Index=descriptormodulo(numberofentriesinthe
table).

5.EachIPCentryhasapermissionsstructurethat
includestheUserIDandgroupIDoftheprocessthat
createdtheentry,auserandgroupIDsetbythe
“control”systemcallandasetofread-write-execute
permissionsforuser,group,andotherssimilartothe
filepermissionmodes.
6.Eachentrycontainsotherstatusinformationsuchas
theprocessIDofthelastprocesstoupdatetheentry
(sendamessage,receiveamessage,attachshared
memoryandsoon)andthetimeoflastaccessor
update.
7.Eachmechanismcontainsa“control”systemcallto
querystatusofanentry,tosetstatusinformation,or
toremovetheentryfromthesystem.

MESSAGES
There are four system calls for messages:
msggetreturns(andpossiblycreates)amessage
descriptor.
msgctlhasoptionstosetandreturnparameters
associatedwithamessagedescriptorandan
optiontoremovedescriptors.
msgsndsendsamessage.
msgrcvreceivesamessage.

1.MessagePassing
IPCfacilityprovidestwooperationsforfixedor
variablesizedmessage:
◦send(message)
◦receive(message)
IfprocessesPandQwishtocommunicate,they
needto:
◦establishacommunicationlink
◦exchangemessagesviasendandreceive
Viewofcommunicationlink:
◦physical(hardwarebus,networklinks,shared
memory,etc.)
◦logical(syntaxandsemantics,abstractions)
14

AMESSAGEQUEUEisaqueueontowhichmessages
canbeplaced.
AMESSAGEiscomposedofamessagetype(whichisa
number),andmessagedata.
AmessagequeuecanbeeitherPRIVATE,ORPUBLIC.
IfitISPRIVATE,itcanbeaccessedonlybyitscreating
processorchildprocessesofthatcreator.
Ifit'sPUBLIC,itcanbeaccessedbyanyprocessthat
knowsthequeue'skey.
Severalprocessesmaywritemessagesontoa
messagequeue,orreadmessagesfromthequeue.
Messagesmaybereadbytype,andthusnothaveto
bereadinaFIFOorderasisthecasewithpipes.

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.Aprocessusesthemsgsndsystemcallto
sendamessage.
msgsnd(msgqid,msg,count,flag);
1.Wheremsgqidisthedescriptorofamessage
queuetypicallyreturnedbyamsggetcall,
2.Msgisapointertoastructureconsistingofa
user-chosenintegertypeandacharacter
array,
3.Countgivesthesizeofthedataarray
4.Flagspecifiestheactionthekernelshould
takeifitrunsoutofinternalbufferspace.
flagsspecifyinghowtosendthemessage.

3. Reading A Message From The Queue -msgrcv()
count=msgrcv(id,msg,maxcount,type,flag)
1.Whereid-isthemessagedescriptor
2.Msg-istheaddressofauserstructuretocontain
thereceivedmessage
3.Maxcount-isthesizeofthedataarrayinmsg,
4.Type-specifiesthemessagetypetheuserwantsto
read
5.Flag-specifieswhatthekernelshoulddoif
messagesareonthequeue.
6.Thereturnvaluecount,isthenumberofbytes
returnedtotheuser.

4.APROCESScanquerythestatusofamessage
descriptor,setitsstatusandremoveamessage
descriptorwiththemsgctlsystemcall.
Thesyntaxofthecallismsgctl(id,cmd,mstatbuf)
Whereid-messagedescriptor
cmdspecifiesthetypeofcommand
mstatbufistheaddressofauserdatastructurethat
willcontaincontrolparametersortheresultofquery.

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

2.Processesmanipulatesemaphores with
thesemopsystemcall:
oldval=semop(id,oplist,count);
whereoplistisapointertoanarrayofsemaphore
operations,and
countisthesizeofthearray.
Thereturnvalue,oldval,isthevalueofthelast
semaphoreoperatedoninthesetbeforethe
operationwasdone.
Theformatofeachelementofoplistis,
•Thesemaphorenumberidentifyingthesemaphore
arrayentrybeingoperatedon
•Theoperation
•Flags

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:

1.TheSocketlayerprovidestheinterface
betweenthesystemcallsandthelowerlayers.
2.TheProtocollayercontainstheprotocol
modulesusedforcommunication,and
3.TheDevicelayercontainsthedevicedrivers
thatcontrolthenetworkdevices.
Socketsthatsharecommoncommunications
properties,suchasnamingconventionsand
protocoladdressformats,aregrouped
intodomains.

1.Thesocketsystemcallestablishesthe
endpointofacommunicationslink.
sd= socket(format, type, protocol);
where format specifies the
communicationsdomain,
typeindicatesthetypeof
communicationoverthesocket,
andprotocolindicatesaparticular
protocoltocontrolthecommunication.

2.Theclosesystemcall-closessockets.
3.Thebindsystemcallassociatesaname
withthesocketdescriptor.
Bindingpreparesasocketforusebyaprocess
bind(sd,address,length);
whereaddresspointstoastructurethat
specifiesanidentifierspecifictothe
communications domainandprotocol
specifiedinthesocketsystemcall.
lengthisthelengthoftheaddressstructure.

4.Theconnectsystemcallrequests
thatthekernelmakeaconnectionto
anexistingsocket:
connect(sd,address,length);
whereaddressistheaddressofthe
targetsocket.Bothsocketsmustuse
thesamecommunicationsdomainand
protocol.

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.
Tags