0% found this document useful (0 votes)
15 views44 pages

OS Practical Sem-I

The document contains multiple C programming assignments focusing on process management using fork, exec, and wait system calls. It includes examples of creating parent and child processes, sorting integers, handling orphan processes, and implementing a simple shell for file operations. Each section provides code snippets along with expected outputs for various functionalities such as counting lines, words, and characters in a file, listing directory contents, and searching for strings in files.

Uploaded by

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

OS Practical Sem-I

The document contains multiple C programming assignments focusing on process management using fork, exec, and wait system calls. It includes examples of creating parent and child processes, sorting integers, handling orphan processes, and implementing a simple shell for file operations. Each section provides code snippets along with expected outputs for various functionalities such as counting lines, words, and characters in a file, listing directory contents, and searching for strings in files.

Uploaded by

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

Assignment 1

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>

void insertion_sort(int a[],int n);


void bubble_sort(int a[],int n);

int pid,n, num[50], i, j, temp;


int main()
{

printf("Parent process executing\n");


printf("How many number you want to enter in the array: ");
scanf("%d",&n);
printf("Enter the array:\n");
for(i = 0 ; i < n ; i++)
scanf("%d",&num[i]);

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

void insertion_sort(int a[],int n)


{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=num[i];
for(j=i-1;j>=0 && num[j]>temp;j--)
num[j+1]=num[j];
num[j+1]=temp;
}

printf("The sorted array of Child Process is:\n");


for(i = 0 ; i < n ; i++)
{
printf("%d ",num[i]);
}
printf("\n");
}

void bubble_sort(int a[],int n)


{
for(i = 0 ; i < n ; i++ )
{
for(j = 0 ; j < n-i-1 ; j++)
{
if(num[j] > num[j+1])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
printf("The sorted array of Parent Process is:\n");
for(i = 0 ; i < n ; i++)
{
printf("%d ",num[i]);
}
printf("\n");

}
OUTPUT:
I Am PARENT HAVING PID=>3794

MY CHILD ID =>3795

I Am CHILD HAVING PID=> 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

Enter the numbers:


4213

CHILD PROCESS EXECUTING------------

The Sorted Array Of Child Process using insertion sort:


1 2 3 4

The Sorted Array Of Parent Process using bubble sort:


2 3 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

The sorted array of child process id


264

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;

case 'n' : while((entry=readdir(dir))!=NULL)

cnt++;
printf("Total No of Entries: %d\n",cnt);
break;

case 'i' : while((entry=readdir(dir))!=NULL)


{
printf("\n%s\t %d",entry->d_name,entry->d_ino);
}
break;
default : printf("Invalid argument");
}
closedir(dir);

}
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

MyShell$ list n DummyFiles


There are 8 files
MyShell$ list i DummyFiles
File Name: . Info: 1080577074
File Name: .. Info: 281111
File Name: dummy.cpp Info: 1080577075
File Name: img5.png Info: 1080577078
File Name: img2.png Info: 1080577079
File Name: img3.png Info: 1080577080
File Name: img1.png Info: 1080577081
File Name: img4.png Info: 1080577082

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

myshell$ search a SHELL f1.txt


1: SHELL COUNT PROGRAM

1: SHELL SEARCH PROGRAM

1: SHELL DIR PROGRAM


myshell$search c SHELL f1.txt
No of occurences of SHELL= 3

ASSIGNMENT 3
SET A:
Q1.
#include<stdio.h>
#include<string.h>

typedef struct process


{
int at,bt,st,ct,tat,wt;
char pname[7];
}process;

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

for(i=0; i<n; i++)


{
printf("\nEnter process %d's name: ",i+1);
scanf("%s",pro[i].pname);
printf("\nEnter process %d's arrival time: ",i+1);
scanf("%d",&pro[i].at);
printf("\nEnter process %d's burst time: ",i+1);
scanf("%d",&pro[i].bt);
}
printf("\nEntered processes are: ");
printf("\nProcess\tAT\tBT");
for(i=0; i<n; i++)
printf("\n%s\t%d\t%d",pro[i].pname,pro[i].at,pro[i].bt);
printf("\n");
printf("\nSorting processes according to arrival time...");
printf("\n");
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(pro[j].at > pro[j+1].at)
{
temp = pro[j];
pro[j] = pro[j+1];
pro[j+1] = temp;
}
}
}
printf("\nProcess\tAT\tBT");
for(i=0; i<n; i++)
printf("\n%s\t%d\t%d",pro[i].pname,pro[i].at,pro[i].bt);
printf("\n");
printf("\nGantt Chart");
printf("\n");
if(pro[0].at>0)
printf("\n%d\t%s\t%d",0,"Idle",pro[0].at);
pro[0].st = pro[0].at;
pro[0].ct = pro[0].st + pro[0].bt;
pro[0].tat = pro[0].ct - pro[0].at;
pro[0].wt = pro[0].tat - pro[0].bt;
avgtat += pro[0].tat;
avgwt += pro[0].wt;
printf("\n%d\t%s\t%d",pro[0].st,pro[0].pname,pro[0].ct);
for(i=1;i<n;i++)
{
if(pro[i].at>pro[i-1].ct)
{
printf("\n%d\t%s\t%d",pro[i-1].ct,"Idle",pro[i].at);
pro[i].st=pro[i].at;
}
else
pro[i].st = pro[i-1].ct;
pro[i].ct = pro[i].st + pro[i].bt;
pro[i].tat = pro[i].ct - pro[i].at;
pro[i].wt = pro[i].tat - pro[i].bt;
printf("\n%d\t%s\t%d",pro[i].st,pro[i].pname,pro[i].ct);
avgtat += pro[i].tat;
avgwt += pro[i].wt;
}

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

Enter process 1's name: p1

Enter process 1's arrival time: 1

Enter process 1's burst time: 2

Enter process 2's name: p2

Enter process 2's arrival time: 2


Enter process 2's burst time: 3

Enter process 3's name: p3

Enter process 3's arrival time: 0

Enter process 3's burst time: 5

Entered processes are:


Process AT BT
p1 1 2
p2 2 3
p3 0 5

Sorting processes according to arrival time...

Process AT BT
p3 0 5
p1 1 2
p2 2 3

Gantt Chart

0 p3 5
5 p1 7
7 p2 10

Average TAT :6.333333


Average WT :3.000000

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;

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

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;

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

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

Data after sorting according to arrival time-:

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 TAT = 6.333333

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

Enter Arrival Time: 2


Enter Burst Time: 3

Enter Arrival Time: 0


Enter Burst Time: 5

Average Waiting Time: 2.000000


Average Turnaround Time: 5.333333

Non preemptive 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);
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>

void print_frames(int frames[], int num_frames) {


for (int i = 0; i < num_frames; i++) {
if (frames[i] == -1)
printf("- ");
else
printf("%d ", frames[i]);
}
printf("\n");
}

// Function to implement FIFO page replacement


int fifo(int ref_str[], int num_pages, int num_frames) {
int frames[num_frames];
int page_faults = 0;
int index = 0;

for (int i = 0; i < num_frames; i++) // Initialize all frames to -1 (indicating they are
empty)
frames[i] = -1;

printf("\nFIFO Page Replacement\n");


for (int i = 0; i < num_pages; i++) {
bool found = false;
// Check if the page is already in a frame
for (int j = 0; j < num_frames; j++) {
if (frames[j] == ref_str[i]) {
found = true;
printf("Page %d: HIT -> ", ref_str[i]);
break;
}
}

// If the page is not found, replace the oldest page (FIFO)


if (!found) {
printf("Page %d: FAULT -> ", ref_str[i]);
frames[index] = ref_str[i];
index = (index + 1) % num_frames;
page_faults++;
}
print_frames(frames, num_frames); // Print current state of frames
}
return page_faults;
}

// Function to implement LRU page replacement


int lru(int ref_str[], int num_pages, int num_frames) {
int frames[num_frames], counter[num_frames];
int page_faults = 0, time = 0;

// Initialize all frames to -1 and counters to 0


for (int i = 0; i < num_frames; i++) {
frames[i] = -1;
counter[i] = 0;
}

printf("\nLRU Page Replacement\n");


for (int i = 0; i < num_pages; i++) {
bool found = false;
int lru_index = 0;

// Check if the page is already in a frame


for (int j = 0; j < num_frames; j++) {
if (frames[j] == ref_str[i]) {
found = true;
counter[j] = ++time;
printf("Page %d: HIT -> ", ref_str[i]);
break;
}
}
// If the page is not found, replace the least recently used page
if (!found) {
printf("Page %d: FAULT -> ", ref_str[i]);
int least_time = counter[0];
for (int j = 1; j < num_frames; j++) {
if (counter[j] < least_time) {
least_time = counter[j];
lru_index = j;
}
}
frames[lru_index] = ref_str[i];
counter[lru_index] = ++time;
page_faults++;
}
// Print current state of frames
print_frames(frames, num_frames);
}
return page_faults;
}

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;

printf("Enter number of memory frames: ");


scanf("%d", &num_frames);
int fifo_faults = fifo(ref_str, num_pages, num_frames); // Implement FIFO
printf("Total Page Faults using FIFO: %d\n", fifo_faults);

int lru_faults = lru(ref_str, num_pages, num_frames); // Implement LRU


printf("Total Page Faults using LRU: %d\n", lru_faults);

return 0;
}

SET B: OPT & MFU


#include <stdio.h>
#include <stdbool.h>
// Function to find the optimal page to replace
int find_opt(int ref_str[], int frames[], int num_frames, int num_pages, int current) {
int pos[num_frames];
for (int i = 0; i < num_frames; i++) pos[i] = -1; // Initialize positions of future use

// Check future use of pages in frames


for (int i = 0; i < num_frames; i++) {
for (int j = current + 1; j < num_pages; j++) {
if (frames[i] == ref_str[j]) {
pos[i] = j; // Found future use at position j
break;
}
}
}
// Find the frame that won't be used for the longest time
int max_pos = -1, index = -1;
for (int i = 0; i < num_frames; i++) {
if (pos[i] == -1) return i; // If the page won't be used in the future, return its index
if (pos[i] > max_pos) {
max_pos = pos[i];
index = i;
}
}
return index;
}
// Function to implement OPT page replacement
int opt(int ref_str[], int num_pages, int num_frames) {
int frames[num_frames];
int page_faults = 0;

// Initialize frames to -1 (empty)


for (int i = 0; i < num_frames; i++) frames[i] = -1;

printf("\nOPT Page Replacement\n");


for (int i = 0; i < num_pages; i++) {
bool found = false;

// Check if the page is already in frames


for (int j = 0; j < num_frames; j++) {
if (frames[j] == ref_str[i]) {
found = true;
printf("Page %d: HIT -> ", ref_str[i]);
break;
}
}

// If page is not found (page fault)


if (!found) {
printf("Page %d: FAULT -> ", ref_str[i]);

// If there's space in frames


bool empty_frame = false;
for (int j = 0; j < num_frames; j++) {
if (frames[j] == -1) {
frames[j] = ref_str[i];
empty_frame = true;
break;
}
}
// If no space, use OPT to replace a page
if (!empty_frame) {
int index = find_opt(ref_str, frames, num_frames, num_pages, i);
frames[index] = ref_str[i];
}
page_faults++;
}

// Print current state of frames


for (int j = 0; j < num_frames; j++) {
if (frames[j] == -1) printf("- ");
else printf("%d ", frames[j]);
}
printf("\n");
}
return page_faults;
}
// Function to implement MFU page replacement
int mfu(int ref_str[], int num_pages, int num_frames) {
int frames[num_frames], freq[num_frames];
int page_faults = 0;

// Initialize frames and frequency array to -1 and 0


for (int i = 0; i < num_frames; i++) {
frames[i] = -1;
freq[i] = 0;
}

printf("\nMFU Page Replacement\n");


for (int i = 0; i < num_pages; i++) {
bool found = false;
int mfu_index = 0;

// Check if the page is already in frames


for (int j = 0; j < num_frames; j++) {
if (frames[j] == ref_str[i]) {
found = true;
freq[j]++; // Increase frequency for a hit
printf("Page %d: HIT -> ", ref_str[i]);
break;
}
}

// If page is not found (page fault)


if (!found) {
printf("Page %d: FAULT -> ", ref_str[i]);

// If there's space in frames


bool empty_frame = false;
for (int j = 0; j < num_frames; j++) {
if (frames[j] == -1) {
frames[j] = ref_str[i];
freq[j] = 1; // Initialize frequency count
empty_frame = true;
break;
}
}

// If no space, replace the most frequently used page


if (!empty_frame) {
int max_freq = freq[0];
for (int j = 1; j < num_frames; j++) {
if (freq[j] > max_freq) {
max_freq = freq[j];
mfu_index = j;
}
}
frames[mfu_index] = ref_str[i];
freq[mfu_index] = 1; // Reset frequency after replacement
}
page_faults++;
}
// Print current state of frames
for (int j = 0; j < num_frames; j++) {
if (frames[j] == -1) printf("- ");
else printf("%d ", frames[j]);
}
printf("\n");
}
return page_faults;
}

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;

printf("Enter number of memory frames: ");


scanf("%d", &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>

void print_pages(int pages[], int n) {


for (int i = 0; i < n; i++) {
if (pages[i] == -1)
printf(" _ ");
else
printf(" %d ", pages[i]);
}
printf("\n");
}

// MRU Page Replacement


int mru(int pages[], int n, int ref_str[], int ref_len) {
int page_faults = 0;
int frames[n];
for (int i = 0; i < n; i++) frames[i] = -1;

int most_recent = -1;

for (int i = 0; i < ref_len; i++) {


int hit = 0;
for (int j = 0; j < n; j++) {
if (frames[j] == ref_str[i]) {
hit = 1;
most_recent = j;
break;
}
}
if (!hit) {
if (most_recent != -1) {
frames[most_recent] = ref_str[i];
} else {
for (int j = 0; j < n; j++) {
if (frames[j] == -1) {
frames[j] = ref_str[i];
break;
}
}
}
most_recent = -1;
page_faults++;
}
print_pages(frames, n);
}
return page_faults;
}

// Second Chance Page Replacement


int second_chance(int pages[], int n, int ref_str[], int ref_len) {
int page_faults = 0, ptr = 0;
int frames[n], ref_bits[n];
for (int i = 0; i < n; i++) {
frames[i] = -1;
ref_bits[i] = 0;
}

for (int i = 0; i < ref_len; i++) {


int hit = 0;
for (int j = 0; j < n; j++) {
if (frames[j] == ref_str[i]) {
hit = 1;
ref_bits[j] = 1; // Second chance given
break;
}
}
if (!hit) {
while (ref_bits[ptr] == 1) {
ref_bits[ptr] = 0;
ptr = (ptr + 1) % n;
}
frames[ptr] = ref_str[i];
ref_bits[ptr] = 1;
ptr = (ptr + 1) % n;
page_faults++;
}
print_pages(frames, n);
}
return page_faults;
}

// Least Frequently Used Page Replacement


int lfu(int pages[], int n, int ref_str[], int ref_len) {
int page_faults = 0;
int frames[n], freq[1000] = {0}, time[1000];
for (int i = 0; i < n; i++) frames[i] = -1;

for (int i = 0; i < ref_len; i++) {


int hit = 0, min_freq = INT_MAX, min_idx = -1;

// Check if page is already in memory


for (int j = 0; j < n; j++) {
if (frames[j] == ref_str[i]) {
hit = 1;
freq[frames[j]]++;
break;
}
}

// If not in memory, replace the LFU page


if (!hit) {
for (int j = 0; j < n; j++) {
if (frames[j] == -1) {
frames[j] = ref_str[i];
freq[frames[j]] = 1;
page_faults++;
hit = 1;
break;
}
if (freq[frames[j]] < min_freq) {
min_freq = freq[frames[j]];
min_idx = j;
}
}
if (!hit) {
frames[min_idx] = ref_str[i];
freq[frames[min_idx]] = 1;
page_faults++;
}
}
print_pages(frames, n);
}
return page_faults;
}

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

printf("\nMost Recently Used (MRU) Page Replacement\n");


int mru_faults = mru(ref_str, n, ref_str, ref_len);
printf("Total page faults in MRU: %d\n", mru_faults);

printf("\nSecond Chance Page Replacement\n");


int sc_faults = second_chance(ref_str, n, ref_str, ref_len);
printf("Total page faults in Second Chance: %d\n", sc_faults);

printf("\nLeast Frequently Used (LFU) Page Replacement\n");


int lfu_faults = lfu(ref_str, n, ref_str, ref_len);
printf("Total page faults in LFU: %d\n", lfu_faults);

return 0;
}

You might also like