CS303-az Lab2
CS303-az Lab2
1. Overview
When a program in user mode requires access to hardware resources such as network interface
or disk, it must ask the kernel to provide access to that resource. This is done via something
called a system call.
When a program makes a system call, the mode is switched from user mode to kernel mode.
Then the kernel provides the resource which the program requested.
Generally, system calls are made by the user level programs in the following situations:
• Creating and managing new processes.
• Creating a connection in the network, sending, and receiving packets.
• Requesting access to a hardware device, like a mouse or a printer.
Main system calls we will be using are summarized in the following table:
Function Description
#include <sys/types.h> Clone the current process. Typically used to
#include <unistd.h> create a new instance of the current execution
Fork() environment; to create a new process running
If (fork() == 0) : this is the child a different program.
If (fork() > 0) : this is the parent
If (fork() < 0) : child process not created
#include <sys/wait.h> Wait termination of a process
Wait()
Waitpid()
#include <unistd.h>
Getpid() Get process identifier
Getppid() Get parent process identifier
#include <unistd.h> Replaces the current process with a new
Exec*() process, loaded from an executable file
2. Exercises
a) Run the following three programs and enumerate how many times “Hello Class” was displayed
for each program:
b) Run the following two programs to display the pid of the child process and the pid of its father
process.
Program 1
Program 2
c) Run the following two programs and explain the order in which the iterations are displayed:
Program 1 Program 2
➔ The father process creates the child but continues ➔Here we manage who will be waiting whom and
its work without caring about the child process. we will do some ordering between the processes.
The father must wait the child to finish before
executing the rest of the iterations.
d) Write a C program that creates two children processes. The first child process displays the
list of integers from 1 to 50. The second child displays the list of integers from 51 to 100.
Output of the Program:
e) Now, update the previous program in a way that the integers from 1 to 100 are displayed in a
perfectly ascending order.