0% found this document useful (0 votes)
63 views10 pages

OS Lab 3

The document describes system calls in operating systems. It discusses how system calls provide an interface between processes and the OS kernel to request services. Some key system calls described are fork(), wait(), and exec(). Fork is used to create new processes. Wait blocks the parent process until a child exits. Exec replaces the current process with a new program. Examples are given to demonstrate how to use these system calls. Two programming tasks are assigned - one involving fork, wait, and sorting an array, and another using execlp to perform directory and file operations.

Uploaded by

alihamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views10 pages

OS Lab 3

The document describes system calls in operating systems. It discusses how system calls provide an interface between processes and the OS kernel to request services. Some key system calls described are fork(), wait(), and exec(). Fork is used to create new processes. Wait blocks the parent process until a child exits. Exec replaces the current process with a new program. Examples are given to demonstrate how to use these system calls. Two programming tasks are assigned - one involving fork, wait, and sorting an array, and another using execlp to perform directory and file operations.

Uploaded by

alihamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

National University of Computer and Emerging Sciences

Lab Manual 03
Operating Systems

(Lab Topic : System Calls)

Department of Computer Science


FAST-NU, Lahore, Pakistan

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.

Services Provided by System Calls:


Services provided by system calls include the following:
 Process creation and management
 Main memory management
 File Access, Directory and File system management
 Device handling(I/O)
 Protection
 Networking, etc.

Types of System Calls:


There are 5 different categories of system calls
1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communication

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();

It takes no parameters but returns an integer value.


Code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf("Welcome to my Program\n");
// make a new process which run same program after this instruction
fork();
printf("Hello world!\n");
return 0;
}
Output
Welcome to my Program
Hello world!
Hello world!
Parent Process Parent Process

Pid (Process identifier) =120 Pid=120

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;
}

Program Before Fork Program After Fork


How to differentiate between a Parent and Child during execution?

Below are different values returned by fork():


 Negative Value: creation of a child process was unsuccessful.
 Zero: Returned to the newly created child process.
 Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
Code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int a= fork();
if(a==0)
{printf("\nHello world!, I am child");
}
else if(a>0){
printf("\nHello world!, I am Parent");
} else{
printf("\nBye Bye world!, Error creating child");
}
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.

// C program to demonstrate working of wait()


#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>

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>

int execlp( const char * file,


const char * arg0,
const char * arg1,

const char * argn,
NULL );

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 main(int argc, char * arg[])

{
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

Example: creating a new directory named Lab using execlp

#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

Task 2: execlp( ) System Call Estimated Time: 30 mins


Using execlp () implement the following:
1- Create a directory named lab3-task2 (mkdir)
2- Create 2 new files in the directory you have created (touch)
3- List the files in the directory (ls)
4- Remove the directory

You might also like