0% found this document useful (0 votes)
76 views4 pages

Background Process &system Call Fork ' Lab # 09

The document discusses background processes and the fork() system call in UNIX. It explains that background processes run independently and in parallel to other processes. The fork() call creates child processes from the parent process. Child processes created via fork() will execute the same code as the parent but have their own memory space. The document provides example programs to demonstrate background processes, getting process IDs, parent-child relationships, and the behavior of fork().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views4 pages

Background Process &system Call Fork ' Lab # 09

The document discusses background processes and the fork() system call in UNIX. It explains that background processes run independently and in parallel to other processes. The fork() call creates child processes from the parent process. Child processes created via fork() will execute the same code as the parent but have their own memory space. The document provides example programs to demonstrate background processes, getting process IDs, parent-child relationships, and the behavior of fork().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Background Process &System Call 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.

Parent And Child


A process in UNIX is not a standalone. In the same way as a human being or animal is born from
another, a process to has to come from the womb of another process. This results in a parent-child
relationship existing between processes. For reference consider Program 4.
Program 4 will give the ID number of its parents.
When we boot the system, a special process called the swapper or scheduler is created with a PID
of 0. The swapper manages memory allocation for processes and influences CPU allocation. The
swapper in turn creates three: the process dispatcher, vhand, and bdflush with ID numbers 1,2 and
3 respectively.
This is done by executing the file init which exists in the etc sub-directory. The process
dispatcher now gives birth to the shell. From now on all processes initiated by us are children of
the shell and in turn descendents of the process dispatcher. This gives rise to a tree-like structure,
with ADAM as the swapper.
UNIX keeps track of all processes in an internal data structure called the process table. A listing
of the process table can be got using the ps-el command.

The 'fork( )'


Processes initiated by us can also create children in the same manner as the swapper and the
process dispatcher did. These children processes are created using the fork ( ) function. It is by
forking processes that we can exploit the multitasking capability of UNIX. For reference consider
Program 5.

Submitted By: Ali Murad


Operating System
Background Process &System Call Lab # 09
‘Fork( )’

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”);
}

Submitted By: Ali Murad


Operating System
Background Process &System Call Lab # 09
‘Fork( )’

Program 6
main( )
{
printf(“This is to demonstrate the fork( )\n”);
fork( );
printf(“Hello World\n”);
}

Exercises

1. Write the output of the following programs.

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;

Submitted By: Ali Murad


Operating System
Background Process &System Call Lab # 09
‘Fork( )’

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.

Submitted By: Ali Murad


Operating System

You might also like