Unix Inter Process Communication
Unix Inter Process Communication
Types of IPC
Pipes
Allow data transfer in a FIFO manner Allow synchronization of process execution Use file system for data storage Can be created using pipe() system call Pipe manipulation is done using regular system calls for files e.g. read(), write() Only related processes can access a pipe
Pipes
Syntax
pipe (file_descriptors) where file_descriptors is a pointer to an array of 2 integers which are the file descriptors for reading and writing the pipe
Named Pipes
Similar to pipes Have directory entries open system call used to open named pipes mknod system call to create named pipes Unrelated processes can also communicate using named pipes
MESSAGES
Allow processes to send data streams to arbitrary processes Four system calls cover all the operations
msgget
Returns the message queue identifier assocaited with the key msgget(key, flag)
msgsnd
msgsnd (id, message, size, flag) Sends message to the queue associated with the message queue identifier specified by id message is of the form struct msgbuf { long mtype; char mtext[]; } Flags can be IPC_NOWAIT- Return -1 if the message cant be stored
msgrcv
msgsnd (id, message, size, type, flag) Reads message from the queue associated with the message queue identifier specified by id
msgctl
Msgctl ( id, command, buffer )
Provides a variety of message control functions as specified by command
Commands can be
IPC_STAT IPC_SET store information about queue in the buffer Change the information in the buffer
IPC_RMID
SHARED MEMORY
Allows processes to share part of their virtual address space System calls similar to the ones in messages
shmget
int shmget ( key, size, flag )
key is the name of the shared memory size is the size of shared memory segment in bytes flag is a combination of IPC_CREAT IPC_EXCL mode
shmat
Inserts a shared memory segment into a process address space char *shmat ( id, at_address, flag ) id is the shared memory segment identifier size is the address in process address space where the shared memory segment has to be attached flag is a combination of SHM_RDONLY read only 0 read,write
shmdt
Removes a shared memory segment into a process address space
shmctl
shmctl ( id, cmd, buf)
SEMAPHORE
Non-negative integer count used to coordinate access to the resources Initial semaphore count set to the number of free resources Count decremented or incremented by the threads or processes as and when they acquire or free resources. Threads block at count zero till it becomes positive
semget
semget ( key, num, flag )
Returns or creates the semaphore identifier associated with the key key is the name of the semaphore set number defines the number of semaphores in the set flag is a combination of
IPC_CREAT
IPC_EXCL
semop
semop ( id, operations, number )
Performs operations on the semaphore id is the semaphore identifier
semctl
semctl ( id, semnum, cmd, ... ) Performs operations on the semaphore as specified by the cmd id is the semaphore identifier Fourth argument is optional, depending upon the operation requested. If required, then its a buffer. cmd GETVAL, SETVAL, GETPID, GETNCNT, GETZCNT
SOCKET
Creates an endpoint for communication and returns a descriptor int socket ( domain, type, protocol ) domain specifies communication domain type type of communication over socket (virtual circuit or datagram) protocol protocol to control the communication
Close
Closes the communication endpoint close ( sd )
Bind
Assigns a name to an unnamed socket int bind ( sd, sockname, namelength)
Connect
Makes a connection to an existing socket int connect ( sd, sockaddr, namelength)
Accept
Receives incoming requests for a connection accept ( fd, addr, addrlen )
addr user data array which kernel fills with the address of the connecting client
send
Transmits data over a connected socket send ( sd, msg, length, flag)
recv
Receives data over a connected socket recv ( sd, buf, length, flag)
shutdown
Closes a socket connection shutdown ( sd, mode )