solution

Consider the following code using the POSIX Pthreads API:

#include

#include

#include

#include

int myglobal = 0;

void* thread_function(void* arg)

{ int i, j;

for(i = 0; i < 25; i++)

{

j = myglobal; j = j + 1;

printf(“.”);

fflush(stdout);

sleep(1);

myglobal = j;

} return NULL;

}

int main(void)

{

pthread_t mythread;

int i;

if (pthread_create(&mythread, NULL, thread_function, NULL) != 0)

{

printf(“error creating thread.”);

abort();

}

for(i = 0; i < 25; i++)

{

myglobal = myglobal + 1;

printf(“o”);

fflush(stdout);

sleep(1);

}

if (pthread_join(mythread, NULL))

{

printf(“error joining thread.”);

abort();

}

printf(“n myglobal equals %d n”, myglobal);

exit(0);

}

2. Here is output from the above program being executed

$ ./thread2

..o.o.o.oo.oo.o..oo.o.o.o.o.oo.o…o.o.o..o.o.o.oo

myglobal equals 26

Is this the output you would expect? What value should myglobal have? What should be the output sequence of the . and o symbols? What has gone wrong and how would you fix it? If you can, propose code modifications that might fix or reduce any problems you identify.

 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!

Hi there! Click one of our representatives below and we will get back to you as soon as possible.

Chat with us on WhatsApp