Computers and Technology, 27.10.2020 23:00 help445492
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;
}
Answers: 1
Computers and Technology, 23.06.2019 07:00
What are three software programs for mobile computing?
Answers: 1
Computers and Technology, 23.06.2019 18:00
Freya realizes she does not have enough in her bank account to use the debit card. she decides to use a credit card instead. which questions should freya answer before using a credit card? check all that apply. can i pay at least the minimum payment each month? can i make payments on time and avoid late fees? will i have to take out a loan? how much in finance charges can i afford to pay? should i talk to a consumer credit counseling service?
Answers: 1
Computers and Technology, 24.06.2019 03:00
Using a conditional expression, write a statement that increments numusers if updatedirection is 1, otherwise decrements numusers. ex: if numusers is 8 and updatedirection is 1, numusers becomes 9; if updatedirection is 0, numusers becomes 7.
Answers: 1
Computers and Technology, 24.06.2019 22:00
What is a number system, and what is the total number of digits used in this system called? a number system is a system that uses different (options: a) numbers b) symbols c) codes d) digits e) alphabets) to represent different numbers. the total number of digits used in a number system is known as its (options: 1) processor 2) converter 3) radix 4) least significant digit 5) most significant digit)
Answers: 1
In the example program below, a variable is used to protect the critical section. Why did it fail? U...
Mathematics, 21.10.2019 20:00
History, 21.10.2019 20:00
Mathematics, 21.10.2019 20:00
Mathematics, 21.10.2019 20:00
Physics, 21.10.2019 20:00
History, 21.10.2019 20:00
Physics, 21.10.2019 20:00
Biology, 21.10.2019 20:00