Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

pipe - Interprocess communication between c code and python

I am trying to plot data related to CPU usage and memory usage in real time on graph. I am reading data using a c code and transferring it to python script using pipes. Now I want to be able to execute the python script directly from the C program as I am trying to make a console application in C. However my program gets stuck at the statement where I run the python script because the script is then waiting for C code to open the pipe from the other end in write mode. Kindly suggest a way to get past this issue. As no matter from which end I try to access the pipe the code gets stuck waiting for the other program to open it in read/write mode.

Note: the use of both languages is mandatory in my case so the structure of program will remain the same that is C will write data to pipe and python will read it to plot on graph

int writepipe(long long a,char pipename[])
{
    int fd;
    //0777 means this file can be read and be edited by anyone
    if(mkfifo(pipename, 0777)==-1) //returns -1 when an error occurs
    {
        if(errno != EEXIST)
        {
            printf("Could not create FIFO file.
");
            return -1;
        }
    }
    
    if(strcmp(pipename,"pipe/cpu_pipe")==0)
    {
        system("python3 cpu_graph.py");
    }
    else
    {
        system("python3 mem_graph.py");

    }


    printf("Opening...
");
    fd = open(pipename, O_WRONLY);
    printf("Opened
");
    char b[2];
    sprintf(b,"%lld",a);
    char *x;
    x=b;
    


    //printf("Writing...
");
    if(write(fd, x, sizeof(b)) == -1)
    {
        printf("Write error
");
        return 1;
    }
    
    close(fd);
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...