Question: 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

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!