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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started