Basic POSIX Process Creation

Each process is identified by a number known as the process ID, or pid for short. The pid for the first process is 1, and each subsequent process is assign a new, unique positive integer. It’s a relatively simple matter to get the process ID number for the current process. The getpid() function returns the current function and the getppid() function returns the parent’s ID. Both functions return the pid_t data type, which is an int.

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

int main(void){
    
    pid_t thisPID = getpid();
    pid_t parentPID = getppid();
    
    printf("PID of current process: %d\n", thisPID);
    printf("PID of parent process: %d\n", parentPID);
    
    return 0;
    
}

Note that the pid_t type is defined in the header file <sys/types.h>.

In Linux, processes form a  hierarchy known as the process tree. Traditionally, the first process is known as init; we can confirm this by issuing the bash command ps aux | head in a terminal or pseudo-terminal. New processes are created with the fork() system call; this system call takes no arguments and creates a duplicate of the calling process. The duplicate is called the parent, and the original process is called the child. If fork() fails, it returns -1.

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

int main(void){
    
    fork();
            
    //prints twice
    printf(“Saluton Mundo!\n”);
    
    fork();
    
    //prints four times
    printf(“Greetings, Professor Falken.\n”);
    
    return 0;
    
}

Note that processes execute in parallel. Both the parent and child processes continue executing the remaining statements in the program. If the fork() function returns a value less than 0, we know that the function call has failed.

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

int main(void){
    
    if((fork())<0){
        printf("fork failed.\n");
        exit(1);
    } else {
        printf("Fork succeded!");
        sleep(1);
        exit(0);
    }
    
    return 0;
    
}

The fork() function is unusual in that it returns two different values. The return value in the child process is zero, but the return value in the parent process is the process ID of the new child.

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

int main(void){
    pid_t pid;
    
    if((pid=fork())<0){
        printf("problem forking.\n");
        exit(1);
    }else if (pid==0){
        printf("In the child process.\n");
    } else {
        printf("In the parent process.\n");
    }
    
    return 0;
    
}

Alright, let’s tie it all together in one altogether nifty example.

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

int main(void){
    
    pid_t child;
    
    if((child=fork())<0){
        printf("Problem with fork()\n");
        exit(1);
    } else if (child == 0){
        printf("In child process, parent ID %d\n", getppid());
        printf("In child process, process ID %d\n", getpid());
    } else {
        printf("In parent process, process ID %d\n", getpid());
    }
    
    return 0;
    
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s