Linux Inter-Process Communication: Message Queues, Shared Memory, and Sockets - Prof. Pras, Study notes of Operating Systems

An overview of inter-process communication (ipc) mechanisms in linux, including message queues, shared memory, and sockets. Ipc enables processes to exchange data and synchronize their execution. Message queues allow for message-passing between processes, while shared memory allows for direct access to a shared region of memory. Sockets enable communication between processes over a network. The basics of each ipc mechanism, including syntax and usage.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-rq3
koofers-user-rq3 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1


What is a process ?
What is process scheduling ?
What are the common operations on processes ?
How to conduct process-level communication ?
How to conduct client-server communication ?


Process
is a program in execution
is an instance of a computer program being sequentially executed
process execution must progress in sequential fashion
process is also called a job
Program Vs. process
program is a passive entity; process is an active entity
program only contains text; process is associated with code, data, PC,
heap, stack, registers, and other information
program becomes a process when an executable file is loaded into
memory
same program executed multiple times will correspond to different
process each time




During execution, the process may be in one of the following
states
new – process is being created
running – instructions are being executed
waiting – waiting for some event to occur
ready – waiting to be assigned a processor
terminated – process has finished execution
Each processor can only run one process at a instant.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Linux Inter-Process Communication: Message Queues, Shared Memory, and Sockets - Prof. Pras and more Study notes Operating Systems in PDF only on Docsity!



    

 



^

^ 

n^ What is a process? n^ What is process scheduling? n^ What are the common operations on processes? n^ How to conduct process-level communication? n^ How to conduct client-server communication?



    

 



^ 

n^ Process^ l^

is a program in execution l is an instance of a computer program being sequentially executed l process execution must progress in sequential fashion l process is also called a

job

n^ Program Vs. process^ l^

program is a

passive

entity; process is an

active^

entity

l^ program only contains text; process is associated with code, data, PC,heap, stack, registers, and other information l^ program becomes a process when an executable file is loaded intomemory l^ same program executed multiple times will correspond to differentprocess each time



    

 



^ 



    

 

^ 

n^ During execution, the process may be in one of the following^ states^ l^

new – process is being created l running – instructions are being executed l waiting – waiting for some event to occur l ready – waiting to be assigned a processor l terminated – process has finished execution

n^ Each processor can only run one process at a instant.



    

 

!

^

^ 



    

 



^ 

n^ PCB is representation of a process in an operating system.^ l^

maintains process-specific information l necessary for scheduling

n^ Information associated with each process^ l^

process state l program counter l CPU registers l CPU scheduling information l memory-management information l accounting information l I/O status information



    

 



^ 



    

 



^  

^ 

^

^ 



    

 



^ + 

n^ A^ context switch

is the process of storing and restoring the

state (

context

) of the CPU such that multiple processes can

share a single CPU resource^ l^ for time-shared or multiprogramming environments^ l^ context

of a process represented in the PCB l^ context switch involves a state

save^ of the current process, and a state

restore

of the process being resumed next l^ switch from

user^ to

kernel^

mode or vice-versa is a

mode^ switch

n^ Context-switch time is overhead^ l^

the system does no useful work while switching l overhead depends on hardware support^ ^ Sun UltraSPARC provides multiple banks of registers^ ^ Intel x86 processors also provide some support



    

 



^ 

n^ Any process can create other processes during its execution^ l^

operating systems have a

primordial

process

l^ creating process called

parent process

l^ new process called

child^ process

l^ processes identified and managed via

a process identifier

( pid )

n^ Resource sharing options^ l^

parent and children share all resources l children share subset of parent’s resources l parent and child share no resources

n^ Execution options^ l^

parent and children execute concurrently l parent waits until children terminate



    

 

!

^ 

n^ Address space options^ l^

child duplicate of parent l child has a program loaded into it

n^ UNIX examples^ l^

fork^ system call creates new process l exec^ system call used after a

fork^ to replace the process’ memory

space with a new program



    

 



^ 

^ 



 ^ ^ ^ 

  ^ 

  ^ !"#^ ^     ^ ^ $%&''# ^  ^ ((  (%&'') ) ^ #



    

 



^ 

n^ Parent waiting for child process to finish



    

 



^ -

n^ Process terminates after executing last statement^ l^

can explicitly invoke the

exit^ system call to terminate

l^ OS implicitly calls exit l^ child can pass return status to parent (via

wait )

l^ process resources are deallocated by operating system n Parent may terminate execution of children processes (

abort )

l^ child has exceeded allocated resources l^ task assigned to child is no longer required l^ if parent is exiting^ ^ some operating system do not allow child to continue if its parentterminates^ ^ all children terminated -

cascading termination



    

 



n^ Communication within the same system. n^ Processes may need to

co-operate

for several reasons

l^ information sharing l^ computation speedup l^ modularity l^ convenience n Cooperating process can affect or be affected by otherprocesses l^ typically, by sharing data n Cooperating processes need

interprocess communication

( IPC )



    

 



^ !" .

^ /

n^ Common paradigm for co-operating processes^ l^

producer

process produces information l^ consumer

process consumes the produced information

n^ Processes need synchronization^ l^

consumer

cannot use information before it is produced by the

producer

n^ Abstraction models^ l^

unbounded-buffer

places no practical limit on the size of the buffer l^ bounded-buffer

assumes that there is a fixed buffer size



    

 

!

n^ Indirect communication^ l^

messages are directed and received from mailboxes (also referredto as ports)^ ^ send

(A, message) – send a message to mailbox A ^ receive

(A, message) – receive a message from mailbox A l^ each mailbox has a unique id l^ processes can communicate only if they share a mailbox l^ properties of communication link^ ^ link may be associated with many processes^ ^ each pair of processes may share several communication links^ ^ link may be unidirectional or bi-directional^ ^ multiple receivers may need synchronization l^ mailbox can be held in the process address space or in the kernel



    

 



n^ Synchronization^ l^

message passing may be either blocking (synchronous) ornon-blocking (asynchronous) l blocking send

has the sender block until the message is received l^ blocking receive

has the receiver block until a message is available

l^ non-blocking

send has the sender send the message and continue l^ non-blocking

receive has the receiver receive a valid message or null

n^ Buffering – queue of messages attached to the link^ l^

zero capacity – 0 messages^ ^ Sender must wait for receiver l bounded capacity – finite length of

n^ messages

^ Sender must wait if link full l unbounded capacity – infinite length ^ Sender never waits



    

 



n^ Provides multiple modes of IPC^ l^

pipes l FIFOs (names pipes) l message queues l shared memory l sockets



    

 



n^ Most basic form of IPC on all Unix systems^ l^

also provides a useful command-line interface

n^ Conduit for two processes to communicate n^ Issues to be addressed^ l^

is communication unidirectional or bidirectional?^ ^ Unix pipes only allow unidirectional communication l should communication processes be related?^ ^ anonymous

pipes can only be constructed between parent-child

l^ can pipes communicate over a network^ ^ processes must be controlled by the same OS n Pipes exist only until the processes exist l^ pre-mature process exit may cause data loss n Data can only be collected in FIFO order



    

 



  +, 

+, * -+,$."/01 ./

 22)34563 -/78 

 + .1   -   ."1(-+    ((!   ( ."1 9$      .1$   .1:  ."1: 8 .1 ."1("$ #^



    

 



'^ ,+

$."/01  ./1 22)34563 -/7+;  -/8      ( -   +<   -      +( ."1   #    .1$ ("$ #



    

 



^

^  

$."/01  ./1 22)34563 -/7+;  -=8         + ) "8   .1 ) /8 #

    ;"8 ( ."1$ ;/8 ##



    

 



n^ Pipes commonly used in most Unix shells^ l^

output of one command is input to the next command l example:

n^     

l^ create a process to run

l^ create a process to run

l^ create a pipe from

to^ 

l^ the standard output of the process to run

is redirected to a pipe

streaming to the process to run

l^ the standard input of the process to run

^ is redirected to be the pipe

from the process running



    

 



^   ^ 

 

^  ^ 

 ^

^     

   



!"#^

"$^ !%#

"$!&''"()*

 ^



^ 

 

+(('

,-.^

 ^ /0'1+

^



 2

3

^



 



^ 

 

 2 3

 

 ^ +4

+ 6  ^ 1



^ 

 

 

1

!&'"7!89#::  ^

 ;



    

 



n^ Message passing in Linux is done via

message queues

n^ msgget –

create a new message queue

l^ return existing queue identifier if it exists n msgsnd –

send a message to the queue

l^ each message should be in a buffer like,^ - $-J

."^

l^        n msgrcv –

receive message from the queue

l^ J 

can be used to get specific messages

n^ msgctl –

perform control operations specified by

cmd

l^ second argument, we use it to terminate queue



    

 



n^ Multiple processes share single chunk of memory. n^ Implementation principles^ l^

uniquely naming the shared segment^ ^ system-wide or anonymous name l specifying access permissions^ ^ read, write, execute l dealing with race conditions^ ^ atomic, synchronized access

n^ Most

thread

-level communication is via shared memory.



    

 



^   ^ 

^  ^  

 ^ 

 ^ 

3<0, ^

^

^ ^  

 





^ 

^ 

!&'&"!=)*(3

!"#^

"$^ !%#

"

^   



  ^ 

^ 9#::

> 

 ^ 

^ ^

 ^ 





 ^ 

 ^ 

+(('

,-.^

 ^ /0'1+

 ^ +4

+^  





^ 

^  

^ 

 ^ 





^ ^ 

^  ^

^



^ 



^ !&'"7!89#::  ^

 ;



    

 



n^ shmget

– create shared memory segment

l^ IPC_PRIVATE

specifies creation of new memory segment of

size

rounded to the system page size l access permissions as for normal file access l returns identifier of shared memory segment

n^ shmat

– attach shared memory segment l must for every process wanting access to the region l segment identified by

segment_id

l^ system chooses a suitable attach address n shmctl –

performs the control operation specified by

cmd

l^ command is

IPC_RMID

to remove shared segment

n^ see program shared_memory2.c n^ Read man pages!



    

 



n^ Sockets^ l^

can be defined as an end-point for communications l two-way communication pipe l can be used in a variety of domains, including

Internet

n^ Unix Domain Sockets^ l^

communication between processes on the same Unix system l special file in the file system

n^ Mostly used for client-server programming^ l^

client^ sending requests for information, processing l server^ waiting for user requests l server performing the requested activity and sending updates to client

n^ Socket communication modes^ l^

connection-based, TCP l connection-less, UDP



    

 



n^ socket ( ) -

create the Unix socket

l^  J   l^ 

 < &%C@

n^  

$   l

J 

l^ J^

^ ^

^ 

n^ 

 $- l (^) l $-



    

 

n^ 

  l

l^        n 

M NN l

$M

l^  

n^  



    

 



^

#^



    

 

!

n^ Remote Method Invocation (RMI)^ l^

Java mechanism (API) to perform RPCs l Java remote method protocol (JRMP) only allows calls from one JVM toanother JVM l CORBA is used to support communication with non-JVM code l client obtains reference to remote object, and invokes methods on them