Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Processes vs Threads: Study the two programs thr_shared.c and proc_shared.c. These programs are identical except that one uses threads and the other uses processes. Compile

Processes vs Threads:

Study the two programs thr_shared.c and proc_shared.c. These programs are identical except that one uses threads and the other uses processes. Compile and execute the programs. (SEE README.txt) What do you conclude from this second experiment? Explain the reason for the difference in behavior.

//THR_SHARED.C

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

#include  #include  #include  #include  #include  void *proc(); int shared_number; main() { int i; pthread_t new_thread; int sleep_time; int seed; shared_number = 1; printf("Enter a positive integer for seed: "); scanf("%d",&seed); srand48(seed); /* initialize seed of random number stream */ /* thr_create(NULL,0,proc,NULL,0,&new_thread);*/ pthread_create(&new_thread,NULL,proc,NULL); /* create new thread */ for (i = 0; i < 50; i++) { printf("MAIN THREAD: i = %d, shared_number = %d ",i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf("sleep time = %d microseconds ",sleep_time); usleep(sleep_time); shared_number = shared_number + 2; } pthread_join(new_thread,NULL); printf("MAIN THREAD: DONE "); } void *proc() { int i = 0; int DONE; DONE = 0; while (!DONE) { i++; if (i%10000 == 0) printf("CHILD THREAD: i = %d,shared_number = %d ",i,shared_number); if (i > 5000000) DONE = 1; } printf("CHILD THREAD: DONE "); } 

//PROC_SHARED.C

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

#include  #include  #include  #include  #include  #include  void *proc(); int shared_number; main() { int i; pthread_t new_thread; int sleep_time; int pid; int status; int seed; shared_number = 1; printf("Enter a positive integer for seed: "); scanf("%d",&seed); srand48(seed); /* initialize random number stream */ if ((pid = fork()) == 0) { /* child process */ proc(); exit(0); } else if (pid == -1) /* error */ { printf("error %d ",errno); exit(-1); } else { /* parent process */ for (i = 0; i < 50; i++) { printf("MAIN PROCESS: i = %d, shared_number = %d ",i,shared_number); sleep_time = 100000.0*drand48(); /* generate random sleep time */ printf("sleep time = %d microseconds ",sleep_time); usleep(sleep_time); shared_number = shared_number + 2; } wait(&status); /* wait for child process */ printf("MAIN PROCESS: DONE "); } } void *proc() { int i; int DONE; DONE = 0; i = 0; while (!DONE) { i++; if (i%10000 == 0) printf("CHILD PROCESS: i = %d,shared_number = %d ",i,shared_number); if (i > 5000000) DONE = 1; } printf("CHILD PROCESS: DONE "); } 

//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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

=+21.16. For the density Cexp( -1x| "), - o Answered: 1 week ago

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

Make efficient use of your practice time?

Answered: 1 week ago