Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

Explain the concept of employment at will.

Answered: 1 week ago

Question

Discuss compensation for sales representatives.

Answered: 1 week ago

Question

Explain termination of employment.

Answered: 1 week ago