Create a Process

#include<unistd.h>
#include<stdio.h>

int main()
{
    pid_t pid;

    pid = fork();

    if(pid<0)
        printf("1. Parent process %d\n",getpid());    
    else if(pid==0)
        printf("2. Child process %d\n",getpid());
    else
        printf("3. Parent ID %d, Child ID %d\n",getpid(), pid);



    return 0;
}

FUNCTIONS: fork- If successful, creates new process and returns process ID of the new process(to the parent) and 0(to new process). If not sucessful, returns -1 to the parent. pid = fork(); Parameters = None; Result Type = pid_t;

getpid- Returns the process id of the calling process. pid = getpid(); Parameters = None; Result Type = pid_t;

wait- Returns the process id of the next child process to exit. The exit status is written into the memory location pointed to by status_ptr. Parameters = int* status_ptr; Result Type = pid_t;

execl - Replaces the function in the process that is executing with the instructions in the executable file specified by the path and file arguments. The argument path specifies the full path name including the executable file name. The argument file specifies just the executable file name; The eclipses indicates there may be more arguments, but the last argument is always null. Parameters = const char path, const char file, ..., NULL Return type = int;

Waiting for a Process: A process can exit independently of its parent process or child process. As a result a child process can exit before or after its parent process, but it is generally preferred for all child process to exit before the parent process exits. When a child process exits before its parent process, the parent process can retrieve the exit status of the child process to determine whether the child process exited successfully or not. A parent process can retrieve the exit status of a child process by calling the wait function, defined in wait.h