如何解决c代码和python的进程间通信
我正在尝试在图表上实时绘制与 cpu 使用率和内存使用率相关的数据。我正在使用 c 代码读取数据并使用管道将其传输到 python 脚本。现在我希望能够直接从 C 程序执行 python 脚本,因为我试图在 C 中创建一个控制台应用程序。但是我的程序卡在我运行 python 脚本的语句中,因为脚本正在等待 C以写入模式从另一端打开管道的代码。请提出一种解决此问题的方法。无论我从哪一端尝试访问管道,代码都会卡住,等待其他程序以读/写模式打开它。
注意:在我的情况下,两种语言的使用都是强制性的,因此程序的结构将保持不变,即 C 将数据写入管道,python 将读取它以在图形上绘图
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.\n");
return -1;
}
}
if(strcmp(pipename,"pipe/cpu_pipe")==0)
{
system("python3 cpu_graph.py");
}
else
{
system("python3 mem_graph.py");
}
printf("opening...\n");
fd = open(pipename,O_WRONLY);
printf("Opened\n");
char b[2];
sprintf(b,"%lld",a);
char *x;
x=b;
//printf("Writing...\n");
if(write(fd,x,sizeof(b)) == -1)
{
printf("Write error\n");
return 1;
}
close(fd);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。