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\n", i); return 0; Recall that because i is a global variable, i++; will compile to something like this: 400728: 8b 04 25 40 10 60 00 mov 0x601040,%eax 40072f: 83 c0 01 add $0x1,%eax 400732: 89 04 25 40 10 60 00 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.

icon
Related questions
Question
Suppose we have a program as follows:
#include <stdio.h>
#include <pthread.h>
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\n", i);
return 0;
Recall that because i is a global variable, i++; will compile to something like this:
400728: 8b 04 25 40 10 60 00 mov 0x601040,%eax
40072f: 83 c0 01 add $0x1,%eax
400732: 89 04 25 40 10 60 00 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.
Transcribed Image Text:Suppose we have a program as follows: #include <stdio.h> #include <pthread.h> 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\n", i); return 0; Recall that because i is a global variable, i++; will compile to something like this: 400728: 8b 04 25 40 10 60 00 mov 0x601040,%eax 40072f: 83 c0 01 add $0x1,%eax 400732: 89 04 25 40 10 60 00 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.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer