ps: 还是学生,初次写博客,太菜了,不好意思
int pipe(int pipefd[2]) | |
---|---|
pipefd | 一个至少具有2int型的数组,用来存读/写描述符 |
成功返回0 | 失败返回-1 |
功能 | 创建有名管道:FIFO |
头文件 | #include <sys/types.h> #include <sys/stat.h> |
原型 | int mkfifo(const char *pathname,mode_t mode |
参数 |
pathname :FIFO的文件名 mode : 权限 |
返回值 | 成功 0
失败 -1 |
-
父进程与子进程采用无名管道进行通信
#include <stdio.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <linux/input.h>
int main(int argc, char const *argv[])
{
int fb[2],fb1[2];
int ret = pipe(fb);
int ret1 = pipe(fb1);
if (ret == -1)
{
perror("pipe()");
return 0;
}
pid_t pid = fork();
if (pid == -1)
{
perror("foek()");
return 0;
}
if (pid == 0)
{
int i = 0;
char pri[100];
char pri1[100];
int flg = 1;
while(1)
{
bzero(pri1,100);
bzero(pri,100);
read(fb1[0],pri1,sizeof(pri1));
printf("父亲来话:%s\n",pri1);
printf("发送会发给父亲:");
fgets(pri,100,stdin);
write(fb[1],pri,strlen(pri));
}
}
if(pid >0)
{
char pri[100];
char pri1[100];
int flg = 1;
while(1)
{
bzero(pri1,100);
bzero(pri,100);
printf("发送会发给儿子:");
fgets(pri1,100,stdin);
write(fb1[1],pri1,strlen(pri1));
read(fb[0],pri,sizeof(pri));
printf("儿子来话: %s\n",pri);
}
}
return 0;
}
-
两个程序采用有名管道通信 分别运行两个程序
-
#include <stdio.h> #include <sys/mman.h> #include <errno.h> #include <unistd.h> #include <string.h> #include <strings.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <linux/input.h> #define FIFO "/tmp/fifo_test" #define FIFO1 "/tmp/fifo_test1" int main(int argc, char const *argv[]) { if (access(FIFO,F_OK)) { int ret = mkfifo(FIFO,0666); if (ret = -1) { perror("[1] : mkfifo fail"); return 0; } } if (access(FIFO1,F_OK)) { int ret1 = mkfifo(FIFO1,0666); if (ret1 = -1) { perror("[2] :mkfifo fail"); return 0; } } int fd1 = open(FIFO1,O_RDONLY); int fd = open(FIFO,O_WRONLY); char buf[100]; char buf1[100]; pid_t par_pid = getpid(); pid_t pid = fork(); if (pid == -1) { perror("fork() fail: "); return 0; } if (pid == 0) { while(1) { bzero(buf,100); bzero(buf1,100); read(fd1,buf1,sizeof(buf1)); if (strncmp(buf1,"exit",4) == 0) { kill(par_pid,9); exit(3); } printf("from to two: %s",buf1); } } if (pid > 0) { pid_t stat; while(1) { waitpid(-1,&stat,WNOHANG); if (WIFEXITED(stat)) { return 0; } bzero(buf,100); bzero(buf1,100); fgets(buf,100,stdin); write(fd,buf,strlen(buf)); usleep(1000); if (strncmp(buf,"exit",4) == 0) { exit(4); } } } return 0; }
#include <stdio.h> #include <sys/mman.h> #include <errno.h> #include <unistd.h> #include <string.h> #include <strings.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <linux/input.h> #define FIFO "/tmp/fifo_test" #define FIFO1 "/tmp/fifo_test1" int main(int argc, char const *argv[]) { if (access(FIFO,F_OK)) { int ret = mkfifo(FIFO,0666); if (ret = -1) { perror("[1]: mkfifo fail"); return 0; } } if (access(FIFO1,F_OK)) { int ret1 = mkfifo(FIFO1,0666); if (ret1 = -1) { perror("[2]: mkfifo fail"); return 0; } } int fd1 = open(FIFO1,O_WRONLY); int fd = open(FIFO,O_RDONLY); char buf[100]; char buf1[100]; pid_t pid = fork(); if (pid == -1) { perror("fork() fail: "); return 0; } if (pid > 0) { while(1) { bzero(buf,100); char buf1[100]; read(fd,buf,sizeof(buf)); usleep(1000); if (strncmp(buf,"exit",4) == 0) { exit(2); } printf("from to one: %s",buf); } } if (pid == 0) { while(1) { bzero(buf,100); char buf1[100]; fgets(buf1,100,stdin); write(fd1,buf1,strlen(buf1)); if (strncmp(buf1,"exit",4) == 0) { exit(1); } } } return 0; }