Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OS Lab2
- //Aim:fork() system call basic
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- int main()
- {
- pid_t pid;
- pid=fork();
- pid=fork();
- printf("\nHello World %d",getpid());
- return 0;
- }
- //fork() with if condition
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- int main()
- {
- pid_t pid;
- pid=fork();
- if(pid==0)
- {
- printf("\nnew process of child with id: %d and Parent: %d\n",getpid(),getppid());
- }
- else
- {
- printf("\nparent with id: %d\n",getpid());
- }
- return 0;
- }
- //Panning
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- void main(void)
- {
- pid_t pid;
- int n,i;
- printf("Enter no of child :");
- scanf("%d",&n);
- printf("\n Parent is %d ",getpid());
- for(i=0;i<n;i++)
- {
- pid=fork();
- if(pid==0)
- {
- printf("\n Child Created %d with %d Parent \n",getpid(),getppid());
- exit(0);
- }
- else
- {
- wait();
- continue;
- }
- }
- }
- //chaining
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- void main(void)
- {
- pid_t pid;
- int n,i;
- printf("Enter no of child :");
- scanf("%d",&n);
- printf("\n Parent is %d ",getpid());
- pid=fork();
- for(i=0;i<n;i++)
- {
- if(pid==0)
- {
- printf("\n Child Created %d with %d Parent \n",getpid(),getppid());
- pid=fork();
- wait();
- }
- else
- {
- wait();
- continue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement