fork 多个进程的问题

发布于 2024-10-02 14:59:07 字数 3898 浏览 0 评论 0原文

我正在实现一个简单的分叉练习,我需要分叉 5 个新进程,并在每个进程中运行一个函数 n 次,并获取完成时间。问题是,它们没有在子进程中更新,代码如下所示:

    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <ctime>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    using namespace std;

    int f1(string file);
    int f2(string file);
    int f3(string file);
    int f4(string file);
    int sendpacket(string echoString);
    typedef int (*ptof)(int,string,double&);


    int p1(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f1(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p2(int n, string file,double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f2(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p3(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f3(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p4(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f4(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p5(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        sendpacket(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int main(int argc, char** argv)
    {
    //read the user value for n
    int n;
    string input = "";
    pid_t pid1, pid2, pid3, pid4, pid5;
    while (true)
    {
        cout << "Enter a number: ";
        getline(cin, input);

        stringstream myStream(input);
        if (myStream >> n)
            break;
        cout << "Invalid number, please try again" << endl;
    }

    //reading filename
    cout << "Enter the name of the file you want read/written and message for sendpacket: ";
    string f;
    cin >> f;

    double elapsedTimes[5];
    pid_t processes[5];
    ptof functions[5] = {p1, p2, p3, p4, p5};
    pid_t pid = fork();
    for(int i =0; i<5; i++){
      switch(pid){
      case -1:
 cout<< "Forking Error" << endl;
 exit(-1);
      case 0:
 functions[i](n,f,elapsedTimes[i]);
      default:
 if(i<4){
 pid = fork();
 break;
 }
 else if(i==4){
  wait(NULL);
  break;
       }
      }

    }        


    for(int i = 0; i<5;i++){
      cout << endl << "Function p" << i+1 << " ran for "; 
      cout <<  elapsedTimes[i] << " seconds." << endl;
    }
    return(0);
    }

这会产生以下输出:

函数 p1 运行了 0 秒。

函数 p2 运行了 6.95322e-310 秒。

函数 p3 运行了 0 秒。

函数 p4 运行了 6.95322e-310 秒。

函数 p5 运行了 2.122e-314 秒。

这是不正确的。我做错了什么?

I'm implementing a simple forking exercise, where I need to fork 5 new processes, and run a function n times in each, and obtain the completion times. Problem is, they're not being updated in the child process, a code that looks like this:

    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <ctime>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    using namespace std;

    int f1(string file);
    int f2(string file);
    int f3(string file);
    int f4(string file);
    int sendpacket(string echoString);
    typedef int (*ptof)(int,string,double&);


    int p1(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f1(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p2(int n, string file,double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f2(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p3(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f3(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p4(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        f4(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int p5(int n, string file, double& elapsed)
    {
    struct timeval time;
    gettimeofday(&time, NULL);
    double t1 = time.tv_sec + (time.tv_usec/1000000.0);

    for(int i = 0; i < n; i++)
    {
        sendpacket(file);
    }

    gettimeofday(&time, NULL);
    double t2 = time.tv_sec + (time.tv_usec/1000000.0);
    elapsed = t2-t1;
    exit(0);
    return 0;
    }

    int main(int argc, char** argv)
    {
    //read the user value for n
    int n;
    string input = "";
    pid_t pid1, pid2, pid3, pid4, pid5;
    while (true)
    {
        cout << "Enter a number: ";
        getline(cin, input);

        stringstream myStream(input);
        if (myStream >> n)
            break;
        cout << "Invalid number, please try again" << endl;
    }

    //reading filename
    cout << "Enter the name of the file you want read/written and message for sendpacket: ";
    string f;
    cin >> f;

    double elapsedTimes[5];
    pid_t processes[5];
    ptof functions[5] = {p1, p2, p3, p4, p5};
    pid_t pid = fork();
    for(int i =0; i<5; i++){
      switch(pid){
      case -1:
 cout<< "Forking Error" << endl;
 exit(-1);
      case 0:
 functions[i](n,f,elapsedTimes[i]);
      default:
 if(i<4){
 pid = fork();
 break;
 }
 else if(i==4){
  wait(NULL);
  break;
       }
      }

    }        


    for(int i = 0; i<5;i++){
      cout << endl << "Function p" << i+1 << " ran for "; 
      cout <<  elapsedTimes[i] << " seconds." << endl;
    }
    return(0);
    }

and this produces this output:

Function p1 ran for 0 seconds.

Function p2 ran for 6.95322e-310 seconds.

Function p3 ran for 0 seconds.

Function p4 ran for 6.95322e-310 seconds.

Function p5 ran for 2.122e-314 seconds.

which is incorrect. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宛菡 2024-10-09 14:59:07

首先: fork() 将生成新进程,而不是新线程,因此声明的变量不会在它们之间共享。要执行类似的操作 - 您将需要线程,而不是进程。

第二:fork()是异步的。它不会等到进程完成,而是一启动就返回(并且获得PID)。我想你自己已经弄清楚了这一点。

First: fork() will spawn new process, not new thread, so your variables that are declared WON'T be shared between them. To do something like that - you will need threads, not processes.

Second: fork() is asynchronous. It doesn't wait until process is finished, it returns as soon as it is started (and PID is obtained). I guess that you figured this one out yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文