Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

=+ (a) Show that C is uncountable but trifling.

Answered: 1 week ago

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago