繁体   English   中英

C++:如何控制进程号

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

我正在尝试使用 C++ 的多进程代码。 我对下面的代码有两个问题:

  • Q1。 似乎“Vtmp”或“Ntmp”没有为每个进程正确存储信息。 我不知道为什么。

  • Q2。 如果 Q1 固定,我的最终目的是让每个 Vtmp(V[i]) 一个接一个运行。 我的意思是我使用多进程方式来读取“Vtmp”,我想
    等到“read[V[0]]”完成,然后“read(V[1])”,等待一段时间
    他们。

  • 我的主要目的是Q2。 如果“读取”是一个耗时的函数,并且会生成许多子进程。 我希望它一个一个运行,每次只有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;
}

作为比较,以下是为每个进程保留 tmp 信息的好代码。 但是我仍然不知道为什么上面的不能。

#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
*/

问题是你在分叉后遇到了一个拼写错误。

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

您的 main 方法和 read 方法中的拼写错误相同,因此子进程始终继续循环并以向量向量的最后一个元素(向量)结束。

暂无
暂无

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

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