Tuesday, January 18, 2011

O.S->WAIT SYSTEM CALL

WAIT  SYSTEM  CALL


 AIM:

            To write a c program to implement wait system calls.

 ALGORITHM:

     STEP 1:  Start the program.
     STEP 2:  Include the header files.
     STEP 3:  Declare the variable pid and assign i=0,pid=fork( ).
     STEP 4:  If pid=0 then child starts.
     STEP 5:  For i=0,i less than 10 print the value of i.
     STEP 6:  Child process ends.
     STEP 7:  Else child ends now in parent process.
     STEP 8:  Stop the program.






















PROGRAM:

  #include<stdio.h>
  #include<sys/wait.h>
  int main()
  {
    int pid,i=0;
    printf("\n ready to fork");
    pid=fork();
    if(pid==0)
     {
       printf("\n child starts\n");
       for(i=0;i<10;i++)
       printf("\n%d\n",i);
       printf("child ends\n");
       exit(0);
     }
    else
     {
       wait(0);
       printf("child ends now in parent process");
       printf("exist status");
     }
  }

OUTPUT:

$./a.out
 Ready to fork
 Child starts
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 Child ends
 Ready to fork child ends now in parent process
 exit status