#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
fork();
puts("Hi");
return 0;
}
// Output:
// Hi
// Hi
// Hi
// Hi
#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
puts("Hi");
return 0;
}
// Output:
// Hi
// Hi
System call fork() is used to create processes. It takes no arguments and
returns a process ID.
The purpose of fork() is to create a new process, which becomes the child
process of the caller.
After a new child process is created, both processes will execute
the next instruction following the fork() system call.
#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
puts("Hi");
return 0;
}
// Output:
// Hi
// Hi
// Hi
// Hi
// Hi
// Hi
// Hi
// Hi
int counter = 0;
int main()
{
int i;
for (i = 0; i < 2; i ++){
fork();
counter++;
printf("counter = %d\n", counter);
}
printf("counter = %d\n", counter);
return 0;
}