1 / 8

Named Pipes

Named Pipes. Kinds of IPC. Mutexes/Conditional Variables/Semaphores Pipes Named pipes Signals Shared memory Messages Sockets. Named Pipes. A pipe is a unidirectional buffer between two processes handled by the kernel A pipe has a file descriptor, just like a file

dana-tran
Download Presentation

Named Pipes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Named Pipes

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

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

  4. Creating a FIFO • C – Library function called mkfifo() • Synopsis #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);

  5. Creating a FIFO file #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <iostream> 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 • use open and close as with ”normal” files • both ends of the pipe need to be opened! • Note: open returns a file descriptor • A process opening a name pipe blocks until some process opens the FIFO at "the other end" • close doesn’t need to block • A reading process blocks if the FIFO is empty • reader and writer become synchronized

  7. Opening a FIFO pipe file • 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 • 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 • Like normal files • using low-level I/O: open, read, write, close • using high level C++ I/O: <fstream> • 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 • 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 onlysome of the data into the FIFO

More Related