Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

fork() example in C language

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

int main()
{
    int a = 1000;
    printf("\nAgain ...%d", (int)getpid());
    if (fork() == 0) {
        printf("\nIn child ...");
        a /= 2;
        printf("\nValue of a = %d. PID: %d", a, (int)getpid());
    }
    else {
        printf("\nIn parent ...");
        if (fork()) {
            printf("\nSecond fork() ... PID: %d", (int)getpid());
            a *= 2;
            printf("\nValue of a = %d. PID: %d", a, (int)getpid());
            if (execlp("ls", "ls", 0) == -1) {
                a = a + 2;
                printf("\nValue of a = %d. PID: %d", a, (int)getpid());
            }
        }
        else {
            printf("\nIncrement ...");
            a += 2;
            printf("\nValue of a = %d. PID: %d", a, (int)getpid());
        }
    }
    a++;
    printf("\nValue of a = %d. PID: %d", a, (int)getpid());
    printf("\nExiting %d\n", getpid());
    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.