Question: Suppose we have a program as follows: #include #include int i = 0 ; void * do _ stuff ( void * arg ) {

Suppose we have a program as follows:
#include
#include
int i =0;
void *do_stuff(void *arg){
i++;
return NULL;
}
int main(){
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, do_stuff, NULL);
pthread_create(&tid2, NULL, do_stuff, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("%d
", i);
return 0;
}
Recall that because iis a global variable, i++; will compile to something like this:
400728: 8b 042540106000 mov 0x601040,%eax
40072f: 83 c001 add $0x1,%eax
400732: 89042540106000 mov %eax,0x601040
A.(8 points) What are all possible outputs of this program? For each output, explain how the kernel
could interleave execution of the two child threads to produce it.
Initials: 14
Suppose we alter do stuff to look as follows:
void *do_stuff(void *arg attribute ((unused))){
int a;
for (a =0; a <1000; a++)
i++;
return NULL;
}
Because the code is not optimized, there will be one load-increment-store sequence per iteration of the
loop.
B.(6 points) For each number, tell whether or not our program could output it, and briefly explain why
or why not.
2000
1500
2
1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!