subject

In the example program below, a variable is used to protect the critical section. Why did it fail? Use pthread_mutex_init( ) and pthread_mutex_lock( )/pthread_mutex_unlock( ) system call to modify this program so that the result is correct. // when input thread number to be 100
// then two output are different
#include
#include

char USAGE[] = "naive_lock n_threads\n"
"USAGE: run n threads with a naive lock\n";

int lock = 0; //0 for unlocked, 1 for locked

int shared = 0; //shared variable

void * incrementer(void * args){
int i;

for(i=0;i 0); //spin until unlocked

lock = 1; //set lock

shared++; //increment

lock = 0; //unlock
}

return NULL;
}

int main(int argc, char * argv[]){
pthread_t * threads;
int n, i;

if(argc < 2){
fprintf(stderr, "USAGE: program number_of_thread\n");
exit(1);
}

//convert argv[1] to a long
if((n = atol(argv[1])) == 0){
fprintf(stderr, "ERROR: Invalid number of threads\n");
exit(1);
}

//allocate array of pthread_t identifiers
threads = calloc(n, sizeof(pthread_t));

//create n threads
for(i=0;i pthread_create(&threads[i], NULL, incrementer, NULL);
}

//join all threads
for(i=0;i pthread_join(threads[i], NULL);
}

//print shared value and result
printf("Shared: %d\n",shared);
printf("Expect: %d\n",n*100);

return 0;
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 04:30
What kind of software users of all skill levels create web pages that include graphics, video, audio, animation, and other special effects? website authoring website software website publishing website editing
Answers: 1
question
Computers and Technology, 22.06.2019 06:00
Write a program that uses a widgetviewer object to do the following: generate two random integers between 1 and 9 (inclusive). name one of them x, the other y. display them to the user using jlabel objects. create a jlabel object displaying the text "enter an operation number." create a jtextfield for the user's input. create a jbutton displaying the text "press here when you've entered your operation." use addandwait to add it to the widgetviewer object. when the user clicks the jbutton, evaluate operation in the following order to determine the one and only mathematical operation to perform on x and y. use a jlabel to display the result. if operation is between 1 and 10 inclusive, add x and y. if operation is evenly divisible by 4, subtract y from x. if operation is evenly divisible by 5, use integer division to divide y into x. if operation is an even number, use floating point division to divide y into x. if none of the other tests on operation apply, multiply x and y. note: operation can be negative or zero.
Answers: 2
question
Computers and Technology, 22.06.2019 21:00
Write a method so that the main() code below can be replaced by the simpler code that calls method original main(): public class calcmiles { public static void main(string [] args) { double milesperhour; double minutestraveled; double hourstraveled; double milestraveled; milesprhour = scnr.nextdouble(); minutestraveled = scnr.nextdouble(); hourstraveled = minutestraveled / 60.0; milestraveled = hourstraveled * milesperhour; system.out.println("miles: " + milestraveled); } }
Answers: 2
question
Computers and Technology, 23.06.2019 02:30
Research data that is presented using descriptive language is said to be
Answers: 2
You know the right answer?
In the example program below, a variable is used to protect the critical section. Why did it fail? U...
Questions
question
Mathematics, 26.08.2021 15:20
question
Chemistry, 26.08.2021 15:20
question
Biology, 26.08.2021 15:20
question
Mathematics, 26.08.2021 15:20
Questions on the website: 13722359