Consider the following program:
Determine which of the following outputs are possible. Note: The atexit function takes a pointer to a function and adds it to a list of functions (initially empty) that will be called when the exit function is called.

/* $begin forkprob2 */


#include "csapp.h"



void end(void)
{

printf("2");


}



int main()
{

if (Fork() == 0)

atexit(end);

if (Fork() == 0)

printf("0");

else

printf("1");

exit(0);
}


/* $end forkprob2 */


A. 112002 B. 211020 C. 102120 D. 122001 E. 100212

Respuesta :

Answer:

The solution of this question is given below in explanation section.

Explanation:

The correct answer to this question is A i.e. 112002 .

the correct code of this question is given below

#include  <stdio.h>

#include  <string.h>

#include  <sys/types.h>

//#include "csapp.h"

void end(void)

{

printf("2");

}

int main()

{

if (fork() == 0)

atexit(end);

if (fork() == 0)

printf("0");

else

printf("1");

exit(0);

}

/* $end forkprob2 */

when you run this program, fork function print different values.

However, it is noted that when you run the program repeatedly, it will show you different values. But the most possible value that this program will show is A.

The picture is attached of the running program to get the idea of code.

Ver imagen asarwar