#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <signal.h>
#include <pthread.h>
#define JTOR "/home/ubuntu/myfifo1"
#define RTOJ "/home/ubuntu/myfifo2"
int fd2=0;
void *read_from_rtoj(void *argv)
{
char *rtoj_buf=malloc(100);
while(1)
{
memset(rtoj_buf,0,100);
read(fd2,rtoj_buf,100);
printf("rose:%s\n",rtoj_buf);
if(strcmp(rtoj_buf,"byebye\n")==0)
{
break;
}
}
}
int main()
{
int ret=mkfifo(JTOR,0777);
mkfifo(RTOJ,0777);
fd2=open(RTOJ,O_RDWR);
if(-1==ret)
{
perror("创建管道文件失败");
}
int fd1 = open(JTOR,O_RDWR);
if(-1==fd1)
{
perror("打开管道文件失败");
return -1;
}
pthread_t tid;
pthread_create(&tid, NULL,read_from_rtoj, NULL);
char *jtor_buf=malloc(100);
while(1)
{
memset(jtor_buf,0,100);
fgets(jtor_buf,100,stdin);
write(fd1,jtor_buf,strlen(jtor_buf));
if(strcmp(jtor_buf,"byebye\n")==0)
{
break;
}
}
}
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <signal.h>
#include <pthread.h>
#define JTOR "/home/ubuntu/myfifo1"
#define RTOJ "/home/ubuntu/myfifo2"
int fd1=0;
void *read_from_jtor(void *argv)
{
char *jtor_buf=malloc(100);
while(1)
{
memset(jtor_buf,0,100);
read(fd1,jtor_buf,100);
printf("jack:%s\n",jtor_buf);
if(strcmp(jtor_buf,"byebye\n")==0)
{
break;
}
}
}
int main()
{
int ret=mkfifo(JTOR,0777);
mkfifo(RTOJ,0777);
int fd2=open(RTOJ,O_RDWR);
if(-1==ret)
{
perror("创建管道文件失败");
}
fd1 = open(JTOR,O_RDWR);
if(-1==fd1)
{
perror("打开管道文件失败");
return -1;
}
pthread_t tid;
pthread_create(&tid, NULL,read_from_jtor, NULL);
char *rtoj_buf=malloc(100);
while(1)
{
memset(rtoj_buf,0,100);
fgets(rtoj_buf,100,stdin);
write(fd2,rtoj_buf,strlen(rtoj_buf));
if(strcmp(rtoj_buf,"byebye\n")==0)
{
break;
}
}
}
2024.8.15
于 2024-08-15 11:30:50 首次发布