operating system concepts

how to fork

Written by  on Oktober 31, 2008

Ich glaub ich hab gerade meinen ersten Prozess unter C geforkt. Auch wenn der Beispielcode nichts sinnvolles tut 😉

Die simple Frage war, was hier vom Parent ausgegeben wird. Recht offensichtlich (und durch ausprobieren rauszufinden ist die Lösung wohl 5!

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int value = 5;
int main()
{
pid_t pid;
    pid = fork();
    if (pid == 0) { /* child process */
        value +=15;
    }
    else if (pid > 0) { /* parent process */
        wait (NULL);
        printf("PARENT: value = %d",value);
    }
}

Operating System Concepts, 7th Edition

Operating System Concepts, 8th Edition