linux环境 c++进程 发送数据到 python进程

c++发送

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> 
#include <unistd.h>
using namespace std;

int communication(void);
int main(void)
{
    char data[]="dssaaaaaa";
    int pipe_fd_wr;
    pipe_fd_wr = communication();
    while(1){
    write(pipe_fd_wr,data,5);
    }

}

int communication(void){
    const char *fifo_name = "../CppToPython";
    int res =0;
    const int open_mode = O_WRONLY;
    int pipe_fd_wr;

    if(access(fifo_name,F_OK)==-1) 
    {
        res = mkfifo(fifo_name,0777);
        if(res!=0)
        {
            fprintf(stderr,"Could not create fifo %s\n",fifo_name);
            exit(EXIT_FAILURE); 
        }
    }
    //以只写阻塞方式打开FIFO文件
    pipe_fd_wr = open(fifo_name, open_mode);

    if(pipe_fd_wr==-1){
        cout<< " open CppToPython err!"<<endl;
    }
    else{
        cout<< " open CppToPython successfully!"<<endl;
    }
    return pipe_fd_wr;
}

 

python接收

import os
import json
from time import sleep

def openFIFO():
    get_info_path = "../CppToPython"
    counter = 1
    
    f = os.open(get_info_path, os.O_RDONLY)
    print("Client open f", f)
    return f

def getData(f):
    data = os.read(f,5)
    return data

def closeFIFO(f):
    os.close(f)

if __name__ == "__main__":
    f = openFIFO()
    while 1:
        data = getData(f)
        sleep(1)
        print(data)
    closeFIFO(f)

阻塞读

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值