Question

Where n is a value read at the beginning.

First of all, when creating the thread and the fork(), what should it do? Simply return NULL or output something?

I've thought of doing something like this, for measuring the threads, after reading n:

pthread_t threads[n];
begin = clock();
for (i = 0; i < n; i++) {
    //cout << " main() created thread number: " << i << endl;
    ret = pthread_create(&threads[i], NULL, Threads, (void *)i);
}
end = clock();
time = (double)(end - begin) / CLOCKS_PER_SEC;

It's accurate? A good way to calculate it? And for the fork()'s I thought of something like this:

begin = clock();

if (fork() == 0) {
    for (i = 0; i < n; i++) {
        j += i;
    }
    exit(0);
}

end = clock();
time = (double)(end - begin) / CLOCKS_PER_SEC;
Was it helpful?

Solution

I decided getting two other codes, where the fork() and the thread would do:

void do_nothing() {
   int i;
   i = 0;
 }

The best way I've found to calculate the time, was executing the invoking the time. For n = 50000 I've obtained:

$ time ./forks

real    0m25.064s
user    0m3.389s
sys     0m12.731s

$ time ./pthreads

real    0m2.164s
user    0m0.262s
sys     0m1.616s

So, I created processes using fork() and created (and destroyed) threads that simply created a variable and gave the value 0 to them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top