1. Create a FIFO
umask (0);
mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);
2. Open a FIFO
fd = open ("/tmp/MYFIFO", O_WRONLY);
fd = open ("tmp/MYFIFO", O_RDONLY|O_NDELAY);
3. Read/Write FIFO
if (read (fd, msgbuf, MSGSIZE+1) < 0)
perror ("Message read failed!");
if ((nwrite = write (fd, msgbuf, MSGSIZE+1)) <= 0)
{
if (nwrite == 0) /* FIFO is full */
perror ("write");
}
4. Close FIFO
close (fd);
umask (0);
mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);
2. Open a FIFO
fd = open ("/tmp/MYFIFO", O_WRONLY);
fd = open ("tmp/MYFIFO", O_RDONLY|O_NDELAY);
3. Read/Write FIFO
if (read (fd, msgbuf, MSGSIZE+1) < 0)
perror ("Message read failed!");
if ((nwrite = write (fd, msgbuf, MSGSIZE+1)) <= 0)
{
if (nwrite == 0) /* FIFO is full */
perror ("write");
}
4. Close FIFO
close (fd);
本文介绍了一种用于进程间通信的方法——FIFO(先进先出)管道。内容包括创建FIFO文件、打开读写FIFO、从FIFO中读取及写入数据、以及关闭FIFO的操作步骤。
678

被折叠的 条评论
为什么被折叠?



