1.创建AB进程,要求如下:
(1)A进程先发送一句话给B进程,B进程接收到后打印到终端上;
(2)在a要求之后,B进程发送一句话给A进程,A进程接收后打印。
(3)重复a, b步骤,直到发送或者接收到quit后,结束AB进程。
附加题
(4)在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据,反之亦然。
进程A代码如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
int fd1, fd2, fd3, fd4;
//接受终端输入的数据,并发送给另一个进程,当前进程A
void *callback1(void *arg)
{
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
write(fd1, buf, sizeof(buf));
if(strcasecmp(buf, "quit") == 0)
exit(0);
bzero(buf, sizeof(buf));
read(fd2, buf, sizeof(buf));
printf("%s\n", buf);
}
}
//接收另一个进程发送的数据
void *callback2(void *arg)
{
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
res = read(fd3, buf, sizeof(buf));
if(res > 0)
{
printf("A receive:%s\n", buf);
if(strcasecmp(buf, "quit") == 0)
exit(0);
bzero(buf, sizeof(buf));
strcpy(buf, "A receive success");
write(fd4, buf, strlen(buf));
}
else if(res == 0)
{
printf("对端退出\n");
exit(0);
}
}
}
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1", 0755) < 0) //A->B A给B发送数据
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2", 0755) < 0) //B->A A接受B成功接收信号
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo3", 0755) < 0) //B->A A接受B发送的数据
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo4", 0755) < 0) //A->B A向B返回成功接收信号
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
fd1 = open("./fifo1", O_WRONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
fd2 = open("./fifo2", O_RDONLY);
if(fd2 < 0)
{
perror("open");
return -1;
}
fd3 = open("./fifo3", O_RDONLY);
if(fd3 < 0)
{
perror("open");
return -1;
}
fd4 = open("./fifo4", O_WRONLY);
if(fd4 < 0)
{
perror("open");
return -1;
}
printf("open fifo success A\n");
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, callback1, NULL) != 0)
{
printf("pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2, NULL, callback2, NULL) != 0)
{
printf("pthread_create failed\n");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
close(fd1);
close(fd2);
close(fd3);
close(fd4);
printf("进程A退出\n");
return 0;
}
进程B代码如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
int fd1, fd2, fd3, fd4;
//接受终端输入的数据,并发送给另一个进程,当前进程B
void *callback1(void *arg)
{
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
write(fd3, buf, sizeof(buf));
if(strcasecmp(buf, "quit") == 0)
exit(0);
bzero(buf, sizeof(buf));
read(fd4, buf, sizeof(buf));
printf("%s\n", buf);
}
}
//接收另一个进程发送的数据
void *callback2(void *arg)
{
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
res = read(fd1, buf, sizeof(buf));
if(res > 0)
{
printf("B receive:%s\n", buf);
if(strcasecmp(buf, "quit") == 0)
exit(0);
bzero(buf, sizeof(buf));
strcpy(buf, "B receive success");
write(fd2, buf, strlen(buf));
}
else if(res == 0)
{
printf("对端退出\n");
exit(0);
}
}
}
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1", 0755) < 0) //A->B B接收A发送的数据
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2", 0755) < 0) //B->A B向A返回成功接收信号
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo3", 0755) < 0) //B->A B向A发送数据
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo4", 0755) < 0) //A->B B接收A成功接收信号
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
fd1 = open("./fifo1", O_RDONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
fd2 = open("./fifo2", O_WRONLY);
if(fd2 < 0)
{
perror("open");
return -1;
}
fd3 = open("./fifo3", O_WRONLY);
if(fd3 < 0)
{
perror("open");
return -1;
}
fd4 = open("./fifo4", O_RDONLY);
if(fd4 < 0)
{
perror("open");
return -1;
}
printf("open fifo success A\n");
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, callback1, NULL) != 0)
{
printf("pthread_create failed\n");
return -1;
}
if(pthread_create(&tid2, NULL, callback2, NULL) != 0)
{
printf("pthread_create failed\n");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
close(fd1);
close(fd2);
close(fd3);
close(fd4);
printf("进程B退出\n");
return 0;
}
运行结果