#include<unistd.h> int pipe(int filedes[2]);
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
/*
* 有亲缘关系的进程间的fifo的使用
* fifo 使用的简单例子
*/
#include "../all.h"
#define FIFO_PATH "/tmp/hover_fifo"
void
do_sig(int signo)
{
if (signo == SIGCHLD)
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
int
main(void)
{
int ret;
int fdr, fdw;
pid_t pid;
char words[10] = "123456789";
char buf[10] = {' '};
// 创建它,若存在则不算是错误,
// 若想修改其属性需要先打开得到fd,然后用fcntl来获取属性,然后设置属性.
if (((ret = mkfifo(FIFO_PATH, FILE_MODE)) == -1)
&& (errno != EEXIST))
perr_exit("mkfifo()");
fprintf(stderr, "fifo : %s created successfully!n", FIFO_PATH);
signal(SIGCHLD, do_sig);
pid = fork();
if (pid == 0) { // child
if ((fdr = open(FIFO_PATH, O_WRONLY)) < 0) // 打开fifo用来写
perr_exit("open()");
sleep(2);
// 写入数据
if (write(fdr, words, sizeof(words)) != sizeof(words))
perr_exit("write");
fprintf(stderr, "child write : %sn", words);
close(fdw);
} else if (pid > 0) { // parent
if ((fdr = open(FIFO_PATH, O_RDONLY)) < 0) // 打开fifo用来读
perr_exit("open()");
fprintf(stderr, "I father read, waiting for child ...n");
if (read(fdr, buf, 9) != 9) //读数据
perr_exit("read");
fprintf(stderr, "father get buf : %sn", buf);
close(fdr);
}
// 到这里fifo管道并没有被删除,必须手动调用函数unlink或remove删除.
return 0;
}
/*
* FIFO server
*/
#include "all.h"
int
main(void)
{
int fdw, fdw2;
int fdr;
char clt_path[PATH_LEN] = {' '};
char buf[MAX_LINE] = {' '};
char *p;
int n;
if (mkfifo(FIFO_SVR, FILE_MODE) == -1 && errno != EEXIST)
perr_exit("mkfifo()");
if ((fdr = open(FIFO_SVR, O_RDONLY)) < 0)
perr_exit("open()");
/*
* 根据fifo的创建规则, 若从一个空管道或fifo读,
* 而在读之前管道或fifo有打开来写的操作, 那么读操作将会阻塞
* 直到管道或fifo不打开来读, 或管道或fifo中有数据为止.
*
* 这里,我们的fifo本来是打开用来读的,但是为了,read不返回0,
* 让每次client端读完都阻塞在fifo上,我们又打开一次来读.
* 见unpv2 charper 4.7
*/
if ((fdw2 = open(FIFO_SVR, O_WRONLY)) < 0)
fprintf(stderr, "open()");
while (1) {
/* read client fifo path from FIFO_SVR */
/* 这里由于FIFO_SVR有打开来写的操作,所以当管道没有数据时,
* read会阻塞,而不是返回0.
*/
if (read(fdr, clt_path, PATH_LEN) < 0) {
fprintf(stderr, "read fifo client path error : %sn", strerror(errno));
break;
}
if ((p = strstr(clt_path, "rn")) == NULL) {
fprintf(stderr, "clt_path error: %sn", clt_path);
break;
}
*p = ' ';
DBG("clt_path", clt_path);
if (access(clt_path, W_OK) == -1) { // client fifo ok, but no permission
perror("access()");
continue;
}
/* open client fifo for write */
if ((fdw = open(clt_path, O_WRONLY)) < 0) {
perror("open()");
continue;
}
if ((n = read(fdr, buf, WORDS_LEN)) > 0) { /* read server words is ok */
printf("server read words : %sn", buf);
buf[n] = ' ';
write(fdw, buf, strlen(buf));
}
}
close(fdw);
unlink(FIFO_SVR);
exit(0);
}
/*
* Fifo client
*
*/
#include "all.h"
int
main(void)
{
int fdr, fdw;
pid_t pid;
char clt_path[PATH_LEN] = {' '};
char buf[MAX_LINE] = {' '};
char buf_path[MAX_LINE] = {' '};
snprintf(clt_path, PATH_LEN, FIFO_CLT_FMT, (long)getpid());
DBG("clt_path1 = ", clt_path);
snprintf(buf_path, PATH_LEN, "%srn", clt_path);
if (mkfifo(clt_path, FILE_MODE) == -1 && errno != EEXIST)
perr_exit("mkfifo()");
/* client open clt_path for read
* open server for write
*/
if ((fdw = open(FIFO_SVR, O_WRONLY)) < 0)
perr_exit("open()");
/* write my fifo path to server */
if (write(fdw, buf_path, PATH_LEN) != PATH_LEN)
perr_exit("write()");
if (write(fdw, WORDS, WORDS_LEN) < 0) /* write words to fifo server */
perr_exit("error");
if ((fdr = open(clt_path, O_RDONLY)) < 0)
perr_exit("open()");
if (read(fdr, buf, WORDS_LEN) > 0) { /* read reply from fifo server */
buf[WORDS_LEN] = ' ';
printf("server said : %sn", buf);
}
close(fdr);
unlink(clt_path);
exit(0);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有