[英]Problem with forking multiple processes
我正在实现一个简单的派生练习,其中需要派生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秒。
这是不正确的。 我究竟做错了什么?
首先: fork()
将产生新的进程,而不是新的线程,因此声明为“不会”的变量将不会在它们之间共享。 为此,您将需要线程而不是进程。
第二: fork()
是异步的。 它不会等到进程完成后才开始运行(并且会获得PID)。 我猜你是自己想出来的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.