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

Kingdom of Saudi Arabia ‫المملكة العربية السعودية‬

Ministry of Higher Education


Najran University ‫وزارة التعليم العالي‬
College of Computer Science and ‫جامعة نجران‬
Information Systems ‫كلية علوم الحاسب ونظم المعلومات‬

Course Code: 227CSS-3 Lab Report #: 3

Course Name: Operating Systems Date:

Student Name: _____________________________________ ID: ___________________

To understand the use fork() system call, getpid( ) and perror( )


error handling function

1. OBJECTIVE AND LEARNING OUTCOMES

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.

1.2 LEARNING OUTCOMES


At the end of the laboratory session, students should be able to;
1. Understand the concept of system calls in the operating system.
2. Understand the basic concept of fork ( ) system call, getpid( ) and perror ( ) error handing
function.
3. Know how to create, compile and execute a C program using fork ( ) and perror ( )
functions under Unix System.

2. INTRODUCTION TO SYSTEM CALLS

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.

3. THE fork () SYSTEM CALL

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

• void perror(char *msg);

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.

Lab Question 5.1:


The following program demonstrates the use of fork ( ). Write this program in Oracle VM
VirtualBox, compile it and run it according to lab instructor instructions and study the output.

#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.

Output of the Problem 5.1

Lab Question 5.2:


Following program demonstrates the use of fork ( ) and getpid( ). Write this program in Oracle
VM VirtualBox, compile it and run it according to lab instructor instructions and study the
output.

#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

Lab Question 5.3:


Following program demonstrates the use of fork ( ) more than one time. Write this program in
Oracle VM VirtualBox, compile it and run it according to lab instructor instructions and study
the output. And write the description of each step.

#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

Lab Question 5.4:


Following program demonstrates the use of perror ( ) function. Write this program in Oracle VM
VirtualBox, compile it and run it according to lab instructor instructions and study the output.
Note: This program guarantees the child process will print its message before the parent process

#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");
}

Output of the Problem 5.4

Comments by the Student:

For The Teacher Use Only:

Comments and Marks:

Signature: Date: / /

Page 6 of 6

You might also like