将上述练习修改成:AB进程能够随时收发形式
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#define N 128
void* callBack_Write(void*arg)
{
int fd_w = open("./myfifo",O_WRONLY);
if(fd_w < 0)
{
perror("open");
pthread_exit(NULL);
}
char str[N] = "";
while(1)
{
bzero(str,sizeof(str));
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = 0;
if(write(fd_w, str, sizeof(str)) < 0)
{
perror("write");
break;
}
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
}
close(fd_w);
pthread_exit(NULL);
}
void* callBack_Read(void* arg)
{
int fd_r = open("./myfifo1", O_RDONLY);
if(fd_r < 0)
{
perror("open");
pthread_exit(NULL);
}
ssize_t res = 0;
char str[N] = "";
while(1)
{
bzero(str, sizeof(str));
res = read(fd_r, str, sizeof(str));
if(res < 0)
{
perror("read");
break;
}
else if(0 == res)
{
printf("对方退出\n");
break;
}
printf("对方说: %s\n", str);
if(strncasecmp(str, "quit", 4) == 0)
{
exit(0);
}
}
close(fd_r);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./myfifo",0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
if(mkfifo("./myfifo1",0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
pthread_t tid_r,tid_w;
if(pthread_create(&tid_w, NULL, callBack_Write, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_r, NULL, callBack_Read, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_w, NULL);
pthread_join(tid_r, NULL);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#define N 128
void* callBack_Write(void* arg)
{
int fd_w = open("./myfifo1", O_WRONLY);
if(fd_w < 0)
{
perror("open");
pthread_exit(NULL);
}
char str[N] = "";
while(1)
{
bzero(str,sizeof(str));
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = 0;
if(write(fd_w, str, sizeof(str)) < 0)
{
perror("write");
break;
}
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
}
close(fd_w);
pthread_exit(NULL);
}
void* callBack_Read(void* arg)
{
int fd_r = open("./myfifo",O_RDONLY);
if(fd_r < 0)
{
perror("open");
pthread_exit(NULL);
}
ssize_t res = 0;
char str[N] = "";
while(1)
{
bzero(str, sizeof(str));
res = read(fd_r, str, sizeof(str));
if(res < 0)
{
perror("read");
break;
}
else if(0 == res)
{
printf("对方退出\n");
break;
}
printf("对方说: %s\n", str);
if(strncasecmp(str, "quit", 4) == 0)
{
exit(0);
}
}
close(fd_r);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./myfifo",0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
if(mkfifo("./myfifo1",0777) < 0)
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
pthread_t tid_w, tid_r;
if(pthread_create(&tid_w, NULL, callBack_Write, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_r, NULL, callBack_Read, NULL) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_w, NULL);
pthread_join(tid_r, NULL);
return 0;
}