mknod namepipe |
#include <sys/types.h>
#include <sys/stat.h>
int mknod(const char *pathname, mode_t mode, dev_t dev);
int mkfifo(const char *pathname, mode_t mode);
umask(0);
if (mknod ("/tmp/fifo", S_IFIFO | 0666, 0) == -1) {
perror("mknod error!");
exit(1);
}
umask(0);
if (mkfifo ("/tmp/fifo", S_IFIFO | 0666, 0) == -1) {
perror("mkfifo error!");
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE];
umask(0);
fd = open(FIFO_NAME, O_RDONLY);
read(fd, buf, BUF_SIZE);
printf("Read content: %s\\n", buf);
close(fd);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#define FIFO_NAME "myfifo"
#define BUF_SIZE 1024
int main(void)
{
int fd;
char buf[BUF_SIZE] = "Hello procwrite, I come from process named procread!";
umask(0);
if (mkfifo(FIFO_NAME, S_IFIFO | 0666) == -1) {
perror("mkfifo error!");
exit(1);
}
if ((fd = open(FIFO_NAME, O_WRONLY)) == -1) {
perror("open error!");
exit(1);
}
write(fd, buf, strlen(buf)+1); /*strlen(buf)+1 是把'\\0'也写过去*/
close(fd);
exit(0);
}
beyes@linux-beyes:~/C/pipe> ./procread.exe
Read content: Hello procwrite, I come from process named procread!
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |