samedi 27 juin 2015

Fork example output

Can anyone explain why this piece of code prints n=3 and not n=4?

int main() {
    int n = 1;
    if (fork() == 0) {
        n = n + 1;
        exit(0);
    }
    n = n + 2;
    printf("%d: %d\n", getpid(), n);
    wait(0);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire