Different types of IPC mechanisms
• pipes
• FIFOs (named pipes)
• message queues
• semaphores
• shared memory
Pipes
* Pipes can be used to communicate between related processes i.e, parent and child (or) between two children of a parent. Pipes provide one-way communication of data. Pipe has two ends, one for reading and one for writing.
* Pipe can be created by using int pipe(int *filedes)
* The pipe system call returns two file descriptors filedes[0] for reading and filedes[1] for writing.
* One process writes onto the pipe using the write end fd and the other process reads the pipe by using the read end fd.
* Just to make sure that each process does either writing or reading using the pipes, the corresponding fd is closed in that process.
* For example, if parent writes and child reads, then in parent process, the programmer can close the read fd and in the child process the write fd can be closed.
* For creating a two-way communication, we need to create two pipes.
* Pipes use kernel memory for the actual pipe buffer. Pipe has a finite size which is set to 4096 bytes atleast.
* Disadvantages: Pipes can be used only between related processes. Pipes does not have any entry in the name space because of which it can not be used between two unrelated processes.
Named Pipes (FIFOs)
* The main difference between a FIFO and a normal pipe is that the FIFOs have an entry in the name space, so that it can be used between two unrelated processes.
* FIFO can be created by using int mknod (char *pathname, int mode, int dev)
* Once the FIFO has been created, it needs to be opened for either reading or writing using the open system call.
* Pipes or FIFO follows below rules for reading or writing
* read of data less than is in the pipe or FIFO returns the requested amount and remaining can be read by subsequent reads
* If more data is requested, only the amount available is returned
* If no data (or) no writer on the pipe, the read will return zero
* If two processes write simultaneously ( total less than max limit), then one process data follows another but won't intermix
* If process writes onto pipe and if no process opens it for reading, then SIGPIPE is generated
* unlink system call can be used to remove a FIFO.
* Pipes and FIFOs are called stream oriented IPC mechanisms, since the data that is flowing on the IPC are just stream of bytes and does not have demarkation for any fixed messages. Hence, when one process writes 100 bytes, another process can read these in 20 bytes each for 5 times.
Message Q, Semaphores, Shared Memory
* These 3 are called system V IPCs. These share a commonality. All the three IPCs can be identified by using a key_t (integer)
* System calls that operate these IPCs also are similar.
* replace ipc with msg/sem/shm to get the corresponding system call for each IPC mechanism
msgsnd/msgrecv - for send / recv in msgQ
semop - opertions on semaphores
shamat/shmdt - operations on shared memory
Message Queues
1. message type
2. length of data portion // This is optional
3. data portion
* For receiving a message, int msgrecv(int msgqid, struct msgbuf *buf, int len, long msgtype, int flags) is used.
* The msgtype indicates the type of the message that needs to be read from the Q.
If the msgtype is 0, first message on the Q is returned
If the msgtype is >0, first message with that msgtype on the Q is returned
Shared Memory
Semaphores
* Semaphores just work with the same kind of calls as anyother IPC mechanisms, like
- semget ( key, numOfSemaphoreSets , permissionflags)
* We can set the value of a semaphore to any value we want by using semctl system call.
* Semctl ( semId, semNum, Cmd, Args ) second arg is the sub semaphore number
Ex : semctl ( semId, 0, GETVAL ) To get the value of a sempahore
semctl ( semId, 0, SETVAL, 13 ) To set the value of a semaphore to 13
* How to use semaphores ?
Struct sembuf{
Ushort sem_num
Short semop
Short semflg
}
semop(semId, sembufPtr, numOfSemsInSecondArg )
Let’s take an example to explain this.
Let’s see the second argument meaning.
No comments:
Post a Comment