fork and counters

Hi all,
I tried to use fork() in a test program. The idea was to assign a different task at each process (a simple multiplication).
In the end, I would like to sum the two results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void main()
{
   int pid; //pid_t pid;

   static int taskcompleted = -1;
   float results[2] = {0., 0.};

   printf("Starting program\n");

   pid = fork();

   if(pid == 0)
   {
     printf("Son!\n");
     results[0] = 4.*3.;
     taskcompleted += 1;
     printf("taskcompleted = %d\n",taskcompleted);
   }
   else if(pid > 0)
   {
     printf("I'm your father...!\n");
     results[1] = 2.*3.;
     taskcompleted += 1;
     printf("taskcompleted = %d\n",taskcompleted);
   }
   if ( taskcompleted == 0 ) {
     float total = results[0]+results[1];
     printf("result = %f\n",total);
   }
}



Starting program
Son!
taskcompleted = 0
result = 12.000000
I'm your father...!
taskcompleted = 0
result = 6.000000


taskcompleted
should be the flag signaling that both processes ended. Clearly it is not the right way to do this.

So, how can I implement a flag for performing a task only after the first process is done?

Many thanks in advance,
Regards,
Marco Bomben
fork creates two identical copies of the program from that point on. They have their own copies of memory. There is no merge to bring them back together. You'd have to arrange for the child to send its results back to its parent (interprocess communication).

It may be more suitable to use seperate threads of execution within the same process rather than seperate threads of execution in different processes. On that basis, you want to check out pthreads.

I knocked up the dot product demo. I hope this helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <unistd.h>
#include <pthread.h>
#include <vector>
#include <iostream>

struct DotParamMult
{
        double  a, b, result;
        pthread_t p;
};

void* multiply(void* vp)
{
        if (DotParamMult *param = reinterpret_cast<DotParamMult*>(vp))
        {
                param->result = param->a * param->b;
        }
        return 0;
}

int main()
{
        double a[] = { 1.0, 2.0, 3.0 };
        double b[] = { 2.0, 3.0, 4.0 };

        std::vector<DotParamMult> params;
        params.resize(sizeof(a)/sizeof(double));

        for (size_t i = 0; i != params.size(); ++i)
        {
                params[i].a = a[i];
                params[i].b = b[i];
                pthread_create(&params[i].p, 0, multiply, &params[i]);
        }

        void* ignore = 0;
        for (size_t i = 0; i != params.size(); ++i)
                pthread_join(params[i].p, &ignore);

        double sum = 0.0;
        for (size_t i = 0; i != params.size(); ++i)
                sum += params[i].result;

        std::cout << sum << std::endl;

        return 0;
}
Last edited on
Thanks for the explanation and the code snippet! That worked!

Many thanks and regards,
Marco
Topic archived. No new replies allowed.