OS Lab 3
OS Lab 3
Lab Manual 03
Operating Systems
System Calls
“In computing, a system call is the programmatic way in which a computer program requests a service from the
kernel of the operating system it is executed on. A system call is a way for programs to interact with the operating
system.”
System Calls provide the Interface between a process and the Operating System.
These calls are generally available as Assembly language instruction.
System Calls can also be made directly through High Level Language programs for certain systems.
UNIX System calls can be invoked directly from a C or C++ program.
Unix and Windows have different system calls as can be seen from the table below
Reference –http://www.cs.columbia.edu/~jae/4118/L02-intro2-osc-ch2.pdf
Fork ()
“Fork system call is used for creating a new process, which is called child process, which runs concurrently with the
process that makes the fork() call (parent process)”
After a new child process is created, both processes will execute the next instruction following the fork()
system call.
A child process uses the same pc(program counter), same CPU registers, same open files which use in the
parent process.
Syntax: fork();
Code Code
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
int main() int main()
{ {
printf("Welcome to my printf("Welcome to my
Program\n"); Program\n");
// make a new process which // make a new process which
run same program after this run same program after this
instruction instruction
fork(); fork();
printf("Hello world!\n"); printf("Hello world!\n");
return 0; return 0;
} }
Child Process
Pid=121
Code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Hello world!\n");
return 0;
}
Output
Hello world!, I am Parent
Hello world!, I am Child
Note: Order of parent and child execution can vary unless controlled
Some more useful System Calls
Wait ()
A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After child
process terminates, parent continues its execution after wait system call instruction.
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
} // Parent will only terminate after its child has terminated
exec()
The exec family of functions replaces the current running process with a new process. It can be used to run a C
program by using another C program. It comes under the header file unistd.h.
There are many members in the exec family we will be seeing “excelp” for now.
The execlp() function replaces the current process image with a new process image specified by file. The new
image is constructed from a regular, executable file called the new process image file. No return is made because the
calling process image is replaced by the new process image.
Syntax
#include <unistd.h>
Arguments
file
Used to construct a pathname that identifies the new process image file. If the file argument contains a slash
character, the file argument is used as the pathname for the file.
arg0, …, argn
Pointers to NULL-terminated character strings. These strings constitute the argument list available to the new
process image. You must terminate the list with a NULL pointer. The arg0 argument must point to a filename
that's associated with the process being started and cannot be NULL.
Example
Program to create image for
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char * arg[])
{
printf(arg[1]);
return 0;
}
Execlp program to run the Process
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
{
int x=execlp("./a.out","a.out","Hello",NULL);
return 0;
}
You can also use execlp to run unix system commands such as ls , cp , mkdir etc. Example is shown below
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
// creating a new directory named Lab using execlp
int x=execlp("mkdir","mkdir","LabTestFolder",NULL);
return 0;
}
Task 1: Fork ( ) and wait ( ) System Call Estimated Time: 60 mins
Create a C program that takes an integer array of 10 values from the user in parent process. The program then
creates a child processes, Child 1 sorts the array in ascending order and displays it on the screen.
Once you are done, modify your program to create 2 child processes:
Child 1 sorts the array in ascending order and displays it on the screen and Child 2 sorts the array in descending
order and displays it on the screen.
Except input and process creation no work should be done by the parent process and parent should terminate after
child process.
Example
Enter 10 Numbers: 5 4 6 9 8 7 1 2 3 5
I am Child 1: 1 2 3 4 5 5 6 7 8 9
I am Child 2: 9 8 7 6 5 5 4 3 2 1
Parent Process terminating