Question: Thread and Process Creation: Study the programs thr_create.c and fork.c. These programs measure the average time to create a thread using thr_create() and to create

Thread and Process Creation:

Study the programs thr_create.c and fork.c. These programs measure the average time to create a thread using thr_create() and to create a process using fork(). Compile and execute the programs (see README). What do you conclude from this experiment? Briefly describe the reasons for the difference in timings.

//THR_CREATE.C

---------------------------------------------------------------------------

#include #include #include #include main() { struct timeval start,end; long forktime; double avgtime; pthread_t last_thread; int i; int iters = 250; void *null_proc(); gettimeofday(&start,NULL);

for (i = 0; i < iters - 1; i++) /* create (iters-1) threads */ pthread_create(&last_thread,NULL,null_proc,NULL); pthread_create(&last_thread,NULL,null_proc,NULL); /* create last thread */ gettimeofday(&end,NULL);

pthread_join(last_thread, NULL); /* wait for last thread */ forktime = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec); avgtime = (double)forktime/(double)iters; printf("Avg thr_create time = %f microsec ", avgtime);

}

void *null_proc() { }

//FORK.C

---------------------------------------------------------------------------

#include  #include  #include  main() { int pid; struct timeval start,end; int i; long forktime; double avgtime; int iters = 250; int status; gettimeofday(&start,NULL); for (i = 0; i < iters; i++) /* create iters processes */ { if ((pid = fork()) == 0) /* child process */ { null_proc(); exit(1); } else if (pid == -1) /* error */ { printf("error %d ",errno); exit(-1); } else /* parent process */ continue; } /* only parent process reaches this point */ gettimeofday(&end,NULL); for ( i = 0; i < iters; i++) /* have to do a wait for each child */ wait(&status); forktime = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec); avgtime = (double)forktime/(double)iters; printf("Avg fork time =%f microsec ", avgtime); } null_proc() { } 

//README.TXT

---------------------------------------------------------------------------

To compile and run a C program (e.g., thr_create.c): gcc -o thr_create thr_create.c -lpthread thr_create On Solaris systems, you can use a high resolution clock to measure the cost of thread and process creation in nanoseconds. This directory also contains versions of fork.c and thr_create.c that use the high res clock. To build and run a classfile from the java code: javac SharedData.java java SharedData

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!