SlideShare a Scribd company logo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
System Software Lab 
   
 
 
 
 
 
 
 
 
 
 
 
 
 
Vishnu K N 
S5 CSE B 59 
1.FCFS Scheduling 
 
#include<stdio.h> 
#include<string.h> 
  
struct process 
{ 
char name[20]; 
int at,bt,wt,tt,status; 
}p[20],temp; 
 
struct gantt 
{ 
int st,ct; 
char name[20]; 
}d[20]; 
 
void main() 
{ 
int n=0,s=0,sum=0,num,i,j,k,idle; 
float avg_wt=0,avg_tt=0; 
char c[20]; 
printf("Enter the number of processes"); 
scanf("%d",&n); 
for(i=0;i<n;i++) 
{ 
printf("Enter the name of the process :"); 
scanf("%s",p[i].name); 
__fpurge(stdin); 
printf("Enter the arrival time of process %d :",i+1); 
scanf("%d",&p[i].at); 
printf("Enter the burst time of process %d :",i+1); 
scanf("%d",&p[i].bt); 
p[i].status=0; 
} 
for(i=0;i<n;i++) 
{ 
for(j=0;j<(n-i-1);j++) 
{ 
if(p[j].at>p[j+1].at) 
{ 
temp=p[j]; 
p[j]=p[j+1]; 
p[j+1]=temp; 
} 
} 
} 
idle=0; 
for(i=0,k=0,num=0;k<n;) 
{ 
if((p[k].at<=i)&&p[k].status==0) 
{ 
//idle=1; 
if(idle==1) 
{ 
d[num].ct=i; 
num++; 
} 
strcpy(d[num].name,p[k].name); 
d[num].st=i; 
printf("%d",d[num].st); 
d[num].ct=d[num].st+p[k].bt; //completion time 
printf("%dn",d[num].ct); 
p[k].wt=d[num].st-p[k].at; //waiting time 
p[k].tt=p[k].wt+p[k].bt; //waiting time 
i=d[num].ct; 
p[k].status=1; 
k++; 
num++; 
idle=0; 
}  
else if(idle==0) 
{ 
//printf("HEREn"); 
strcpy(d[num].name,"idle"); 
d[num].st=i; 
idle=1;i++; 
} 
else 
{ 
i++; 
} 
 
 
} 
 
printf("Process AT BT CT WT TTn"); 
for(i=0;i<n;i++) 
{ 
printf("%s  %3d %3d %3d %3d 
%3dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt); 
} 
 
 
for(i=0;i<num;i++) 
{ 
printf("_________"); 
} 
printf("n|"); 
for(i=0;i<num;i++) 
{ 
printf("%s |",d[i].name); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("---------"); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("%d ",d[i].st); 
} 
printf("%dn",d[i-1].ct); 
 
printf("The average waiting time :"); 
for(i=0;i<n;i++) 
{ 
avg_wt=avg_wt+p[i].wt; 
} 
avg_wt=avg_wt/n; 
printf("%f",avg_wt); 
printf("nThe average turn around time is :"); 
for(i=0;i<n;i++) 
{ 
avg_tt=avg_tt+p[i].tt; 
} 
avg_tt=avg_tt/n; 
printf("%f",avg_tt); 
 
} 
 
Output  
Enter the number of processes:2 
Enter the name of the process :a 
Enter the arrival time of process 1 :0 
Enter the burst time of process 1 :2 
Enter the name of the process :b 
Enter the arrival time of process 2 :2 
Enter the burst time of process 2 :4 
 
Process AT BT CT WT TT 
a  0 2 6 0 2 
b  2 4 0 0 4 
___________ 
|a |b | 
------------------ 
0 2 6 
The average waiting time :0.000000 
The average turnaround time is :3.000000 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2.SJF Scheduling 
 
#include<stdio.h> 
#include<string.h> 
  
struct process 
{ 
char name[20]; 
int at,bt,wt,tt,status; 
}p[20],temp; 
 
struct gantt 
{ 
int st,ct; 
char name[20]; 
}d[20]; 
 
void main() 
{ 
int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls; 
float avg_wt=0,avg_tt=0; 
char c[20]; 
printf("Enter the number of processes"); 
scanf("%d",&n); 
for(i=0;i<n;i++) 
{ 
printf("Enter the name of the process :"); 
scanf("%s",p[i].name); 
__fpurge(stdin); 
printf("Enter the arrival time of process %d :",i+1); 
scanf("%d",&p[i].at); 
printf("Enter the burst time of process %d :",i+1); 
scanf("%d",&p[i].bt); 
p[i].status=0; 
} 
for(i=0;i<n;i++) 
{ 
for(j=0;j<(n-i-1);j++) 
{ 
if(p[j].at>p[j+1].at) 
{ 
temp=p[j]; 
p[j]=p[j+1]; 
p[j+1]=temp; 
} 
} 
} 
 
for(i=0;i<n;i++) 
{ 
printf("%sn",p[i].name);} 
ls=0;idle=0; 
for(i=0;ls<n;) 
{ 
flag=0,found=0; 
for(j=0;j<n;j++) 
{ 
if(p[j].status==0&&p[j].at<=i&&found==0) 
{ 
k=j;flag++;found++; 
} 
else if(p[j].status==0&&p[j].at<=i&&found>0) 
{ 
if(p[j].bt<p[k].bt) 
k=j; 
} 
} 
if(flag==0&&idle==0) 
{ 
strcpy(d[num].name,"idle"); 
d[num].st=i; 
idle=1;i++; 
} 
else if(flag>0) 
{ 
if(idle==1) 
{ 
d[num].ct=i; 
num++; 
} 
strcpy(d[num].name,p[k].name); 
d[num].st=i; 
d[num].ct=i+p[k].bt; 
p[k].wt=d[num].st-p[k].at; 
p[k].tt=p[k].wt+p[k].bt; 
i=d[num].ct; 
num++; 
p[k].status=1; 
idle=0; 
ls++;k++; 
found++; 
} 
else 
{ 
i++; 
}  
 
  
} 
 
printf("Process AT BT CT WT TTn"); 
for(i=0;i<n;i++) 
{ 
printf("%s  %d %d %d %d %dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt); 
} 
for(i=0;i<num;i++) 
{ 
printf("___________"); 
} 
printf("n|"); 
for(i=0;i<num;i++) 
{ 
printf("%4s |",d[i].name); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("-----------"); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("%d ",d[i].st); 
} 
printf("%dn",d[i-1].ct); 
 
printf("The average waiting time :"); 
for(i=0;i<n;i++) 
{ 
avg_wt=avg_wt+p[i].wt; 
} 
avg_wt=avg_wt/n; 
printf("%f",avg_wt); 
printf("nThe average turn around time is :"); 
for(i=0;i<n;i++) 
{ 
avg_tt=avg_tt+p[i].tt; 
} 
avg_tt=avg_tt/n; 
printf("%fn",avg_tt); 
 
} 
 
 
Output 
  
Enter the number of processes:2 
Enter the name of the process :a 
Enter the arrival time of process 1 :0 
Enter the burst time of process 1 :3 
Enter the name of the process :b 
Enter the arrival time of process 2 :0 
Enter the burst time of process 2 :2 
 
Process AT BT CT WT TT 
a  0 3 5 2 5 
b  0 2 0 0 2 
_____________ 
| b | a | 
---------------------- 
0 2 5 
The average waiting time :1.000000 
The average turnaround time is :3.500000 
 
 
 
 
 
 
 
 
 
3.Priority Scheduling 
 
#include<stdio.h> 
#include<string.h> 
  
struct process 
{ 
char name[20]; 
int at,bt,wt,tt,status,priority; 
}p[20],temp; 
 
struct gantt 
{ 
int st,ct; 
char name[20]; 
}d[20]; 
 
void main() 
{ 
int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls; 
float avg_wt=0,avg_tt=0; 
char c[20]; 
printf("Enter the number of processes"); 
scanf("%d",&n); 
for(i=0;i<n;i++) 
{ 
printf("Enter the name of the process :"); 
scanf("%s",p[i].name); 
__fpurge(stdin); 
printf("Enter the arrival time of process %d :",i+1); 
scanf("%d",&p[i].at); 
printf("Enter the burst time of process %d :",i+1); 
scanf("%d",&p[i].bt); 
printf("Enter the priority of process %d :",i+1); 
scanf("%d",&p[i].priority); 
p[i].status=0; 
} 
for(i=0;i<n;i++) 
{ 
for(j=0;j<(n-i-1);j++) 
{ 
if(p[j].at>p[j+1].at) 
{ 
temp=p[j]; 
p[j]=p[j+1]; 
p[j+1]=temp; 
} 
} 
} 
 
for(i=0;i<n;i++) 
{ 
printf("%sn",p[i].name);} 
ls=0;idle=0; 
for(i=0;ls<n;) 
{ 
flag=0,found=0; 
for(j=0;j<n;j++) 
{ 
if(p[j].status==0&&p[j].at<=i&&found==0) 
{ 
k=j;flag++;found++; 
} 
else if(p[j].status==0&&p[j].at<=i&&found>0) 
{ 
if(p[j].priority<p[k].priority) 
k=j; 
} 
} 
if(flag==0&&idle==0) 
{ 
strcpy(d[num].name,"idle"); 
d[num].st=i; 
idle=1;i++; 
} 
else if(flag>0) 
{ 
if(idle==1) 
{ 
d[num].ct=i; 
num++; 
} 
strcpy(d[num].name,p[k].name); 
d[num].st=i; 
d[num].ct=i+p[k].bt; 
p[k].wt=d[num].st-p[k].at; 
p[k].tt=p[k].wt+p[k].bt; 
i=d[num].ct; 
num++; 
p[k].status=1; 
idle=0; 
ls++;k++; 
found++; 
} 
else 
{ 
i++; 
}  
 
  
} 
 
printf("Process AT BT PT CT WT TTn"); 
for(i=0;i<n;i++) 
{ 
printf("%s  %d %d %d %d %d 
%dn",p[i].name,p[i].at,p[i].bt,p[i].priority,d[i+1].ct,p[i].wt,p[i].tt); 
} 
for(i=0;i<num;i++) 
{ 
printf("___________"); 
} 
printf("n|"); 
for(i=0;i<num;i++) 
{ 
printf("%4s |",d[i].name); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("-----------"); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("%d ",d[i].st); 
} 
printf("%dn",d[i-1].ct); 
 
printf("The average waiting time :"); 
for(i=0;i<n;i++) 
{ 
avg_wt=avg_wt+p[i].wt; 
} 
avg_wt=avg_wt/n; 
printf("%f",avg_wt); 
printf("nThe average turn around time is :"); 
for(i=0;i<n;i++) 
{ 
avg_tt=avg_tt+p[i].tt; 
} 
avg_tt=avg_tt/n; 
printf("%fn",avg_tt); 
 
} 
 
Output 
 
Enter the number of processes:4 
Enter the name of the process :a 
Enter the arrival time of process 1 :0 
Enter the burst time of process 1 :4 
Enter the priority of process 1 :1 
Enter the name of the process :b 
Enter the arrival time of process 2 :0 
Enter the burst time of process 2 :5 
Enter the priority of process 2 :2 
Enter the name of the process :c 
Enter the arrival time of process 3 :1 
Enter the burst time of process 3 :3 
Enter the priority of process 3 :2 
Enter the name of the process :d 
Enter the arrival time of process 4 :20 
Enter the burst time of process 4 :4 
Enter the priority of process 4 :3 
 
 
 
 
 
 
 
Process AT BT PT CT WT TT 
a  0 4 1 9 0 4 
b  0 5 2 12 4 9 
c  1 3 2 20 8 11 
d  20 4 3 24 0 4 
 
_________________________________ 
| a | b | c |idle | d | 
------------------------------------------------------- 
0 4 9 12 20 24 
The average waiting time :3.000000 
The average turnaround time is :7.000000 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4.Round Robin Scheduling 
 
#include<stdio.h> 
#include<string.h> 
  
struct process 
{ 
char name[20]; 
int at,bt,wt,tt,status,left; 
}p[20],temp; 
 
struct gantt 
{ 
int st,ct; 
char name[20]; 
}d[20]; 
int front=-1,rear=0; 
 
void main() 
{ 
int t=0; 
int left=0,q[30]; 
void enqueue(int j) 
{ 
q[rear]=j; 
rear++; 
if(front==-1) 
{ 
front++; 
} 
} 
int dequeue() 
{ 
int item; 
item=q[front]; 
front++; 
if(front==rear) 
{ 
front=-1; 
rear=0; 
} 
return item; 
} 
printf("Enter the value for time quantum :n"); 
scanf("%d",&t); 
int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls; 
float avg_wt=0,avg_tt=0; 
char c[20]; 
printf("Enter the number of processes"); 
scanf("%d",&n); 
for(i=0;i<n;i++) 
{ 
printf("Enter the name of the process :"); 
scanf("%s",p[i].name); 
__fpurge(stdin); 
printf("Enter the arrival time of process %d :",i+1); 
scanf("%d",&p[i].at); 
printf("Enter the burst time of process %d :",i+1); 
scanf("%d",&p[i].bt); 
p[i].status=0; 
p[i].left=p[i].bt; 
} 
ls=0; 
idle=0; 
for(i=0;ls<n;) 
{ 
for(j=0;j<n;j++) 
{ 
if(p[j].status==0&&p[j].at<=i) 
{ 
enqueue(j); 
p[j].status=1; 
} 
} 
if(idle==0&&front==-1) 
{ 
strcpy(d[num].name,"idle"); 
d[num].st=i; 
idle=1; 
i++; 
} 
else if(front!=-1) 
{ 
if(idle==1) 
{ 
d[num].ct=i; 
idle=0; 
num++; 
} 
k=dequeue(); 
d[num].st=i; 
strcpy(d[num].name,p[k].name); 
if(p[k].left<=t) 
{ 
d[num].ct=i+p[k].left; 
i=d[num].ct; 
p[k].tt=i-p[k].at; 
p[k].wt=p[k].tt-p[k].bt; 
p[k].status=1; 
ls++; 
num++; 
} 
else 
{ 
d[num].ct=i+t; 
i=i+t; 
p[k].left=p[k].left-t; 
num++; 
for(j=0;j<n;j++) 
{ 
if(p[j].status==0&&p[j].at<=i) 
{ 
enqueue(j); 
p[j].status=1; 
} 
 
} 
enqueue(k); 
} 
}  
else 
{ 
i++; 
} 
} 
 
printf("Process AT BT CT WT TTn"); 
for(i=0;i<n;i++) 
{ 
printf("%s  %d %d %d %d %dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt); 
} 
for(i=0;i<num;i++) 
{ 
printf("___________"); 
} 
printf("n|"); 
for(i=0;i<num;i++) 
{ 
printf("%4s |",d[i].name); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("-----------"); 
} 
printf("n"); 
for(i=0;i<num;i++) 
{ 
printf("%d ",d[i].st); 
} 
printf("%dn",d[i-1].ct); 
 
printf("The average waiting time :"); 
for(i=0;i<n;i++) 
{ 
avg_wt=avg_wt+p[i].wt; 
} 
avg_wt=avg_wt/n; 
printf("%f",avg_wt); 
printf("nThe average turn around time is :"); 
for(i=0;i<n;i++) 
{ 
avg_tt=avg_tt+p[i].tt; 
} 
avg_tt=avg_tt/n; 
printf("%fn",avg_tt); 
 
 
} 
 
 
 
Output 
 
Enter the value for time quantum : 
2 
Enter the number of processes:4 
Enter the name of the process :p0 
Enter the arrival time of process 1 :0 
Enter the burst time of process 1 :4 
Enter the name of the process :p1 
Enter the arrival time of process 2 :2 
Enter the burst time of process 2 :3 
Enter the name of the process :p2 
Enter the arrival time of process 3 :3 
Enter the burst time of process 3 :5 
Enter the name of the process :p3 
Enter the arrival time of process 4 :20 
Enter the burst time of process 4 :3 
Process AT BT CT WT TT 
p0  0 4 4 2 6 
p1  2 3 6 4 7 
p2  3 5 8 4 9 
p3  20 3 9 0 3 
_________________________________________________________________ 
| p0 | p1 | p0 | p2 | p1 | p2 | p2 |idle | p3 | p3 | 
-------------------------------------------------------------------------------------------------------------- 
0 2 4 6 8 9 11 12 20 22 23 
The average waiting time :2.500000 
The average turnaround time is :6.250000 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5.Producer Consumer 
 
#include<stdio.h> 
#include<pthread.h> 
#include<semaphore.h> 
#include<time.h> 
 
sem_t mutex,empty,full; 
int buffer[5],get=0,item=0,gitem,pro[20],con[20],put=0; 
 
void *producer(void *args) 
{ 
 
do 
{ 
sem_wait(&empty); 
sem_wait(&mutex); 
buffer[put%5]=item; 
item++; 
printf("nProducer %d produces %d item buffered %d :%d",*(int *) 
args,buffer[put%5],put%5,item); 
put++; 
sem_post(&mutex); 
sem_post(&full); 
sleep(1); 
}while(1); 
} 
void *consumer(void *args) 
{ 
do 
{ 
sem_wait(&full); 
sem_wait(&mutex); 
gitem=buffer[get%5]; 
printf("nConsumer %d consumes %d item buffered %d :%d", *(int 
*) args,buffer[get%5],get%5,gitem); 
get ++; 
sem_post(&mutex); 
sem_post(&empty); 
sleep(1); 
}while(1); 
} 
 
void main() 
{ 
int p,c,j,k; 
pthread_t a[10],b[10]; 
sem_init(&mutex,0,1); 
sem_init(&full,0,0); 
sem_init(&empty,0,5); 
printf("Enter the number of consumers"); 
scanf("%d",&c); 
printf("Enter the number of producers"); 
scanf("%d",&p); 
for(j=0;j<p;j++) 
{ 
pro[j]=j; 
pthread_create(&a[j],NULL,&producer,&pro[j]); 
} 
for(k=0;k<c;k++) 
{ 
con[k]=k; 
pthread_create(&b[k],NULL,&consumer,&con[k]); 
} 
for(j=0;j<p;p++) 
{ 
pthread_join(a[j],NULL); 
} 
for(k=0;k<c;k++) 
{ 
pthread_join(b[k],NULL); 
} 
 
} 
 
Output 
 
Enter the number of consumers:2 
Enter the number of producers:2 
 
Producer 0 produces 0 item buffered 0 :1 
Producer 1 produces 1 item buffered 1 :2 
Consumer 0 consumes 0 item buffered 0 :0 
Consumer 1 consumes 1 item buffered 1 :1 
Producer 1 produces 2 item buffered 2 :3 
Producer 0 produces 3 item buffered 3 :4 
6.Dining Philosopher’s 
 
#include<stdio.h> 
#include<semaphore.h> 
#include<pthread.h> 
#include<time.h> 
 
#define N 5 
#define THINKING 0 
#define HUNGRY 1 
#define EATING 2 
#define LEFT ((ph_num+4)%N) 
#define RIGHT ((ph_num+1)%N) 
 
 
sem_t mutex, s[N]; 
 
void *philosopher(void *num); 
void take_fork(int); 
void put_fork(int); 
void test(int); 
int state[N]; 
int ph_num[N]={0,1,2,3,4}; 
 
void main() 
{  
int i; 
pthread_t thread_id[N]; 
sem_init(&mutex,0,1); 
for(i=0;i<N;i++) 
{ 
sem_init(&s[i],0,0); 
} 
 
for(i=0;i<N;i++) 
{ 
pthread_create(&thread_id[i],NULL,&philosopher,&ph_num[i]); 
printf("philosopher %d is thinking n",i+1); 
} 
 
for(i=0;i<N;i++) 
{ 
pthread_join(thread_id[i],NULL); 
} 
 
} 
 
void *philosopher(void *num) 
{ 
 
while(1){ 
int *i=num; 
sleep(1); 
take_fork(*i); 
sleep(0); 
put_fork(*i); 
} 
} 
 
void take_fork(int ph_num) 
{ 
sem_wait(&mutex); 
state[ph_num]=HUNGRY; 
printf("philosopher %d is hungry n",ph_num+1); 
test(ph_num); 
sem_post(&mutex); 
sem_wait(&s[ph_num]); 
sleep(1); 
} 
 
void test(int ph_num) 
{ 
 
if(state[ph_num]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING) 
{ 
state[ph_num]=EATING; 
sleep(2); 
printf("philosopher %d takes fork %d and %d n",ph_num+1,LEFT+1,ph_num+1); 
printf("philosopher %d is eating n",ph_num+1); 
sem_post(&s[ph_num]); 
} 
} 
 
void put_fork(int ph_num) 
{ 
sem_wait(&mutex); 
state[ph_num]=THINKING; 
printf("philosopher %d putting fork %d and %d down n",ph_num+1,LEFT+1,ph_num+1); 
printf("philosopher %d is thinking n",ph_num+1); 
test(LEFT); 
test(RIGHT); 
sem_post(&mutex); 
} 
 
Output 
 
philosopher 1 is thinking  
philosopher 2 is thinking  
philosopher 3 is thinking  
philosopher 4 is thinking  
philosopher 5 is thinking  
philosopher 2 is hungry  
philosopher 2 takes fork 1 and 2  
philosopher 2 is eating  
philosopher 1 is hungry  
philosopher 3 is hungry  
philosopher 4 is hungry  
philosopher 4 takes fork 3 and 4  
philosopher 4 is eating  
philosopher 5 is hungry  
philosopher 2 putting fork 1 and 2 down  
philosopher 2 is thinking  
philosopher 1 takes fork 5 and 1  
philosopher 1 is eating  
philosopher 4 putting fork 3 and 4 down  
philosopher 4 is thinking  
philosopher 3 takes fork 2 and 3  
philosopher 3 is eating  
philosopher 2 is hungry  
philosopher 1 putting fork 5 and 1 down  
philosopher 1 is thinking  
philosopher 5 takes fork 4 and 5  
philosopher 5 is eating  
philosopher 4 is hungry  
philosopher 3 putting fork 2 and 3 down  
philosopher 3 is thinking  
philosopher 2 takes fork 1 and 2  
philosopher 2 is eating  
philosopher 1 is hungry  
7.Banker’s Algorithm 
 
#include<stdio.h> 
#include<stdlib.h> 
 
void request(int Alloc[10][10],int Need[10][10],int Avail[10][10],int pid,int m) 
{ 
int req[1][10]; 
int i; 
printf("n Enter request to be executed :- n"); 
for(i=0;i<m;i++){ 
printf(" Request for resource %d : ",i+1); 
scanf("%d",&req[0][i]); 
} 
 
for(i=0;i<m;i++){ 
if(req[0][i] > Need[pid][i]){ 
printf("n not possiblen"); 
exit(0); 
} 
} 
 
for(i=0;i<m;i++){ 
if(req[0][i] > Avail[0][i]){ 
printf("n not possiblen"); 
exit(0); 
} 
} 
for(i=0;i<m;i++){ 
Avail[0][i]-=req[0][i]; 
Alloc[pid][i]+=req[0][i]; 
Need[pid][i]-=req[0][i]; 
} 
} 
 
 
void print(int x[][10],int n,int m){ 
int i,j; 
for(i=0;i<n;i++){ 
printf("n"); 
for(j=0;j<m;j++){ 
printf("%dt",x[i][j]); 
} 
} 
} 
 
void input(int Alloc[][10],int Need[][10],int Max[10][10],int Work[1][10],int *n,int *m){ 
int i,j; 
printf("n Enter total no. of processes : "); 
scanf("%d",n); 
printf("n Enter total no. of resources : "); 
scanf("%d",m); 
for(i=0;i<*n;i++){ 
printf("n Process %dn",i+1); 
for(j=0;j<*m;j++){ 
printf(" Allocation for resource %d : ",j+1); 
scanf("%d",&Alloc[i][j]); 
printf(" Maximum for resource %d : ",j+1); 
scanf("%d",&Max[i][j]); 
} 
} 
printf("n Available resources : n"); 
for(i=0;i<*m;i++){ 
printf(" Resource %d : ",i+1); 
scanf("%d",&Work[0][i]); 
} 
 
for(i=0;i<*n;i++) 
for(j=0;j<*m;j++) 
Need[i][j]=Max[i][j]-Alloc[i][j]; 
 
printf("n Allocation Table "); 
print(Alloc,*n,*m); 
printf("n Maximum Table"); 
print(Max,*n,*m); 
printf("n Need Table"); 
print(Need,*n,*m); 
 
} 
 
 
int safety(int Alloc[][10],int Need[][10],int Avail[1][10],int n,int m,int a[]){ 
 
int i,j,k,x=0; 
int F[10],Work[1][10]; 
int pflag=0,flag=0; 
for(i=0;i<n;i++) 
F[i]=0; 
for(i=0;i<m;i++) 
Work[0][i]=Avail[0][i]; 
 
for(k=0;k<n;k++){ 
for(i=0;i<n;i++){ 
if(F[i] == 0){ 
flag=0; 
for(j=0;j<m;j++){ 
if(Need[i][j] > Work[0][j]) 
flag=1; 
} 
if(flag == 0 && F[i] == 0){ 
for(j=0;j<m;j++) 
Work[0][j]+=Alloc[i][j]; 
F[i]=1; 
pflag++; 
a[x++]=i; 
} 
} 
} 
if(pflag == n) 
return 1; 
} 
return 0; 
} 
int banker(int Alloc[][10],int Need[][10],int Work[1][10],int n,int m){ 
int j,i,a[10]; 
j=safety(Alloc,Need,Work,n,m,a); 
if(j != 0 ){ 
printf("nn"); 
for(i=0;i<n;i++) 
printf(" P%d ",a[i]); 
printf("n Safe Sequence has been found.n"); 
return 1; 
}else{ 
printf("n Not Permittedn"); 
return 0; 
} 
} 
 
 
 
 
 
 
 
int main(){ 
int r; 
int Alloc[10][10]; 
int Max[10][10]; 
int Need[10][10]; 
int Work[1][10]; 
int n,m,pid,ch; 
input(Alloc,Need,Max,Work,&n,&m); 
r=banker(Alloc,Need,Work,n,m); 
if(r !=0 ){ 
printf("Press 0 to request:n"); 
scanf("%d",&ch); 
if(ch==0) 
{ 
printf("n Enter requesting process's' no : "); 
scanf("%d",&pid); 
request(Alloc,Need,Work,pid,m); 
r=banker(Alloc,Need,Work,n,m); 
if(r == 0 ) 
exit(0); 
} 
} 
else 
exit(0); 
return 0; 
} 
 
 
 
 
 
 
 
 
 
 
Output 
 
Enter total no. of processes : 5 
 
Enter total no. of resources : 3 
 
Process 1 
Allocation for resource 1 : 0 
Maximum for resource 1 : 7 
Allocation for resource 2 : 1 
Maximum for resource 2 : 5 
Allocation for resource 3 : 0 
Maximum for resource 3 : 3 
 
Process 2 
Allocation for resource 1 : 2 
Maximum for resource 1 : 3 
Allocation for resource 2 : 0 
Maximum for resource 2 : 2 
Allocation for resource 3 : 0 
Maximum for resource 3 : 2 
 
Process 3 
Allocation for resource 1 : 3 
Maximum for resource 1 : 9 
Allocation for resource 2 : 0 
Maximum for resource 2 : 0 
Allocation for resource 3 : 2 
Maximum for resource 3 : 2 
 
Process 4 
Allocation for resource 1 : 1 
Maximum for resource 1 : 2 
Allocation for resource 2 : 1 
Maximum for resource 2 : 1 
Allocation for resource 3 : 1 
Maximum for resource 3 : 2 
 
Process 5 
Allocation for resource 1 : 0 
Maximum for resource 1 : 4 
Allocation for resource 2 : 0 
Maximum for resource 2 : 3 
Allocation for resource 3 : 2 
Maximum for resource 3 : 3 
 
Available resources :  
Resource 1 : 3 
Resource 2 : 3 
Resource 3 : 2 
 
Allocation Table  
0 1 0  
2 0 0  
3 0 2  
1 1 1  
0 0 2  
Maximum Table 
7 5 3  
3 2 2  
9 0 2  
2 1 2  
4 3 3  
Need Table 
7 4 3  
1 2 2  
6 0 0  
1 0 1  
4 3 1  
 
P1 P3 P4 P2 P0   
Safe Sequence has been found. 
Press 0 to request: 
0 
 
Enter requesting process's' no : 1 
 
Enter request to be executed :-  
Request for resource 1 : 1 
Request for resource 2 : 0 
Request for resource 3 : 2 
 
P1 P3 P4 P2 P0   
Safe Sequence has been found. 
 
 
 
 
8. Fifo Page Replacement 
#include<stdio.h> 
void main() 
{ 
int frame[10], i, j,n, a[50], f, k, avail; 
int count=j=0; 
printf("Enter the no. of pages:"); 
scanf("%d" ,&n); 
printf("Enter the no. of frames:"); 
scanf("%d" ,&f); 
printf("Enter the reference string:"); 
for(i=0;i<n;i++) 
scanf("%d", &a[i]); 
for(i=0;i<f;i++) 
frame[i]=-1; 
for(i=0;i<n;i++) 
{ 
avail=0; 
for(k=0; k<f;k++) 
if(frame[k]==a[i]) 
avail=1; 
if (avail==0) 
{ 
frame[j]=a[i]; 
j=(j+1)%f; 
count++; 
} 
for(k=0; k<f; k++) 
printf("%dt", frame[k]); 
} 
printf("nPage Fault:%dn", count); 
} 
Output 
Enter the no. of pages: 7 
Enter the no. of frames: 3 
Enter the reference string: 6 4 8 2 4 7 6 
6 -1 -1 
6 4 -1 
6 4 8 
2 4 8 
2 4 8 
2 7 8 
2 7 6 Page Fault: 6 
9. Lru Page Replacement 
 
#include<stdio.h> 
int findLRU(int time[], int n) 
{ 
int min, pos,i; 
min=time[0]; 
pos=0; 
for (i=1;i<n;i++) 
if (time[i]<min) 
{ 
min=time[i]; 
pos=i; 
} 
return pos; 
} 
void main() 
{ 
int f, n,i,k,j,pos, frames[10], pages[30], count=0, time[10],flag1,flag2, faults=0; 
printf("Enter no. of pages:"); 
scanf("%d" ,&n); 
printf("Enter the no. of frames:"); 
scanf("%d" ,&f); 
printf("Enter the reference string:"); 
for(i=0;i<n;i++) 
scanf("%d", &pages[i]); 
for(i=0;i<f;i++) 
frames[i]=-1; 
for(i=0;i<n;i++) 
{ 
flag1=flag2=0; 
for(j=0; j<f;j++) 
if(frames[j]==pages[i]) 
{ 
count++; 
time[j]=count; 
flag1=flag2=1; 
break; 
} 
if (flag1==0) 
{ 
for(j=0;j<f;j++) 
{if (frames[j]==-1) 
{ 
count++; 
faults++; 
frames[j]=pages[i]; 
time[j]=count; 
flag2=1; 
break; 
} 
} 
} 
if (flag2==0) 
{ 
pos=findLRU(time,f); 
count++; 
faults++; 
frames[pos]=pages[i]; 
time[pos]=count; 
} 
for(k=0; k<f; k++) 
printf("%dt", frames[k]); 
printf("n"); 
} 
printf("Page Fault:%dn", faults); 
} 
Output 
Enter no. of pages: 8 
Enter the no. of frames: 3 
Enter the reference string: 5 7 8 3 7 3 9 4 
5 -1 -1 
5 7 -1 
5 7 8 
3 7 8 
3 7 8 
3 7 8 
3 7 9 
3 4 9 
Page Fault: 6 
 
 
 
 
 
 
10. Disk Scheduling 
 
#include<stdio.h> 
void main() 
{ 
printf("MENU n1.FCFSn2.SCANn3.CSCAN"); 
int ch; 
char c; 
do 
{ printf("nEnter your choice:"); 
scanf("%d", &ch); 
switch(ch) 
{ 
case 1: fcfs(); 
break; 
case 2: scan(); 
break; 
case 3: cscan(); 
break; 
default: printf("Wrong Choicen"); 
} 
printf("nDo you want another implementation?(y/n):"); 
__fpurge(stdin); 
scanf("%c", &c); 
}while (c=='y'); 
} 
void fcfs() 
{ 
int q[50],n, head,i,j,k, seek=0,diff; 
printf("Enter size of queue:"); 
scanf("%d",&n); 
printf("Enter the queue:"); 
for(i=1;i<=n;i++) 
scanf("%d",&q[i]); 
printf("Enter head position:"); 
scanf("%d",&head); 
q[0]=head; 
printf("Seek Movement:n"); 
for (j=0;j<n;j++) 
{ 
diff=abs(q[j+1]-q[j]); 
seek+=diff; 
printf("%d--->", q[j]); 
}printf("%d", q[j]); 
printf("n Total Head Movements:%dn", seek); 
} 
void scan() 
{ 
int i,j,sum=0, n, d[20], head, temp, max,loc,diff1,diff2,diff,temp1[20],k=0; 
printf("Enter size of queue:"); 
scanf("%d", &n); 
printf("Enter the queue:"); 
for(i=0;i<n;i++) 
scanf("%d",&d[i]); 
printf("Enter head position:"); 
scanf("%d",&head); 
d[n]=head; 
n++; 
for (i=0;i<n-1;i++) 
{ 
for(j=0;j<n-i-1;j++) 
{ 
if(d[j]>d[j+1]) 
{ 
temp=d[j]; 
d[j]=d[j+1]; 
d[j+1]=temp; 
} 
} 
} 
max=199; 
for(i=0;i<n;i++) 
{ 
if (head==d[i]) 
{ 
loc=i; 
break; 
} 
} 
diff1=head; 
diff2=max-head; 
if (diff1<diff2) 
{ 
for(i=loc;i>=0;i--) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("0--->"); 
temp1[k]=0; 
k++; 
for(i=loc+1; i<n-1;i++) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
}printf("%d",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
else 
{ 
for(i=loc; i<n;i++) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("%d--->", max); 
temp1[k]=max; 
k++; 
for(i=loc-1;i>0;i--) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("%dn",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
for(i=0;i<k-1;i++) 
{ 
diff=abs(temp1[i]-temp1[i+1]); 
sum+=diff; 
} 
printf("nHead Movements:%dn", sum); 
} 
void cscan() 
{ 
int i,j,sum=0, n, d[20], head, temp, max,loc,diff1,diff2,diff,temp1[20],k=0; 
printf("Enter size of queue:"); 
scanf("%d", &n); 
printf("Enter the queue:"); 
for(i=0;i<n;i++) 
scanf("%d",&d[i]); 
printf("Enter head position:"); 
scanf("%d",&head); 
d[n]=head; 
n++; 
for (i=0;i<n-1;i++) 
{ 
for(j=0;j<n-i-1;j++) 
{ 
if(d[j]>d[j+1]) 
{ 
temp=d[j]; 
d[j]=d[j+1]; 
d[j+1]=temp; 
} 
}} 
max=199; 
for(i=0;i<n;i++) 
{ 
if (head==d[i]) 
{ 
loc=i; 
break; 
} 
} 
diff1=head; 
diff2=max-head; 
if (diff1<diff2) 
{ 
for(i=loc;i>=0;i--) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("0--->"); 
temp1[k]=0; 
k++; 
printf("%d--->",max); 
temp1[k]=max; 
k++; 
for(i=n-1; i>loc+1;i--) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("%d",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
else 
{ 
for(i=loc; i<n;i++) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("%d--->", max); 
temp1[k]=max; 
k++; 
printf("0--->"); 
temp1[k]=0; 
k++; 
for(i=loc-1;i>0;i--) 
{ 
printf("%d--->",d[i]); 
temp1[k]=d[i]; 
k++; 
} 
printf("%dn",d[i]); 
temp1[k]=d[i]; 
k++;} 
for(i=0;i<k-1;i++) 
{ 
diff=abs(temp1[i]-temp1[i+1]); 
sum+=diff; 
} 
printf("nHead Movements:%dn", sum); 
} 
Output 
MENU 
1. FCFS 
2. SCAN 
3. CSCAN 
Enter your choice: 1 
Enter size of queue: 6 
Enter the queue: 53 45 78 87 34 56 
Enter head position: 40 
Seek Movement: 
40--->53--->45--->78--->87--->34--->56 
Total Head Movements: 138 
Do you want another implementation? (y/n): y 
MENU 
1. FCFS 
2. SCAN 
3. CSCAN 
Enter your choice: 2 
Enter size of queue: 5 
Enter the queue: 56 78 90 45 79 
Enter head position: 55 
55--->45--->0--->56--->78--->79--->90 
Head Movements: 145 
Do you want another implementation? (y/n): y 
MENU 
1. FCFS 
2. SCAN 
3. CSCAN 
Enter your choice: 3 
Enter size of queue: 6 
Enter the queue: 56 89 98 50 90 34 
Enter head position: 55 
55--->50--->34--->0--->199--->98--->90--->89--->56 
Head Movements: 397 
Do you want another implementation? (y/n): n 
 
 
 
 
 
 
11. Single Level Directory 
 
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
struct 
{ 
char dname[10], fname[10][10]; 
int fcnt; 
} dir; 
void main() 
{ 
int i,ch,flag; 
char f[30]; 
dir.fcnt=0; 
printf("Enter the name of the Directory:"); 
scanf("%s", &dir.dname); 
while(1) 
{ 
flag=0; 
printf("nMENU n1.Create Filen2.Delete Filen3.Search Filen4.Displayn5.Exitn"); 
printf("nEnter your choice:"); 
scanf("%d", &ch); 
switch (ch) 
{ 
case 1:printf("Enter name of the file:"); 
scanf("%s", &dir.fname[dir.fcnt]); 
dir.fcnt++; 
printf("File Createdn"); 
break; 
case 2: printf("Enter name of the file to be deleted:"); 
scanf("%s", &f); 
for(i=0;i<dir.fcnt;i++) 
{ 
if (strcmp(f,dir.fname[i])==0) 
{ 
printf("File Deleted!n"); 
strcpy(dir.fname[i], dir.fname[dir.fcnt-1]); 
flag=1; 
} 
} 
if (flag==0) 
printf("File Not Found!n"); 
else 
dir.fcnt--; 
break; 
case 3: printf("Enter name of the file to be searched:"); 
scanf("%s", &f);for(i=0;i<dir.fcnt;i++) 
{ 
if (strcmp(f,dir.fname[i])==0) 
{ 
printf("File Found!n"); 
flag=1; 
break; 
} 
} 
if (flag==0) 
printf("File Not Found!n"); 
break; 
case 4: if (dir.fcnt!=0) 
for(i=0;i<dir.fcnt;i++) 
printf("%sn", dir.fname[i]); 
else printf("No files Present!"); 
break; 
default: exit(0); 
} 
} 
} 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Output 
Enter the name of the Directory: DIR1 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 1 
Enter name of the file: file1 
File Created 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 1 
Enter name of the file: file2 
File Created 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display5. Exit 
Enter your choice: 1 
Enter name of the file:file3 
File Created 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 4 
file1 
file2 
file3 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 3 
Enter name of the file to be searched: file4 
File Not Found! 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 2 
Enter name of the file to be deleted: file2 
File Deleted! 
MENU 
1. Create File 
2. Delete File 
3. Search File 
4. Display 
5. Exit 
Enter your choice: 4 
file1 
file3 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12. Two- Level Directory 
 
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
struct 
{ 
char dname[10], fname[10][10]; 
int fcnt; 
} dir[10]; 
void getfile(int i) 
{ 
int j,ch,flag, flag1; 
char f[30]; 
while(1) 
{ 
flag=flag1=0; 
printf("nDirectory: %s", dir[i].dname); 
printf("nMENU n1.Create Filen2.Delete Filen3.Search Filen4.Displayn5.Exit 
Directoryn"); 
printf("nEnter your choice:"); 
scanf("%d", &ch); 
switch(ch) 
{ 
case 1:printf("Enter name of the file:"); 
scanf("%s", &f); 
for(j=0;j<dir[i].fcnt;j++) 
{ 
if (strcmp(f,dir[i].fname[j])==0) 
{ printf("File of same name already exists. Create 
againn"); 
flag1=1; 
break; 
} 
} 
if (flag1==0) 
{ 
strcpy(dir[i].fname[j],f); 
dir[i].fcnt++; 
printf("File Createdn"); 
} 
break; 
case 2: printf("Enter name of the file to be deleted:"); 
scanf("%s", &f);for(j=0;j<dir[i].fcnt;j++) 
{ 
if (strcmp(f,dir[i].fname[j])==0) 
{ 
printf("File Deleted!n"); 
strcpy(dir[i].fname[j], dir[i].fname[dir[i].fcnt-1]); 
flag=1; 
} 
} 
if (flag==0) 
printf("File Not Found!n"); 
else 
dir[i].fcnt--; 
break; 
case 3: printf("Enter name of the file to be searched:"); 
scanf("%s", &f); 
for(j=0;j<dir[i].fcnt;j++) 
{ 
if (strcmp(f,dir[i].fname[j])==0) 
{ 
printf("File Found!n"); 
flag=1; 
break; 
} 
} 
if (flag==0) 
{ 
printf("File Not Found!n"); 
} 
break; 
case 4: if (dir[i].fcnt!=0) 
for(j=0;j<dir[i].fcnt;j++) 
{ 
printf("%sn", dir[i].fname[j]); 
} 
else printf("No files Present!n"); 
break; 
default: return; 
} 
} 
} 
void main() 
{ 
int i,ch,flag,flag1; 
char f[30]; 
int dcnt=0; 
while(1) 
{flag=flag1=0; 
printf("nMENU n1.Create Directoryn2.Delete Directoryn3.Search 
Directoryn4.Choose Directoryn5.Displayn6.Exitn"); 
printf("nEnter your choice:"); 
scanf("%d", &ch); 
switch(ch) 
{ 
case 1:printf("Enter name of the directory:"); 
scanf("%s", &f); 
for(i=0;i<dcnt;i++) 
{ 
if (strcmp(f,dir[i].dname)==0) 
{ printf("Directory of same name already exists. Create 
againn"); 
flag1=1; 
break; 
} 
} 
if (flag1==0) 
{ 
strcpy(dir[i].dname,f); 
dir[dcnt].fcnt=0; 
dcnt++; 
printf("Directory Createdn"); 
} 
dir[dcnt].fcnt=0; 
break; 
case 2: printf("Enter name of the directory to be deleted:"); 
scanf("%s", &f); 
for(i=0;i<dcnt;i++) 
{ 
if (strcmp(f,dir[i].dname)==0) 
{ 
printf("Directory Deleted!n"); 
strcpy(dir[i].dname, dir[dcnt-1].dname); 
flag=1; 
} 
} 
if (flag==0) 
printf("Directory Not Found!n"); 
else 
dcnt--; 
break; 
case 3: printf("Enter name of the directory to be searched:"); 
scanf("%s", &f); 
for(i=0;i<dcnt;i++) 
{ 
if (strcmp(f,dir[i].dname)==0) 
{printf("Directory Found!n"); 
flag=1; 
break; 
} 
} 
if (flag==0) 
{ 
printf("Directory Not Found!n"); 
} 
break; 
case 4: printf("Enter name of the directory:"); 
scanf("%s", &f); 
for(i=0;i<dcnt;i++) 
{ 
if (strcmp(f,dir[i].dname)==0) 
{ 
flag=1; 
break; 
} 
} 
if (flag==0) 
{ 
printf("Directory Not Found!n"); 
} 
else 
getfile(i); 
break; 
case 5: if (dcnt!=0) 
for(i=0;i<dcnt;i++) 
{ 
printf("%sn", dir[i].dname); 
} 
else printf("No directories present!n"); 
break; 
default: exit(0); 
} 
} 
} 
 
Output 
MENU 
1. Create Directory 
2. Delete Directory 
3. Search Directory 
4. Choose Directory 
5. Display 
6. Exit 
Enter your choice: 1 
Enter name of the directory: DIR1 
Directory Created 
MENU 
1. Create Directory 
2. Delete Directory 
3. Search Directory 
4. Choose Directory 
5. Display 
6. Exit 
Enter your choice: 1 
Enter name of the directory: DIR2 
Directory Created 
MENU 
1. Create Directory 
2. Delete Directory 
3. Search Directory 
4. Choose Directory 
5. Display 
6. Exit 
Enter your choice:4 
Enter name of the directory:DIR2 
MENU 
1.Create File 
2.Delete File 
3.Search File 
4.Display 
5.Exit Directory 
Enter your choice:1 
Enter name of the file:file1 
File Created 
MENU 
1.Create File 
2.Delete File 
3.Search File 
4.Display 
5.Exit Directory 
Enter your choice:1 
Enter name of the file:file2 
File Created 
MENU 
1.Create File2.Delete File 
3.Search File 
4.Display 
5.Exit Directory 
Enter your choice:5 
MENU 
1.Create Directory 
2.Delete Directory 
3.Search Directory 
4.Choose Directory 
5.Display 
6.Exit 
Enter your choice : 4 
Enter name of the directory: DIR1 
MENU 
1.Create File 
2.Delete File 
3.Search File 
4.Display 
5.Exit Directory 
Enter your choice:4 
No files Present! 
MENU 
1.Create File 
2.Delete File 
3.Search File 
4.Display 
5.Exit Directory 
Enter your choice:5 
MENU 
1.Create Directory 
2.Delete Directory 
3. Search Directory 
4. Choose Directory 
5. Display 
6. Exit 
Enter your choice: 6 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13. 2 Pass Assembler 
 
PASS 1: 
#include<stdio.h> 
#include<math.h> 
#include<string.h> 
#include<stdlib.h> 
void main() 
{ 
char label[10], opcode[10], operand[10], mnemonic[10],code[10]; 
int locctr,length,start=0; 
FILE *fp1, *fp2, *fp3, *fp4, *fp5; 
fp1=fopen("input.txt", "r"); 
fp2=fopen("symbol.txt","w"); 
fp3=fopen("inter.txt", "w"); 
fp4=fopen("optab.txt","r"); 
fscanf(fp1, "%s%s%s", label, opcode, operand); 
if (strcmp(opcode, "START")==0) 
{ 
start=atoi(operand); 
locctr=start; 
fprintf(fp3, "%dt%st%st%sn",locctr, label,opcode, operand); 
fscanf(fp1, "%s%s%s", label, opcode, operand); 
} else locctr=0; 
while(strcmp(opcode, "END")!=0) 
{ 
fprintf(fp3, "%dt", locctr); 
if (strcmp(label, "**")!=0) 
{ 
if (strcmp(label,"-")!=0) 
fprintf(fp2, "%st%dn", label,locctr); 
fscanf(fp4, "%s%s", code,mnemonic); 
while(strcmp(code, "END")!=0) 
{ 
if (strcmp(opcode, code)==0) 
{ 
locctr+=3; 
break; 
} 
fscanf(fp4, "%s%s",code,mnemonic); 
} 
if(strcmp(opcode, "WORD")==0) 
locctr+=3; 
else if(strcmp(opcode, "RESW")==0) 
locctr+=3*(atoi(operand)); 
else if(strcmp(opcode, "RESB")==0) 
locctr+=atoi(operand);else if(strcmp(opcode, "BYTE")==0) 
{ 
if (operand[0]=='X') 
locctr+=1; 
else if (operand[0]=='C') 
locctr+=(strlen(operand)-3); 
} 
else locctr+=3; 
} 
fprintf(fp3, "%st%st%sn", label,opcode, operand); 
fscanf(fp1, "%s%s%s", label,opcode, operand); 
} 
fprintf(fp3, "%dt%st%st%sn",locctr, label,opcode, operand); 
fp5=fopen("length.txt","w"); 
length=locctr-start; 
fprintf(fp5, "%d", length); 
fclose(fp1); 
fclose(fp2); 
fclose(fp3); 
fclose(fp4); 
fclose(fp5); 
} 
PASS 2: 
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
void main() 
{ 
FILE *fp1,*fp2,*fp3,*fp4,*fp5; 
char 
opcode[10],operand[10],label[10],code[10],mnemonic[10],loc[10],len[10],l[10],laddr[10],sflag=0,s
tart[10 
]; 
int slen,i; 
fp1=fopen("inter.txt","r"); 
fp2=fopen("obj.txt","w"); 
fp3=fopen("length.txt","r"); 
fp4=fopen("optab.txt","r"); 
fp5=fopen("symbol.txt","r"); 
fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand); 
if(strcmp(opcode,"START")==0) 
{ 
fscanf(fp3,"%s",len); 
strcpy(start,len); 
} 
fprintf(fp2,"H^%s^%s^%sn",label,loc,len); 
fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand); 
fprintf(fp2,"T^00%s^%s^",loc,len); 
while(!feof(fp1)) 
{ 
rewind(fp4);if(strcmp(label,"-")==0) 
{ 
fscanf(fp4,"%s%s",code,mnemonic); 
while(!feof(fp4)) 
{ 
if(strcmp(opcode,code)==0) 
{ fprintf(fp2,"%s",mnemonic); 
fscanf(fp5,"%s%s",l,laddr); 
rewind(fp5); 
while(!feof(fp5)) 
{if(strcmp(operand,l)==0) 
{ 
fprintf(fp2,"%s^",laddr); 
sflag=1; 
} 
fscanf(fp5,"%s%s",l,laddr); 
} 
if(sflag==0) 
fprintf(fp2,"0^"); 
sflag=0; 
} 
fscanf(fp4,"%s%s",code,mnemonic); 
} 
} 
else if((strcmp(opcode,"WORD")==0)||(strcmp(opcode,"BYTE")==0)) 
{ if(strcmp(opcode,"WORD")==0) 
fprintf(fp2,"00000%s^",operand); 
else if((strcmp(opcode,"BYTE")==0)) 
{ 
slen=strlen(operand); 
if(operand[0]=='X') 
{ for(i=2;i<slen-1;i++) 
fprintf(fp2,"%x",operand[i]); 
} 
else if(operand[0]=='C') 
{ for(i=2;i<slen-1;i++) 
fprintf(fp2,"%x",operand[i]); 
} 
} 
} 
fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand); 
} 
fprintf(fp2,"nE^%s",start); 
fclose(fp1); 
fclose(fp2); 
fclose(fp3); 
fclose(fp4); 
fclose(fp5); 
} 
INPUT FILE: 
COPY START 2100 
- LDA FIVE 
- STA NUM1 
- LDCH NUM2 
- STCH C1 
NUM1 RESW 2 
FIVE  WORD 3 
NUM2 BYTE X'F1' 
C1 RESB 1 
- END - 
 
OPTAB: 
STA 0C 
LDCH 50 
LDA 00 
STCH 54 
END 
SYMTAB: 
NUM1 2112 
FIVE 2118 
NUM2 2121 
C1 2122 
 
 
 
 
INTERMEDIATE FILE: 
2100 COPY  START 2100   
2100 - LDA FIVE 
2103 - STA NUM1 
2106 - LDCH NUM2 
2109 - STCH C1 
2112 NUM1 RESW 2 
2118 FIVE WORD 3 
2121 NUM2 BYTE X’F1’ 
2122 C1 RESB 1 
2123 - END - 
 
 
LENGTH: 
23 
OBJECT PROGRAM: 
H^COPY^2100^23 
T^002100^23^002118^0C2112^502121^542122^2122^000003^46f31 
E^23 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14. 2 Pass Macro Processor 
 
PASS 1: 
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
void main() 
{ 
char mnem[10],operand[10],label[10]; 
FILE *fp1,*fp2,*fp3; 
fp1=fopen("input.txt","r"); 
fp2=fopen("namtab.txt","w"); 
fp3=fopen("deftab.txt","w"); 
fscanf(fp1,"%s%s%s",label,mnem,operand); 
while(strcmp(mnem,"MEND")!=0) 
{ 
if(strcmp(mnem,"MACRO")==0) 
{ 
fprintf(fp2,"%sn",label); 
fprintf(fp3,"%st%sn",label,operand); 
} 
else 
fprintf(fp3,"%st%sn",mnem,operand); 
fscanf(fp1,"%s%s%s",label,mnem,operand); 
} 
fprintf(fp3,"%sn",mnem); 
fclose(fp1); 
fclose(fp2); 
fclose(fp3); 
} 
PASS 2: 
#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
void main() 
{ 
char opcode[10],mne[10],oper[10],code[10],label[10],name[10],lab[10],mnem[10],opc[10]; 
int start,locctr,length,i; 
FILE *fp1,*fp2,*fp3,*fp4,*fp5; 
fp1=fopen("input.txt","r"); 
fp2=fopen("namtab.txt","r");fp3=fopen("deftab.txt","r"); 
fp4=fopen("argtab.txt","rw"); 
fp5=fopen("expand.txt","w"); 
fscanf(fp1,"%s%s%s",label,mne,oper); 
while(strcmp(mne,"END")!=0) 
{ 
if(strcmp(mne,"MACRO")==0) 
{ 
while(strcmp(mne,"MEND")!=0) 
fscanf(fp1,"%s%s%s",label,mne,oper); 
} 
else 
{ 
fscanf(fp2,"%s",name); 
if(strcmp(mne,name)==0) 
{ 
length=strlen(oper); 
for(i=0;i<length;i++) 
{ 
if(oper[i]==',') 
fprintf(fp4,"n"); 
else 
fprintf(fp4,"%c",oper[i]); 
} 
fprintf(fp5,"%st%sn",mne,oper); 
fscanf(fp3,"%st%s",lab,mnem); 
fscanf(fp3,"%st%s",lab,mnem); 
while(strcmp(lab,"MEND")!=0) 
{ 
fprintf(fp5,"%st",lab); 
if(mnem[0]=='&') 
{ 
fscanf(fp4,"%s",opc); 
fprintf(fp5,"%sn",opc); 
} 
fscanf(fp3,"%s%s",lab,mnem); 
} 
} 
else 
fprintf(fp5,"%st%st%sn",label,mne,oper); 
} 
fscanf(fp1,"%s%s%s",label,mne,oper); 
} 
fclose(fp1); 
fclose(fp2); 
fclose(fp3); 
fclose(fp4); 
fclose(fp5); 
} 
INPUT FILE: 
EX1 MACRO &A,&B 
- LDA  &A 
- STA &B 
- MEND  - 
SAMPLE  START 1000 
- EX1 N1, N2 
N1  RESW  1 
N2  RESW  1 
-  END - 
 
NAME TABLE: 
EX1 
 
DEFINITION TABLE: 
EX1  &A,&B 
LDA  &A 
STA  &B 
MEND 
ARGUMENT TABLE: 
N1 
N2 
 
OUTPUT : 
SAMPLE START 1000 
EX1 N1,N2 
LDA N1 
STA N2 
N1 RESW 1 
N2 RESW 1 
 
 
 
 
 
 
 
 
 
 
15. Symbol Table 
 
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
struct sym{ 
int key; 
char name[20]; 
}ind[11]; 
void insert(int n, char nam[20]) 
{ 
FILE *f; 
f=fopen("sym.txt","w"); 
int flag=0,x,i,j; 
for(i=0;i<11;i++) 
if(ind[i].key==0) 
flag=1; 
if(flag==0) 
printf("nSymbol table is fulln"); 
else 
{ 
x=n%11; 
if(ind[x].key==0) 
{ 
ind[x].key=n; 
strcpy(ind[x].name,nam); 
printf("nThe label is enteredn"); 
} 
else 
{ 
for(i=x,j=x;;) 
{ 
if(ind[i].key==0) 
{ 
ind[i].key=n; 
strcpy(ind[i].name,nam); 
printf("nThe label is enteredn"); 
break; 
} 
else if(ind[j].key==0) 
{ 
ind[j].key=n; 
strcpy(ind[j].name,nam); 
printf("nThe label is enteredn"); 
break; 
} 
if(i!=10) 
i++;if(j!=0) 
j--; 
if((i==10)&&(j==0)) 
break; 
} 
} 
} 
for(i=0;i<11;i++) 
fprintf(f,"n%dt%s",ind[i].key,ind[i].name); 
fclose(f); 
} 
void main() 
{ 
int ch,addr,x,flag=0; 
char n[20],i; 
for(i=0;i<11;i++) 
ind[i].key=0; 
do{ 
printf("nEnter your choice : n1.Create Labeln2.Search labeln3.Displayn4.Exit n"); 
scanf("%d",&ch); 
switch(ch) 
{ 
case 1: printf("nEnter the name of the label: "); 
scanf("%s",n); 
printf("Enter the address of the label: "); 
scanf("%d",&addr); 
insert(addr,n); 
break; 
case 2: printf("nEnter the name of the label to be searched: "); 
scanf("%s",n); 
for(i=0;i<11;i++) 
{ 
if(strcmp(ind[i].name,n)==0) 
{ 
printf("nLabel Foundn"); 
flag=1; 
break; 
} 
} 
if(flag==0) 
printf("nLabel not foundn"); 
flag=0; 
break; 
case 3: printf("nThe symbol table isn"); 
for(i=0;i<11;i++) 
printf("n%dt%s",ind[i].key,ind[i].name); 
break; 
case 4 : printf("nExitingn"); 
break; 
default : printf("nEnter the right choicen"); 
break; 
} 
}while(ch!=4); 
} 
 
Output 
Enter your choice: 
1. Create Label 
2. Search label 
3. Display 
4. Exit 
1 
Enter the name of the label: hi 
Enter the address of the label: 100 
The label is entered 
Enter your choice: 
1. Create Label 
2. Search label 
3. Display 
4. Exit 
1 
Enter the name of the label: bye 
Enter the address of the label: 200 
The label is entered 
Enter your choice: 
1. Create Label 
2. Search label 
3. Display 
4. Exit 
3 
 
 
The symbol table is 
0 
100 hi 
200 bye 
0 
0 
0 
0 
0 
0 
0 
Enter your choice: 
1. Create Label 
2. Search label 
3. Display 
4. Exit 
4 
Exiting 
 
 
 

More Related Content

DOCX
Os lab file c programs
PPT
Searching in c language
PPT
Binary search trees
PPT
Priority queues
PPTX
Data structure - Graph
PPTX
Chapter 05 classes and objects
PPTX
Array in c programming
PDF
Arrays and function basic c programming notes
Os lab file c programs
Searching in c language
Binary search trees
Priority queues
Data structure - Graph
Chapter 05 classes and objects
Array in c programming
Arrays and function basic c programming notes

What's hot (20)

PDF
UNIT I LINEAR DATA STRUCTURES – LIST
PPTX
Data Structure and Algorithm - Divide and Conquer
PPTX
Data structure power point presentation
PPTX
Queue Implementation Using Array & Linked List
PPTX
Structure in C
PPTX
Data Structures in Python
PDF
PPTX
Graph in data structure
PPTX
Cohen sutherland line clipping
PPTX
Unit I-Data Structures_Intoduction.pptx
PPTX
Pointers in c - Mohammad Salman
PPTX
Programming in c Arrays
PPTX
Dynamic memory allocation
PPTX
6-Python-Recursion PPT.pptx
PPTX
Graphs in data structure
PPT
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPT
DATA STRUCTURES
UNIT I LINEAR DATA STRUCTURES – LIST
Data Structure and Algorithm - Divide and Conquer
Data structure power point presentation
Queue Implementation Using Array & Linked List
Structure in C
Data Structures in Python
Graph in data structure
Cohen sutherland line clipping
Unit I-Data Structures_Intoduction.pptx
Pointers in c - Mohammad Salman
Programming in c Arrays
Dynamic memory allocation
6-Python-Recursion PPT.pptx
Graphs in data structure
Unit 1 chapter 1 Design and Analysis of Algorithms
DATA STRUCTURES
Ad

Similar to System Software /Operating System Lab Report (20)

PDF
Os lab upto 1st mid
PDF
Os lab 1st mid
PDF
Os lab upto_1st_mid
PDF
Cpd lecture im 207
TXT
Programs for Operating System
 
DOCX
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
DOCX
Operating system labs
DOCX
SaraPIC
DOCX
ADA FILE
DOCX
Assignment no39
PPTX
4. chapter iii
PDF
C programms
DOC
C basics
PDF
VTU Network lab programs
PDF
C programs Set 2
PDF
Simple C programs
DOCX
C lab manaual
PDF
C Programming lab
DOC
PDF
2D array
Os lab upto 1st mid
Os lab 1st mid
Os lab upto_1st_mid
Cpd lecture im 207
Programs for Operating System
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Operating system labs
SaraPIC
ADA FILE
Assignment no39
4. chapter iii
C programms
C basics
VTU Network lab programs
C programs Set 2
Simple C programs
C lab manaual
C Programming lab
2D array
Ad

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
composite construction of structures.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
PPT on Performance Review to get promotions
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Project quality management in manufacturing
Construction Project Organization Group 2.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
composite construction of structures.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mechanical Engineering MATERIALS Selection
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Foundation to blockchain - A guide to Blockchain Tech
PPT on Performance Review to get promotions
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Embodied AI: Ushering in the Next Era of Intelligent Systems
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CH1 Production IntroductoryConcepts.pptx
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
Well-logging-methods_new................
CYBER-CRIMES AND SECURITY A guide to understanding
Project quality management in manufacturing

System Software /Operating System Lab Report

  • 1.                                             System Software Lab                                Vishnu K N  S5 CSE B 59 
  • 2. 1.FCFS Scheduling    #include<stdio.h>  #include<string.h>     struct process  {  char name[20];  int at,bt,wt,tt,status;  }p[20],temp;    struct gantt  {  int st,ct;  char name[20];  }d[20];    void main()  {  int n=0,s=0,sum=0,num,i,j,k,idle;  float avg_wt=0,avg_tt=0;  char c[20];  printf("Enter the number of processes");  scanf("%d",&n);  for(i=0;i<n;i++)  {  printf("Enter the name of the process :");  scanf("%s",p[i].name);  __fpurge(stdin);  printf("Enter the arrival time of process %d :",i+1);  scanf("%d",&p[i].at);  printf("Enter the burst time of process %d :",i+1);  scanf("%d",&p[i].bt);  p[i].status=0;  }  for(i=0;i<n;i++)  {  for(j=0;j<(n-i-1);j++)  {  if(p[j].at>p[j+1].at)  {  temp=p[j];  p[j]=p[j+1]; 
  • 3. p[j+1]=temp;  }  }  }  idle=0;  for(i=0,k=0,num=0;k<n;)  {  if((p[k].at<=i)&&p[k].status==0)  {  //idle=1;  if(idle==1)  {  d[num].ct=i;  num++;  }  strcpy(d[num].name,p[k].name);  d[num].st=i;  printf("%d",d[num].st);  d[num].ct=d[num].st+p[k].bt; //completion time  printf("%dn",d[num].ct);  p[k].wt=d[num].st-p[k].at; //waiting time  p[k].tt=p[k].wt+p[k].bt; //waiting time  i=d[num].ct;  p[k].status=1;  k++;  num++;  idle=0;  }   else if(idle==0)  {  //printf("HEREn");  strcpy(d[num].name,"idle");  d[num].st=i;  idle=1;i++;  }  else  {  i++;  }      }   
  • 4. printf("Process AT BT CT WT TTn");  for(i=0;i<n;i++)  {  printf("%s  %3d %3d %3d %3d  %3dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt);  }      for(i=0;i<num;i++)  {  printf("_________");  }  printf("n|");  for(i=0;i<num;i++)  {  printf("%s |",d[i].name);  }  printf("n");  for(i=0;i<num;i++)  {  printf("---------");  }  printf("n");  for(i=0;i<num;i++)  {  printf("%d ",d[i].st);  }  printf("%dn",d[i-1].ct);    printf("The average waiting time :");  for(i=0;i<n;i++)  {  avg_wt=avg_wt+p[i].wt;  }  avg_wt=avg_wt/n;  printf("%f",avg_wt);  printf("nThe average turn around time is :");  for(i=0;i<n;i++)  {  avg_tt=avg_tt+p[i].tt;  }  avg_tt=avg_tt/n;  printf("%f",avg_tt); 
  • 5.   }    Output   Enter the number of processes:2  Enter the name of the process :a  Enter the arrival time of process 1 :0  Enter the burst time of process 1 :2  Enter the name of the process :b  Enter the arrival time of process 2 :2  Enter the burst time of process 2 :4    Process AT BT CT WT TT  a  0 2 6 0 2  b  2 4 0 0 4  ___________  |a |b |  ------------------  0 2 6  The average waiting time :0.000000  The average turnaround time is :3.000000                                             
  • 6. 2.SJF Scheduling    #include<stdio.h>  #include<string.h>     struct process  {  char name[20];  int at,bt,wt,tt,status;  }p[20],temp;    struct gantt  {  int st,ct;  char name[20];  }d[20];    void main()  {  int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls;  float avg_wt=0,avg_tt=0;  char c[20];  printf("Enter the number of processes");  scanf("%d",&n);  for(i=0;i<n;i++)  {  printf("Enter the name of the process :");  scanf("%s",p[i].name);  __fpurge(stdin);  printf("Enter the arrival time of process %d :",i+1);  scanf("%d",&p[i].at);  printf("Enter the burst time of process %d :",i+1);  scanf("%d",&p[i].bt);  p[i].status=0;  }  for(i=0;i<n;i++)  {  for(j=0;j<(n-i-1);j++)  {  if(p[j].at>p[j+1].at)  {  temp=p[j];  p[j]=p[j+1]; 
  • 8. num++;  p[k].status=1;  idle=0;  ls++;k++;  found++;  }  else  {  i++;  }        }    printf("Process AT BT CT WT TTn");  for(i=0;i<n;i++)  {  printf("%s  %d %d %d %d %dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt);  }  for(i=0;i<num;i++)  {  printf("___________");  }  printf("n|");  for(i=0;i<num;i++)  {  printf("%4s |",d[i].name);  }  printf("n");  for(i=0;i<num;i++)  {  printf("-----------");  }  printf("n");  for(i=0;i<num;i++)  {  printf("%d ",d[i].st);  }  printf("%dn",d[i-1].ct);    printf("The average waiting time :");  for(i=0;i<n;i++)  { 
  • 9. avg_wt=avg_wt+p[i].wt;  }  avg_wt=avg_wt/n;  printf("%f",avg_wt);  printf("nThe average turn around time is :");  for(i=0;i<n;i++)  {  avg_tt=avg_tt+p[i].tt;  }  avg_tt=avg_tt/n;  printf("%fn",avg_tt);    }      Output     Enter the number of processes:2  Enter the name of the process :a  Enter the arrival time of process 1 :0  Enter the burst time of process 1 :3  Enter the name of the process :b  Enter the arrival time of process 2 :0  Enter the burst time of process 2 :2    Process AT BT CT WT TT  a  0 3 5 2 5  b  0 2 0 0 2  _____________  | b | a |  ----------------------  0 2 5  The average waiting time :1.000000  The average turnaround time is :3.500000                   
  • 10. 3.Priority Scheduling    #include<stdio.h>  #include<string.h>     struct process  {  char name[20];  int at,bt,wt,tt,status,priority;  }p[20],temp;    struct gantt  {  int st,ct;  char name[20];  }d[20];    void main()  {  int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls;  float avg_wt=0,avg_tt=0;  char c[20];  printf("Enter the number of processes");  scanf("%d",&n);  for(i=0;i<n;i++)  {  printf("Enter the name of the process :");  scanf("%s",p[i].name);  __fpurge(stdin);  printf("Enter the arrival time of process %d :",i+1);  scanf("%d",&p[i].at);  printf("Enter the burst time of process %d :",i+1);  scanf("%d",&p[i].bt);  printf("Enter the priority of process %d :",i+1);  scanf("%d",&p[i].priority);  p[i].status=0;  }  for(i=0;i<n;i++)  {  for(j=0;j<(n-i-1);j++)  {  if(p[j].at>p[j+1].at)  { 
  • 12. p[k].tt=p[k].wt+p[k].bt;  i=d[num].ct;  num++;  p[k].status=1;  idle=0;  ls++;k++;  found++;  }  else  {  i++;  }        }    printf("Process AT BT PT CT WT TTn");  for(i=0;i<n;i++)  {  printf("%s  %d %d %d %d %d  %dn",p[i].name,p[i].at,p[i].bt,p[i].priority,d[i+1].ct,p[i].wt,p[i].tt);  }  for(i=0;i<num;i++)  {  printf("___________");  }  printf("n|");  for(i=0;i<num;i++)  {  printf("%4s |",d[i].name);  }  printf("n");  for(i=0;i<num;i++)  {  printf("-----------");  }  printf("n");  for(i=0;i<num;i++)  {  printf("%d ",d[i].st);  }  printf("%dn",d[i-1].ct);   
  • 13. printf("The average waiting time :");  for(i=0;i<n;i++)  {  avg_wt=avg_wt+p[i].wt;  }  avg_wt=avg_wt/n;  printf("%f",avg_wt);  printf("nThe average turn around time is :");  for(i=0;i<n;i++)  {  avg_tt=avg_tt+p[i].tt;  }  avg_tt=avg_tt/n;  printf("%fn",avg_tt);    }    Output    Enter the number of processes:4  Enter the name of the process :a  Enter the arrival time of process 1 :0  Enter the burst time of process 1 :4  Enter the priority of process 1 :1  Enter the name of the process :b  Enter the arrival time of process 2 :0  Enter the burst time of process 2 :5  Enter the priority of process 2 :2  Enter the name of the process :c  Enter the arrival time of process 3 :1  Enter the burst time of process 3 :3  Enter the priority of process 3 :2  Enter the name of the process :d  Enter the arrival time of process 4 :20  Enter the burst time of process 4 :4  Enter the priority of process 4 :3               
  • 14. Process AT BT PT CT WT TT  a  0 4 1 9 0 4  b  0 5 2 12 4 9  c  1 3 2 20 8 11  d  20 4 3 24 0 4    _________________________________  | a | b | c |idle | d |  -------------------------------------------------------  0 4 9 12 20 24  The average waiting time :3.000000  The average turnaround time is :7.000000                                                               
  • 15. 4.Round Robin Scheduling    #include<stdio.h>  #include<string.h>     struct process  {  char name[20];  int at,bt,wt,tt,status,left;  }p[20],temp;    struct gantt  {  int st,ct;  char name[20];  }d[20];  int front=-1,rear=0;    void main()  {  int t=0;  int left=0,q[30];  void enqueue(int j)  {  q[rear]=j;  rear++;  if(front==-1)  {  front++;  }  }  int dequeue()  {  int item;  item=q[front];  front++;  if(front==rear)  {  front=-1;  rear=0;  }  return item;  } 
  • 16. printf("Enter the value for time quantum :n");  scanf("%d",&t);  int n=0,s=0,sum=0,num,i,j,k=0,idle,flag,found,ls;  float avg_wt=0,avg_tt=0;  char c[20];  printf("Enter the number of processes");  scanf("%d",&n);  for(i=0;i<n;i++)  {  printf("Enter the name of the process :");  scanf("%s",p[i].name);  __fpurge(stdin);  printf("Enter the arrival time of process %d :",i+1);  scanf("%d",&p[i].at);  printf("Enter the burst time of process %d :",i+1);  scanf("%d",&p[i].bt);  p[i].status=0;  p[i].left=p[i].bt;  }  ls=0;  idle=0;  for(i=0;ls<n;)  {  for(j=0;j<n;j++)  {  if(p[j].status==0&&p[j].at<=i)  {  enqueue(j);  p[j].status=1;  }  }  if(idle==0&&front==-1)  {  strcpy(d[num].name,"idle");  d[num].st=i;  idle=1;  i++;  }  else if(front!=-1)  {  if(idle==1)  {  d[num].ct=i; 
  • 18. printf("%s  %d %d %d %d %dn",p[i].name,p[i].at,p[i].bt,d[i+1].ct,p[i].wt,p[i].tt);  }  for(i=0;i<num;i++)  {  printf("___________");  }  printf("n|");  for(i=0;i<num;i++)  {  printf("%4s |",d[i].name);  }  printf("n");  for(i=0;i<num;i++)  {  printf("-----------");  }  printf("n");  for(i=0;i<num;i++)  {  printf("%d ",d[i].st);  }  printf("%dn",d[i-1].ct);    printf("The average waiting time :");  for(i=0;i<n;i++)  {  avg_wt=avg_wt+p[i].wt;  }  avg_wt=avg_wt/n;  printf("%f",avg_wt);  printf("nThe average turn around time is :");  for(i=0;i<n;i++)  {  avg_tt=avg_tt+p[i].tt;  }  avg_tt=avg_tt/n;  printf("%fn",avg_tt);      }       
  • 19. Output    Enter the value for time quantum :  2  Enter the number of processes:4  Enter the name of the process :p0  Enter the arrival time of process 1 :0  Enter the burst time of process 1 :4  Enter the name of the process :p1  Enter the arrival time of process 2 :2  Enter the burst time of process 2 :3  Enter the name of the process :p2  Enter the arrival time of process 3 :3  Enter the burst time of process 3 :5  Enter the name of the process :p3  Enter the arrival time of process 4 :20  Enter the burst time of process 4 :3  Process AT BT CT WT TT  p0  0 4 4 2 6  p1  2 3 6 4 7  p2  3 5 8 4 9  p3  20 3 9 0 3  _________________________________________________________________  | p0 | p1 | p0 | p2 | p1 | p2 | p2 |idle | p3 | p3 |  --------------------------------------------------------------------------------------------------------------  0 2 4 6 8 9 11 12 20 22 23  The average waiting time :2.500000  The average turnaround time is :6.250000                               
  • 20. 5.Producer Consumer    #include<stdio.h>  #include<pthread.h>  #include<semaphore.h>  #include<time.h>    sem_t mutex,empty,full;  int buffer[5],get=0,item=0,gitem,pro[20],con[20],put=0;    void *producer(void *args)  {    do  {  sem_wait(&empty);  sem_wait(&mutex);  buffer[put%5]=item;  item++;  printf("nProducer %d produces %d item buffered %d :%d",*(int *)  args,buffer[put%5],put%5,item);  put++;  sem_post(&mutex);  sem_post(&full);  sleep(1);  }while(1);  }  void *consumer(void *args)  {  do  {  sem_wait(&full);  sem_wait(&mutex);  gitem=buffer[get%5];  printf("nConsumer %d consumes %d item buffered %d :%d", *(int  *) args,buffer[get%5],get%5,gitem);  get ++;  sem_post(&mutex);  sem_post(&empty);  sleep(1);  }while(1);  }   
  • 21. void main()  {  int p,c,j,k;  pthread_t a[10],b[10];  sem_init(&mutex,0,1);  sem_init(&full,0,0);  sem_init(&empty,0,5);  printf("Enter the number of consumers");  scanf("%d",&c);  printf("Enter the number of producers");  scanf("%d",&p);  for(j=0;j<p;j++)  {  pro[j]=j;  pthread_create(&a[j],NULL,&producer,&pro[j]);  }  for(k=0;k<c;k++)  {  con[k]=k;  pthread_create(&b[k],NULL,&consumer,&con[k]);  }  for(j=0;j<p;p++)  {  pthread_join(a[j],NULL);  }  for(k=0;k<c;k++)  {  pthread_join(b[k],NULL);  }    }    Output    Enter the number of consumers:2  Enter the number of producers:2    Producer 0 produces 0 item buffered 0 :1  Producer 1 produces 1 item buffered 1 :2  Consumer 0 consumes 0 item buffered 0 :0  Consumer 1 consumes 1 item buffered 1 :1  Producer 1 produces 2 item buffered 2 :3  Producer 0 produces 3 item buffered 3 :4 
  • 22. 6.Dining Philosopher’s    #include<stdio.h>  #include<semaphore.h>  #include<pthread.h>  #include<time.h>    #define N 5  #define THINKING 0  #define HUNGRY 1  #define EATING 2  #define LEFT ((ph_num+4)%N)  #define RIGHT ((ph_num+1)%N)      sem_t mutex, s[N];    void *philosopher(void *num);  void take_fork(int);  void put_fork(int);  void test(int);  int state[N];  int ph_num[N]={0,1,2,3,4};    void main()  {   int i;  pthread_t thread_id[N];  sem_init(&mutex,0,1);  for(i=0;i<N;i++)  {  sem_init(&s[i],0,0);  }    for(i=0;i<N;i++)  {  pthread_create(&thread_id[i],NULL,&philosopher,&ph_num[i]);  printf("philosopher %d is thinking n",i+1);  }    for(i=0;i<N;i++)  {  pthread_join(thread_id[i],NULL); 
  • 23. }    }    void *philosopher(void *num)  {    while(1){  int *i=num;  sleep(1);  take_fork(*i);  sleep(0);  put_fork(*i);  }  }    void take_fork(int ph_num)  {  sem_wait(&mutex);  state[ph_num]=HUNGRY;  printf("philosopher %d is hungry n",ph_num+1);  test(ph_num);  sem_post(&mutex);  sem_wait(&s[ph_num]);  sleep(1);  }    void test(int ph_num)  {    if(state[ph_num]==HUNGRY && state[LEFT]!=EATING && state[RIGHT]!=EATING)  {  state[ph_num]=EATING;  sleep(2);  printf("philosopher %d takes fork %d and %d n",ph_num+1,LEFT+1,ph_num+1);  printf("philosopher %d is eating n",ph_num+1);  sem_post(&s[ph_num]);  }  }    void put_fork(int ph_num)  {  sem_wait(&mutex); 
  • 24. state[ph_num]=THINKING;  printf("philosopher %d putting fork %d and %d down n",ph_num+1,LEFT+1,ph_num+1);  printf("philosopher %d is thinking n",ph_num+1);  test(LEFT);  test(RIGHT);  sem_post(&mutex);  }    Output    philosopher 1 is thinking   philosopher 2 is thinking   philosopher 3 is thinking   philosopher 4 is thinking   philosopher 5 is thinking   philosopher 2 is hungry   philosopher 2 takes fork 1 and 2   philosopher 2 is eating   philosopher 1 is hungry   philosopher 3 is hungry   philosopher 4 is hungry   philosopher 4 takes fork 3 and 4   philosopher 4 is eating   philosopher 5 is hungry   philosopher 2 putting fork 1 and 2 down   philosopher 2 is thinking   philosopher 1 takes fork 5 and 1   philosopher 1 is eating   philosopher 4 putting fork 3 and 4 down   philosopher 4 is thinking   philosopher 3 takes fork 2 and 3   philosopher 3 is eating   philosopher 2 is hungry   philosopher 1 putting fork 5 and 1 down   philosopher 1 is thinking   philosopher 5 takes fork 4 and 5   philosopher 5 is eating   philosopher 4 is hungry   philosopher 3 putting fork 2 and 3 down   philosopher 3 is thinking   philosopher 2 takes fork 1 and 2   philosopher 2 is eating   philosopher 1 is hungry  
  • 25. 7.Banker’s Algorithm    #include<stdio.h>  #include<stdlib.h>    void request(int Alloc[10][10],int Need[10][10],int Avail[10][10],int pid,int m)  {  int req[1][10];  int i;  printf("n Enter request to be executed :- n");  for(i=0;i<m;i++){  printf(" Request for resource %d : ",i+1);  scanf("%d",&req[0][i]);  }    for(i=0;i<m;i++){  if(req[0][i] > Need[pid][i]){  printf("n not possiblen");  exit(0);  }  }    for(i=0;i<m;i++){  if(req[0][i] > Avail[0][i]){  printf("n not possiblen");  exit(0);  }  }  for(i=0;i<m;i++){  Avail[0][i]-=req[0][i];  Alloc[pid][i]+=req[0][i];  Need[pid][i]-=req[0][i];  }  }      void print(int x[][10],int n,int m){  int i,j;  for(i=0;i<n;i++){  printf("n");  for(j=0;j<m;j++){  printf("%dt",x[i][j]);  } 
  • 26. }  }    void input(int Alloc[][10],int Need[][10],int Max[10][10],int Work[1][10],int *n,int *m){  int i,j;  printf("n Enter total no. of processes : ");  scanf("%d",n);  printf("n Enter total no. of resources : ");  scanf("%d",m);  for(i=0;i<*n;i++){  printf("n Process %dn",i+1);  for(j=0;j<*m;j++){  printf(" Allocation for resource %d : ",j+1);  scanf("%d",&Alloc[i][j]);  printf(" Maximum for resource %d : ",j+1);  scanf("%d",&Max[i][j]);  }  }  printf("n Available resources : n");  for(i=0;i<*m;i++){  printf(" Resource %d : ",i+1);  scanf("%d",&Work[0][i]);  }    for(i=0;i<*n;i++)  for(j=0;j<*m;j++)  Need[i][j]=Max[i][j]-Alloc[i][j];    printf("n Allocation Table ");  print(Alloc,*n,*m);  printf("n Maximum Table");  print(Max,*n,*m);  printf("n Need Table");  print(Need,*n,*m);    }      int safety(int Alloc[][10],int Need[][10],int Avail[1][10],int n,int m,int a[]){    int i,j,k,x=0;  int F[10],Work[1][10];  int pflag=0,flag=0; 
  • 27. for(i=0;i<n;i++)  F[i]=0;  for(i=0;i<m;i++)  Work[0][i]=Avail[0][i];    for(k=0;k<n;k++){  for(i=0;i<n;i++){  if(F[i] == 0){  flag=0;  for(j=0;j<m;j++){  if(Need[i][j] > Work[0][j])  flag=1;  }  if(flag == 0 && F[i] == 0){  for(j=0;j<m;j++)  Work[0][j]+=Alloc[i][j];  F[i]=1;  pflag++;  a[x++]=i;  }  }  }  if(pflag == n)  return 1;  }  return 0;  }  int banker(int Alloc[][10],int Need[][10],int Work[1][10],int n,int m){  int j,i,a[10];  j=safety(Alloc,Need,Work,n,m,a);  if(j != 0 ){  printf("nn");  for(i=0;i<n;i++)  printf(" P%d ",a[i]);  printf("n Safe Sequence has been found.n");  return 1;  }else{  printf("n Not Permittedn");  return 0;  }  }     
  • 28.           int main(){  int r;  int Alloc[10][10];  int Max[10][10];  int Need[10][10];  int Work[1][10];  int n,m,pid,ch;  input(Alloc,Need,Max,Work,&n,&m);  r=banker(Alloc,Need,Work,n,m);  if(r !=0 ){  printf("Press 0 to request:n");  scanf("%d",&ch);  if(ch==0)  {  printf("n Enter requesting process's' no : ");  scanf("%d",&pid);  request(Alloc,Need,Work,pid,m);  r=banker(Alloc,Need,Work,n,m);  if(r == 0 )  exit(0);  }  }  else  exit(0);  return 0;  }                      Output   
  • 29. Enter total no. of processes : 5    Enter total no. of resources : 3    Process 1  Allocation for resource 1 : 0  Maximum for resource 1 : 7  Allocation for resource 2 : 1  Maximum for resource 2 : 5  Allocation for resource 3 : 0  Maximum for resource 3 : 3    Process 2  Allocation for resource 1 : 2  Maximum for resource 1 : 3  Allocation for resource 2 : 0  Maximum for resource 2 : 2  Allocation for resource 3 : 0  Maximum for resource 3 : 2    Process 3  Allocation for resource 1 : 3  Maximum for resource 1 : 9  Allocation for resource 2 : 0  Maximum for resource 2 : 0  Allocation for resource 3 : 2  Maximum for resource 3 : 2    Process 4  Allocation for resource 1 : 1  Maximum for resource 1 : 2  Allocation for resource 2 : 1  Maximum for resource 2 : 1  Allocation for resource 3 : 1  Maximum for resource 3 : 2    Process 5  Allocation for resource 1 : 0  Maximum for resource 1 : 4  Allocation for resource 2 : 0  Maximum for resource 2 : 3  Allocation for resource 3 : 2  Maximum for resource 3 : 3 
  • 30.   Available resources :   Resource 1 : 3  Resource 2 : 3  Resource 3 : 2    Allocation Table   0 1 0   2 0 0   3 0 2   1 1 1   0 0 2   Maximum Table  7 5 3   3 2 2   9 0 2   2 1 2   4 3 3   Need Table  7 4 3   1 2 2   6 0 0   1 0 1   4 3 1     P1 P3 P4 P2 P0    Safe Sequence has been found.  Press 0 to request:  0    Enter requesting process's' no : 1    Enter request to be executed :-   Request for resource 1 : 1  Request for resource 2 : 0  Request for resource 3 : 2    P1 P3 P4 P2 P0    Safe Sequence has been found.         
  • 31. 8. Fifo Page Replacement  #include<stdio.h>  void main()  {  int frame[10], i, j,n, a[50], f, k, avail;  int count=j=0;  printf("Enter the no. of pages:");  scanf("%d" ,&n);  printf("Enter the no. of frames:");  scanf("%d" ,&f);  printf("Enter the reference string:");  for(i=0;i<n;i++)  scanf("%d", &a[i]);  for(i=0;i<f;i++)  frame[i]=-1;  for(i=0;i<n;i++)  {  avail=0;  for(k=0; k<f;k++)  if(frame[k]==a[i])  avail=1;  if (avail==0)  {  frame[j]=a[i];  j=(j+1)%f;  count++;  }  for(k=0; k<f; k++)  printf("%dt", frame[k]);  }  printf("nPage Fault:%dn", count);  }  Output  Enter the no. of pages: 7  Enter the no. of frames: 3  Enter the reference string: 6 4 8 2 4 7 6  6 -1 -1  6 4 -1  6 4 8  2 4 8  2 4 8  2 7 8  2 7 6 Page Fault: 6 
  • 32. 9. Lru Page Replacement    #include<stdio.h>  int findLRU(int time[], int n)  {  int min, pos,i;  min=time[0];  pos=0;  for (i=1;i<n;i++)  if (time[i]<min)  {  min=time[i];  pos=i;  }  return pos;  }  void main()  {  int f, n,i,k,j,pos, frames[10], pages[30], count=0, time[10],flag1,flag2, faults=0;  printf("Enter no. of pages:");  scanf("%d" ,&n);  printf("Enter the no. of frames:");  scanf("%d" ,&f);  printf("Enter the reference string:");  for(i=0;i<n;i++)  scanf("%d", &pages[i]);  for(i=0;i<f;i++)  frames[i]=-1;  for(i=0;i<n;i++)  {  flag1=flag2=0;  for(j=0; j<f;j++)  if(frames[j]==pages[i])  {  count++;  time[j]=count;  flag1=flag2=1;  break;  }  if (flag1==0)  {  for(j=0;j<f;j++)  {if (frames[j]==-1) 
  • 33. {  count++;  faults++;  frames[j]=pages[i];  time[j]=count;  flag2=1;  break;  }  }  }  if (flag2==0)  {  pos=findLRU(time,f);  count++;  faults++;  frames[pos]=pages[i];  time[pos]=count;  }  for(k=0; k<f; k++)  printf("%dt", frames[k]);  printf("n");  }  printf("Page Fault:%dn", faults);  }  Output  Enter no. of pages: 8  Enter the no. of frames: 3  Enter the reference string: 5 7 8 3 7 3 9 4  5 -1 -1  5 7 -1  5 7 8  3 7 8  3 7 8  3 7 8  3 7 9  3 4 9  Page Fault: 6             
  • 34. 10. Disk Scheduling    #include<stdio.h>  void main()  {  printf("MENU n1.FCFSn2.SCANn3.CSCAN");  int ch;  char c;  do  { printf("nEnter your choice:");  scanf("%d", &ch);  switch(ch)  {  case 1: fcfs();  break;  case 2: scan();  break;  case 3: cscan();  break;  default: printf("Wrong Choicen");  }  printf("nDo you want another implementation?(y/n):");  __fpurge(stdin);  scanf("%c", &c);  }while (c=='y');  }  void fcfs()  {  int q[50],n, head,i,j,k, seek=0,diff;  printf("Enter size of queue:");  scanf("%d",&n);  printf("Enter the queue:");  for(i=1;i<=n;i++)  scanf("%d",&q[i]);  printf("Enter head position:");  scanf("%d",&head);  q[0]=head;  printf("Seek Movement:n");  for (j=0;j<n;j++)  {  diff=abs(q[j+1]-q[j]);  seek+=diff;  printf("%d--->", q[j]); 
  • 35. }printf("%d", q[j]);  printf("n Total Head Movements:%dn", seek);  }  void scan()  {  int i,j,sum=0, n, d[20], head, temp, max,loc,diff1,diff2,diff,temp1[20],k=0;  printf("Enter size of queue:");  scanf("%d", &n);  printf("Enter the queue:");  for(i=0;i<n;i++)  scanf("%d",&d[i]);  printf("Enter head position:");  scanf("%d",&head);  d[n]=head;  n++;  for (i=0;i<n-1;i++)  {  for(j=0;j<n-i-1;j++)  {  if(d[j]>d[j+1])  {  temp=d[j];  d[j]=d[j+1];  d[j+1]=temp;  }  }  }  max=199;  for(i=0;i<n;i++)  {  if (head==d[i])  {  loc=i;  break;  }  }  diff1=head;  diff2=max-head;  if (diff1<diff2)  {  for(i=loc;i>=0;i--)  {  printf("%d--->",d[i]); 
  • 36. temp1[k]=d[i];  k++;  }  printf("0--->");  temp1[k]=0;  k++;  for(i=loc+1; i<n-1;i++)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }printf("%d",d[i]);  temp1[k]=d[i];  k++;  }  else  {  for(i=loc; i<n;i++)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("%d--->", max);  temp1[k]=max;  k++;  for(i=loc-1;i>0;i--)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("%dn",d[i]);  temp1[k]=d[i];  k++;  }  for(i=0;i<k-1;i++)  {  diff=abs(temp1[i]-temp1[i+1]);  sum+=diff;  }  printf("nHead Movements:%dn", sum);  } 
  • 37. void cscan()  {  int i,j,sum=0, n, d[20], head, temp, max,loc,diff1,diff2,diff,temp1[20],k=0;  printf("Enter size of queue:");  scanf("%d", &n);  printf("Enter the queue:");  for(i=0;i<n;i++)  scanf("%d",&d[i]);  printf("Enter head position:");  scanf("%d",&head);  d[n]=head;  n++;  for (i=0;i<n-1;i++)  {  for(j=0;j<n-i-1;j++)  {  if(d[j]>d[j+1])  {  temp=d[j];  d[j]=d[j+1];  d[j+1]=temp;  }  }}  max=199;  for(i=0;i<n;i++)  {  if (head==d[i])  {  loc=i;  break;  }  }  diff1=head;  diff2=max-head;  if (diff1<diff2)  {  for(i=loc;i>=0;i--)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("0--->"); 
  • 38. temp1[k]=0;  k++;  printf("%d--->",max);  temp1[k]=max;  k++;  for(i=n-1; i>loc+1;i--)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("%d",d[i]);  temp1[k]=d[i];  k++;  }  else  {  for(i=loc; i<n;i++)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("%d--->", max);  temp1[k]=max;  k++;  printf("0--->");  temp1[k]=0;  k++;  for(i=loc-1;i>0;i--)  {  printf("%d--->",d[i]);  temp1[k]=d[i];  k++;  }  printf("%dn",d[i]);  temp1[k]=d[i];  k++;}  for(i=0;i<k-1;i++)  {  diff=abs(temp1[i]-temp1[i+1]);  sum+=diff;  } 
  • 39. printf("nHead Movements:%dn", sum);  }  Output  MENU  1. FCFS  2. SCAN  3. CSCAN  Enter your choice: 1  Enter size of queue: 6  Enter the queue: 53 45 78 87 34 56  Enter head position: 40  Seek Movement:  40--->53--->45--->78--->87--->34--->56  Total Head Movements: 138  Do you want another implementation? (y/n): y  MENU  1. FCFS  2. SCAN  3. CSCAN  Enter your choice: 2  Enter size of queue: 5  Enter the queue: 56 78 90 45 79  Enter head position: 55  55--->45--->0--->56--->78--->79--->90  Head Movements: 145  Do you want another implementation? (y/n): y  MENU  1. FCFS  2. SCAN  3. CSCAN  Enter your choice: 3  Enter size of queue: 6  Enter the queue: 56 89 98 50 90 34  Enter head position: 55  55--->50--->34--->0--->199--->98--->90--->89--->56  Head Movements: 397  Do you want another implementation? (y/n): n             
  • 40. 11. Single Level Directory    #include<stdio.h>  #include<stdlib.h>  #include<string.h>  struct  {  char dname[10], fname[10][10];  int fcnt;  } dir;  void main()  {  int i,ch,flag;  char f[30];  dir.fcnt=0;  printf("Enter the name of the Directory:");  scanf("%s", &dir.dname);  while(1)  {  flag=0;  printf("nMENU n1.Create Filen2.Delete Filen3.Search Filen4.Displayn5.Exitn");  printf("nEnter your choice:");  scanf("%d", &ch);  switch (ch)  {  case 1:printf("Enter name of the file:");  scanf("%s", &dir.fname[dir.fcnt]);  dir.fcnt++;  printf("File Createdn");  break;  case 2: printf("Enter name of the file to be deleted:");  scanf("%s", &f);  for(i=0;i<dir.fcnt;i++)  {  if (strcmp(f,dir.fname[i])==0)  {  printf("File Deleted!n");  strcpy(dir.fname[i], dir.fname[dir.fcnt-1]);  flag=1;  }  }  if (flag==0)  printf("File Not Found!n"); 
  • 41. else  dir.fcnt--;  break;  case 3: printf("Enter name of the file to be searched:");  scanf("%s", &f);for(i=0;i<dir.fcnt;i++)  {  if (strcmp(f,dir.fname[i])==0)  {  printf("File Found!n");  flag=1;  break;  }  }  if (flag==0)  printf("File Not Found!n");  break;  case 4: if (dir.fcnt!=0)  for(i=0;i<dir.fcnt;i++)  printf("%sn", dir.fname[i]);  else printf("No files Present!");  break;  default: exit(0);  }  }  }                                     
  • 42. Output  Enter the name of the Directory: DIR1  MENU  1. Create File  2. Delete File  3. Search File  4. Display  5. Exit  Enter your choice: 1  Enter name of the file: file1  File Created  MENU  1. Create File  2. Delete File  3. Search File  4. Display  5. Exit  Enter your choice: 1  Enter name of the file: file2  File Created  MENU  1. Create File  2. Delete File  3. Search File  4. Display5. Exit  Enter your choice: 1  Enter name of the file:file3  File Created  MENU  1. Create File  2. Delete File  3. Search File  4. Display  5. Exit  Enter your choice: 4  file1  file2  file3  MENU  1. Create File  2. Delete File  3. Search File  4. Display 
  • 43. 5. Exit  Enter your choice: 3  Enter name of the file to be searched: file4  File Not Found!  MENU  1. Create File  2. Delete File  3. Search File  4. Display  5. Exit  Enter your choice: 2  Enter name of the file to be deleted: file2  File Deleted!  MENU  1. Create File  2. Delete File  3. Search File  4. Display  5. Exit  Enter your choice: 4  file1  file3                                           
  • 44. 12. Two- Level Directory    #include<stdio.h>  #include<stdlib.h>  #include<string.h>  struct  {  char dname[10], fname[10][10];  int fcnt;  } dir[10];  void getfile(int i)  {  int j,ch,flag, flag1;  char f[30];  while(1)  {  flag=flag1=0;  printf("nDirectory: %s", dir[i].dname);  printf("nMENU n1.Create Filen2.Delete Filen3.Search Filen4.Displayn5.Exit  Directoryn");  printf("nEnter your choice:");  scanf("%d", &ch);  switch(ch)  {  case 1:printf("Enter name of the file:");  scanf("%s", &f);  for(j=0;j<dir[i].fcnt;j++)  {  if (strcmp(f,dir[i].fname[j])==0)  { printf("File of same name already exists. Create  againn");  flag1=1;  break;  }  }  if (flag1==0)  {  strcpy(dir[i].fname[j],f);  dir[i].fcnt++;  printf("File Createdn");  }  break;  case 2: printf("Enter name of the file to be deleted:"); 
  • 45. scanf("%s", &f);for(j=0;j<dir[i].fcnt;j++)  {  if (strcmp(f,dir[i].fname[j])==0)  {  printf("File Deleted!n");  strcpy(dir[i].fname[j], dir[i].fname[dir[i].fcnt-1]);  flag=1;  }  }  if (flag==0)  printf("File Not Found!n");  else  dir[i].fcnt--;  break;  case 3: printf("Enter name of the file to be searched:");  scanf("%s", &f);  for(j=0;j<dir[i].fcnt;j++)  {  if (strcmp(f,dir[i].fname[j])==0)  {  printf("File Found!n");  flag=1;  break;  }  }  if (flag==0)  {  printf("File Not Found!n");  }  break;  case 4: if (dir[i].fcnt!=0)  for(j=0;j<dir[i].fcnt;j++)  {  printf("%sn", dir[i].fname[j]);  }  else printf("No files Present!n");  break;  default: return;  }  }  }  void main()  { 
  • 46. int i,ch,flag,flag1;  char f[30];  int dcnt=0;  while(1)  {flag=flag1=0;  printf("nMENU n1.Create Directoryn2.Delete Directoryn3.Search  Directoryn4.Choose Directoryn5.Displayn6.Exitn");  printf("nEnter your choice:");  scanf("%d", &ch);  switch(ch)  {  case 1:printf("Enter name of the directory:");  scanf("%s", &f);  for(i=0;i<dcnt;i++)  {  if (strcmp(f,dir[i].dname)==0)  { printf("Directory of same name already exists. Create  againn");  flag1=1;  break;  }  }  if (flag1==0)  {  strcpy(dir[i].dname,f);  dir[dcnt].fcnt=0;  dcnt++;  printf("Directory Createdn");  }  dir[dcnt].fcnt=0;  break;  case 2: printf("Enter name of the directory to be deleted:");  scanf("%s", &f);  for(i=0;i<dcnt;i++)  {  if (strcmp(f,dir[i].dname)==0)  {  printf("Directory Deleted!n");  strcpy(dir[i].dname, dir[dcnt-1].dname);  flag=1;  }  }  if (flag==0) 
  • 47. printf("Directory Not Found!n");  else  dcnt--;  break;  case 3: printf("Enter name of the directory to be searched:");  scanf("%s", &f);  for(i=0;i<dcnt;i++)  {  if (strcmp(f,dir[i].dname)==0)  {printf("Directory Found!n");  flag=1;  break;  }  }  if (flag==0)  {  printf("Directory Not Found!n");  }  break;  case 4: printf("Enter name of the directory:");  scanf("%s", &f);  for(i=0;i<dcnt;i++)  {  if (strcmp(f,dir[i].dname)==0)  {  flag=1;  break;  }  }  if (flag==0)  {  printf("Directory Not Found!n");  }  else  getfile(i);  break;  case 5: if (dcnt!=0)  for(i=0;i<dcnt;i++)  {  printf("%sn", dir[i].dname);  }  else printf("No directories present!n");  break; 
  • 48. default: exit(0);  }  }  }    Output  MENU  1. Create Directory  2. Delete Directory  3. Search Directory  4. Choose Directory  5. Display  6. Exit  Enter your choice: 1  Enter name of the directory: DIR1  Directory Created  MENU  1. Create Directory  2. Delete Directory  3. Search Directory  4. Choose Directory  5. Display  6. Exit  Enter your choice: 1  Enter name of the directory: DIR2  Directory Created  MENU  1. Create Directory  2. Delete Directory  3. Search Directory  4. Choose Directory  5. Display  6. Exit  Enter your choice:4  Enter name of the directory:DIR2  MENU  1.Create File  2.Delete File  3.Search File  4.Display  5.Exit Directory  Enter your choice:1  Enter name of the file:file1 
  • 49. File Created  MENU  1.Create File  2.Delete File  3.Search File  4.Display  5.Exit Directory  Enter your choice:1  Enter name of the file:file2  File Created  MENU  1.Create File2.Delete File  3.Search File  4.Display  5.Exit Directory  Enter your choice:5  MENU  1.Create Directory  2.Delete Directory  3.Search Directory  4.Choose Directory  5.Display  6.Exit  Enter your choice : 4  Enter name of the directory: DIR1  MENU  1.Create File  2.Delete File  3.Search File  4.Display  5.Exit Directory  Enter your choice:4  No files Present!  MENU  1.Create File  2.Delete File  3.Search File  4.Display  5.Exit Directory  Enter your choice:5  MENU  1.Create Directory  2.Delete Directory 
  • 50. 3. Search Directory  4. Choose Directory  5. Display  6. Exit  Enter your choice: 6                                                                             
  • 51. 13. 2 Pass Assembler    PASS 1:  #include<stdio.h>  #include<math.h>  #include<string.h>  #include<stdlib.h>  void main()  {  char label[10], opcode[10], operand[10], mnemonic[10],code[10];  int locctr,length,start=0;  FILE *fp1, *fp2, *fp3, *fp4, *fp5;  fp1=fopen("input.txt", "r");  fp2=fopen("symbol.txt","w");  fp3=fopen("inter.txt", "w");  fp4=fopen("optab.txt","r");  fscanf(fp1, "%s%s%s", label, opcode, operand);  if (strcmp(opcode, "START")==0)  {  start=atoi(operand);  locctr=start;  fprintf(fp3, "%dt%st%st%sn",locctr, label,opcode, operand);  fscanf(fp1, "%s%s%s", label, opcode, operand);  } else locctr=0;  while(strcmp(opcode, "END")!=0)  {  fprintf(fp3, "%dt", locctr);  if (strcmp(label, "**")!=0)  {  if (strcmp(label,"-")!=0)  fprintf(fp2, "%st%dn", label,locctr);  fscanf(fp4, "%s%s", code,mnemonic);  while(strcmp(code, "END")!=0)  {  if (strcmp(opcode, code)==0)  {  locctr+=3;  break;  }  fscanf(fp4, "%s%s",code,mnemonic);  }  if(strcmp(opcode, "WORD")==0)  locctr+=3; 
  • 52. else if(strcmp(opcode, "RESW")==0)  locctr+=3*(atoi(operand));  else if(strcmp(opcode, "RESB")==0)  locctr+=atoi(operand);else if(strcmp(opcode, "BYTE")==0)  {  if (operand[0]=='X')  locctr+=1;  else if (operand[0]=='C')  locctr+=(strlen(operand)-3);  }  else locctr+=3;  }  fprintf(fp3, "%st%st%sn", label,opcode, operand);  fscanf(fp1, "%s%s%s", label,opcode, operand);  }  fprintf(fp3, "%dt%st%st%sn",locctr, label,opcode, operand);  fp5=fopen("length.txt","w");  length=locctr-start;  fprintf(fp5, "%d", length);  fclose(fp1);  fclose(fp2);  fclose(fp3);  fclose(fp4);  fclose(fp5);  }  PASS 2:  #include<stdio.h>  #include<string.h>  #include<stdlib.h>  void main()  {  FILE *fp1,*fp2,*fp3,*fp4,*fp5;  char  opcode[10],operand[10],label[10],code[10],mnemonic[10],loc[10],len[10],l[10],laddr[10],sflag=0,s tart[10  ];  int slen,i;  fp1=fopen("inter.txt","r");  fp2=fopen("obj.txt","w");  fp3=fopen("length.txt","r");  fp4=fopen("optab.txt","r");  fp5=fopen("symbol.txt","r");  fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand); 
  • 53. if(strcmp(opcode,"START")==0)  {  fscanf(fp3,"%s",len);  strcpy(start,len);  }  fprintf(fp2,"H^%s^%s^%sn",label,loc,len);  fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand);  fprintf(fp2,"T^00%s^%s^",loc,len);  while(!feof(fp1))  {  rewind(fp4);if(strcmp(label,"-")==0)  {  fscanf(fp4,"%s%s",code,mnemonic);  while(!feof(fp4))  {  if(strcmp(opcode,code)==0)  { fprintf(fp2,"%s",mnemonic);  fscanf(fp5,"%s%s",l,laddr);  rewind(fp5);  while(!feof(fp5))  {if(strcmp(operand,l)==0)  {  fprintf(fp2,"%s^",laddr);  sflag=1;  }  fscanf(fp5,"%s%s",l,laddr);  }  if(sflag==0)  fprintf(fp2,"0^");  sflag=0;  }  fscanf(fp4,"%s%s",code,mnemonic);  }  }  else if((strcmp(opcode,"WORD")==0)||(strcmp(opcode,"BYTE")==0))  { if(strcmp(opcode,"WORD")==0)  fprintf(fp2,"00000%s^",operand);  else if((strcmp(opcode,"BYTE")==0))  {  slen=strlen(operand);  if(operand[0]=='X')  { for(i=2;i<slen-1;i++)  fprintf(fp2,"%x",operand[i]); 
  • 54. }  else if(operand[0]=='C')  { for(i=2;i<slen-1;i++)  fprintf(fp2,"%x",operand[i]);  }  }  }  fscanf(fp1,"%s%s%s%s",loc,label,opcode,operand);  }  fprintf(fp2,"nE^%s",start);  fclose(fp1);  fclose(fp2);  fclose(fp3);  fclose(fp4);  fclose(fp5);  }  INPUT FILE:  COPY START 2100  - LDA FIVE  - STA NUM1  - LDCH NUM2  - STCH C1  NUM1 RESW 2  FIVE  WORD 3  NUM2 BYTE X'F1'  C1 RESB 1  - END -    OPTAB:  STA 0C  LDCH 50  LDA 00  STCH 54  END  SYMTAB:  NUM1 2112  FIVE 2118  NUM2 2121  C1 2122         
  • 55. INTERMEDIATE FILE:  2100 COPY  START 2100    2100 - LDA FIVE  2103 - STA NUM1  2106 - LDCH NUM2  2109 - STCH C1  2112 NUM1 RESW 2  2118 FIVE WORD 3  2121 NUM2 BYTE X’F1’  2122 C1 RESB 1  2123 - END -      LENGTH:  23  OBJECT PROGRAM:  H^COPY^2100^23  T^002100^23^002118^0C2112^502121^542122^2122^000003^46f31  E^23                                                 
  • 56. 14. 2 Pass Macro Processor    PASS 1:  #include<stdio.h>  #include<string.h>  #include<stdlib.h>  void main()  {  char mnem[10],operand[10],label[10];  FILE *fp1,*fp2,*fp3;  fp1=fopen("input.txt","r");  fp2=fopen("namtab.txt","w");  fp3=fopen("deftab.txt","w");  fscanf(fp1,"%s%s%s",label,mnem,operand);  while(strcmp(mnem,"MEND")!=0)  {  if(strcmp(mnem,"MACRO")==0)  {  fprintf(fp2,"%sn",label);  fprintf(fp3,"%st%sn",label,operand);  }  else  fprintf(fp3,"%st%sn",mnem,operand);  fscanf(fp1,"%s%s%s",label,mnem,operand);  }  fprintf(fp3,"%sn",mnem);  fclose(fp1);  fclose(fp2);  fclose(fp3);  }  PASS 2:  #include<stdio.h>  #include<stdlib.h>  #include<string.h>  void main()  {  char opcode[10],mne[10],oper[10],code[10],label[10],name[10],lab[10],mnem[10],opc[10];  int start,locctr,length,i;  FILE *fp1,*fp2,*fp3,*fp4,*fp5;  fp1=fopen("input.txt","r");  fp2=fopen("namtab.txt","r");fp3=fopen("deftab.txt","r");  fp4=fopen("argtab.txt","rw");  fp5=fopen("expand.txt","w"); 
  • 58. fclose(fp4);  fclose(fp5);  }  INPUT FILE:  EX1 MACRO &A,&B  - LDA  &A  - STA &B  - MEND  -  SAMPLE  START 1000  - EX1 N1, N2  N1  RESW  1  N2  RESW  1  -  END -    NAME TABLE:  EX1    DEFINITION TABLE:  EX1  &A,&B  LDA  &A  STA  &B  MEND  ARGUMENT TABLE:  N1  N2    OUTPUT :  SAMPLE START 1000  EX1 N1,N2  LDA N1  STA N2  N1 RESW 1  N2 RESW 1                     
  • 59. 15. Symbol Table    #include<stdio.h>  #include<string.h>  #include<stdlib.h>  struct sym{  int key;  char name[20];  }ind[11];  void insert(int n, char nam[20])  {  FILE *f;  f=fopen("sym.txt","w");  int flag=0,x,i,j;  for(i=0;i<11;i++)  if(ind[i].key==0)  flag=1;  if(flag==0)  printf("nSymbol table is fulln");  else  {  x=n%11;  if(ind[x].key==0)  {  ind[x].key=n;  strcpy(ind[x].name,nam);  printf("nThe label is enteredn");  }  else  {  for(i=x,j=x;;)  {  if(ind[i].key==0)  {  ind[i].key=n;  strcpy(ind[i].name,nam);  printf("nThe label is enteredn");  break;  }  else if(ind[j].key==0)  {  ind[j].key=n;  strcpy(ind[j].name,nam); 
  • 60. printf("nThe label is enteredn");  break;  }  if(i!=10)  i++;if(j!=0)  j--;  if((i==10)&&(j==0))  break;  }  }  }  for(i=0;i<11;i++)  fprintf(f,"n%dt%s",ind[i].key,ind[i].name);  fclose(f);  }  void main()  {  int ch,addr,x,flag=0;  char n[20],i;  for(i=0;i<11;i++)  ind[i].key=0;  do{  printf("nEnter your choice : n1.Create Labeln2.Search labeln3.Displayn4.Exit n");  scanf("%d",&ch);  switch(ch)  {  case 1: printf("nEnter the name of the label: ");  scanf("%s",n);  printf("Enter the address of the label: ");  scanf("%d",&addr);  insert(addr,n);  break;  case 2: printf("nEnter the name of the label to be searched: ");  scanf("%s",n);  for(i=0;i<11;i++)  {  if(strcmp(ind[i].name,n)==0)  {  printf("nLabel Foundn");  flag=1;  break;  }  } 
  • 61. if(flag==0)  printf("nLabel not foundn");  flag=0;  break;  case 3: printf("nThe symbol table isn");  for(i=0;i<11;i++)  printf("n%dt%s",ind[i].key,ind[i].name);  break;  case 4 : printf("nExitingn");  break;  default : printf("nEnter the right choicen");  break;  }  }while(ch!=4);  }    Output  Enter your choice:  1. Create Label  2. Search label  3. Display  4. Exit  1  Enter the name of the label: hi  Enter the address of the label: 100  The label is entered  Enter your choice:  1. Create Label  2. Search label  3. Display  4. Exit  1  Enter the name of the label: bye  Enter the address of the label: 200  The label is entered  Enter your choice:  1. Create Label  2. Search label  3. Display  4. Exit  3     
  • 62. The symbol table is  0  100 hi  200 bye  0  0  0  0  0  0  0  Enter your choice:  1. Create Label  2. Search label  3. Display  4. Exit  4  Exiting