Article Title: LINUX Process Control (3 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
5.4. Pipelines (FIFO)
The PIPE method is similar to that of an unnamed pipeline. Another pipeline is named First In (First Out ). The difference between the two is that PIPE is not persistent, that is, after all processes using PIPE terminate, PIPE automatically disappears; while FIFO corresponds to a physical file, which is persistent, in addition, multiple non-inherited processes can open the physical file and use the same FIFO.
5.4.1. Create and delete a FIFO file
Creating a FIFO file is similar to creating a common file, but the created file is used for FIFO.
Prototype:
# Include
# Include
Int mkfifo (const char * pathname, mode_t mode );
The pathname parameter is the full path name of the FIFO file to be created;
The parameter mode is the file access permission.
If the creation is successful, 0 is returned; otherwise,-1 is returned.
Example:
// MkFifo. cpp {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{
# Include
# Include
# Include
# Include
Int main (int argc, char * argv [])
{
Mode_t iRight = 0666;
If (argc! = 2 ){
Puts ("Usage: mkw.o.exe {filename }");
Return-1;
}
If (mkfifo (argv [1], iRight) <0 ){
Perror ("mkfifo fail ");
Return-2;
}
Return 0;
}
// MkFifo. cpp }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}
Shell> mkw.o.exe MyFifo. dat
Shell> ll
Total 16
-Rw-r -- 1 ghaha 296 Oct 29 MkFifo. cpp
Prw-rw-r -- 1 ghaha 0 Oct 29 MyFifo. dat
-Rwxrwxr-x 1 ghaha 11939 Oct 29 mk1_o.exe
Shell> MkFifo MyFifo. dat // a file error is reported when the second creation is performed.
Mkfifo fail: File exists
The function prototype for deleting files is:
# Include
Int unlink (const char * pathname );
5.4.2. open or close a FIFO file
Open functions are used to open/close a file of the FIFO type, just like a common file. If the O_WRONLY option is enabled, the write end of the FIFO is enabled. If the O_RDONLY option is used, the read end of the FIFO is enabled. Both the write end and the read end can be opened by several processes at the same time.
If you enable FIFO in Read mode and no other processes enable FIFO in write mode, the open function is blocked. Similarly, if you enable FIFO in write mode, and no other processes read in FIFO mode, the open function will also be blocked. However, if the open function contains the O_NONBLOCK option, the call to open is not blocked in both cases.
Similar to PIPE, when the FIFO is disabled, if the read end is disabled first, the process that writes data to the FIFO will continue to receive the SIG_PIPE signal.
5.4.3. read/write FIFO
Read/write FIFO can be used in the same way as normal files,
Example:
// WriteFifo. cpp {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{{{{{{{
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Int main (int argc, char * argv [])
{
Char szBuf [128];
Int iRet, fdFifo;
Puts ("open for writing .");
FdFifo = open ("MyFifo. pip", O_WRONLY );
If (fdFifo <0 ){
Perror ("open fifo fail ");
Return-1;
}
Puts ("begin write \ n ");
Srand (time (NULL ));
For (int I = 0; I <10; I ++ ){
Sprintf (szBuf, "ps % d write % d", getpid (), I );
Printf ("% s \ n", szBuf );
IRet = write (fdFifo, szBuf, strlen (szBuf ));
If (iRet <0 ){
Perror ("write fifo fail ");
Return-2;
}
Int iuSec = int (1000*1000 * (double (rand ()/RAND_MAX ));
Usleep (iuSec );
}
Close (fdFifo );
Return 0;
}
// WriteFifo. cpp }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
[1] [2] [3] [4] Next page