Nothing Special   »   [go: up one dir, main page]

Fork Lecture

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

fork() in C

Fork system call use for creates a new process, which is called child process, which runs
concurrently with process (which process called system call fork) and this process is called
parent process. After a new child process created, both processes will execute the next
instruction following the fork() system call. A child process use same pc(program counter), same
CPU registers, same open files which use in parent process.

It take no parameters and return integer value. Below are different values returned by fork().

Negative Value: creation of a child process was unsuccessful.


Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created child
process.

Predict the Output of the following programs:

1. Predict the Output of the following program.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {

// make two process which run same


// program after this instruction
fork();

printf("Hello world!\n");
return 0;
}

Output:

Hello world!
Hello world!
Calculate number of times hello is printed.
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}

Output:

hello
hello
hello
hello
hello
hello
hello
hello

Number of times hello printed is equal to number of process created. Total Number
of Processes = 2n where n is number of fork system calls. So here n = 3, 23 = 8

Let us put some label names for the three lines:


fork (); // Line 1
fork (); // Line 2
fork (); // Line 3

L1 // There will be 1 child process


/ \ // created by line 1.
L2 L2 // There will be 2 child processes
/ \ / \ // created by line 2
L3 L3 L3 L3 // There will be 4 child processes
// created by line 3

So there are total eight processes (new child processes and one original process).
Predict the Output of the following program
#include<stdio.h>
#include <sys/types.h>
#include<unistd.h>
void forkexample()
{
// child process because return value zero
if (fork()==0)
printf("Hello from Child!\n");

// parent process because return value non-zero.


else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}

Output:

1.
Hello from Child!
Hello from Parent!
(or)
2.
Hello from Parent!
Hello from Child!

In the above code, a child process is created, fork() returns 0 in the child process and positive
integer to the parent process.
Here, two outputs are possible because parent process and child process are running
concurrently. So we don’t know if OS first give control to which process a parent process or a
child process.

Important: Parent process and child process are running same program, but it does not mean
they are identical. OS allocate different data and state for these two processes and also control
flow of these processes can be different . See next example
Predict the Output of the following program.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

void forkexample()
{
int x = 1;

if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}

4. Output:
5. Parent has x = 0
6. Child has x = 2
7. (or)
8. Child has x = 2
9. Parent has x = 0

Here, global variable change in one process does not affected two other processes
because data/state of two processes are different. And also parent and child run
simultaneously so two outputs are possible.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
if(fork())
fork();

printf("I am pid %d!\n", getpid());

return 0;
}
#include <sys/types.h>
#include <unistd.h>
#include<stdio.h>

int main()
{
printf("hello world from a process ID %d\n", getpid());
printf("hello world from a parent process ID %d\n\n", getppid());
return 0;
}

Exec() can also be used without fork() its not necessary to use it in Fork Call..
#include<stdio.h>

#include<sys/wait.h>

#include<unistd.h>

int main()

if (fork()== 0)

printf("HC: hello from child\n");

else

printf("HP: hello from parent\n");

wait(NULL);

printf("CT: child has terminated\n");

printf("Bye\n");

return 0;

EXEC
exec is a functionality of an operating system that runs an executable file in the context of an
already existing process, replacing the previous executable. This act is also referred to as an
overlay. Since a new process is not created, the original process identifier (PID) does not change,
but the machine code, data, heap, and stack of the process are replaced by those of the new
program.
#include<stdio.h>
#include<unistd.h>

int main()
{
int i;

printf("I am EXEC.c called by execv() ");


printf("\n");
return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);

/*All statements are ignored after execvp() call as this whole


process(execDemo.c) is replaced by another process (EXEC.c)
*/
printf("Ending-----");

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */

int main(int argc,char** argv)


{
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0)
{ /* child process */
// static char *argv[]={"echo","Gcu is my name.",NULL};
execv("/bin/echo",argv);
exit(127); /* only if execv fails */
}
else
{ /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
}
return 0;
}

You might also like