简体   繁体   English

C++:如何控制进程号

[英]C++: How to control process number

I am trying a multiprocess code with C++.我正在尝试使用 C++ 的多进程代码。 I have two questions about the code below:我对下面的代码有两个问题:

  • Q1. Q1。 It seems 'Vtmp' or 'Ntmp' did not store the info properly for each process.似乎“Vtmp”或“Ntmp”没有为每个进程正确存储信息。 I don't know why.我不知道为什么。

  • Q2. Q2。 If Q1 fixed, My final purpose is to let each Vtmp(V[i]) run one by one.如果 Q1 固定,我的最终目的是让每个 Vtmp(V[i]) 一个接一个运行。 I mean I used a multi-processly way to read "Vtmp", I want to我的意思是我使用多进程方式来读取“Vtmp”,我想
    wait until "read[V[0]]" is done, then "read(V[1])", wait some times between等到“read[V[0]]”完成,然后“read(V[1])”,等待一段时间
    them.他们。

  • My main purpose is Q2.我的主要目的是Q2。 If "read" is a time-consuming function and generates many subprocesses.如果“读取”是一个耗时的函数,并且会生成许多子进程。 I want it run one by one, each time with only 6 subprocesses in theback-end, not 18 subprocesses running together.我希望它一个一个运行,每次只有6个后端子进程,而不是18个子进程一起运行。

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

void read(vector<double> Vsub){
    pid_t pid;
    double Ntmp;
    for(int i = 0; i < Vsub.size(); i++){
        Ntmp = Vsub[i];
        pid = fork();
        if(pid = 0||pid == -1) break;
    }
    if(pid == -1){
        cout<<"fail to fork!"<<endl;
        exit(1);
    }
    else if(pid == 0){
        cout<<" This is childern process Vsub, id = "<<getpid()<<endl;
        cout<<" This is childern process Vsub, id = "<<Ntmp<<endl;
        sleep(30000);
        exit(0);
    }
    else {
        cout<<"This is main process, id = "<<getpid()<<endl;
    }

}
int main() {
     vector<vector<double>> V;
    V.push_back({1,2,3,4,5,6});
    V.push_back({11,12,13,14,15,16});
    V.push_back({21,22,23,24,25,26});
    pid_t pid;
    vector<double> Vtmp;
    cout<<" *******************   "<<V.size()<<endl;
    for(int it = 0; it < V.size(); ++it){
        Vtmp = V[it];
        pid = fork();
        if(pid = 0||pid == -1) break;
    }
    if(pid == -1){
        cout<<"fail to fork!"<<endl;
        exit(1);
    }
    else if(pid == 0){
        cout<<" This is childern process V, id = "<<getpid()<<" Vtmp "<<Vtmp[0]<<endl;
       read(Vtmp);
        sleep(30000);
        exit(0);
    }
    else {
        cout<<"This is main process, id = "<<getpid()<<endl;
    }
    return 0;
}

To be compared, the following is the good code to keep tmp info for each process.作为比较,以下是为每个进程保留 tmp 信息的好代码。 But I still don't know why the above one can not.但是我仍然不知道为什么上面的不能。

#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include<vector>
#include <iostream>

using namespace std;
int main()
{
    string sMatch;
    pid_t pid;
    vector<string> provList;
    provList.push_back("100");
    provList.push_back("200");
    provList.push_back("300");
    provList.push_back("400");
    provList.push_back("500");
    cout<<"main process,id="<<getpid()<<endl;
    for (vector<string>::iterator it = provList.begin(); it != provList.end(); ++it)
    {
        sMatch=*it;
        pid = fork();
        if(pid==0||pid==-1)
        {
            break;
        }
    }
    if(pid==-1)
    {
        cout<<"fail to fork!"<<endl;
        exit(1);
    }
    else if(pid==0)
    {
      
        cout<<"this is children process,id="<<getpid()<<",start to process "<<sMatch<<endl;
        sleep(10);
        exit(0);
    }
    else
    {
    
        cout<<"this is main process,id="<<getpid()<<",end to process "<<sMatch<<endl;
        exit(0);
    }
    return 0;
}
/*-------------------------------------------------------*/
/*
main process,id=63854
this is children process,id=63855,start to process 100
this is children process,id=63856,start to process 200
this is children process,id=63857,start to process 300
this is children process,id=63858,start to process 400
this is main process,id=63854,end to process 500
this is children process,id=63859,start to process 500
*/

The problem is that you got a typo in your condition after forking.问题是你在分叉后遇到了一个拼写错误。

if(pid = 0||pid == -1) break;

You have the same typo in your main as well as in your read method so the child process always continues the cycle and ends with the last element(vector) of your vector of vectors.您的 main 方法和 read 方法中的拼写错误相同,因此子进程始终继续循环并以向量向量的最后一个元素(向量)结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM