我正在实施一个简单的分叉练习,我需要分叉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秒。
这是不正确的。我做错了什么?
答案 0 :(得分:0)
首先:fork()
将生成新进程,而不是新线程,因此声明的变量不会在它们之间共享。要做那样的事情 - 你需要线程,而不是进程。
第二:fork()
是异步的。它不会等到进程完成,它会在启动后立即返回(并获得PID)。我想你自己想出了这个。