Professional Documents
Culture Documents
Practical Sheet 2
Practical Sheet 2
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.
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.
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.
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
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());
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