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?
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.
精彩评论