Question
The Lost Update Problem: Study the programs shared_data.c and SharedData.java. In these examples, multiple threads are both updating (i.e., modifying) a shared variable. Compile and
The Lost Update Problem:
Study the programs shared_data.c and SharedData.java. In these examples, multiple threads are both updating (i.e., modifying) a shared variable. Compile and execute both (view README.txt). Are the programs behaving "correctly"? What do you conclude from this experiment?
//SHARED_DATA.C
--------------------------------------------------------------------------------------------------
#include#include #include #include #include void *proc(); int shared_number; main() { int i; pthread_t new_thread; int sleep_time; int number; int seed; shared_number = 1; printf("Enter a positive integer for seed: "); scanf("%d",&seed); srand48(seed); pthread_create(&new_thread,NULL,proc,NULL); for (i = 0; i < 20; i++) { number = shared_number; 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 = number + 2; } pthread_join(new_thread,NULL); printf("MAIN THREAD: DONE "); } void *proc() { int number,i; int sleep_time; printf("CHILD "); for (i = 0; i < 10; i++) { number = shared_number; printf("CHILD 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 = number + 200000; } printf("CHILD THREAD: DONE "); }
//SHAREDDATA.JAVA
--------------------------------------------------------------------------------------------------
import java.util.Random; public class SharedData extends Thread { static Random gRandom = new Random(); static int gSharedNumber = 0; private int gID; public SharedData(int id) { this.gID = id; } public void run() { for(int i=0;i<20;i++) { int number = gSharedNumber; System.out.println("SharedData["+gID+"](i="+i+") = "+gSharedNumber); try { Thread.sleep(gRandom.nextInt(1000)); } catch (InterruptedException e) { /* ignore */ } if(gID == 0) gSharedNumber = number + 1; else gSharedNumber = number + 1000; } } public static void main(String args[]) throws Exception { SharedData sd1 = new SharedData(0); SharedData sd2 = new SharedData(1); sd1.start(); sd2.start(); sd1.join(); sd2.join(); } }
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