0% found this document useful (0 votes)
234 views49 pages

Interprocess Communication: Unit - V

Returns the semaphore identifier. 33 17 January 2022

Uploaded by

Shakaal G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views49 pages

Interprocess Communication: Unit - V

Returns the semaphore identifier. 33 17 January 2022

Uploaded by

Shakaal G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

INTERPROCESS

COMMUNICATION
UNIT - V
Outline
 Pipes
 Coprocesses
 FIFOs
 Message Queues
 Semaphores
 Shared Memory
 Client Server Properties
 Passing file descriptors

2 17 January 2022
Pipes
 Pipes have two limitations:
 They have been half duplex
 These can be used only between processes that have a
common ancestors.
 A pipe can be created by calling the pipe function:

 Two file descriptors are returned through the filedes:


 filedes[0] – open for reading
 filedes[1] – open for writing

3 17 January 2022
Two ways to view a half duplex pipe
 The fstat function returns a file type of FIFO for the file
descriptor of either end of a pipe.

 We can test for a pipe with the S_IFIFO macro.

4 17 January 2022
Half – duplex pipe after a fork Pipe from parent to child

5 17 January 2022
Rules
 When one end of the pipe is closed, the following two
rules apply:
 If we read from a pipe whose write end has been closed, read
returns 0 to indicate an end of file after all the data has been
read.
 If we write to a pipe whose read end has been closed, the
signal SIGPIPE is generated.
 If we either ignore the signal or catch it and return from the signal
handler, write returns –1 with errno set to EPIPE.

6 17 January 2022
popen and pclose Functions
 popen and pclose functions will create the pipe, fork a child, closes the unused end
of the pipe, executing a shell to run the command, and waiting for the command to
terminate.

 The function popen does a fork and exec to execute the cmdstring, and returns a
standard I/O file pointer.

 If type is “r”, the file pointer is connected to the standard output of cmdstring.

 If type is “w”, the file pointer is connected to the standard input of cmdstring.

7 17 January 2022
Result of fp = popen(cmdstring,”r”)

Result of fp = popen(cmdstring,”w”)

8 17 January 2022
 The pclose function closes the standard input stream,
waits for the command to terminate, and returns the
termination status of the shell.

 If the shell cannot be executed, the termination status


returned by pclose is as if the shell had executed
exit(127).

9 17 January 2022
Coprocesses
 A UNIX system filter is a program that reads from
standard input and writes to standard output.

 A filter becomes a coprocess when the same program


generates the filter's input and reads the filter's output.

10 17 January 2022
FIFOs

 FIFOs are sometimes called as named pipes.

 With FIFOs, unrelated processes can exchange data.


#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

 The mode argument is same as for the open function.

11 17 January 2022
Uses of FIFOs
 FIFOs are used by shell commands to pass data from one
shell pipeline to another without creating intermediate
temporary files.

 FIFOs are used in client server applications to pass data


between the client and the servers.

12 17 January 2022
Using FIFOs to Duplicate Output Streams
 FIFOs can be used to duplicate an stream in a series of
shell commands.

 This prevents writing the data intermediate disk file.


mkfifo fifo1
prog3 < fifo1 &
prog1 < infile | tee fifo1 | prog2

13 17 January 2022
14 17 January 2022
Client – Server Communication Using a
FIFO

15 17 January 2022
XSI IPC
 Message Queues

 Semaphores

 Shared Memory

16 17 January 2022
Permissions Structure
 Every XSI IPC structure has a permissions structure
associated with it

17 17 January 2022
Permissions Structure
 All fields are initialized when the IPC structure is
created

 mode is the same as for the open function but


no execute permission

 uid, gid, and mode can be modified with msgctl,


semctl or shmctl

 Only the creator of the IPC or root can change


these
18 17 January 2022
Configuration Limits
 All three forms of XSI IPC have built in limits.

 Most of these limits can be changed by reconfiguring the


kernel.

19 17 January 2022
Message Queues
 Similar to FIFO, allows two or more processes to
exchange data.

 Not FIFO, messages may be retrieved out of order.

 Message queue remains until deleted or system rebooted.

20 17 January 2022
Message Queues
 Message Queue is a linked lists of messages stored within the
Kernel memory and identified by a message queue identifier.

 Each message queue has its own identifier

 Messages within queue can be of different types

 Queue opened or created with msgget

 New messages added with msgsnd

 Messages fetched with msgrcv

21 17 January 2022
22 17 January 2022
msgget

 opens or creates a queue. flag parameter is used to


initialize the mode member of the permission structure

 Returns message queue ID if OK and -1 on error

23 17 January 2022
msgsnd

 msqid internal ID of message queue nbytes size in bytes of the


message flag
 OR of zero or more of: IPC_NOWAIT, MSG_NOERROR

 ptr points to a long integer that contains the positive integer


message type and it is immediately followed by the message
data

24 17 January 2022
 ptr pointer to the actual message.
 Must be of the form

25 17 January 2022
msgrcv

 Retrieves a message from the queue


 nbytes is the size in bytes of the text portion of the
structure pointed to by ptr
 ptr is a pointer to a structure that will hold the
message retrieved from the queue

26 17 January 2022
 The type argument lets us specify which message we
want.

type == 0 The first message on the queue is returned


type > 0 The first message on the queue whose message type equals
type is returned
type < 0 The first message on the queue whose message type is the
lowest value less than or equal to the absolute value of type is
returned

27 17 January 2022
msgctl

 Performs various operations on a message queue.

28 17 January 2022
 The cmd argument specifies the command to be
performed on the queue specified by msqid.

IPC_STAT Fetch the msqid_ds structure for this queue.


IPC_SET Copy the following fields from the structure pointed to by buf to the
msqid_ds structure associated with this queue: msg_perm and
msg_qbytes. Only the superuser can increase the value of msg_qbytes.
IPC_RMID Remove the message queue from the system and any data still on the
queue.This removal is immediate.

29 17 January 2022
Semaphores
 Semaphore is a counter used to provide access to a shared resource
for multiple processes.

 To obtain a shared resource, a process need to do the following:

 Test the semaphore that controls the resource.

 If the value of the semaphore is positive, the process can use the
resource.
 In this case, the process decrements the semaphore value by 1, indicating
that it has used one unit of the resource.

 Otherwise, if the value of the semaphore is 0, the process goes to


sleep until the semaphore value is greater than 0. When the process
wakes up, it returns to step 1.

30 17 January 2022
 When a process is done with shared resource that is
controlled by a semaphore, the semaphore value is
incremented by 1.

 If any other processes are asleep, waiting for the


semaphore, they are awakened.

 A common form of semaphore is called a binary


semaphore.
 It controls a single resource, and its value is initialized to 1.

31 17 January 2022
 The kernel maintains a semid_ds structure for each
semaphore set:

 Each semaphore is represented by an anonymous


structure contains at least the following members:

32 17 January 2022
semget

 The ipc_perm structure is initialized.


 The mode member of this structure is set to the
corresponding permission bits of flag.
 sem_otime is set to 0.
 sem _ctime is set to current time.
 sem_nsems is set to nsems.

33 17 January 2022
semctl

 The fourth argument is optional, depending on the


command requested, and if present, is of type semun, a
union of various command specific arguments:

34 17 January 2022
 The cmd argument specifies one of the following ten
commands to be performed on the set specified by semid.

 The five commands that refer to one particular


semaphore value use semnum to specify one member of
the set.

 The value of semnum is between 0 and nsems-1.

35 17 January 2022
IPC_STAT Fetch the semid_ds structure, storing it in the structure pointed to by arg.buf
IPC_SET Set the sem_perm.uid, sem_perm.gid, sem_perm.mode
IPC_RMID Remove the semaphore set from the system.This removal is immediate.
GETVAL Return the value of semval for the member semnum
SETVAL Set the value of semval for the member semnum
GETPID Return the value of sempid for the member semnum
GETNCNT Return the value of semncnt for the member semnum
GETZCNT Return the value of semzcnt for the member semnum
GETALL Fetch all semaphore values in the set. Values stored in array pointed to by arg.array
SETALL Set all semaphore values in the set to the values pointed to by arg. array

36 17 January 2022
semop

 The semoparray argument is a pointer to an array of semaphore operations,


represented by sembuf structures:

37 17 January 2022
 The nops argument specifies the number of operations in
the array.

 The operation on each member of the set is specified by


the corresponding sem_op value.
 The value can be negative, 0, or positive.

38 17 January 2022
Case 1 (sem_op is positive)
 This case corresponds to the returning of the resources
by the process.

 The value of sem_op is added to the semaphore’s value.

39 17 January 2022
Case 2 (sem_op is negative)
 If the semaphore’s value is less than the absolute value of
sem_op, the following conditions apply.
 If IPC_NOWAIT is specified, semop returns with an error of
EAGAIN.
 If IPC_NOWAIT is not specified, the semncnt value for this
semaphore is incremented and the calling process is suspended
until one of the following occurs:
 The semaphore value becomes greater than or equal to the absolute
value of sem_op.
 The semaphore is removed from the system.
 A signal is caught by the process, and the signal handler returns.

40 17 January 2022
Case 3 (sem_op is 0)
 Means that the calling process wants to wait until the
semaphore value becomes 0.
 If the semaphore value is currently 0, the function returns
immediately.
 Otherwise,
 If IPC_NOWAIT is specified, return is made with an error of
EAGAIN.
 If IPC_NOWAIT is not specified, the semzcnt value for this
semaphore is incremented and the calling process is suspended until
one of the following occurs:
 The semaphore value becomes 0.
 The semaphore is removed from the system.
 A signal is caught by the process, and the signal handler returns.

41 17 January 2022
Shared Memory
 Shared memory allows two or more processes to share a
given region of memory.

 This is the fastest form of IPC, because the data does not
need to be copied between the client and the server.

42 17 January 2022
 The kernel maintains a structure with at least the following
members for each shared memory segment:

 The type shmatt_t is defined to be an unsigned integer at least


as large as unsigned short.

43 17 January 2022
shmget

 ipc_perm structure is initialized


 shm_lpid, shm_nattach, shm_atime, shm_dtime are all set to
0
 shm_ctime is set to the current time
 shm_segsz is set to the size requested

44 17 January 2022
shmctl

 The cmd argument specifies one of the following five


commands on the segment specified by shmid:
IPC_STAT Fetch the shmid_ds structure for this segment
IPC_SET Set the shm_perm.uid, shm_perm.gid, and shm_perm.mode
IPC_RMID Remove the shared memory segment set from the system
SHM_LOCK Lock the shared memory segment in memory
SHM_UNLOCK Unlock the shared memory segment

45 17 January 2022
shmat
 Once the shared memory segment has been created, a
process attaches it to its address space by calling shmat.

 If addr is 0, the segment is attached at the first available address


selected by the kernel.
 If addr is non zero and SHM_RND is not specified, the segment
is attached at the address given by addr.
 If addr is non zero and SHM_RND is specified, the segment is
attached at the address given by (addr – (addr modulus
SHMLBA))
SHMLBA – low boundary address multiple
46 17 January 2022
shmdt

 The addr argument is the value that was returned by a


previous call to shmat.
 If successful, shmdt will decrement the shm_nattch counter in
the associated shmid_ds structure.

47 17 January 2022
Memory layout on an Intel Based Linux System

48 17 January 2022
Questions
1. Explain how we can communicate with other process
with same ancestors using pipes.
2. Explain coprocesses.
3. What are message queues? Explain the functions related
to message queues.
4. How semaphore is different than message queues?
5. How shared memory is used to communicate with two
processes?
6. Explain STREAMS based pipes and bring out the
differences between STREAMS pipe and a pipe.

49 17 January 2022

You might also like