fork子进程
原函数
void forktest(int *i, pid_t parentid)
{
tesseract::TessBaseAPI *chi_api = new tesseract::TessBaseAPI();
if (chi_api->Init("/usr/share/tesseract-ocr/4.00/tessdata/", "chi"))
{
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
deviceINFO cdc3f475;
auto armymodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/army.onnx");
auto constructmodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/construct.onnx");
auto attack_homemodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/attack_home.onnx");
auto naimodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/nai.onnx");
auto duchengmodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/ducheng.onnx");
auto homemodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/home.onnx");
auto xunlianmodel = fastdeploy::vision::detection::YOLOv5("/home/yuxian/coc/fastdepoly/xunlian.onnx");
// fork();
// fork();
cv::Mat mat(1080, 1920, CV_8UC3);
if (getpid() - parentid == 0)
{
*i = 0;
std::cout << "这是主进程。此时 i = " << *i << " pid = " << getpid() << std::endl;
FILE *file = popen("adb -s cdc3f475 shell /data/local/tmp/minitouch -i", "w");
cdc3f475.touchFile = file;
cdc3f475.ADB_IP = "cdc3f475";
cdc3f475.mat = &mat;
cdc3f475.armyModelPoint = &armymodel;
cdc3f475.constructModelPoint = &constructmodel;
cdc3f475.attack_homeModelPoint = &attack_homemodel;
cdc3f475.naiModelPoint = &naimodel;
cdc3f475.duchengModelPoint = &duchengmodel;
cdc3f475.homeModelPoint = &homemodel;
cdc3f475.xunlianModelPoint = &xunlianmodel;
cdc3f475.chi_api = chi_api;
control(&cdc3f475);
pclose(file);
}
else if ((int)(getpid() - parentid) == 1)
{
}
std::cout << getpid() << " parent id = " << getppid() << std::endl;
sleepMills(1000);
}
2.函数解析
*在fork过后,子进程就会被创建,可以根据getpid来分辨所在进程,由于子进程接下来同样会运行父进程的相同代码。
*所以子if结构内进行独立资源的定义,而在if外进行共用资源的定义。
*主要共用资源有 fastdeploy所有的onnx模型,以及tesseract所需要的api指针。