Saturday, January 15, 2011

OS-> GETPID SYSTEM CALL

GETPID SYSTEM CALL


AIM:
   
            To write a c program to implement getpid system call.

ALGORITHM:

    STEP 1:  Start the program.
    STEP 2:  Include the header file.
    STEP 3:  Declare the variable pid.
    STEP 4:  Assign pid=fork().
    STEP 5:  If pid is greater than zero display the parent and child before
                    fork.
    STEP 6:  Else if pid==0 display parent and child on executing
    STEP 7:  Else display fork error.
    STEP 8:  Stop the program.



















PROGRAM:

#include<stdio.h>
int main()
 {
   int pid;
   printf("before fork\n");
   pid=fork();
   if(pid>0)
    {
      sleep(1);
      printf("parent%d,\nchild%d",getpid(),pid);
    }
   elseif(pid==0)
    {
      printf("parent%d,\nchild%d",getppid(),getpid());
    }
   else
    {
      printf("\nfork error");
      exit(1);
    }
   printf("both executing\n");
   exit(0);
 }


OUTPUT:

 $ cc getpid.c
 $./a.out
 Before fork
 Parent 3406
 Child 3407 both executing
 Parent 3406
 Child 3407 both executing