参考
https://www.cnblogs.com/52php/p/5840229.html
https://www.cnblogs.com/MrListening/p/5858358.html
https://www.cnblogs.com/biyeymyhjob/archive/2012/11/03/2751593.html
https://zhuanlan.zhihu.com/p/58489873
https://www.cnblogs.com/-wenli/p/13100104.html
http://www.cs.ecu.edu/karl/4630/spr01/example1.html
https://www.geeksforgeeks.org/named-pipe-fifo-example-c-program/
Python os.mkfifo() 方法
https://www.runoob.com/python/os-mkfifo.html
Python os.open() 方法
https://www.runoob.com/python/os-open.html
py_server.py
import os
_PATH_NAME="/tmp/file.tmp"
if not os.path.exists(_PATH_NAME):
os.mkfifo(_PATH_NAME, 0o666)
fd = os.open(_PATH_NAME, os.O_WRONLY)
os.write(fd, str.encode("This is test"))
os.close( fd )
py_client.py
import os
_PATH_NAME="/tmp/file.tmp"
if not os.path.exists(_PATH_NAME):
os.mkfifo(_PATH_NAME, 0o666)
r1 = os.open(_PATH_NAME, os.O_RDONLY) # | os.O_NONBLOCK
msg = os.read(r1, 100)
print("rec:", msg.decode())
server.cpp
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define _PATH_NAME_ "/tmp/file.tmp"
#include <iostream>
#include <chrono>
#include <thread>
bool CreateFIFO(string fileName) {
if (access(fileName.c_str(), F_OK) == -1) {
int ret = mkfifo(fileName.c_str(), S_IFIFO | 0666);
if (ret == -1) {
printf("make fifo error\n");
return false;
}
}
return true;
}
// g++ server.cpp -std=c++11
int main() {
CreateFIFO(_PATH_NAME_);
int fd = open(_PATH_NAME_, O_WRONLY);
int cnt = 0;
vector<string> datas = {"12345", "hello world", "1", "2", "2233"};
for (string& elem : datas) {
int ret = write(fd, elem.c_str(), elem.length() + 1);
if (ret < 0) {
printf("write error");
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
close(fd);
return 0;
}
client.cpp
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#define _PATH_NAME_ "/tmp/file.tmp"
#define _SIZE_ 100
int main() {
if (access(_PATH_NAME_, F_OK) == -1) {
int ret = mkfifo(_PATH_NAME_, S_IFIFO | 0666);
if (ret == -1) {
printf("make fifo error\n");
return 1;
}
}
int fd = open(_PATH_NAME_, O_RDONLY); // | O_NONBLOCK
if (fd < 0) {
printf("open file error");
return 1;
}
printf("open success\n");
char buf[_SIZE_];
while (1) {
int ret = read(fd, buf, sizeof(buf));
if (ret < 0) {
printf("read end or error\n");
break;
}
if (ret > 0) {
printf("%d %s\n", ret, buf);
}
if (ret == 0) {
break;
}
}
close(fd);
return 0;
}
上面c++和python进程通信收发可以互换,实现c++和c++或python之间的进程通信。
open默认是阻塞的,参考https://www.cnblogs.com/52php/p/5840229.html
非阻塞我想一个应用是可以用于定时器查询。
fork启动第一个子进程,而用exec系列函数启动第三方应用,如python等。exec族函数介绍:https://blog.csdn.net/u014530704/article/details/73848573
fork, execv example:http://www.cs.ecu.edu/karl/4630/spr01/example1.html
execvp更好用,不用设置绝对路径。execvpe 可以带新的环境变量设置,但是要设置PATH等环境变量,否则执行python等失败。
execvpe example: https://members.tripod.com/vitaly_Filatov/ng/tc/tc_000.70.html
execv超时kill
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<stdlib.h>
#include<string.h>
#include <signal.h>
#include <iostream>
#include <chrono>
#include <thread>
int main() {
pid_t pid;
char* const parmList[] = {"/root/anaconda3/envs/tf1.15/bin/python3", "test_py.py", "1234", "5678", NULL};
if ((pid = fork()) == -1) {
perror("fork error");
} else if (pid == 0) {
printf("child thread run\n");
execv("/root/anaconda3/envs/tf1.15/bin/python3", parmList);
printf("Return not expected. Must be an execv error\n");
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
kill(pid, SIGKILL); // kill child thread
printf("parent thread run\n");
}
return 0;
}