1.创建AB进程,要求如下:
a. A进程先发送一句话给B进程,B进程接收到后打印到终端上;
b.在a要求之后,B进程发送一句话给A进程,A进程接收后打印;
c.重复a, b步骤,直到发送或者接收到quit后,结束AB进程。
ubuntu@ubuntu:~/32/exercise$ cat a.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
char buf[128]="";
ssize_t res = 0;
while(1)
{
int fd = open("./myfifo",O_WRONLY);
if(fd<0)
{
perror("open");
return -1;
}
printf("please input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(0==strcasecmp(buf,"quit"))
break;
if(write(fd,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
close(fd);
int fd1 = open("./myfifo",O_RDONLY);
if(fd1<0)
{
perror("open");
return -1;
}
bzero(buf,sizeof(buf));
res = read(fd,buf,sizeof(buf));
if(res<0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
printf("%s\n",buf);
if(0==strcasecmp(buf,"quit"))
break;
close(fd1);
}
return 0;
}
ubuntu@ubuntu:~/32/exercise$ cat b.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, const char *argv[])
{
char buf[128]="";
ssize_t res = 0;
while(1)
{
int fd = open("./myfifo",O_RDONLY);
if(fd<0)
{
perror("open");
return -1;
}
bzero(buf,sizeof(buf));
res = read(fd,buf,sizeof(buf));
if(res<0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
printf("%s\n",buf);
if(0==strcasecmp(buf,"quit"))
break;
close(fd);
int fd1 = open("./myfifo",O_WRONLY);
if(fd1<0)
{
perror("open");
return -1;
}
printf("please input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(0==strcasecmp(buf,"quit"))
break;
if(write(fd,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
close(fd1);
}
return 0;
}
2.在第1题的基础上实现,A能随时发信息给B,B能随时接收A发送的数据,反之亦然。
ubuntu@ubuntu:~/32/work/io/day8$ cat AcommunicationB.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
pthread_t tid1,tid2;
char buf[128]="";
void* callBack_w(void* arg)
{
int fd = open("./A",O_WRONLY);
if(fd<0)
{
perror("open");
return NULL;
}
while(1)
{
// printf("please input:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(0==strcasecmp(buf,"quit"))
break;
if(write(fd,buf,sizeof(buf))<0)
{
perror("write");
return NULL;
}
}
close(fd);
pthread_exit(NULL);
}
void* callBack_r(void* arg)
{
int fd = open("./B",O_RDONLY);
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
res = read(fd,buf,sizeof(buf));
if(res<0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
// printf("receive:%s\n",buf);
printf("%s\n",buf);
if(0==strcasecmp(buf,"quit"))
break;
}
close(fd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./A",0775)<0)
{
if(errno!=17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./B",0775)<0)
{
if(errno!=17)
{
perror("mkfifo");
return -1;
}
}
pthread_create(&tid1,NULL,callBack_w,NULL);
pthread_create(&tid2,NULL,callBack_r,NULL);
pthread_join(tid2,NULL);
return 0;
}
ubuntu@ubuntu:~/32/work/io/day8$ cat BcommunicationA.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
pthread_t tid1,tid2;
char buf[128]="";
void* callBack_r(void* arg)
{
int fd = open("./A",O_RDONLY);
while(1)
{
bzero(buf,sizeof(buf));
ssize_t res=0;
res = read(fd,buf,sizeof(buf));
if(res<0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
// printf("receive:%s\n",buf);
printf("%s\n",buf);
if(0==strcasecmp(buf,"quit"))
break;
}
close(fd);
pthread_exit(NULL);
}
void* callBack_w(void* arg)
{
int fd = open("./B",O_WRONLY);
while(1)
{
// printf("please input:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(0==strcasecmp(buf,"quit"))
break;
if(write(fd,buf,sizeof(buf))<0)
{
perror("write");
return NULL;
}
}
close(fd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_create(&tid1,NULL,callBack_w,NULL);
pthread_create(&tid2,NULL,callBack_r,NULL);
pthread_join(tid2,NULL);
return 0;
}