FORK IT!

 #include<stdio.h>

 void main()
 {
     printf("Printing in Parent, only run once\n");
     printf("Process ID and parent process ID in parent: %d , %d\n",getpid(),getppid());
     fork();
     printf("Process ID and parent process ID in (runs in parent and child): %d , %d\n",getpid(),getppid());
     while(1);
 }

 #include<stdio.h>

 void main()
 {
     printf("Printing in Parent, only run once\n");
     printf("Process ID and parent process ID in parent: %d , %d\n",getpid(),getppid());
     fork();
     printf("Process ID and parent process ID in (runs in parent and child): %d , %d\n",getpid(),getppid());
     while(1);
 }

#include<stdio.h>
void main()
{
    printf(" Started from main, Calling forks\n");
    fork(); //fork1
    printf(" Fork 1 was called.\n");
    fork(); //fork2
    printf(" Fork 2 was called.\n");
    fork(); //fork3
    printf(" Fork 3 was called.\n");
    fork(); //fork4
    printf(" Fork 4 was called.\n");
    while(1);
}

void main()
{
        int i;

        for(i=1;i<=3;i++)
        {
                if(fork()==0)
                {
                        printf("Loop %d - PID:%d\tPPID:%d\n",i,getpid(),getppid()  ) ;
                }

        }
        while(1);
}

Leave a Reply