Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Practical Exercise 2

OPERATING SYSTEM INTERNALS AND BIOMETRICS

Learning Objectives: Students will be able to learn basic Systems Call.


Fork : Creates a new process, which is called child process, which runs concurrently with the
process that makes the fork() call (parent process). After a new child process is created, both
processes will execute the next instruction following the fork() system call. A child process
uses the same Program Counter (PC), same CPU registers, same open files which use in the
parent process.
Exercise 1. Run the following programme and observe the output.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork a process
//* the child and parent will execute every line of code
after the fork (each separately)*/
fork();
printf("Hello world!\n");
return 0;
}
Question 01 : Note down the output you have received. Explain it.

Hello world!
Hello world!

Explanation :
When the folk() function is called, the main process creates a child process. Both the
child process and the main process execute concurrently and therefore the text "hello
world!" will be printed twice from both the processes.

Exercise 2. Run the following programme and observe the output.


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

1
Practical Exercise 2
OPERATING SYSTEM INTERNALS AND BIOMETRICS
{
pid_t pid_val;
fork();
pid_val = getpid();
printf("Hello world!\n");
printf("My PID value = %d\n",pid_val);//get the process
ID(PID)
return 0;
}
Question 02 : Note down the output you have received. Explain it.

Hello world!
My PID value = 758467
Hello world!
My PID value = 758470

Explanation:
The program declares a variable named pid_val of type pid_t which is used to store the ID of a process. Once the
folk () function is called , the main process would create a child process and they would have 2 separate execution
lines and the remaining code would be executed twice as a result of this. The main process display "Hello World"
and it's process ID and so will the Child process.

Exercise 3. Run the following programme and observe the output.


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int main()
{
pid_t pid_val;
int pid_va_p = fork();//get the parent PID
pid_val = getpid();
printf("Hello world!\n");
printf("My PID value = %d and My Parent PID value =
%d\n",pid_val, pid_va_p);
return 0;
}

2
Practical Exercise 2
OPERATING SYSTEM INTERNALS AND BIOMETRICS

Question 03 : Note down the output you have received. Explain it.

Hello world!
My PID value = 3646799 and My Parent PID value = 3646802
Hello world!
My PID value = 3646802 and My Parent PID value = 0

Explanation:
When the folk() function is called a new child process is created by the main process. The folk () will
return a value of 0 to the child process and it will return the process id of the child process to the
main process and this value will stored in the pid_val_p variable.

Exercise 4. Run the following programme and observe the output.


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

int main() {
if(fork() == 0)
if(fork())
printf("Hello world!!\n");
exit(0);
}
Question 04 : Note down the output you have received. Explain it.

Hello World!!

Explanation
The fork () function is called and a child process will be created. Inside this child process, another fork() call is
made within a nested if statement. This creates a grandchild process. In the parent process (created by the
initial fork() call), the nested if statement evaluates to true, which will print "hello World!!" . However, in the
grandchild process, the nested if statement is not executed, so the printf() function is not called. Finally, all
processes exit after their respective executions, with "Hello world!!" printed only once by the parent process

Exercise 5. Run the following programme and observe the output.


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

3
Practical Exercise 2
OPERATING SYSTEM INTERNALS AND BIOMETRICS
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("I am: %d\n", (int) getpid());

pid_t pid = fork();


printf("fork returned: %d\n", (int) pid);

if (pid < 0) { /* error occurred */


perror("Fork failed");
}
if (pid == 0) { /* child process */
printf("I am the child with pid %d\n", (int) getpid());
printf("Child process is exiting\n");
exit(0);
}
/* parent process */
printf("I am the parent waiting for the child process to
end\n");
wait(NULL);
printf("parent process is exiting\n");
return(0);
}
Question 05 : Note down the output you have received. Explain it.
I am: 489709
fork returned: 0
I am the child with pid 489712
Child process is exiting
I am: 489709
fork returned: 489712
I am the parent waiting for the child process to end
parent process is exiting

Explanation:
At the start of the program, process id of the main process is printed. Following this, a new child process is created after folk() function is called. The return
value of fork() is different between the parent and child processes, with the child receiving a return value of 0 and the parent receiving the process ID of the
child. Since the child process receives a pid of 0, the if block where the pid is equal to zero would be executed printing itself as the child process along with
it's id and it will print that it's exiting. When the parent process is under executing it would not satisfy any of the if blocks since it receives a pid of the child
process (pid > 0) so it will print itself as the parent process and it will wait until the child process executes and will print itself as exiting

You might also like