1.要求实现AB进程对话
a.A进程先发送—句话给B进程,B进程接收后打印
b.B进程再回复—句话给A进程,A进程接收后打印
c.重复1.2步骤,当收到quit后,要结束AB进程
2.在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。
通过线程实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
struct info
{
sem_t sem1,sem2;
int fd_r;
int fd_w;
};
void *WriteMesge(void *arg)
{
char buf[128]="";
struct info *ADC=(struct info*)arg;
while(1)
{
if(sem_wait(&ADC->sem1)<0)
{
perror("sem_post");
pthread_exit(NULL);
}
bzero(buf,sizeof(buf));
// printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(write(ADC->fd_w,buf,sizeof(buf))<0)
{
perror("write");
pthread_exit(NULL);
}
// printf("写入成功\n");
sem_post(&ADC->sem1);
if(strcmp(buf,"quit")==0)
break;
}
// sem_post(&ADC->sem1);
pthread_exit(NULL);
}
void *ReadMesge(void *arg)
{
struct info *ADC=(struct info*)arg;
while(1)
{
char buf[128]="";
if(sem_wait(&ADC->sem2)<0)
{
perror("sem_post");
pthread_exit(NULL);
}
ssize_t res=0;
bzero(buf,sizeof(buf));
res=read(ADC->fd_r,buf,sizeof(buf));
if(res<0)
{
perror("read");
pthread_exit(NULL);
}else if(0==res)
{
printf("对端程序退出\n");
break;
}
if(strcmp(buf,"quit")==0)
res=0;
printf("buf=%s\n",buf);
//printf("res=%ld buf=%s\n",res,buf);
sem_post(&ADC->sem2);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
struct info ADC;
if(mkfifo("./fifo",0775)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0775)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("fifo success\n");
ADC.fd_r=open("./fifo",O_RDONLY);
if(ADC.fd_r<0)
{
perror("open");
return -1;
}
ADC.fd_w=open("./fifo1",O_WRONLY);
if(ADC.fd_w<0)
{
perror("open");
return -1;
}
printf("open wr success\n");
if(sem_init(&ADC.sem1,0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&ADC.sem2,0,1)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,WriteMesge,&ADC)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,ReadMesge,&ADC)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
sem_destroy(&ADC.sem1);
pthread_join(tid1,NULL);
sem_destroy(&ADC.sem1);
close(ADC.fd_w);
close(ADC.fd_r);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/93d5c38b3504115b4923b9cc91bbd21a.png)
通过进程实现
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0775)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo1",0775)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("fifo success\n");
int fd_r=open("./fifo",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
int fd_w=open("./fifo1",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("open wr success\n");
pid_t cpid=fork();
if(cpid>0)
{
while(1)
{
char buf[128]="";
ssize_t res=0;
bzero(buf,sizeof(buf));
res=read(fd_r,buf,sizeof(buf));
if(res<0)
{
perror("read");
return -1;
}else if(0==res)
{
printf("对端程序退出\n");
break;
}
if(strcmp(buf,"quit")==0)
break;
printf("buf=%s\n",buf);
}
}else if(0==cpid)
{
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(write(fd_w,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
if(strcmp(buf,"quit")==0)
break;
}
}else
{
perror("fork");
return -1;
}
close(fd_w);
close(fd_r);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/dd3c3350a4717d0d5e0e0c2a1145bef4.png)