lab manual
lab manual
School of Engineering
Department of Computer Science & Engineering
List of Experiments
Course: Operating System Code: 16CS372/16CT371
Exp. Division of
List of Experiments
No Experiments
Write a C program to create a new process that exec a new
1 program using system calls fork(), execlp() & wait()
System Calls Write a C program to display PID and PPID using system calls
2
getpid () & getppid ()
Write a C program using I/O system calls open(), read() &
3 write() to copy contents of one file to another file
Write a C program to implement multithreaded program using
4
pthreads
Process
Management Write C program to simulate the following CPU scheduling
5 algorithms
a) FCFS b) SJF c) Priority d) Round Robin
Process Write a C program to simulate producer-consumer problem
6
synchronization using semaphores
Write a C program to simulate Bankers algorithm for the
7
Deadlock purpose of deadlock avoidance.
8 Write a C program to simulate deadlock detection.
Write a C program to simulate paging technique of memory
9
Memory management
Management Write a C program to simulate page replacement algorithms
10
a) FIFO b) LRU c) LFU
Write a C program to simulate the following file organization
11 techniques
a) Single level directory b) Two level directory
I/O System
Write a C program to simulate the following file allocation
12 strategies.
a) Sequential b) Indexed
Instructions
1. Use Ubuntu OS
2. Use cc or gcc compiler for compiling C program
3. Type the program in gedit text editor. Save the program with .c file extension in
current directory
4. Compile the program at the terminal using the command
cc filename.c
5. Execute the program using the command
./a.out
Give instructions on
1. What is a process? How to create a process
2. What are system calls?
1. Write a C program to create a new process that exec a new program using system calls
fork(), execlp() & wait()
Program description:
C program (or, process, at run time) needs to create a process which would execute a program.
Here, two system calls are of interest, fork and exec. The fork system call in Unix creates a new
process. The new process (the child process) is an exact copy of the calling process (the parent
process). however, you want the new process to run a new program. When, for example, you
type "date" on the unix command line, the command line interpreter (the so-called "shell") forks
so that momentarily 2 shells are running, then the code in the child process is replaced by the
code of the "date" program by using one of the family of exec system calls execlp(). The parent
process waits for the child to exit.
System calls used
1. fork ( )
fork() is used to create new processes. The new process consists of a copy of the address space
of the original process. Upon successful completion, fork() returns 0 to the child process
and returns the process ID of the child process to the parent process.
Syntax:
#include <unistd.h>
int fork( );
2. execlp ( )
Used after the fork () system call by one of the two processes to replace the process memory
space with a new program. It loads a binary file into memory destroying the memory image
of the program containing the execlp system call and starts its execution. The child process
overlays its address space with the UNIX command /bin/ls using the execlp system call.
Syntax:
#include <unistd.h>
int execlp(const char *file, const char *arg, ...);
The initial argument is the name of a file that is to be executed. The next arguments describe a
list of one or more pointers to null-terminated strings that represent the argument list available to
the executed program. The list of arguments mustbe terminated by a NULL pointer,
3. wait ( )
The parent waits for the child process to complete using the wait system call. The wait
system call returns the process identifier of a terminated child, so that the parent can tell
which of its possibly many children has terminated.
Syntax:
#include <sys/wait.h>
4. exit( )
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data
(output) to its parent process (via the wait system call).
Syntax:
#include <std1ib.h>
void exit(int status);
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void main(intargc,char *arg[])
{
int pid;
pid=fork();
if(pid<0)
{
printf("fork failed");
exit(1);
}
else if(pid==0)
{
execlp("date","ls",NULL );
exit(0);
}
else
{
Program Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main()
{
int pid;
pid=fork();
if(!pid)
{
printf("Child process...");
printf("\n\nChild PID : %d",getpid());
printf("\nParent PID : %d",getppid());
printf("\n\nFinished with child\n");
}
else
{
wait(NULL);
printf("\nParent process");
printf("\nPARENT PID : %d",getpid());
printf("\nChild PID : %d",pid);
}
}
Expt. 3. Write a C program using I/O system calls open(), read() &
write() to copy contents of one file to another file
Program Description: Assume that one.txt is well defined. What we want is to create a new file
called two.txt and copy over all the contents of one.txt.
SYSTEM CALLS USED:
open(): allows you to open or create file for reading and/or writing
Syntax:
#include <fcntl.h>
int open (const char* Path, int flags [, int mode ]);
Returns: file descriptor if OK, -1 on error
The pathname is the name of the file to open or create.
Then a flag in the second parameter determine the method in which the file is to be opened:
O_RDONLY, O_WRONLY or O_RDWR.
The third argument determine the permissions of the file if it is created: S_IRUSR (Set read
rights for the owner to true), S_IWUSR (Set write rights for the owner to true)
read(): used to read data into a buffer.
syntax:
#include <unistd.h>
size_t read (int fd, void* buf, size_t cnt);
Returns the number of bytes that were read.
fd: The file descriptor of where to read the input
buf: A character array where the read content will be stored.
cnt: The number of bytes to read
write(): used to write data out of a buffer.
Syntax:
#include <unistd.h>
size_t write (int fd, void* buf, size_t cnt);
Returns: the number of bytes that were written
fd: The file descriptor of where to write the output.
Buf: A pointer to a buffer which will be written to the file.
cnt: The number of bytes to write.
Program code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include <fcntl.h>
void main()
{
char buff;
int fd,fd1;
fd=open("one.txt",O_RDONLY);
fd1=open("two.txt",O_WRONLY|O_CREAT);
while(read(fd,&buff,1))
write(fd1,&buff,1);
printf("The copy of a file is successed");
close(fd);
close(fd1);
}
Program creates 5 threads with the pthread_create() routine. Each thread prints a Hello World
message and then terminates with a call to pthread_exit().
● attr: attribute object that may be used to set thread attributes. You can specify a thread
attributes object, or NULL for the default values.
● start_routine: the C routine that the thread will execute once it is created.
● arg: A single argument that may be passed to start_routine. It must be passed by
reference as a pointer cast of type void. NULL may be used if no argument is to be
passed.
pthread_exit(): terminate the calling thread
Syntax():
pthread_exit(status)
Program Code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
OUTPUT:
1. FCFS Scheduling
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
avwt/=n;
avtat/=n;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
2. SJF Scheduling
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0, total1=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,
total1=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
//sorting burst time, priority and process number in ascending order using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
return 0;
}
4. Round Robin
#include<stdio.h>
int main()
{
int i,tbt=0,nop,ts=0,flag[20],rem[20];
int from,wt[20],tt[20],b[20],twt=0,ttt=0;
int dur;
float awt,att;
while(from<tbt)
{
if(!flag[i]) //true only when process has burst time
{
if(rem[i]<=ts) //burst time should be equal or less than time slice
{
dur=rem[i];
flag[i]=1; // make the value false
tt[i]=dur+from;
wt[i]=tt[i]-b[i]; // subtract
}
else
dur=ts;
printf("%7d%15d%15d\n",i+1,from,from+dur);
rem[i]= rem[i]-dur;
from = from+dur;
}
i=(i+1)%nop;
}
for(i=0;i<nop;i++)
{
twt+=wt[i];
ttt+=tt[i];
}
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
void producer();
void consumer();
int wait(int);
int signal(int);
int main()
{
int n;
do
{
printf("\n1.producer\n2.consumer\n3.exit\n");
printf("\nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3:
exit(0);
break;
}
}while(n!=3);
return 0;
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the items%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nconsumerconsumes the item %d",x);
x--;
mutex=signal(mutex);
}
Expt 7. Bankers Algorithm
#include<stdio.h>
void main()
{
int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10]
[5],MAX[10][5],pno,P[10],j,rz, count=0;
printf("\n");
for(i=0;i <pno;i++)
{
P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{
count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
P[++d]=P[i];
}
if(d!=-1)
{
pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}
Exp. 8 Deadlock Detection
#include<stdio.h>
int main()
{
int alloc[10][10], request[10][10], avail[10], work[10], n, m, i, j,k, true=1, false=0, finish[n];
printf("Enter number of processes: \n");
scanf("%d",&n);
nop = ms/ps;
printf("\nThe no. of pages available in memory are:%d ",nop);
}
return 0;
}
OUTPUT:
Enter the memory size: 1000
Enter the page size: 100
The no. of pages available in memory are:10
Enter number of processes:3
Enter no. of pages required for p[1]:4
Enter page table entry for p[1]: 8 6 9 5
Enter no. of pages required for p[2]: 5
Enter pagetable entry for p[2]: 1 4 5 7 3
Enter no. of pages required for p[3]: 5
Memory is Full
Enter process no. and page number and offset:
2
3
60
The Physical Address is: 560
for(i=0;i<no;i++)
{
frame[i]= -1;
}
j=0;
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
{
if(frame[k]==a[i])
avail=1;
}
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
#include<stdio.h>
int findLRU(int time[], int n){
int i, minimum = time[0], pos = 0;
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos,
faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
if(flag1 == 0){
for(j = 0; j <no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j <no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
Output
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3
5 -1 -1
5 7 -1
5 7 -1
576
576
376
Total Page Faults = 4
}
printf("\n\nTotal number of page faults--%d",pf);
return 0;
}
OUTPUT:
Enter number of page references -- 12
Enter the reference string—1 2 3 4 5 2 5 2 5 1 4 3
Enter the available no. of frames-- 3
The Page Replacement Process is–
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 3 PF No. 5
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
OUTPUT:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
}
OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Directory created
File A2 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice – 6
int main()
{
int i, j, n; char s[20];
printf("\nEnter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("\nEnter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("\nEnter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched: ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME \tSTART BLOCK \tNO OF BLOCKS \tBLOCKS OCCUPIED\n");
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
return 0;
}
OUTPUT:
Enter no of files :3
Enter file name 1 :AAA
Enter starting block of file 1 :85
Enter no of blocks in file 1 :6
Enter file name 2 :BBB
Enter starting block of file 2 :102
Enter no of blocks in file 2 :4
Enter file name 3 :CCC
Enter starting block of file 3 :60
Enter no of blocks in file 3 :4
Enter the file name to be searched: BBB
OUTPUT:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0): 1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
Allocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0): 0
***************************************************************