0% found this document useful (0 votes)
105 views

Unix Inter Process Communication

Pipe allows 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() Named Pipes similar to pipes Have directory entries "open" system call used to open 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.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Unix Inter Process Communication

Pipe allows 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() Named Pipes similar to pipes Have directory entries "open" system call used to open 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.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Unix Inter Process Communication

Types of IPC

Pipes Named Pipes Messages Shared Memory Semaphores Sockets

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 msgsend msgrec msgctl

msgget
Returns the message queue identifier assocaited with the key msgget(key, flag)

key is the name of the message queue


flag is the modes which can be passed

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

MSG_NOERROR- Message bigger than size is shortened with no error returned

msgrcv
msgsnd (id, message, size, type, flag) Reads message from the queue associated with the message queue identifier specified by id

message is of the form


struct msgbuf { long mtype; char mtext[];

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

remove the message queue identifier specified by id

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

int shmdt ( dt_address )


dt_address is the address in the process address space of a shared memory segment to be detached

shmctl
shmctl ( id, cmd, buf)

Provides shared memory control operations as specified by the cmd


cmd IPC_STAT IPC_SET IPC_RMID SHM_LOCK SHM_UNLOCK

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

operations is the array of semaphore operation structure


operation consists of - semaphore number -operation -flags

number is the number of entries in operations

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

Other Socket System Calls

Close
Closes the communication endpoint close ( sd )

sd is the socket descriptor

Bind
Assigns a name to an unnamed socket int bind ( sd, sockname, namelength)

sd is the socket descriptor


sockname is the name of the socket namelength is the length of the name

Connect
Makes a connection to an existing socket int connect ( sd, sockaddr, namelength)

sd is the socket descriptor


sockaddr is the name of the target socket

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)

msg pointer to data being sent


length length of the data

recv
Receives data over a connected socket recv ( sd, buf, length, flag)

buf pointer to data being sent


length length of the data

shutdown
Closes a socket connection shutdown ( sd, mode )

mode specifies which side no longer permits data transmission

You might also like