Advertisement
tsnaik

OS lab2

Jul 13th, 2015
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. OS Lab2
  2.  
  3. //Aim:fork() system call basic
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<sys/types.h>
  8.  
  9. int main()
  10. {
  11.     pid_t pid;
  12.     pid=fork();
  13.    
  14.    
  15.     pid=fork();
  16.     printf("\nHello World %d",getpid());
  17.    
  18.     return 0;
  19. }
  20.  
  21.  
  22. //fork() with if condition
  23.  
  24.  
  25. #include<stdio.h>
  26. #include<stdlib.h>
  27. #include<sys/types.h>
  28.  
  29. int main()
  30. {
  31.     pid_t pid;
  32.     pid=fork();
  33.    
  34.     if(pid==0)
  35.     {
  36.       printf("\nnew process of child with id: %d and Parent: %d\n",getpid(),getppid());
  37.     }
  38.     else
  39.     {
  40.         printf("\nparent with id: %d\n",getpid());
  41.     }  
  42.    
  43.     return 0;
  44. }
  45.  
  46.  
  47. //Panning
  48.  
  49. #include  <stdio.h>
  50. #include  <stdlib.h>
  51. #include  <sys/types.h>
  52.  
  53.  
  54.  
  55. void  main(void)
  56. {
  57.      pid_t  pid;
  58.      
  59.      int n,i;
  60.  
  61.     printf("Enter no of child :");
  62.     scanf("%d",&n);
  63.      
  64.         printf("\n Parent is %d ",getpid());
  65.     for(i=0;i<n;i++)
  66.     {
  67.         pid=fork();
  68.  
  69.         if(pid==0)
  70.         {
  71.             printf("\n Child Created %d  with %d Parent \n",getpid(),getppid());
  72.             exit(0);
  73.         }
  74.         else
  75.         {
  76.             wait();
  77.             continue;
  78.         }
  79.     }
  80.    
  81.  
  82. }
  83.  
  84.  
  85. //chaining
  86.  
  87. #include  <stdio.h>
  88. #include  <string.h>
  89. #include  <sys/types.h>
  90.  
  91.  
  92.  
  93. void  main(void)
  94. {
  95.      pid_t  pid;
  96.      
  97.      int n,i;
  98.  
  99.     printf("Enter no of child :");
  100.     scanf("%d",&n);
  101.      
  102.     printf("\n Parent is %d ",getpid());
  103.     pid=fork();
  104.  
  105.     for(i=0;i<n;i++)
  106.     {
  107.        
  108.         if(pid==0)
  109.         {
  110.             printf("\n Child Created %d  with %d Parent \n",getpid(),getppid());
  111.             pid=fork();
  112.             wait();
  113.         }
  114.         else
  115.         {
  116.             wait();
  117.             continue;
  118.         }
  119.     }
  120.    
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement