Background Process &system Call Fork ' Lab # 09
Background Process &system Call Fork ' Lab # 09
‘Fork( )’
LAB#09
Background Process &System Call ‘Fork()’
OBJECTIVE
Study the features of Background Process.
THEORY
First compile the above program using command ‘gcc program name.cc’ , then execute it
through the command ‘./a.out &’. The & will make it a background process, which is terminated
only if the process ends, or is interrupted with a DEL key. While the said program is running in
the background, open a file with any editor and type something, save it and execute it. For
reference consider Program 1.
Process Identification
There is a function – getpid( ) – that enables us to get the identification number of a process. For
reference consider Program 2 and Program 3.
The pid value will depend on the number of processes already running. It will always be unique.
This pid cannot be changed although it may be reused once the process no longer exists.
Run the program 3 twice. Both times as a background process, i.e suffix it with an ampersand
(&). Once both are running as a background processes key the ps-a command and you will see
that memory contains these two processes.
The child process begins from the fork( ). All the statements after the call to fork( ) will be
executed twice. Once by the parent process and once by the child process. But had there been any
statements before the fork( ) they would have been only executed by the parent process. For
reference consider Program 6.
Example programs
Program 1
main ( )
{
long i;
for (i=0; i<=400000; i++);
printf(“l is %d\n”,i);
}
Program 2
main ( )
{
int pid;
pid = getpid( );
printf(“Process ID is %\n”,pid);
}
Program 3
main( )
{
long i;
printf(“Process ID is %\n”,getpid( ));
for(i=0;i<=4000000;i++);
printf(“I is %/d\n”,i);
}
Program 4
main ( )
{
int ppid;
ppid = getppid( );
printf(“Parent Process ID is %d\n”,ppid);
}
Program 5
main( )
{
fork( );
printf(“Hello World\n”);
}
Program 6
main( )
{
printf(“This is to demonstrate the fork( )\n”);
fork( );
printf(“Hello World\n”);
}
Exercises
Program 1
main ( )
{
long i;
for (i=0; i<=400000; i++);
printf(“l is %d\n”,i);
}
Output:[1] 2278
Program 2
main ( )
{
long i;
for (i=0; i<=400000; i++);
printf(“l is %d\n”,i);
}
Output:[1] 2361
Process ID is 2316
[1] + Exit 20
Program 3
main( )
{
long i;
printf(“Process ID is %\n”,getpid( ));
for(i=0;i<=4000000;i++);
printf(“I is %/d\n”,i);
}
Output:[p1] 2384
Parent process ID is 2114
[1] + Exit 26
Program 4
main ( )
{
int ppid;
ppid = getppid( );
printf(“Parent Process ID is %d\n”,ppid);
}
Output:[1] 2419
Hellow world
Hellow world
[1] +Exit 12
Program 5
main( )
{
Int pid;
Pid=fork();
If(pid>0)
Printf(“parent process PID is %d\n”,pid);
}
Output:[1]2455
Parent process PID is 2455
[1] + Fruit 27.
Program 6
Main()
{
Int pid;
Pid=fork();
If(pid==o)
Printf (“child process\n);
}
Output:
[1] 2736
Child process
[1] + exit-178
Program 7
Main()
{
Int pid;
Pid=fork();
If(pid==0)
{
Printf (“I am the child,my process ID is %d \n”,getpid());
Printf(“The child’s parent process ID is %d \n”,getpid());
}
Else
{
Printf (“I am the parent,my process ID is %d \n”,getpid());
Printf(“The parents parent process ID is %d \n”,getpid());
}}
Output:[1] 2824
I am the parent, my process ID is 2824.
The parents parent process ID is 2114.
I am the child, my process ID is 2826.
The child’s parent process ID is 1.