Question: #include #include #include #include jmp_buf trampoline, jb1, jb2; void thread1() { //while(1) { /* The following context-switches to the other thread. */ if (!setjmp(jb1)) {

#include

#include

#include

#include

jmp_buf trampoline, jb1, jb2;

void thread1()

{

//while(1) {

/* The following context-switches to the other thread. */

if (!setjmp(jb1)) {

printf("Step 8 ");

longjmp(jb2, 1);

/* NOTREACHED */

} else {

printf("Step 7 ");

}

// }

}

int thread2()

{

//while(1) {

/* The following context-switches to the other thread. */

if (!setjmp(jb2)) {

printf("Step 6 ");

longjmp(jb1, 1);

/* NOTREACHED */

} else {

printf("Step 9 ");

}

//}

}

void thread2_launchpad()

{

/* We create a new stack area for thread2 to run in by doing

* a dummy allocation of space (an array) on the stack.

* The reason for assigning 1 to the last element of the

* array is simply to avoid the compiler optimising away the

0 * array (as it would otherwise never be used).

*/

char space_for_thread[4096];

space_for_thread[4096-1] = 1;

printf("Step 5 ");

thread2();

}

int main(int argc, char* argv[])

{

printf("Step 1 ");

if (!setjmp(trampoline)) {

/* First return from trampoline - initialise. */

printf("Step 2 ");

if (!setjmp(jb1)) {

/* First return from setjmp snapshotting thread1 */

printf("Step 3 ");

/* Go to 2nd return of trampoline to create thread2 */

longjmp(trampoline, 1);

/* NOTREACHED */

} else {

/* We get here when we have been resumed

* by thread2 */

printf("Step 7 ");

thread1(); /* run thread1 */

}

} else {

/* Second return of trampoline caused by longjmp() above */

printf("Step 4 ");

thread2_launchpad(); /* launchpad for thread2 to

* run in distinct stack area */

}

}

Can you explain what this code does please?

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!