实现A,B进程对话。
A进程传递信息给B进程,B进程打印
B进程传递信息给B进程,A进程打印
直到收到quit后,结束A,B进程
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
int main(int argc, const char *argv[])
{
charbuf1[32];
charbuf2[32];
intret;
if(mkfifo("./fifo1",0777)<0){
if(errno!=17){
perror("mkfifo2error");
return-1;
}
}
if(mkfifo("./fifo2",0777)<0){
if(errno!=17){
perror("mkfifo2error");
}
}
printf("makefifo succeed\n");
intfd_r=open("./fifo1",O_RDONLY);
if(fd_r<0){
perror("openerror 1");
return-1;
}
intfd_w=open("./fifo2",O_WRONLY);
if(fd_w<0){
perror("openerror 2");
return-1;
}
while(1){
ret=read(fd_r,buf2,sizeof(buf2));
if(ret==0){
break;
}
printf("%s\n",buf2);
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1]='\0';
if(strcmp(buf1,"quit")==0){
break;
}
write(fd_w,buf1,sizeof(buf1));
}
close(fd_r);
close(fd_w);
return0;
}
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
int main(int argc, const char *argv[])
{
charbuf1[32];
charbuf2[32];
int ret;
intfd_w=open("./fifo1",O_WRONLY);
if(fd_w<0){
perror("openerror");
return-1;
}
intfd_r=open("./fifo2",O_RDONLY);
if(fd_r<0){
perror("open2 error");
return-1;
}
while(1){
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1]='\0';
if(strcmp(buf1,"quit")==0){
break;
}
write(fd_w,buf1,sizeof(buf1));
ret=read(fd_r,buf2,sizeof(buf2));
if(ret==0){
break;
}
printf("%s\n",buf2);
}
close(fd_r);
close(fd_w);
return0;
}
2.在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
void* callread(void* arg)
{
intret=0;
charbuf2[32]="";
intfd_r=*((int*)arg);
while(1){
ret=read(fd_r,buf2,sizeof(buf2));
if(ret==0){
_exit(5);
}
printf("%s\n",buf2);
}
}
void* callwrite(void* arg)
{
charbuf1[32]="";
intfd_w=*((int*)arg);
while(1){
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1]='\0';
if(strcmp(buf1,"quit")==0){
_exit(5);
}
write(fd_w,buf1,sizeof(buf1));
}
}
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1",0777)<0){
if(errno!=17){
perror("mkfifo2error");
return-1;
}
}
if(mkfifo("./fifo2",0777)<0){
if(errno!=17){
perror("mkfifo2error");
}
}
printf("makefifo succeed\n");
intfd_r=open("./fifo1",O_RDONLY);
if(fd_r<0){
perror("openerror 1");
return-1;
}
intfd_w=open("./fifo2",O_WRONLY);
if(fd_w<0){
perror("openerror 2");
return-1;
}
pthread_ttid1,tid2;
if(pthread_create(&tid1,NULL,callwrite,(void*)&fd_w)){
fprintf(stderr,"pthread_create");
return-1;
}
if(pthread_create(&tid2,NULL,callread,(void*)&fd_r)){
fprintf(stderr,"pthread_create");
return-1;
}
/*
while(1){
}
*/
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd_r);
close(fd_w);
return0;
}
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
void* callread(void* arg){
intfd_r=*((int*)arg);
int ret=0;
char buf2[32]="";
while(1){
ret=read(fd_r,buf2,sizeof(buf2));
if(ret==0){
_exit(5);
}
printf("%s\n",buf2);
}
}
void* callwrite(void* arg){
intfd_w=*((int*)arg);
charbuf1[32]="";
while(1){
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1]='\0';
if(strcmp(buf1,"quit")==0){
_exit(5);
}
write(fd_w,buf1,sizeof(buf1));
}
}
int main(int argc, const char *argv[])
{
intfd_w=open("./fifo1",O_WRONLY);
if(fd_w<0){
perror("openerror");
return-1;
}
intfd_r=open("./fifo2",O_RDONLY);
if(fd_r<0){
perror("open2 error");
return-1;
}
pthread_ttid1,tid2;
if(pthread_create(&tid1,NULL,callread,(void*)&fd_r)){
fprintf(stderr,"pthread_create");
return-1;
}
if(pthread_create(&tid2,NULL,callwrite,(void*)&fd_w)){
fprintf(stderr,"pthread_create");
return-1;
}
/*
while(1){
*/
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd_r);
close(fd_w);
return0;
}