157 views
0 0 votes
1. Consider the following program.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x;
} global_t;

global_t *g;

int main(void) {
    g = (global_t*)malloc(sizeof(global_t));
    g->x = 5;

    if (fork() == 0) {
        if (fork() == 0) {
            g->x--;
            printf("%d", g->x);
        } else {
            g->x++;
        }
        wait(NULL);
        g->x++;
        printf("%d", g->x);
    } else {
        g->x = 2 * g->x;
        wait(NULL);
        g->x++;
        printf("%d", g->x);
    }

    printf("%d", g->x);
    return 0;
}
What are the possible outputs of this program?

Please log in or register to answer this question.

Position:
Show:

Related questions

1 1 vote
1 1 answer
303
303 views
RUPALI_SHAHAPURE asked Sep 26, 2024
303 views
A program executes eight system calls. Four of these calls are fork calls and three of the remaining calls require disk I/O. The last system call is the exit call, throug...
0 0 votes
0 0 answers
205
205 views
RUPALI_SHAHAPURE asked Sep 26, 2024
205 views
Consider a program with a single CPU burst of 100 time units and a single disk I/O of 1000 time units at the end. If the timer interval is fifteen time units, how many ti...
0 0 votes
0 0 answers
171
171 views
RUPALI_SHAHAPURE asked Sep 26, 2024
171 views
A program executes eight system calls. Four of these calls are fork calls and three of the remaining calls require disk I/O. The last system call is the exit call, throug...
0 0 votes
0 0 answers
133
133 views
RUPALI_SHAHAPURE asked Sep 26, 2024
133 views
Consider the following programs. Assume that execv does not encounter any error during execution.#include <stdio.h>#include <unistd.h>int main(void) { int x = getpid()...