Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Processes II<br />
FIFOs<br />
First In, First Out is similar to a pipe (System V). FIFOs are used by <strong>the</strong> System V line printer.<br />
A FIFO is created by <strong>the</strong> mknod system call.<br />
int mknod(char *pathname, int mode, int dev); /etc/mknod name p<br />
Pipe/FIFO rules for reading and writing:<br />
- read ask for less data than is in pipe<br />
- returns requested data<br />
- leaves remainder<br />
- ask for more - only return what is available<br />
- no data in pipe - read returns zero - EOF<br />
- writes less than capacity of pipe (4096 bytes) - write is guaranteed to be atomic<br />
- write to a pipe & no read process - SIGPIPE signal<br />
Consider a daemon that uses a FIFO to receive client requests:<br />
- Daemon opens FIFO for read-only & its typical state is waiting in a read system call for a client request.<br />
- Client processes are started and <strong>the</strong>y open <strong>the</strong> FIFO for writing, write <strong>the</strong>ir request, & exit.<br />
- What happens is that <strong>the</strong> read returns zero to <strong>the</strong> daemon every time a client process terminates, if no o<strong>the</strong>r<br />
clients have FIFO open for writing.<br />
- Daemon has to <strong>the</strong>n open <strong>the</strong> FIFO again and it waits here until client opens FIFO for writing.<br />
- To avoid this, <strong>the</strong> daemon opens FIFO two times - once for reading & once for writing.<br />
- File descriptor returned for reading is used to read <strong>the</strong> client requests & fd for writing is never used.<br />
- By having FIFO always open for writing <strong>the</strong> reads do not return EOF, but wait for next client request.<br />
Client-Server FIFO example<br />
- mknod to create <strong>the</strong> FIFOs (may already exist). After fork both processes must open each of 2 FIFOs.<br />
- Parent process remove FIFOs <strong>with</strong> unlink system call, after waiting for <strong>the</strong> child to terminate.<br />
- The order of <strong>the</strong> open calls is important, and avoids a deadlock condition.<br />
- With pipes <strong>the</strong> client and server had to originate from <strong>the</strong> same process - no restriction <strong>with</strong> FIFOs.<br />
Streams and Messages<br />
The data is a stream of bytes <strong>with</strong> no interpretation done by <strong>the</strong> system. Many UNIX processes that need to impose<br />
a message structure on top of a stream based IPC facility do it using <strong>the</strong> newline character to separate messages.<br />
! Name Spaces<br />
The name is how <strong>the</strong> client and server "connect" to exchange messages<br />
IPC type Name space Identification<br />
pipe (no name) file descriptor<br />
FIFO pathname file descriptor<br />
message queue key_t key identifier<br />
shared memory key_t key identifier<br />
semaphore key_t key identifier<br />
unix socket pathname file descriptor<br />
key_t key<br />
ftok function converts a pathname to a IPC key<br />
#include <br />
#include <br />
key_t ftok(char *pathname, char proj);<br />
Guarantee a unique key<br />
32-bit inode<br />
=> 16-bits<br />
8-bit major device number<br />
8-bit minor device number => 8-bits<br />
8-bit project<br />
=> 8-bits<br />
135