Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 106

Inter-process Communication

• Objectives
– Introduction to IPC

– PIPES

– FIFO

– Identifiers and Keys

– Permission structure

– Message Queue

Unix System Programming Satyam Computer Services Ltd 1


Inter-process Communication

• Introduction

– Multiple co-operating processes needs to

• Communicate to perform a related operation

– Processes must communicate in order to

• Share information and resources

Unix System Programming Satyam Computer Services Ltd 2


Inter-process Communication

• Introduction

– Since processes themselves cannot access the

• Address space of other processes

– The kernel provides the facility for IPC

• Inter Process Communication

Unix System Programming Satyam Computer Services Ltd 3


Inter-process Communication

• Introduction
– The Kernel is responsible for the data transfer
• Between processes
– The kernel Provides facilities such as
• Signals
• PIPE
• FIFO
• Message Queue
• Shared memory
• Semaphore

Unix System Programming Satyam Computer Services Ltd 4


Inter-process Communication

• Introduction
– Purpose of Interprocess communication

– Data Transfer
• The amount of data sent vary from

– One byte to several mega bytes

– Sharing data
• Multiple processes may wish to operate on Shared data

– Any changes will be immediately visible

Unix System Programming Satyam Computer Services Ltd 5


Inter-process Communication

• Introduction
– Purpose of Interprocess communication ( Contd.)
– Event Notification
• A process can notify another process about
– An occurrence of an event
• Example
– SIGCHLD,
– SIGHUP etc

Unix System Programming Satyam Computer Services Ltd 6


Inter-process Communication

• Introduction
– Purpose of Interprocess communication (Contd.)
– Resource Sharing
• A set of process can define their own protocol
– For accessing a specific resource
– Implemented using
• locking or
• Synchronization

Unix System Programming Satyam Computer Services Ltd 7


Inter-process Communication

• Introduction
– Purpose of Interprocess communication (Contd.)

– Process Control
• A process may take complete control over

• Execution of other processes

– Example
• Debuggers

Unix System Programming Satyam Computer Services Ltd 8


Inter-process Communication

PIPES

Unix System Programming Satyam Computer Services Ltd 9


Inter-process Communication

• PIPES

– In traditional implementation a pipe is a

• Unidirectional

• First-in-First-out

• Unstructured data stream

Unix System Programming Satyam Computer Services Ltd 10


Inter-process Communication

• PIPES

– Writers add data to the stream

• At one end of the pipe

– Readers retrieve data from the stream

• At the other end

Unix System Programming Satyam Computer Services Ltd 11


Inter-process Communication

• PIPES

– Once the data is read it is removed and is unavailable

• To other readers

– Pipes provides

• Simple control-flow mechanism

Unix System Programming Satyam Computer Services Ltd 12


Inter-process Communication

• PIPES

– A process attempting to read from an empty pipe

• Blocks until more data is written

– A process attempting to write to a full pipe

• Blocks until another process reads data

Unix System Programming Satyam Computer Services Ltd 13


Inter-process Communication

• PIPES

User Process
fd[0] fd[1]

pipe
kernel

Unix System Programming Satyam Computer Services Ltd 14


Inter-process Communication

• PIPES
– A pipe is created by calling the pipe system call

int pipe ( int filedes [ 2 ] ) ;


Returns 0 if OK
-1 on error

– Two file descriptors are returned through


• filedes argument

Unix System Programming Satyam Computer Services Ltd 15


Inter-process Communication

• PIPES

– The filedes [ 0 ]

• Is open for reading

– The filedes [ 1 ]

• Is open for writing

Unix System Programming Satyam Computer Services Ltd 16


Inter-process Communication

• PIPES

– The fstat function returns

• A file type of FIFO for the descriptors

– of either end

– And can be tested

• Using the macro S_ISFIFO

Unix System Programming Satyam Computer Services Ltd 17


Inter-process Communication

• PIPES

– Normally a the process that calls pipe then calls fork

• Creating an IPC channel from the

• Parent to the child

Unix System Programming Satyam Computer Services Ltd 18


Inter-process Communication

• PIPES

parent child
fd[0] fd[1] fd[0] fd[1]

pipe
kernel

Unix System Programming Satyam Computer Services Ltd 19


Inter-process Communication

• PIPES
– If the direction of data flow desired is

– From parent to the child


• The parent closes the

– Read end of pipe fd [ 0 ]

• The child closes the

– Write end of pipe fd [ 1 ]

Unix System Programming Satyam Computer Services Ltd 20


Inter-process Communication

• PIPES

parent child
fd[0] fd[1] fd[0] fd[1]

pipe
kernel

Unix System Programming Satyam Computer Services Ltd 21


Inter-process Communication

• PIPES
– Reading from a pipe whose write end has been closed

– After all the data has been read


• read returns zero indicating EOF

– The EOF is generated when all the


• Writers to this pipe are closed

– Multiple processes can have


• The either end of the pipe

Unix System Programming Satyam Computer Services Ltd 22


Inter-process Communication

• PIPES

– The file descriptors returned from pipe can be

• Duplicated using dup2 system call

– Hence multiple process can use the pipe open for

• Reading or writing

Unix System Programming Satyam Computer Services Ltd 23


Inter-process Communication

• PIPES
– Write to a pipe whose read end is closed
– The signal SIGPIPE is generated
• If the signal is either
– Ignored or
– Handled
– Write returns an error with errno
» set to EPIPE

Unix System Programming Satyam Computer Services Ltd 24


Inter-process Communication

• PIPES
– The kernel's pipe buffer size is specified by
• The constant PIPE_BUF

– A write of PIPE_BUF bytes or less is guaranteed


• Not to be interleaved with other writes

– If multiple writers are involved and writing more than


• PIPE_BUF

• It might be interleaved

Unix System Programming Satyam Computer Services Ltd 25


Inter-process Communication

• PIPES
– Example of a pipe from the parent to child
main ( void ) {
int n, fd [ 2 ] ;
pid_t pid ;
char line [ MAX ] ;
pipe ( fd ) ;
pid = fork ( ) ;
if ( pid > 0 ) { / *parent */
close ( fd [ 0 ] ) ; / * read */
write ( fd [ 1 ] , "hello\n" ,6 ) ;
}

Unix System Programming Satyam Computer Services Ltd 26


Inter-process Communication

• PIPES
– Example of a pipe from the parent to child ( Contd.)

else if ( pid = = 0 ) { /* child */


close ( fd [ [ 1 ] ) ; /* write */
read ( fd [ 0 ], line , MAX ) ;
}
exit ( 0 );
}

Unix System Programming Satyam Computer Services Ltd 27


Inter-process Communication

• PIPES
– The Files descriptors returned from pipe

• Can be duplicated

– Example

• The read end of the pipe can be duplicated with

– The STDIN_FILENO

Unix System Programming Satyam Computer Services Ltd 28


Inter-process Communication

• PIPES
– Example of redirecting the read end to STDIN_FILENO
// . . . other lines of code

if ( pid = = 0 ) { /* child */
close ( fd [ 1 ] /* write */
if ( fd [ 0 ] ! = STDIN_FILENO ) {
dup2 ( fd [ 0 ], STDIN_FILENO );
close ( fd [ 0 ] );
}

// . . . other lines of code

Unix System Programming Satyam Computer Services Ltd 29


Inter-process Communication

• PIPES
– For bi-directional communication

– The parent creates two pipes

• pipe1 & pipe2

– The parent closes

• The read end of pipe1 and

• Write end of pipe2

Unix System Programming Satyam Computer Services Ltd 30


Inter-process Communication

• PIPE

– The child closes

• The write end of pipe1 and

• Read end of pipe2

– Some form of synchronization is necessary

Unix System Programming Satyam Computer Services Ltd 31


Inter-process Communication

• PIPES

• Disadvantages of Pipes
– They are half-duplex

– Data flows in one direction

– They can be used only between processes


• With common ancestor

– Once read the data is removed from the pipe

Unix System Programming Satyam Computer Services Ltd 32


Inter-process Communication

• PIPES
• Disadvantages of Pipes (Contd.)
– Bytes in the pipe are treated as byte streams
• No knowledge of message boundaries

– If multiple processes writes data


• The reader cannot determine the boundaries

– If multiple readers are present a writer cannot


• Direct data to specific reader

Unix System Programming Satyam Computer Services Ltd 33


Inter-process Communication

• PIPES
• popen call
FILE * popen ( const char *cmdstr,
const char * type
);
Returns file pointer on success
NULL on error

Unix System Programming Satyam Computer Services Ltd 34


Inter-process Communication

• PIPES
• popen call
– Creates the pipe

– Forks a child

– Closes all the unused ends of the pipe

– Execes a shell to execute the command

– Waits for the command to terminate

Unix System Programming Satyam Computer Services Ltd 35


Inter-process Communication

• PIPES
• popen call
– If the type specified is " r "
– The file pointer is connected to the
• Standard output of cmdstr

parent fp child
stdout cmdstr

Unix System Programming Satyam Computer Services Ltd 36


Inter-process Communication

• PIPES
• popen call
– If the type specified is " w "
– The file pointer is connected to the
• Standard input of cmdstr

parent fp child
stdin cmdstr

Unix System Programming Satyam Computer Services Ltd 37


Inter-process Communication

• PIPES
• pclose call

int pclose ( FILE * fp ) ;


Returns Termination status of cmdstr
-1 on error

Unix System Programming Satyam Computer Services Ltd 38


Inter-process Communication

• PIPES
• pclose call
– Waits for the command to terminate and returns
• The termination status of the shell

– If the shell cannot be executed


• The termination status is returned
• The return is as if the shell executed
– exit ( 127 )

Unix System Programming Satyam Computer Services Ltd 39


Inter-process Communication

• PIPES
– Example of popen call

fp = popen ( " ls *.c " , " r " );

fp = popen ( " cmd 2 > &1 " , " r " ) ;

– The shell can expand any special characters


• in cmdstr

Unix System Programming Satyam Computer Services Ltd 40


Inter-process Communication

FIFOs

Unix System Programming Satyam Computer Services Ltd 41


Inter-process Communication

• FIFO
– FIFO stands for first-in-first-out

– It is very similar to the PIPE

– Provides unidirectional data flow

– A FIFO has a name associated with it


• Hence called named pipe

– These named pipes exists as files


• Allowing unrelated processes to communicate

Unix System Programming Satyam Computer Services Ltd 42


Inter-process Communication

• FIFO
– A FIFO is a type of file
• An entry is made in the directory where it is created

– The stat function returns a file


• Of type of FIFO and

– Can be tested using

– S_ISFIFO macro

Unix System Programming Satyam Computer Services Ltd 43


Inter-process Communication

• FIFO
– To create a FIFO

int mkfifo (const char *pathname ,


mode_t mode
) ;
Returns 0 on success
-1 on error

– The pathname for a fifo exists on the file system

Unix System Programming Satyam Computer Services Ltd 44


Inter-process Communication

• FIFO
– The specification for the mode argument is same
• As for the open system call
– The fifo created can have the permissions for
• User,
• Group and
• Others
– Once created
• The fifo can be opened with the open system call

Unix System Programming Satyam Computer Services Ltd 45


Inter-process Communication

• FIFO
– The normal file operations also apply for fifo
• close , read , write , unlink etc . .
– SVR4 and BSD also support
• The mkfifo command
– Hence FIFO can also be created using a shell
$ mkfifo fifoname
– They can also be accessed
• With normal shell I/O redirection

Unix System Programming Satyam Computer Services Ltd 46


Inter-process Communication

• FIFO
• Opening a FIFO
– Without the O_NONBLOCK flag

– open with read-only blocks until


• Some other process opens for writing

– open with write only blocks until


• Some other process opens for reading

Unix System Programming Satyam Computer Services Ltd 47


Inter-process Communication

• FIFO
• Opening a FIFO With the
– O_NONBLOCK flag specified
– open for read only
• Returns immediately
– open for write only
• Returns an error
• With the errno set to ENXIO
– No process has fifo open for reading

Unix System Programming Satyam Computer Services Ltd 48


Inter-process Communication

• FIFO
– If write to a FIFO with no process open for reading
• Signal SIGPIPE is generated

– When the last writer closes the FIFO


• an EOF is generated for the reader

– The PIPE_BUF provides the maximum amount of


• data that can be written atomically

Unix System Programming Satyam Computer Services Ltd 49


Inter-process Communication

• FIFO
• Uses of FIFO
– By shell commands to pass data from one
• shell pipe to another

– without creating temporary files

– By client-server applications for sending


• data between clients and server

Unix System Programming Satyam Computer Services Ltd 50


Inter-process Communication

• A simple Client – Server example

• Problem statement

– Open an existing file and

– Read the contents

– If the file does not exist then

– An error must be returned

Unix System Programming Satyam Computer Services Ltd 51


Inter-process Communication

• A simple Client – Server example

• Design
– The server creates the IPC channel
• Well Known

– The Client reads the filename

– Writes it on to the IPC channel and waits

– The Server reads from the IPC channel

Unix System Programming Satyam Computer Services Ltd 52


Inter-process Communication

• A simple Client – Server example

• Design

– The Server tries to open the file

– Read the contents of the file

– Write the contents to the IPC channel

• and waits

Unix System Programming Satyam Computer Services Ltd 53


Inter-process Communication

• A simple Client – Server example

• Design

• The client reads the contents of

• the IPC channel and

• writes it on to the STDOUT

Unix System Programming Satyam Computer Services Ltd 54


Inter-process Communication

• A simple Client – Server example

• Implementation using FIFOs

– The server creates a well-known FIFO

• i.e. all clients know the path name

– All clients sends their requests on this

• Well known FIFO

Unix System Programming Satyam Computer Services Ltd 55


Inter-process Communication

• A simple Client – Server example

• Implementation using FIFOs


– The client also sends the process ID
• With the request

– The server creates a unique FIFO


• based on the clients Process ID

– The server sends its response on this


• newly created IPC
Unix System Programming Satyam Computer Services Ltd 56
Inter-process Communication

Identifiers and Keys

Unix System Programming Satyam Computer Services Ltd 57


Inter-process Communication

• Identifiers and Keys


– Every IPC structure in the kernel is referred to by

• a non-negative integer identifier

– It is in the ipc_perm structure

– A key must be specified

• when creating an IPC channel

– This key is converted to an identifier

Unix System Programming Satyam Computer Services Ltd 58


Inter-process Communication

• Identifiers and Keys

– If the key is of the type IPC_PRIVATE

– It guarantees that the IPC structure

• created is new

– The identifier returned is stored such that

• other processes can access it

Unix System Programming Satyam Computer Services Ltd 59


Inter-process Communication

• Identifiers and Keys

• The clients and server can agree upon a


– well known key by defining it in the
• common header file

– The server then creates the IPC structure


• using the well know key

– The server must detect that the IPC structure


• Created must not already exist
Unix System Programming Satyam Computer Services Ltd 60
Inter-process Communication

• Identifiers and Keys

– A new IPC structure is created if either

– key is IPC_PRIVATE

– key is not currently associated with the

• IPC structure of the particular type and

• The IPC_CREAT bit is set

Unix System Programming Satyam Computer Services Ltd 61


Inter-process Communication

• Identifiers and Keys

– To reference the existing channel the key must

• Equal the key that was specified when created and

• The IPC_CREATE must not be specified

– It is not possible to reference an existing channel if the

• key is specified as IPC_PRIVATE

Unix System Programming Satyam Computer Services Ltd 62


Inter-process Communication

• Identifiers and Keys

– To create a new IPC structure but

– Making sure that we don't reference an existing one

• We must use IPC_CREATE and IPC_EXECL

– The call returns an error EEXIST

• if the channel already exists

Unix System Programming Satyam Computer Services Ltd 63


Inter-process Communication

• Permission structure
– There is a permission structure associated with
• each IPC structure
– This structure defines the permissions and owner
struct ipc_perm {
uid_t uid ; /* EUID */
gid_t gid ; /* EGID */
uid_t cuid ; /* Creators EUID */
gid_t cgid ; /* Creators EGID */
mode_t mode ; /* access mode */
ulong seq ; /* slot usage seq no */
key_t key ; /* key */
} ;
Unix System Programming Satyam Computer Services Ltd 64
Inter-process Communication

• Permission structure

– All the fields are initialized when created

• except seq

– The uid, gid and mode bit can be modified

– To change these the process must either be

• the creator or the superuser

Unix System Programming Satyam Computer Services Ltd 65


Inter-process Communication

• Permission structure
– The mode field specifies the permissions

Permission Message Q Semaphore Shared memory


user read MSG_R SEM_R SHM_R
user write MSG_W SEM_W SHM_W
Group read MSG_R>>3 SEM_R>>3 SHM_R>>3
Group write MSG_W>>3 SEM_W>>3 SHM_W>>3
Other read MSG_R>>6 SEM_R>>6 SHM_R>>6
Other write MSG_W>>6 SEM_W>>6 SHM_W>>6
Unix System Programming Satyam Computer Services Ltd 66
Inter-process Communication

• IPC structures
– They are system wide

– They do not have a reference count

– If a message Queue is created by a process and


• Terminates the IPC structure is not removed

– The IPC structure and data remains in the channel


• until other process reads it or removes it explicitly

Unix System Programming Satyam Computer Services Ltd 67


Inter-process Communication

Message Queues

Unix System Programming Satyam Computer Services Ltd 68


Inter-process Communication

• Message Queue
– Message Queue are a linked list of messages

• stored within the kernel

– They are identified by a message queue identifier

– A new message Queue is created or an existing one

• Is accessed by msgget system call

– This call returns the message queue ID

Unix System Programming Satyam Computer Services Ltd 69


Inter-process Communication

• Message Queue
– Each Queue has the structure

• msqid_ds associated with it

– This structure defines the current status

• of the queue

Unix System Programming Satyam Computer Services Ltd 70


Inter-process Communication

struct msquid_ds {
struct ipc_prem msg_perm ;
struct msg * msg_first ;
struct msg * msg_last ;
ulong msg_cbytes ;
ulong msg_qnum ;
ulong msg_qbytes ;
pid_t msg_lspid ;
pid_t msg_lrpid ;
time_t msg_stime ;
time_t msg_rtime ;
time_t msg_ctime ;
} ;
Unix System Programming Satyam Computer Services Ltd 71
Inter-process Communication

• Message Queue
int msgget ( key_t key , int flag ) ;
Returns message queue ID on success
-1 on error

• The ipc_perms structure is filled


– The mode bits are set corresponding flag bits
– msg_qnum, msg_lspid, msg_lrpid, msg_stime and
– msg_rtime are set to zero

Unix System Programming Satyam Computer Services Ltd 72


Inter-process Communication

• Message Queue
– msg_ctime is set to the current time.

– msg_qbytes is set to the system limit

– On success the call returns a non-negative

• Queue ID

– This value is used with other message Queue functions

Unix System Programming Satyam Computer Services Ltd 73


Inter-process Communication

• Message Queue
– Data is placed on the message Queue using

int msgsnd ( int msqid ,


const void * ptr ,
size_t nbytes ,
int flag
) ;
Returns 0 on success
-1 on error

Unix System Programming Satyam Computer Services Ltd 74


Inter-process Communication

• Message Queue
– ptr points to the MyMesg structure

struct MyMesg {
long mtype ; /* positive message type */

char text [ MAX ] ;


/*data of length nbytes */

} ;

– nbytes corresponds to the size of the message


Unix System Programming Satyam Computer Services Ltd 75
Inter-process Communication

• Message Queue

– A flag value of IPC_NOWAIT can be specified

– If the message Queue is full

– msgsnd returns immediately

• with an error of EAGAIN

Unix System Programming Satyam Computer Services Ltd 76


Inter-process Communication

• Message Queue

• If IPC_NOWAIT is not specified


– The call is blocked until
• There is enough space for the message

– The Queue is removed from the system


• Returning an error EIDRM

– A signal is caught and the signal handler


• Returns and errno is set to EINTR
Unix System Programming Satyam Computer Services Ltd 77
Inter-process Communication

• Message Queue

• Each message is composed of


– Positive long integer type field

– A nonnegative length ( nbytes ) and

– Actual data bytes

– Messages are always placed at the


• end of the Queue

Unix System Programming Satyam Computer Services Ltd 78


Inter-process Communication

• Message Queue
– Messages are received from a queue by msgrcv
int msgrcv ( int msqid ,
void * ptr ,
size_t nbytes ,
long type ,
int flag
) ;
Returns size of data portion of the message
-1 on error

Unix System Programming Satyam Computer Services Ltd 79


Inter-process Communication

• Message Queue
– The ptr argument points to the struct MyMesg
• A Long integer followed by
• A data buffer

– nbytes
– specifies the size of the data buffer
• If the returned message is larger than nbytes
• The message is truncated
– If the MSG_NOERROR bit in the flag is set
Unix System Programming Satyam Computer Services Ltd 80
Inter-process Communication

• Message Queue

– If the returned message is larger than nbytes and

• The flag is not specified

• An error of E2BIG is returned

• The message stays on the queue

Unix System Programming Satyam Computer Services Ltd 81


Inter-process Communication

• Message Queue
– The type argument lets us specify
• Which message is desired

• type = = 0

– The first message on the queue is returned

• type > 0

– First message on the message queue

– whose message type equals type is returned


Unix System Programming Satyam Computer Services Ltd 82
Inter-process Communication

• Message Queue

– type < 0

• first message on the queue

• whose message type is the lowest or equal

– To absolute value of type is returned

Unix System Programming Satyam Computer Services Ltd 83


Inter-process Communication

• Message Queue

– To make the operation non -blocking

• A flag value of IPC_NOWAIT is specified

• It causes msgrcv

– To return an error of ENOMSG

– if a message of the specified type is not available

Unix System Programming Satyam Computer Services Ltd 84


Inter-process Communication

• Message Queue
– If a flag value of IPC_NOWAIT is not specified
– The process is blocked until
• A message of the specified type is available
• The queue is removed from the system
– An error of EIDRM is returned
• A signal is caught and
– The signal handler returns
– An error of EINTR is returned
Unix System Programming Satyam Computer Services Ltd 85
Inter-process Communication

• Message Queue
– Message control

int msgctl ( int msqid,


int cmd,
struct msqid_ds *buff
);
Returns 0 on success
-1 on error

Unix System Programming Satyam Computer Services Ltd 86


Inter-process Communication

• Message Queue
– The cmd argument
• IPC_STAT
– Fetches the msqid_ds structure
• IPC_SET
– Set the fields of the structure msg_perm and
– Set the msg_qbytes
– Only a superuser or a process with
» EUID equal to cuid or uid of the structure can issue

Unix System Programming Satyam Computer Services Ltd 87


Inter-process Communication

• Message Queue
– IPC_RMID
• Removes the message queue
• Any data on the system is also removed
• This removal is immediate
– Other process trying to use this queue
» Gets an error EIDRM
– Only a superuser or a process with
» EUID equal to cuid or uid of the structure can issue
Unix System Programming Satyam Computer Services Ltd 88
IPC

Shared Memory

89
Inter-process Communication

• Shared Memory:
– Allows two or more processes
• To share a given region of memory

– This is the fastest form of IPC


• As the data does not need to be copied

– Between client and server

– Semaphores are used


• To synchronize shared memory access

Unix System Programming Satyam Computer Services Ltd 90


Inter-process Communication

• shared memory
– The kernel maintains the shmid_ds structure
• For each shared memory segment

struct shmid_ds {
struct ipc_perm shm_perm;
struct anon_map *shm_amp;
int shm_segsz ;
ushort shm_lkcnt ;
pid_t shm_lpid ;

Unix System Programming Satyam Computer Services Ltd 91


Inter-process Communication

• shared memory:

pid_t shm_nattch;
ulong shm_cnattch ;
time_t shm_atime;
time_t shm_dtime ;
time_t shm_ctime;
};

Unix System Programming Satyam Computer Services Ltd 92


Inter-process Communication

• shared memory
– To obtain a shared memory identifier
• The function called is shmget

int shmget(key_t key , int size , int flag );

Returns shared memory ID if OK,

-1 on error

Unix System Programming Satyam Computer Services Ltd 93


Inter-process Communication

• shared memory

– shm-lpid , shm_nattach ,shm-atime , and

– shm_dtime are all set to 0

• shm_ctime is set to the current time.

Unix System Programming Satyam Computer Services Ltd 94


Inter-process Communication

• shared memory
– For a new segment being created

• Typically in the server

– The size is specified

– For referencing an existing segment

• In a client

• The size is specified as 0

Unix System Programming Satyam Computer Services Ltd 95


Inter-process Communication

• shared memory
int shmctl (int shmid ,
int cmd ,
struct shmid_ds *buf
);
returns : 0 if ok , -1 on error

– The cmd argument specifies


• one of the following five commands to be performed

Unix System Programming Satyam Computer Services Ltd 96


Inter-process Communication

• shared memory

– IPC_STAT

• Fetch the shmid_ds structure for this segment

– IPC_RMID

• Remove the shared memory segment set from the system

Unix System Programming Satyam Computer Services Ltd 97


Inter-process Communication

• shared memory

– An attachment count is maintained

• for shared memory segments

– The shm_nattch field

• The segment is not removed until

– The last process using the segment terminates

– or detaches

Unix System Programming Satyam Computer Services Ltd 98


Inter-process Communication

• shared memory

– IPC_SET

• set the following three fields of the structure pointed to by buf

in the structure associated with this segment

– shm_perm.uid

– shm_perm.gid

– shm_perm.mode

Unix System Programming Satyam Computer Services Ltd 99


Inter-process Communication

• shared memory

– SHM_LOCK

• Lock the shared memory segment in memory

• It can be executed only by the super user

– SHM_UNLOCK

• Unlock the shared memory segment

• It can be executed only by the superuser


Unix System Programming Satyam Computer Services Ltd 100
Inter-process Communication

• shared memory
– Once a shared memory segment is created
• A process attaches it to its address space by calling shmat

void *shmat ( int shmid ,


void *addr ,
int flag
);
Returns
pointer to shared memory segment if OK,
-1 on error

Unix System Programming Satyam Computer Services Ltd 101


Inter-process Communication

• shared memory

– The address at which segment is attached

• Depends on the addr argument and SHM_RND

– If the address is zero

• The segment is attached

– At the first available address selected by the kernel

Unix System Programming Satyam Computer Services Ltd 102


Inter-process Communication

• shared memory

– If the addr is nonzero and SHM_RND is specified

• The segment is attached at the address

– Given by (addr -(addr modulus SHMLBA ))

– Round the address to the next multiple of SHMLBA

Unix System Programming Satyam Computer Services Ltd 103


Inter-process Communication

• shared memory

– If the addr is nonzero and SHM_RND is not specified

• The segment is attached at the given address

Unix System Programming Satyam Computer Services Ltd 104


Inter-process Communication

• shared memory

– if SHM_RDONLY bit is specified in flag

• The segment is attached read-only

• otherwise the segment is attached read-write

Unix System Programming Satyam Computer Services Ltd 105


Inter-process Communication

• shared memory
– The segment can be detached using shmdt call

int shmdt ( void *addr );

Returns 0 if ok ,

-1 on error

Unix System Programming Satyam Computer Services Ltd 106

You might also like