OS Practical Sem-I
OS Practical Sem-I
SetA
Q1.
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include <unistd.h>
int main()
{
int pid;
pid=fork();
if(pid==0){
printf("I am child process\n");
printf("Child process=> PPID=%d, PID=%d\n",getppid(),getpid());
}
else
{
printf("I am parent process\n");
printf("Parent process => %d\n",getpid());
printf("Waiting for child to finish\n");
wait(NULL);
printf("Child process finished\n");
}
}
OUTPUT:
I am parent process
Parent process => 11462
Waiting for child to finish
I am child process
Child process=> PPID=11462, PID=11463
Child process finished
SET B
Q1.
//Implement the C program to accept n integers to be sorted. Mainfunction creates child process
using fork system call.Parent process sorts the integers using bubble sort and waits for process using
wait system call. Child process sorts the integers using insertion sort.
#include <stdio.h>
#include <stdlib.h>
pid=fork();
if(pid == 0)
{
printf("Child process executing\n");
insertion_sort(num,n);
sleep(5);
}
else
{
sleep(5);
bubble_sort(num,n);
wait(NULL);
}
return 0;
}
}
OUTPUT:
I Am PARENT HAVING PID=>3794
MY CHILD ID =>3795
MY PARENT PID=>3794
Q2.
//Write a c program to illustrate the concepts of orphan process , parent process create a child and
terminates before child has finished its.So child process becomes orphan process (use fork() ,
sleep() , getpid() ,getppid()).SETB_Q2
#include<stdio.h>
#include<sys/types.h>
int main()
{
int pid;
pid=fork();
if(pid==0)
{
sleep(5); //child goes to sleep and in the mean time parent terminates
printf("I am child having PID %d\n",getpid());
printf("My parent PID is %d\n",getppid());
}
else
{
printf("I am parent having PID %d\n",getpid());
printf("My child PID is %d\n",pid);
sleep(5);
}
}
OUTPUT:
PARENT PROCESS EXECUTING-----------
how many numbers you want to enter-
4
SET C
Q1.
// c program to illustrate use of fork() & execyp() system call for process creation
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
char * command ="ls";
char * argument_list[] ={"ls","-1",NULL};
printf("befor calling execvp()\n");
printf("creating another process usinf fork()...\n");
if (fork() ==0)
{
//Newly spawed child process. This willl be taken over by "ls -l"
int status_code = execvp(command, argument_list);
printf("ls -l has taken comtrol of this child process. This won't execute unless it terminated
abnormally\n");
if(status_code==-1)
{
printf("Terminated Incorrently\n");
return 1;
}
}
else{
printf("This line will be printed\n");
}
return 0;
}
OUTPUT:
Parent process is executing
How many numbers u want in the array
3
Enter the numbers
2
6
4
the sorted array of parent process is
Child process is executing
246
Q2.
//Implement the C program to create a child process using fork() , display parent and child process
id.Child process will display the message "I am child process" and the parent process should display
"I am parent process". SETC_Q2
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
int i,pid;
pid = fork();
if(pid == 0) {
printf("I am Child Process\n");
printf("Child process => PPID=%d, PID=%d\n", getppid(), getpid());
exit(0);
}
else {
printf("I am Parent Process\n");
printf("Parent process => PID=%d\n", getpid());
printf("Waiting for child processes to finish...\n");
wait(NULL);
printf("child process finished.\n");
}
}
OUTPUT:
ASSIGNMENT 2
SET A
Q1.
#include<stdio.h>
#include<stdlib.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp;
int pid;
void count(char *t2,char *t3)
{
int charcount=0,wordcount=0,linecount=0;
if((fp=fopen(t3,"r"))==NULL)
printf("File not found");
else
{
if(strcmp(t2,"l")==0)
{
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
linecount++;
}
printf("The total no. of lines :%d\n",linecount);
}
else if(strcmp(t2,"w")==0)
{
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n' || ch==' ')
wordcount++;
}
printf("The total no. of words :%d\n",wordcount);
}
else if(strcmp(t2,"c")==0)
{
while((ch=fgetc(fp))!=EOF)
{
if(ch!=' ' && ch!='\n')
charcount++;
}
printf("The total no. of characters :%d\n",charcount);
}
else
printf("Command not found");
}
fclose(fp);
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"pause")==0)
exit(0);
else if(strcmp(t1,"count")==0)
count(t2,t3);
else
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)
{
if(strcmp(t1,"exit")==0)
exit(0);
//wait(NULL);
exit(0);
}
}}
OUTPUT:
// FILE CONTENTS( name -f1.txt) //
TYBCS OPERATING SYSTEM
BATCH B2
ASSIGENMENT2 QUESTION
SHELL COUNT PROGRAM
SHELL SEARCH PROGRAM
SHELL DIR PROGRAM
COUNTING CHARACTER LINES AND WORDS
OUTPUT:
MYSHELL$ count c f1.txt
Total character count:127
MYSHELL$ count l f1.txt
Total lines=8
MYSHELL$ count w f1.txt
Total words :24
SET B
Q1.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
char *buff,*t1,*t2,*t3,ch;
int pid;
void list(char t2,char *t3)
{
DIR *dir;
struct dirent *entry;
int cnt=0;
dir=opendir(t3);
if (dir==NULL)
{
printf("Directory %s not found",t3);
return;
}
switch(t2)
{
case 'f' : while((entry=readdir(dir))!=NULL)
{
printf("%s\n",entry->d_name);
}
break;
cnt++;
printf("Total No of Entries: %d\n",cnt);
break;
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"pause")==0)
exit(0);
else if(strcmp(t1,"list")==0)
list(t2[0],t3);
else
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)
{
if(strcmp(t1,"exit")==0)
exit(0);
}
wait(NULL);
exit(0);
}
}
}
OUTPUT:
MyShell$ list f DummyFiles
.
..
dummy.cpp
img5.png
img2.png
img3.png
img1.png
img4.png
SET C
Q1.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
char *buff,*t1,*t2,*t3,ch;
FILE *fp;
int pid;
void typeline(char *t2,char *t3)
{
int i,n,count=0,num;
if((fp=fopen(t3,"r"))==NULL)
printf("File not found\n");
if(strcmp(t2,"a")==0)
{
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
return;
}
n=atoi(t2);
if(n>0)
{
i=0;
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
{
i++;
if(i==n)
{
break;
}
}
printf("%c",ch);
}
printf("\n");
}
else
{
count=0;
while((ch=fgetc(fp))!=EOF)
if(ch=='\n')
count++;
fseek(fp,1,SEEK_SET);
i=0;
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
i++;
if(i==count+n)
break;
}
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
}
fclose(fp);
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s",t1,t2,t3);
if(strcmp(t1,"pause")==0)
exit(0);
else if(strcmp(t1,"typeline")==0)
typeline(t2,t3);
else
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)
{
if(strcmp(t1,"exit")==0)
exit(0);
}
wait(NULL);
exit(0);
}
}
}
OUTPUT
[root@localhost Desktop]# cc shelltypeline.c
[root@localhost Desktop]# ./a.out
myshell$typeline a f1.txt
123456
78910
1112
788O643
xcgcgc
6nbcgd
vb
vgfbhghgd
myshell$typeline -1 f1.txt
vgfbhghgd
myshell$typeline +3 f1.txt
123456
78910
1112
myshell$typeline +2 f1.txt
123456
78910
myshell$typeline -2 f1.txt
vb
vgfbhghgd
myshell$pause
*/
Q2.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
char *buff,*t1,*t2,*t3,*t4,ch;
FILE *fp;
int pid;
void search(char *t2,char *t3,char *t4)
{
int i=1,count=0;
char *p;
if((fp=fopen(t4,"r"))==NULL)
printf("File not found\n");
else
{
if(strcmp(t2,"f")==0)
{
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))!=NULL)
{
printf("%d: %s\n",i,buff);
break;
}
}
//i++;
}
else if(strcmp(t2,"c")==0)
{
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))!=NULL)
{
count++;
}
}
printf("No of occurences of %s= %d\n",t3,count);
}
else if(strcmp(t2,"a")==0)
{
while(fgets(buff,80,fp))
{
if((strstr(buff,t3))!=NULL)
{
printf("%d: %s\n",i,buff);
//i++;
}
}
else
printf("Command not found\n");
fclose(fp);
}
}
main()
{
while(1)
{
printf("myshell$");
fflush(stdin);
t1=(char *)malloc(80);
t2=(char *)malloc(80);
t3=(char *)malloc(80);
t4=(char *)malloc(80);
buff=(char *)malloc(80);
fgets(buff,80,stdin);
sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);
if(strcmp(t1,"pause")==0)
exit(0);
else if(strcmp(t1,"search")==0)
search(t2,t3,t4);
else
{
pid=fork();
if(pid<0)
printf("Child process is not created\n");
else if(pid==0)
{
if(strcmp(t1,"exit")==0)
exit(0);
//wait(NULL);
exit(0);
}
}
OUTPUT
// FILE CONTENTS( name -f1.txt) //
TYBCS OPERATING SYSTEM
BATCH B2
ASSIGENMENT2 QUESTION
SHELL COUNT PROGRAM
SHELL SEARCH PROGRAM
SHELL DIR PROGRAM
COUNTING CHARACTER LINES AND WORDS
OUTPUT:
myshell$ search f TYBCS f1.txt
1: TYBCS OPERATING SYSTEM
ASSIGNMENT 3
SET A:
Q1.
#include<stdio.h>
#include<string.h>
int main()
{
process pro[10], temp;
int n, i, j;
float avgtat = 0, avgwt = 0;
printf("\nHow many processes? Enter a value < 10:");
scanf("%d",&n);
printf("\n");
printf("\nAverage TAT :%f ",avgtat/n);
printf("\nAverage WT :%f ",avgwt/n);
printf("\n");
return 0;
}
OUTPUT:
How many processes? Enter a value < 10:3
Process AT BT
p3 0 5
p1 1 2
p2 2 3
Gantt Chart
0 p3 5
5 p1 7
7 p2 10
Q2.
#include<stdio.h>
#include<string.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev;
void getinput()
{
int i;
void printinput()
{
int i;
printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].bt>tab[j+1].bt)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].tbt<min)
{
min = tab[i].tbt;
mini = i;
}
return mini;
}
void processinput()
{
int i,j;
finish=k=0;
while(finish!=n)
{
if(arrived(time))
{
i = getmin(time);
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
tab[i].ct=time;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}
void ganttchart()
{
int i,j=1;
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
}
OUTPUT:
Enter No.of Processes:3
Process name:p1
Burst time:2
Arrival time:1
Process name:p2
Burst time:3
Arrival time:2
Process name:p3
Burst time:5
Arrival time:0
Entered data-:
Process BT AT
p1 2 1
p2 3 2
p3 5 0
Process BT AT
p1 2 1
p2 3 2
p3 5 0
*******Final Table*********
Process AT BT CT TAT WT
p1 1 2 7 6 4
p2 2 3 10 8 5
p3 0 5 5 5 0
Average WT = 3.000000
******Gantt Chart*******
0 p3 5
5 p1 7
7 p2 10
SET B
Q1.
#include <stdio.h>
int main()
{
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Details of %d Processesn", limit);
for(i = 0; i < limit; i++)
{
printf("\nEnter Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
burst_time[9] = 9999;
for(time = 0; count != limit; time++)
{
smallest = 9;
for(i = 0; i < limit; i++)
{
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] >
0)
{
smallest = i;
}
}
burst_time[smallest]--;
if(burst_time[smallest] == 0)
{
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest];
}
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
return 0;
}
OUTPUT:
Enter the Total Number of Processes: 3
Enter Details of 3 Processesn
Enter Arrival Time: 1
Enter Burst Time: 2
int finish,time,n,k,prev,q;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
printf("Enter priority: ");
scanf("%d",&tab[i].p);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;
printf("\nProcess\tBT\tAT\tpriority");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at,tab[i].p);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].p<min)
{
min = tab[i].p;
mini = i;
}
return mini;
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\nAverage TAT = %f",AvgTAT);
printf("\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
while(finish!=n)
{
if(arrived(time))
{
i=getmin(time);
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
pre-emptive priority
#include<stdio.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt,p;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev,q;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
printf("Enter priority: ");
scanf("%d",&tab[i].p);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;
printf("\nProcess\tBT\tAT\tpriority");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at,tab[i].p);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
int getmin(int t)
{
int i,mini,min=99;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0 && tab[i].p<min)
{
min = tab[i].p;
mini = i;
}
return mini;
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\nAverage TAT = %f",AvgTAT);
printf("\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
while(finish!=n)
{
if(arrived(time))
{
i=getmin(time);
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
}
Round Robin
#include<stdio.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev,q;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
printf("Enter Time Quantum: ");
scanf("%d",&q);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;
printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
clrscr();
printf("\n *******Final Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
time=tab[0].at;
while(finish!=n)
{
if(arrived(time))
{
if(tab[i].tbt!=0)
{
for(j=0;j<q;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
if(time < tab[(i+1)%n].at)
{
i=0;
}
else
i = (i+1)%n;
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
}
ASSIGNMENT-4: Demand paging
SET A: FIFO & LRU
#include <stdio.h>
#include <stdbool.h>
for (int i = 0; i < num_frames; i++) // Initialize all frames to -1 (indicating they are
empty)
frames[i] = -1;
int main() {
int ref_str[] = {12, 15, 12, 18, 6, 8, 11, 12, 0, 9, 2, 6, 8, 11, 15, 19, 8};
int num_pages = sizeof(ref_str) / sizeof(ref_str[0]);
int num_frames;
return 0;
}
int main() {
int ref_str[] = {12, 15, 12, 18, 6, 8, 11, 12, 19, 12, 6, 8, 12, 15, 19, 8};
int num_pages = sizeof(ref_str) / sizeof(ref_str[0]);
int num_frames;
// Implement OPT
int opt_faults = opt(ref_str, num_pages, num_frames);
printf("Total Page Faults using OPT: %d\n", opt_faults);
// Implement MFU
int mfu_faults = mfu(ref_str, num_pages, num_frames);
printf("Total Page Faults using MFU: %d\n", mfu_faults);
return 0;
}
SET C
MRU, Second Chance, LFU
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main() {
int ref_str[] = {2, 5, 2, 8, 5, 4, 1, 2, 3, 2, 6, 12, 5, 9, 8};
int ref_len = sizeof(ref_str) / sizeof(ref_str[0]);
int n;
printf("Enter the number of memory frames: ");
scanf("%d", &n);
return 0;
}