1、a.A进程先发送一句话给B进程,B进程接收到后打印到终端上;b.在a要求之后,B进程发送一句话给A进程,A进程接收后打印。c.重复a, b步骤,直到发送或者接收到quit后,结束AB进程。
//1
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo1");
return -1;
}
}
printf("create fifo success\n");
printf("create fifo1 success\n");
int fd = open("./fifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fp = open("./fifo1",O_RDONLY);
if(fp < 0)
{
perror("open");
return -1;
}
printf("open fifo success\n");
printf("open fifo1 success\n");
char buf[32] = "";
char buf1[32] = "";
ssize_t res = 0;
while(1)
{
//写
bzero(buf,sizeof(buf));
printf("input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fd,buf,sizeof(buf)) < 0)
{
perror("w-write");
return -1;
}
printf("write success\n");
//读
bzero(buf,sizeof(buf));
res = read(fp,buf,sizeof(buf));
if(res < 0)
{
printf("w-read failed\n");
return -1;
}
else if(0 == res)
{
printf("w-input close\n");
break;
printf("%s\n",buf);
}
if(!strcmp(buf,"quit"))
break;
printf("res1 = %ld %s\n",res,buf);
bzero(buf,sizeof(buf));
}
close(fd);
close(fp);
return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo1");
return -1;
}
}
printf("create fifo success\n");
printf("create fifo1 success\n");
int fd = open("./fifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fp = open("./fifo1",O_WRONLY);
if(fp < 0)
{
perror("open");
return -1;
}
printf("open fifo success\n");
printf("open fifo1 success\n");
ssize_t res = 0;
char buf[32] = "";
char buf1[32] = "";
//读
while(1)
{
bzero(buf,sizeof(buf));
//read
res = read(fd,buf,sizeof(buf));
if(res < 0)
{
printf("r-read failed\n");
return -1;
}
else if(0 == res)
{
printf("r-input close\n");
break;
}
if(!strcmp(buf,"quit"))
break;
printf("res = %ld %s\n",res,buf);
bzero(buf,sizeof(buf));
//write
printf("input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fp,buf,sizeof(buf)) < 0)
{
perror("r_write");
return -1;
}
printf("r-write success\n");
}
close(fd);
close(fp);
return 0;
}
a.A进程先发送一句话给B进程,B进程接收到后打印到终端上;b.在a要求之后,B进程发送一句话给A进程,A进程接收后打印。c.重复a, b步骤,直到发送或者接收到quit后,结束AB进程。
【---------------------------------待修正----------------------------------------】
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
void* callBack(void* arg)//r
{
int fp =*(int* )arg;
char buf[32] = "";
ssize_t res = 0;
while(1)
{
//读
bzero(buf,sizeof(buf));
read(fp,buf,sizeof(buf));
if(0 == read(fp,buf,sizeof(buf)))
{
printf("1-input close\n");
break;
}
if(!strcmp(buf,"quit"))
break;
printf("%s\n",buf);
}
pthread_exit(NULL);
}
void* callBack1(void* arg)//w
{
int fd =*(int* )arg;
char buf[32] = "";
ssize_t res = 0;
//写
while(1)
{
bzero(buf,sizeof(buf));
printf("input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fd,buf,sizeof(buf)) < 0)
{
perror("w-write");
pthread_exit(NULL);
}
}
printf("write success\n");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo1");
return -1;
}
}
printf("create fifo success\n");
printf("create fifo1 success\n");
int fd = open("./fifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fp = open("./fifo1",O_RDONLY);
if(fp < 0)
{
perror("open");
return -1;
}
printf("open fifo success\n");
printf("open fifo1 success\n");
//create thread
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,callBack,&fd);
pthread_create(&tid2,NULL,callBack1,&fp);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd);
close(fp);
return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
void* callBack(void* arg)//r
{
int fd =*(int* )arg;
char buf[32] = "";
ssize_t res = 0;
while(1)
{
//读
bzero(buf,sizeof(buf));
read(fd,buf,sizeof(buf));
if(0 == read(fd,buf,sizeof(buf)))
{
printf("1-input close\n");
break;
}
if(!strcmp(buf,"quit"))
break;
printf(" %s\n",buf);
}
pthread_exit(NULL);
}
void* callBack1(void* arg)//w
{
int fp =*(int* )arg;
char buf[32] = "";
ssize_t res = 0;
//写
while(1)
{
bzero(buf,sizeof(buf));
//printf("input>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fp,buf,sizeof(buf)) < 0)
{
perror("w-write");
pthread_exit(NULL);
}
}
printf("write success\n");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./fifo",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo");
return -1;
}
}
if(mkfifo("./fifo1",0777) < 0)
{
if(17 != errno)//文件存在不算错误
{
perror("fifo1");
return -1;
}
}
printf("create fifo success\n");
printf("create fifo1 success\n");
int fd = open("./fifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
int fp = open("./fifo1",O_WRONLY);
if(fp < 0)
{
perror("open");
return -1;
}
printf("open fifo success\n");
printf("open fifo1 success\n");
//create thread
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,callBack,&fp);
pthread_create(&tid2,NULL,callBack1,&fd);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(fd);
close(fp);
return 0;
}