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 18:30
Technician a says that a shop towel should never be used to clean around the flange area before replacing an automatic transmission filter. technician b says that a dimpled transmission fluid pan can be repaired. who is right
Answers: 3
question
Computers and Technology, 23.06.2019 06:00
Which statistical function in a spreadsheet you to see how far each number varies, on average, from the average value of the list?
Answers: 2
question
Computers and Technology, 23.06.2019 09:30
You have been supporting csm tech publishing's windows server 2016 server network for over a year. the office has two windows server 2016 servers running active directory and a number of other roles. management has informed you that a small sales office is opening in the same building three floors up. the sales manager wants to install a sales application on a server located in the sales office. this server will have limited physical security because there's no special room dedicated for it, which means it will be accessible to non-it personnel and visitors. you're considering installing windows server 2016 server core on the new server because accessing its console regularly probably won't be necessary, and this server will be managed from one of the other csm tech publishing servers. what are the benefits and drawbacks of using server core for this branch office? what are some things you should do to set up this server management environment?
Answers: 1
question
Computers and Technology, 23.06.2019 12:50
Which syntax error in programming is unlikely to be highlighted by a compiler or an interpreter? a variable name misspelling a missing space a comma in place of a period a missing closing quotation mark
Answers: 1
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
Biology, 05.12.2020 06:20
question
Mathematics, 05.12.2020 06:20
question
Mathematics, 05.12.2020 06:20
question
Mathematics, 05.12.2020 06:20
Questions on the website: 13722361