2024.8.15

#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;
		}
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值