-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.c
28 lines (21 loc) · 871 Bytes
/
fork.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// C program demonstrating the use of fork() system call
// I prefer not to go gentle ~Pratham Nikam
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
void main()
{
// Returned value from fork() call is stored
int gotfromfork = fork();
// if returned value is -1 means child process was not created successfully.
if(gotfromfork == -1) printf("Child was not created successfully.\n");
// if returned value is +ve means child process was created successfully and
// the value is process id of new child process and we are currently in parent process.
else if(gotfromfork > 0) printf("Child created successfully with process id : %d \n", gotfromfork);
// We are currently working in newly created child process
else printf("Hi from child!!!\n");
pid_t pid;
pid = getpid();
printf("Hello pratham!!! My process id is %d \n",pid);
return;
}