Os Lab File New
Os Lab File New
(KCS-451)
LABORATORY FILE
COMPUTER SCIENCE
&
ENGINEERING
● Disk space
If installing the domain host installation package or resource installation package:
● 1.2 GB for the installation package and extraction 400 MB for the /tmp directory or other
temporary directory, if specified by setting the IATEMPDIR environment variable. (This space
is cleaned up and available after the installation completes.)
● 1.5 GB for the installation directory
Experiment No.2
Objective: To create a new child process using fork system call.
Programs using the Following system calls of Unix / Linux operating system: fork, getpid.
⮚ The fork system call is used to create a new process called child process.
⮚ Both the child and parent continue to execute the instructions following fork call
⮚ getpid()and getppid()
Experiment No.3.a
Objective: Implement CPU Scheduling Policies: FCFS
#include<stdio.h>
#include<conio.h>
void main()
{
int n,b[10],w[10],i,j,h;
float avg=0;
clrscr();
printf("\n\tJOB SCHEDULING ALGORITHM[FCFS]");
printf("\n\t********************************\n");
printf("\nEnter how many jobs:");
scanf("%d",&n);
printf("\nEnter burst time for corresponding job....\n");
for(i=0;i<n;i++)
{
printf("\nProcess %d:",i+1);
scanf("%d",&b[i]);
}
w[0]=0;
printf("\nprocess 1 waiting time is 0");
for(i=1;i<n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",i+1,w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\n\t");
for(i=0;i<n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
}
getch();
}
Process 1 : 4
Process 2 : 2
Process 3 : 8
Process 4 : 2
Process 5 : 9
Process 1:5
Process 2:2
Process 3:3
Experiment No.3.c
Objective: Implement CPU Scheduling Policies: Priority Scheduling
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int n,b[10],w[10],i,j,h,t,tt;
float avg=0;
clrscr();
printf("\n\tPRIORITY SCHEDULING ALGORITHM");
printf("\n\t*****************************\n");
printf("\nEnter howmany jobs:");
scanf("%d",&n);
printf("\nEnter burst time & priority for corresponding job....\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d:",i);
scanf("%d %d",&b[i],&p[i]); a[i]=i;
}
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if(p[i]<p[j])
{
t=b[i]; tt=a[i];
b[i]=b[j]; a[i]=a[j];
b[j]=t; a[j]=tt;
}
w[1]=0;
printf("\nprocess %d waiting time is 0",a[1]);
for(i=2;i<=n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",a[i],w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\n\t");
for(i=1;i<=n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
}
getch();
}
INPUT:
Enter how many jobs:3
Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
PRIORITY SCHEDULING ALGORITHM
**************************************
Enter how many jobs:3
Enter burst time & priority for corresponding job....
Process 1: 5 2
Process 2: 7 1
Process 3: 6 3
OUTPUT
process3 waiting time is 0
Process1 waiting time: 6
Process2 waiting time: 11
total waiting time:17.000000
the average waiting time is: 5.666667
Experiment No.4.i
Objective: WAP in C to implement sequential file storage allocation technique: Contiguous
Memory Allocation using array.
#include<stdio.h>
#include<conio.h>
struct fileTable
{
Char name[20];
int sb;
nob;
} ft[30];
void main()
{
int i, j, n; char s[20]; clrscr();
printf("Enter 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("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter 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("\n File name Start block No of blocks Blocks 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);
}
getch();
}
INPUT:
Enter no of files :3
Enter file name 1 : A
Enter starting block of file 1 :85 Enter no of blocks in file 1 :6
OUTPUT:
Experiment No.4.ii
Objective: WAP in C to implement sequential file storage allocation technique: Linked
Allocation using linked list.
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20]; int nob;
struct block *sb;
}ft[30];
struct block
{
int bno;
struct block *next;
};
void main()
{
int i, j, n; char s[20];
struct block *temp; clrscr();
printf("Enter 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("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
}
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("\n File name No of blocks Blocks occupied");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d ",temp->bno);
temp = temp->next;
}
}
getch();
}
INPUT:
Enter no of files 2
Enter file 1 : A
Enter no of blocks in file 1 4
Enter the blocks of the file 1 : 12 23 9 4
Enter file 2 : G
Enter no of blocks in file 2 5
Enter the blocks of the file 2 88 77 66 55 44
Enter the file to be searched : G
OUTPUT:
File name No of blocks Blocks occupied
G 5 88 🡪 77🡪66🡪55🡪 44
Experiment No.4.iii
Objective: WAP in C to implement sequential file storage allocation technique: Indexed File
Allocation
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob, blocks[30];
} ft[30];
void main()
{
int i, j, n; char s[20];
clrscr();
printf("Enter 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("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :"); for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}
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("\n File Name No of Blocks Blocks Occupied");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob); for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
}
getch();
}
INPUT:
Enter no of files 2
Enter file 1: A
Enter no of blocks in file 1 4
Enter the blocks of the file 1 : 12 23 9 4
Enter file 2: G
Enter no of blocks in file 2 5
Enter the blocks of the file 2 88 77 66 55 44
Enter the file to be searched: G
OUTPUT:
File Name No of Blocks Blocks Occupied
G 5 88, 77, 66, 55, 44
Experiment No.5
Objective: WAP in C to implement best fit memory management scheme of contiguous
allocation technique.
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
printf("Block %d:",i);scanf("%d",&b[i]);
printf("Enter the size of the files :-\n"); for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i]; if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest; bf[ff[i]]=1; lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
INPUT
Enter the number of blocks: 3
Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
Experiment No.6
Objective: WAP in C to Implement Banker’s algorithm for finding out the safe sequence.
#include<stdio.h>
#include<conio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i);
printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter New Request Details -- ");
printf("\nEnter pid \t -- \t");
scanf("%d",&id);
printf("Enter Request for Resources \t -- \t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i] += newr;
avail[i]=avail[i] - newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n Request Not Granted -- Deadlock Occurred");
printf("\n System is in Unsafe State");
goto y;
}
}
printf("\n System is in safe state");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}
INPUT
Enter number of processes – 5
Enter number of resources – 3
Enter details for P0
Enter allocation -- 0 1 0
Enter Max -- 7 5 3
Enter details for P1
Enter allocation -- 2 0 0
Enter Max -- 3 2 2
Enter details for P2
Enter allocation -- 3 0 2
Enter Max -- 9 0 2
Enter details for P3
Enter allocation -- 2 1 1
Enter Max -- 2 2 2
Enter details for P4
Enter allocation -- 0 0 2
Enter Max -- 4 3 3
OUTPUT
P1 is visited ( 5 3 2)
P3 is visited ( 7 4 3)
P4 is visited ( 7 4 5)
P0 is visited ( 7 5 5)
P2 is visited ( 10 5 7)
System is in safe state
The Safe Sequence is -- (P1 P3 P4 P0 P2)
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1
Experiment No.7
Objective: WAP in C to implement resource allocation graph (RAG). Detects whether a
deadlock exists or not (Check cycles in the graph).
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++){
for(j=0; j<np+nr;j++){
printf("%d ", rag[i][j]);
}
printf("\n ");
}
return 0;
}
Input:
Process ID Resource Handling Resource Requesting
Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1
Output:
P0 P1 P2 R0 R1 R2
P0 0 0 0 0 0 1
P1 0 0 0 1 0 0
P2 0 0 0 0 1 0
R0 1 0 0 0 0 0
R1 0 1 0 0 0 0
R2 0 0 1 0 0 0
Experiment No.8
Objective: Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each
type of method used for storing graph.
#include<stdio.h>
int main()
{
int np, nr, temp, temp1;
printf("enter number of resources: ");
scanf("%d", &nr);
printf("enter number of processs: ");
scanf("%d", &np);
int rag[nr+np][nr+np];
int i, j;
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
rag[i][j]=0;
}
}
for(i=0;i<np;i++){
printf("enter the number of resources process %d, holding", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d holding: ", j);
scanf("%d", &temp1);
rag[np+temp1][i]=1;
}
printf("enter the number of resources process %d, requesting", i);
scanf("%d", &temp);
for(j=0; j<temp;j++)
{
printf("enter the ressorce number process %d requesting: ", i);
scanf("%d", &temp1);
rag[i][np+temp1]=1;
}
}
for(i=0;i<np+nr;i++)
{
for(j=0; j<np+nr;j++)
{
printf("%d ", rag[i][j]);
}
printf("\n ");
}
int wfg[np][np];
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
wfg[i][j]=0;
}
}
int k;
for(i=0;i<np;i++)
{
for(j=np;j<np + nr; j++)
{
if(rag[i][j] == 1)
{
for(k=0;k<np;k++)
{
if(rag[j][k] == 1)
wfg[i][k] = 1;
}
}
}
}
for(i=0;i<np;i++)
{
for(j=0; j<np;j++)
{
printf("%d ", wfg[i][j]);
}
printf("\n ");
}
return 0;
}
Input:
Process ID Resource Holding Resource Requesting
Process 0 R0 R2
Process 1 R1 R0
Process 2 R2 R1
Output
WFG P0 P1 P2
P0 0 0 1
P1 1 0 0
P2 0 1 0
Experiment No.9
Objective: Implement the solution for Bounded Buffer (producer-consumer) problem using
inter process communication techniques-Semaphores.
Description of the producer-consumer problem
Producer-consumer problem is a common paradigm for cooperating processes. A producer process
produces information that is consumed by a consumer process. One solution to the
producer-consumer problem uses shared memory. To allow producer and consumer processes to run
concurrently, there must be available a buffer of items that can be filled by the producer and
emptied by the consumer. This buffer will reside in a region of memory that is shared by the
producer and consumer processes. A producer can produce one item while the consumer is
consuming another item. The producer and consumer must be synchronized, so that the consumer
does not try to consume an item that has not yet been produced.
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”, &choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
Break;
case 2:
if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3.
Exit Enter your choice: 3
Experiment No.10
Objective: Implement the solutions for Readers-Writers problem using inter process
communication technique –Semaphore
This program provides a possible solution for first readers writers problem using mutex and
semaphore. In this program I have used 10 readers and 5 producers to demonstrate the solution.
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;
void *writer(void *wno)
{
sem_wait(&wrt);
cnt = cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);
}
void *reader(void *rno)
{
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1)
{
sem_wait(&wrt);
// If this id the first reader, then it will block the writer
}
pthread_mutex_unlock(&mutex);
// Reading Section
printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);
// Reader acquire the lock before modifying numreader
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0)
{
sem_post(&wrt);
// If this is the last reader, it will wake up the writer.
}
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);
int a[10] = {1,2,3,4,5,6,7,8,9,10};
//Just used for numbering the producer and consumer
for(int i = 0; i < 10; i++) {
pthread_create(&read[i], NULL, (void *)reader, (void *)&a[i]);
}
for(int i = 0; i < 5; i++)
{
pthread_create(&write[i], NULL, (void *)writer, (void *)&a[i]);
}
for(int i = 0; i < 10; i++)
{
pthread_join(read[i], NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_join(write[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt)
return 0;
}
Output