Child Parent Programing Basics Linux OS Programing - COFPROG

Child Parent Programing Basics Linux OS Programing


Create a program to demonstrate 5 child processes having common parent process.
  

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

int main()
{
    int pid, i;

    for(i=0;i<5;i++)
    {

        pid = fork(); //crate a new child process.
  
        if(pid < 0)
            printf("\nProcess not created..");
        if(pid == 0)
        {
            printf("\nIn child process with PID = %d and PPID = %d",getpid(),getppid());  
            while(1);
        }
  
        if(pid > 0)
        {
            printf("\nIn parent process with PID = %d and PPID = %d",getpid(),getppid());  
        }
    }

    return 0;
}


process: 
- process is part of group related instructions that are being executed. It contains the program code and its current activity state.
- Process is usually made up of multiple threads of execution that execute instructions concurrently.
 

Create a program to demonstrate 5 child processes having different parent processes.

 

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

int main()
{
    int pid, i;

    for(i=0;i<5;i++)
    {
        pid = fork();
  
        if(pid < 0)
            printf("\nProcess not created..\n");
        if(pid == 0)
        {
            printf("\nIn child process with PID = %d and PPID = %d\n",getpid(),getppid());  
        }
  
        if(pid > 0)
        {
            printf("\nIn parent process with PID = %d and PPID = %d\n",getpid(),getppid());  
        //    exit(0);
            break;
        }
    }
    while(1);

    return 0;
}



 

fork()The fork() is a system which will create a new child process. The child process created is an identical process to the parent except that has a new system process ID. The process is copied in memory from its parent process, then a new process structure is assigned by the kernel. The return value of the function is which discriminates the two threads of execution. A 0 is returned by the fork function in the child's process, while the PID of the child process is returned in the parent's process.
Previous
Next Post »

BOOK OF THE DAY