1、FIFO实现管道半双工通信
进程一代码:
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>char buf[128] = "";
void *fifo_w(void *arg)
{
ssize_t res = 0;
int fd = open("./fifo", O_WRONLY);
if(fd <0)
{
perror("open");
return NULL;
}while(1)
{
bzero(buf, sizeof(buf));printf("一号进程发送>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
res = write(fd, buf, sizeof(buf));
if(res<0)
{
perror("write");
return NULL;
}else if(0 == strcmp(buf, "quit"))
{
printf("一号进程退出\n");
break;
}
}
pthread_exit(NULL);
close(fd);}
void *fifo_r(void *arg)
{
ssize_t res = 0;
int fd_2 = open("./2_fifo", O_RDONLY);
if(fd_2 <0)
{
perror("open");
return NULL;
}while(1)
{//接受二号进程的内容
bzero(buf, sizeof(buf));
res = read(fd_2, buf, sizeof(buf));
if(0 == strcmp(buf, "quit"))
{
printf("一号进程已退出\n");
break;
}else if(res >0)
{
printf("二号进程发给一号进程的内容\n");
printf("\n%s\n", buf);
}else if(res<0)
{
perror("read");
return NULL;
}}
pthread_exit(NULL);
close(fd_2);}
int main(int argc, const char *argv[])
{
umask(0);if(mkfifo("./fifo", 0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("FIFO create success\n");if(mkfifo("./2_fifo", 0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("FIFO create success\n");
pthread_t pid1, pid2;
if(pthread_create(&pid1, NULL,fifo_w, NULL ) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid2, NULL,fifo_r, NULL ) != 0)
{
perror("pthread_create");
return -1;
}pthread_join(pid1,NULL);
pthread_join(pid2,NULL);system("rm fifo");
system("rm 2_2_fifo");return 0;
}
进程二代码:
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>char buf[128] = "";
void *fifo_w(void *arg)
{
ssize_t res = 0;
int fd_2 = open("./2_fifo", O_WRONLY);
if(fd_2 <0)
{
perror("open");
return NULL;
}while(1)
{
bzero(buf, sizeof(buf));
printf("二号进程发送>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
res = write(fd_2, buf, sizeof(buf));
if(res<0)
{
perror("write");
return NULL;
}else if(0 == strcmp(buf, "quit"))
{
printf("二号进程退出\n");
break;
}
printf("二号进程已发送,等待一号回复\n");}
pthread_exit(NULL);
close(fd_2);}
void *fifo_r(void *arg)
{
ssize_t res = 0;
int fd = open("./fifo", O_RDONLY);
if(fd <0)
{
perror("open");
return NULL;
}while(1)
{
bzero(buf, sizeof(buf));
res = read(fd, buf, sizeof(buf));
if(0 == strcmp(buf, "quit"))
{
printf("一号进程已退出\n");
break;
}else if(res >0)
{
printf("一号进程发给二号进程的内容\n");
printf("%s\n", buf);
}else
{
perror("read");
return NULL;
}
}
pthread_exit(NULL);
close(fd);}
int main(int argc, const char *argv[])
{
umask(0);if(mkfifo("./fifo", 0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("FIFO create success\n");if(mkfifo("./2_fifo", 0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("FIFO create success\n");
pthread_t pid1, pid2;
if(pthread_create(&pid1, NULL,fifo_w, NULL ) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid2, NULL,fifo_r, NULL ) != 0)
{
perror("pthread_create");
return -1;
}pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
return 0;
}