227CSS 3 - Lab 3
227CSS 3 - Lab 3
227CSS 3 - Lab 3
1.1 OBJECTIVE
The objective of the lab exercise is to give the general overview of fork ( ) system call and error
handing function perror ( ). C language will be used as an example to demonstrate how to write a
program by using the system call fork ( ) and perror ( ) function using Oracle VM VirtualBox in
a Linux environment.
In computing, a system call is how a program requests a service from an operating system's
kernel. This may include hardware-related services (e.g. accessing the hard disk), creating and
executing new processes, and communicating with integral kernel services (like scheduling).
System calls provide an essential interface between a process and the operating system.
A system call fork ( ) used to create processes. It takes no arguments and returns a process ID.
The purpose of fork () is to create a new process, which becomes the child process of the caller.
After creating a new child process, both processes will execute the next instruction following the
Page 1 of 6
fork () system call. Therefore, we have to distinguish the parent from the child. This can be done
by testing the returned value of fork ():
• If fork () returns a negative value, the creation of a child process was unsuccessful.
• fork () returns a zero to the newly created child process.
• fork () returns a positive value, the process ID of the child process, to the parent. The
returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is
an integer. Moreover, a process can use function getpid ( ) to retrieve the process ID
assigned to this process.
4. perror () function
The perror () function is another of C's error-handling tools. When called, perror() displays a
message on stderr describing the most recent error that occurred during a library function call or
system call. The prototype, in STDIO.H, is
5. LAB EXERCISE
Experiment: Actually, there are three distinct operations involved: creating a new child process,
causing the new process to execute a program, and coordinating the completion of the child
process with the original program.
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
int main(void)
{
printf("Hello World!\n");
fork( );
printf("I am after forking\n");
printf("\tI am process %d.\n", getpid( ));
}
Page 2 of 6
Note: When this program is executed, it first prints Hello World! . When the fork is executed, an
identical process called the child is created. Then, both the parent and the child process begin
execution at the next statement.
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
int main(void)
{
int pid;
printf("Hello World!\n");
printf("I am the parent process and pid is : %d .\n",getpid());
printf("Here i am before use of forking\n");
pid = fork();
printf("Here I am just after forking\n");
if (pid == 0)
printf("I am the child process and pid is :%d.\n",getpid());
else
printf("I am the parent process and pid is: %d .\n",getpid());
}
Page 3 of 6
Output of the Problem 5.2
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
main(void)
{
printf("Here I am just before first forking statement\n");
fork();
printf("Here I am just after first forking statement\n");
fork();
printf("Here I am just after second forking statement\n");
printf("\t\tHello World from process %d!\n", getpid());
}
Page 4 of 6
Output and description of the Problem 5.3
#include <stdio.h>
int main(void)
{
int pid;
int status;
printf("Hello World!\n");
pid = fork( );
Page 5 of 6
if (pid < 0) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
printf("I am the parent process.\n");
}
Signature: Date: / /
Page 6 of 6