创建
AB
进程,实现
AB
进程的通话。
1
)
A
进程先发送一句话给
B
进程,
B
进程接收打印。
2
)
B
进程发送与句话给
A
进程,
A
进程接收后打印。
3
)重复
1
)
2
)步骤即可。
4
)当
A
进程或者
B
进程发送
quit
后,
AB
进程均要结束。
实现随时收发
A进程
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
//A写入
pthread_t R,W;
void * callback_W(void* arg)
{
int fd1=open("./fifo1",O_WRONLY);
if(fd1<0)
{
perror("open");
return NULL;
}
char buf[128]="";
char arr[10]="quit";
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入A进程向B进程接发送的话:\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(write(fd1,buf,sizeof(buf))<0)
{
perror("write");
}
if(strcmp(buf,arr)==0)
{
close(fd1);
break;
}
}
pthread_cancel(R);
pthread_exit(NULL);
}
//A读取
void * callback_R(void* arg)
{
int fd2=open("./fifo2",O_RDONLY);
if(fd2<0)
{
perror("open");
return NULL;
}
char buf[128]="";
char arr[10]="quit";
while(1)
{
bzero(buf,sizeof(buf));
ssize_t res;
res=read(fd2,buf,sizeof(buf));
if(res<0)
{
perror("read");
}
else if(res==0)
{
break;
}
printf("A进程接收到的话:%s\n",buf);
if(strcmp(buf,arr)==0)
{
close(fd2);
break;
}
}
pthread_cancel(W);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo1",0777)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfio success");
if(mkfifo("./fifo2",0777)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfio success");
//线程创建
if(pthread_create(&W,NULL,callback_W,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&R,NULL,callback_R,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(W,NULL);
pthread_join(R,NULL);
return 0;
}
B进程
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
//B接受
pthread_t W,R;
void * callback_R(void *arg)
{
int fd1=open("./fifo1",O_RDONLY);
if(fd1<0)
{
perror("open");
}
while(1)
{
char buf[128]="";
char arr[10]="quit";
bzero(buf,sizeof(buf));
ssize_t res;
res=read(fd1,buf,sizeof(buf));
if(res<0)
{
perror("read");
return NULL;
}
else if(res==0)
{
break;
}
printf("B进程接收到的话:%s\n",buf);
if(strcmp(buf,arr)==0)
{
close(fd1);
break;
}
}
pthread_cancel(W);
pthread_exit(NULL);
}
B写入
void * callback_W(void *arg)
{
int fd2=open("./fifo2",O_WRONLY);
if(fd2<0)
{
perror("open");
return NULL;
}
while(1)
{
char buf[128]="";
char arr[10]="quit";
bzero(buf,sizeof(buf));
printf("请输入B进程向A进程接发送的话:\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(write(fd2,buf,sizeof(buf))<0)
{
perror("write");
return NULL;
}
if(strcmp(buf,arr)==0)
{
close(fd2);
break;
}
}
pthread_cancel(R);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo1",0777)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfio success");
if(mkfifo("./fifo2",0777)<0)
{
if(17!=errno)
{
perror("mkfifo");
return -1;
}
}
printf("mkfio success");
if(pthread_create(&W,NULL,callback_W,NULL)!=0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&R,NULL,callback_R,NULL)!=0)
{
perror("pthread_create");
return -1;
}
pthread_join(W,NULL);
pthread_join(R,NULL);
return 0;
}