Presentation is loading. Please wait.

Presentation is loading. Please wait.

Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.

Similar presentations


Presentation on theme: "Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets."— Presentation transcript:

1 Named Pipes

2 Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets

3 Named Pipes u A pipe is a unidirectional buffer between two processes handled by the kernel u A pipe has a file descriptor, just like a file u Unnamed pipes can only be used between processes with a common ancestor  Named pipes do not require any relationship u Named pipes are special files within the file system  you can use ‘ls’ and see them

4 Creating a FIFO u C – Library function called mkfifo() u Synopsis #include int mkfifo(const char *pathname, mode_t mode);

5 Creating a FIFO file #include using namespace std; int main(){ if(access("my_fifo",F_OK) == 0) cout << "FIFO already exists" << endl; else { int res = mkfifo("my_fifo", 0777); if (res==0) cout << "FIFO created" << endl; else cout << "FIFO not created" << endl; }

6 Working with FIFOs u use open and close as with ”normal” files  both ends of the pipe need to be opened!  Note: open returns a file descriptor u A process opening a name pipe blocks until some process opens the FIFO at "the other end"  close doesn’t need to block u A reading process blocks if the FIFO is empty  reader and writer become synchronized

7 Opening a FIFO pipe file u Named pipes are opened like other files  For example, by using the open system call  flags specify whether to read or write, and whether to block u Opening a FIFO with the O_NONBLOCK flag makes access to the pipe non-blocking:  for O_READ, open returns immediately  even if the writing end has not been opened by another process  for O_WRITE, open returns immediately, but returns -1 if the reading end is not open  Also subsequent read and write operations become nonblocking!

8 Reading and writing to FIFOs u Like normal files  using low-level I/O: open, read, write, close  using high level C++ I/O: u For FIFOs in blocking mode  reading from an empty fifo blocks  writing to a full FIFO blocks  size of FIFO buffer PIPE_BUF = 4096 KB u For FIFOs opened with O_NONBLOCK  reading from an empty FIFO succeeds with 0 bytes of data  writing to a full or nearly full FIFO may fail, or may write only some of the data into the FIFO


Download ppt "Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets."

Similar presentations


Ads by Google