Lab 2 - Fork System Call
Lab 2 - Fork System Call
Matric No:
Group:
Unix Process
• An entity that executes a given piece of code
• has its own execution stack
• has its own set of memory pages
• has its own file descriptors table
• A unique process ID
This system call causes the current process to be split into two processes
• a parent process
• a child processes
Please compile and run the following code on your Ubuntu / Linux
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
fork();
return 0;
How many times the program print its output? Please provide screenshot. Please briefly
explain why.
Page | 1
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
Based on code given in 2.1 above, please comment out the line #include <unistd.h>
and compile and run the code, what is the output, please also provide screenshot. Explain
why.
• 0 if it is a child process
• Positive value if it is a parent
• -1 if there is an error
break;
break;
perror("fork");
exit(1);
exit(0);
Page | 2
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
What is the Parent and Child Process PID? Please provide screenshot.
#include <sys/wait.h>
break;
wait(NULL);
break;
perror("fork");
exit(1);
exit(0);
Page | 3
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
What is the difference from previous code output, please also provide screenshot of the
output, why is that?
To make things easier and modular, we often use function to execute a child or parent task.
Please compile and run the following code on your Ubuntu / Linux
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void childTask() {
printf("Salam, saya anak tau\n");
}
void parentTask() {
printf("Dan saya adalah bapaknya n");
}
int main(void) {
pid_t pid = fork();
if(pid == 0) {
childTask();
exit(EXIT_SUCCESS);
}
else if(pid > 0) {
wait(NULL);
parentTask();
}
else {
printf("Unable to create child process.");
}
return EXIT_SUCCESS;
}
Page | 4
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
How many times the program print its output? Please provide screenshot. Please briefly
explain why it is easier to use function.
Now, please compile and run the following code on your Ubuntu / Linux
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
for(int i = 1; i < 13; i++) {
pid_t pid = fork();
if(pid == 0) {
printf("Child process => PPID=%d,
PID=%d\n", getppid(), getpid());
exit(0);
}
else {
printf("Parent process => PID=%d\n", getpid());
printf("Waiting for child processes to finish...\n");
wait(NULL);
printf("child process finished.\n");
}
}
return EXIT_SUCCESS;
}
How many child processes has been created? Please provide screenshot.
Page | 5
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.
Prepared by: [email protected] | 11 October 2020 | FSKM, UiTM Shah Alam
Now based on what you have learn in this lab, modify above code or create a new code to
create a program which the 4-child process will ask user to enter its name and display back
name which has been given. The parent process will then wait for all child to finish its job and
then print out “Job is done” and then exit. Your code must use fork, wait, function and loop
to achieve the output. Please provide screenshot of your output.
Page | 6
Disclaimer: Author is not held responsible if the lab exercise is targeted to unauthorized parties or host. This is
purely for educational purposes.