82
Dr.S.J.S Paul Memorial College of Engineering and Technology Ramanathapuram Revenue Village, Villianur commune Pondicherry-605 502. (Approved by AICTE and Affiliated to Pondicherry University) Operating System Laboratory Manual

Operating System Lab_manual

Embed Size (px)

Citation preview

Page 1: Operating System Lab_manual

Dr.S.J.S Paul Memorial College of Engineering and Technology

Ramanathapuram Revenue Village, Villianur commune Pondicherry-605 502.

(Approved by AICTE and Affiliated to Pondicherry University)

Operating System Laboratory Manual

Page 2: Operating System Lab_manual

Operating System Lab

1. Study of basic Unix / Linux commands.

Aim:

To study the basic commands Unix / Linux.

Commands:

kdir-for creating a directory ,it also creates a number of sub-directories with one mkdir command

who am i-it identifies the invoking user and lists the user name,terminal line and the data and time of login

who-it displays data about all the users that have logged into the systems

finger-displays login name,terminal name,write status,idle time,llogin time and location for each user who is currently logged in

pwd-print working directory(pwd)-ascertains the current working directory

cat- to create a new file or display the content of the specified file if the file names are specified then the two files are concatenated and it will display its contents

tput- clear-the screen is instantly cleared and the shell prompt appears

ls-it is listing,it lists the directory

cd-changes directory

copy(cp<file1><file2>)-copies the file from one to other

delete(rm)-to remove or delete a file

tty-displays terminal number

date-displays current date and time

man-to get online help

processor status command(ps)-displays the attributes of a process

calendar(cal)-displays calendar of specified year (or) month

path(echo path)-to view the sequence of directories that the system will search to look for a command specified

Page 3: Operating System Lab_manual

echo-displays messages on the screen

tput-clears the entire screen similar to that of the clrscr command

spell-checks your spellings and produces list of it

ispell-perform corrections on line

wc-counts the number of lines ,words and characters

od-displays octal value of each character and is used to display invisible characters

cmp-compares the two files

nl-line numbering ,sets the initial valye,increment and width of the number

mesg-used to prevent others from writing to your terminal with writh command

news-reads messages published by the systen administrator

tr-translates characters (change of case of alphabets)

write-used for two way communication

mail-sends message to any user evn if the user is not logged in

wall-write to all command is to send message by system administrator to all user who are logged into the system

elm-handles multimedia attachments

kill-kills the process(terminates)

stty-setting terminal characteristics used to display and set various terminal commands

vi-locates,creates file

find-locates files in a directory and in a sub directory

sh-runs the corresponding file

calculator-used to perform a calculator(bc,dc)

Page 4: Operating System Lab_manual

COMPARE TWO STRINGS

SOURCE CODE:

clearecho "OUTPUT:"echo "Enter first string"read str1echo "Enter second string"read str2if [ $str1 = str2]then echo "Strings are equal"else echo"String are not equal"fi

SAMPLE INPUT AND OUTPUT:

Enter first stringOperating

Enter second string operatingStrings are equal

Page 5: Operating System Lab_manual

REVERSE A STRING

SOURCE CODE

clearecho "OUTPUT:"echo "Enter the string"read strlen='echo $str| wc -c'while [$len -gt 0 ]do temp='echo $str | cut -c $len' rev= 'echo $rev$temp' len='expr $len - 1'doneecho "The reverse string is $rev"

SAMPLE INPUT AND OUTPUT:

Enter the string Computer

The reverse string retupmoc

Page 6: Operating System Lab_manual

FIBONACCI SERIES

SOURCE CODE:

clearecho "OUTPUT"echo "Enter the limit"read n

echo "The fibonacci series"b=0c=1d=0i=0if [ $n -ge 2]then echo "$b $c" n=$(($n -2)) while [ $i -lt $n ] do a=$(($b+$c)) b=$c c=$a echo "$c" i=$(($i+1)) doneelse echo "$b" fi

SAMPLE INPUT AND OUTPUT:

Enter the limit5The fibonacci series01123

Page 7: Operating System Lab_manual

SWAPPING TWO NUMBERS

SOURCE CODE:

clearecho "OUTPUT:"echo "Enter first number to be swapped"read aecho "Enter second number to be swapped'read becho "Before swapping"echo"A=$a B=$b"a=$(($a+$b))b=$(($a-$b))a=$(($a-$b))echo"After swapping"echo"A=$a B=$b

SAMPLE INPUT AND OUTPUT:

Enter first number to be swapped 12Enter second number to be swapped 21Before swappingA=12 B=21After swappingA=21 B=12

Page 8: Operating System Lab_manual

SUM OF DIGITS

SOURCE CODE:

clearecho "OUTPUT:"echo "Enter an interger"read numsum=0digit=0while [ $num -gt 0 ]do a=$(($num%10)) digit=$(($digit+1)) sum=$(($sum+$a)) num=$(($num/10))doneecho "The no of digits in the integer is $digit"echo "The sum of digits is $sum"

SAMPLE INPUT AND OUTPUT:

Enter an integer1234The no of digits in the integer is 4The sum of digits is 10

Page 9: Operating System Lab_manual

CONVERT LOWER TO UPPER

SOURCE CODE:

clearecho "OUTPUT:"echo "Enter a string"read strtemp='echo $str | tr [a-z] [A-Z]'echo "The case changed string is $temp"

SAMPLE INPUT AND OUTPUT:

Enter a string systemThe case changed string is SYSTEM

Page 10: Operating System Lab_manual

CURRENTLY LOGGED IN OR NOT

SOURCE CODE:

clearecho "OUTPUT:"echo "Enter the name of the user:"read namewho>tempif ( grep $name temo > temp1 )then echo "$name is currently logged in"else echo "$name is not currently logged in"fi

SAMPLE INPUT AND OUTPUT:

Enter the name of the user: AnuAnu is currently logged in

Page 11: Operating System Lab_manual

FORK AND WAIT SYSTEM CALL

SOURCE CODE:

#include<stdio.h>main(){int pid;pid=fork();printf(“\nHELLO\n”);if(pid==0){Wait(20);printf(“\n CHILD PROCESS IS EXECUTED \n”);printf(“\n THE ID NUMBER OF THE CHILD PROCESS IS %d\n”,getpid());}else if(pid>0){Wait(50);printf(“\n PARENT PROCESS IS EXECUTED”);printf(“\n THE ID NUMBER OF PARENT PROCESS IS %d\n”,getpid());}}

SAMPLE INPUT AND OUTPUT:

[iiiit01@localhost cpro]$ cc pro3.c[iiiit01@localhost cpro]$./a.outHELLOCHILD PROCESS IS EXECUTEDTHE ID NUMBER OF THE CHILD PROCESS IS 3150HELLOPARENT PROCESS IS EXECUTEDTHE ID NUMBER OF PARENT PROCESS IS 3149

Page 12: Operating System Lab_manual

CREATE A CHILD PROCESS USING SLEEP

SOURCE CODE:

#include<stdio.h>main(){int pid;pid=fork();printf(“\nHELLO\n”);if(pid==0){printf(“\n CHILD PROCESS IS EXECUTED \n”);printf(“\n THE ID NUMBER OF THE CHILD PROCESS IS %d\n”,getpid());sleep(30);}else if(pid>0){Sleep(20);printf(“\n PARENT PROCESS IS EXECUTED”);printf(“\n THE ID NUMBER OF PARENT PROCESS IS %d\n”,getpid());}} SAMPLE INPUT AND OUTPUT: [iiiit01@localhost cpro]$ cc pro2.c[iiiit01@localhost cpro]$./a.outHELLOCHILD PROCESS IS EXECUTEDTHE ID NUMBER OF THE CHILD PROCESS IS 3206HELLOPARENT PROCESS ISRUNNINGTHE ID NUMBER OF PARENT PROCESS IS 2964

Page 13: Operating System Lab_manual

EXIT SYSTEM CALL

SOURCE CODE:

#include<stdio.h>#include<stdlib.h>void main(){int i, a;for(i=1;i<=3;i++){printf("Enter the Chocie(1=exit/2=Continue): ");scanf("%d", &a);if(a==1){printf("U choose to exit");exit(0);}else{ printf("U choose to Continue");}}}

SAMPLE INPUT AND OUTPUT:

Enter the Chocie(1=exit/2=Continue): 1U choose to exit

Page 14: Operating System Lab_manual

DIRECTORY SYSTEM CALL

SOURCE CODE:

#include<stdio.h>#include<dirent.h>Main(int args,char*argv[]){DIR*dirname;Struct dirent*preaddr;Dirname=opendir(argv[1]);While(1){Preaddr=readdir(dirname);If(preaddr==NULL){Closedir(dirname);Exit(0);}Printf(“\n\n FOUND ENTRY%s:%s:”,argv[1],preaddr->d_name);}}

SAMPLE INPUT AND OUTPUT:

[iiiit0]@localhost cpro]$cc pro1.c[iiiit0]@localhost cpro]$ mkdir hiram[iiiit0]@localhost cpro]$ /a.out hiram

FOUND ENTRY hiram:.:FOUND ENTRY hiram:..:

Page 15: Operating System Lab_manual

FILE SYSTEM CALL USING OPEN AND CLOSE

SOURCE CODE:

#include<stdio.h>#define size 10main (int argc,char * argr[]){int i,n,rd,wd,cr,fd;char s[size];if(argc<3){printf(“illegal input”);exit(1);}fd=open(argr[1],0)if(fd==-1){printf(“error occurred”);exit(1);}cr=create(argr[2],9999);if(cr==-1){printf(“file not created”);exit(1);}rd=read(fd,s,size);while(rd>0){wd=write(cr,s,size);rd=read(fd,s,size);}close(fd);close(cr);printf(“file completed”);}

Page 16: Operating System Lab_manual

SAMPLE INPUT AND OUTPUT:

[iiiit01@localhost cpro]$ cc pro5.c [iiiit01@localhost cpro]$./a.out output peruoutFile completed[iiiit01@localhost cpro]$ vi peruout

Hi how are u

Page 17: Operating System Lab_manual

SIMULATION OF LS COMMAND

SOURCE CODE:

#include<stdio.h>main(){Char * one[3];One[0]=”1s”;One[1]=”-1”;One[2]=(char*)0;Exec1(“/bin/1s”,one[0],one[1],one[2]);}

SAMPLE INPUT AND OUTPUT:

total 44

-rwxrwxr-x 1 iiiit08 iiiit08 11570 jan 1 00:54 a.out-rw-rw-r—1 iiiit08 iiiit08 709 jan 1 1999 fc.c-rw-rw-r—1 iiiit08 iiiit08 421 jan 1 00:46 fcfs.c-rw-rw-r—1 iiiit08 iiiit08 128 jan 1 00:54 ls.c-rw-rw-r—1 iiiit08 iiiit08 76 jan 1 1999 output.c-rw-rw-r—1 iiiit08 iiiit08 0 jan 1 00:47 pi.c-rw-rw-r—1 iiiit08 iiiit08 0 jan 1 00:47 pi.c-rw-rw-r—1 iiiit08 iiiit08 1180 jan 1 00:49 rr.c-rw-rw-r—1 iiiit08 iiiit08 766 jan 1 00:49 sjf.c-rw-rw-r—1 iiiit08 iiiit08 178 jan 1 00:53 stf.c-rw-rw-r—1 iiiit08 iiiit08 258 jan 21 2001 wait.c

Page 18: Operating System Lab_manual

SIMULATION OF GREP COMMAND

SOURCE CODE:

#include<stdio.h>#include<string.h>void main(){ FILE *fp; char mstr[500], cstr[50], c, *ptr, fname[20];int i=0, count=0; fflush(stdin); printf("\nENTER THE STRING AND FILE NAME :"); scanf("%s %s", cstr, fname); fp=fopen(fname, "r"); while((c=getc(fp))!=EOF) { mstr[i]=c; if(c=='\n') { mstr[i]='\0'; ptr=strstr(mstr, cstr); if(ptr!=NULL) { printf("\n[%s] FOUND IN \n\n%s\n",cstr, mstr); count++; } i=0; }i++; }if(count==0){ printf("\nNo Occurences Found");}else{ printf("\nOccurences found:%d", count);}}

SAMPLE INPUT AND OUTPUT:

ENTER THE STRING AND FILE NAME : hello file1.txt[hello] FOUND INHello world, how are u?

Page 19: Operating System Lab_manual

FIRST COME FIRST SERVE (FCFS) SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>void main(){ int a[15],b[15], n, i, sum=0; float avg; clrscr(); printf("\n\tFirst come First Serve\n"); printf("\nEnter the No of process:"); scanf("%d", &n); for(i=1;i<=n;i++) { printf("\nEnter the Burst time for Process %d:",i); scanf("%d",&a[i]); } clrscr(); printf("\n\t\t\a\aBefore FCFS Scheduling\n\n"); printf("\n\tProcess\t\tBrust Time\n"); for(i=1;i<=n;i++) { printf("\n\tProcess%d\t%d\n", i, a[i]); } b[1]=0; for(i=2;i<=n;i++) { if(i==2) { b[i]=a[i-1]; } else { b[i]=b[i-1]+a[i-1]; } } printf("\n\n\n\t\t\aAfter FCFS Scheduling\n\n"); printf("\n\t\aProcess\t\tBurst Time\tWaiting Time\n\n\n"); for(i=1;i<=n;i++) { sum=sum+b[i]; printf("\n\t\aProcess%d\t%d\t\t%d\n", i, a[i],b[i]); } avg=sum/n;

Page 20: Operating System Lab_manual

printf("\n\n\t\aaverage waiting Time:\a\a%f", avg); getch();}

SAMPLE INPUT AND OUTPUT:

First come First Serve

Enter the No of process: 3Enter the Burst time for Process 1: 6Enter the Burst time for Process 2: 3Enter the Burst time for Process 3: 4

Before FCFS Scheduling

Process Burst Time

1 62 33 4

After FCFS Scheduling

Process Burst Time Waiting Time

1 6 02 3 63 4 9

Average waiting Time: 5

Page 21: Operating System Lab_manual

SHORTEST JOB FIRST SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>void main(){ int a[15][5],b[15], n, i, sum=0; float avg; int temp, temp1, j; clrscr(); printf("\n\tShortest Job First\n"); printf("\nEnter the No of process:"); scanf("%d", &n); for(i=1;i<=n;i++) { a[i][0]=i; printf("\nEnter the Burst time for Process %d:",i); scanf("%d",&a[i][1]); } clrscr(); printf("\n\n\n\t\t\a\aBefore SJF Scheduling\n"); printf("\n\n\tProcess\t\tBurst Time\n"); for(i=1;i<=n;i++) { printf("\n\n\t%d\t\t%d",a[i][0], a[i][1]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(a[i][1]>a[j][1]) { temp=a[i][1]; a[i][1]=a[j][1]; a[j][1]=temp; temp1=a[i][0]; a[i][0]=a[j][0]; a[j][0]=temp1; } } } b[1]=0; for(i=2;i<=n;i++) { if(i==2)

Page 22: Operating System Lab_manual

{ b[i]=a[i-1][1]; } else { b[i]=b[i-1]+a[i-1][1]; } } printf("\n\n\n\n\n\t\t\a\aAfter SJF Scheduling\n"); printf("\n\n\tProcess\t\tBurst Time\tTurn Around Time"); for(i=1;i<=n;i++) { sum=sum+b[i]; printf("\a\n\n\t%d\t\t%d\t\t%d",a[i][0], a[i][1], b[i]); } avg=sum/n; printf("\n\n\n\t\aaverage waiting Time:\a\a%3.1f", avg); getch(); }

SAMPLE INPUT AND OUTPUT

Shortest Job FirstEnter the No of process:3Enter the Burst time for Process 1: 6Enter the Burst time for Process 2: 1Enter the Burst time for Process 3: 2

Before SJF SchedulingProcess Burst Time

1 6 2 1 3 2

After SJF SchedulingProcess Burst Time Turn Around Time

2 1 0 3 2 1 1 6 3

Average waiting Time:1.0

Page 23: Operating System Lab_manual

PRIORITY SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>void main(){ int a[15][5],b[15], n, i, sum=0; float avg; int temp, temp1, j; clrscr(); printf("\n\tPriority Scheduling\n"); printf("\nEnter the No of process:"); scanf("%d", &n); for(i=1;i<=n;i++) { a[i][0]=i; printf("\nEnter the Burst time for Process %d:",i); scanf("%d",&a[i][1]); printf("\nEnter the Priority for Process %d:", i); scanf("%d", &a[i][2]); } clrscr(); printf("\n\n\n\t\t\a\aBefore Priority Scheduling\n"); printf("\n\nProcess\t\tPriority\tBurst Time\n"); for(i=1;i<=n;i++) { printf("\n\n%d\t\t%d\t\t%d",a[i][0], a[i][2], a[i][1]); } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(a[i][2]>a[j][2]) { temp=a[i][1]; a[i][1]=a[j][1]; a[j][1]=temp; temp1=a[i][0]; a[i][0]=a[j][0]; a[j][0]=temp1; temp1=a[i][2]; a[i][2]=a[j][2]; a[j][2]=temp1; } }

Page 24: Operating System Lab_manual

} b[1]=0; for(i=2;i<=n;i++) { if(i==2) { b[i]=a[i-1][1]; } else { b[i]=b[i-1]+a[i-1][1]; } } printf("\n\n\n\n\n\t\t\a\aAfter Priority Scheduling\n"); printf("\n\nProcess\t\tPriority\tBurst Time\tTurn Around Time"); for(i=1;i<=n;i++) { sum=sum+b[i]; printf("\a\n\n%d\t\t%d\t\t%d\t\t%d",a[i][0], a[i][2] ,a[i][1], b[i]); } avg=sum/n; printf("\n\n\n\t\aaverage waiting Time:\a\a%3.1f", avg); getch(); }

SAMPLE INPUT AND OUTPUT

Priority SchedulingEnter the No of process:Enter the Burst time for Process 1:Enter the Priority for Process 1:Enter the Burst time for Process 2:Enter the Priority for Process 2:Enter the Burst time for Process 3:Enter the Priority for Process 3:

Before Priority SchedulingProcess Priority Burst Time

1 2 42 1 53 3 6

After Priority SchedulingProcess Priority Burst Time Turn Around Time

2 1 5 01 2 4 53 3 6 9

Average waiting Time:4.0

Page 25: Operating System Lab_manual

ROUND ROBIN SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>void main(){ clrscr(); int nz, n, i, a[20][5]; int ts, w=0, count; printf("\n\t\t\tRound Robin Scheduling\n"); printf("\nEnter the No. of Process:"); scanf("%d", &n); for(i=1;i<=n;i++) { printf("\nEnter the Burst time for process%d:", i); scanf("%d", &a[i][1]); a[i][0]=i; } for(i=1;i<=n;i++) { a[i][2]=0; } ts=5; i=1; clrscr(); printf("\n\n\t\t\tAfter Round Robin Scheduling\n\n\n"); printf("Process No.\tBurst Time\twaiting time\tRemaining\n\n"); while(count<n) { int count=0; if(a[i][1]>=ts) { printf("Process%d\t",i); printf("%d\t\t", a[i][1]); printf("%d\t\t", w); a[i][2]=a[i][2]+w; w=w+ts; a[i][1]=a[i][1]-ts; printf("%d\n\n", a[i][1]); }

Page 26: Operating System Lab_manual

else if(a[i][1]<ts && a[i][1]!=0) { printf("Process%d\t",i); printf("%d\t\t", a[i][1]); printf("%d\t\t", w); //printf("0\n\n"); a[i][2]=a[i][2]+w; w=w+a[i][1]; a[i][1]=0; printf("%d\n\n", a[i][1]); } if(i==n) { i=1; for(int z=1;z<=n;z++) { if(a[z][1]==0) { count++; printf("count=%d\t n=%d\ti=%d\n\n", count, n, i); } } } else { i++; } printf("Loop exit count=%d",count); getch();} int twt=0, awt; printf("\n\nTotal Waititng time of processes\n\n"); for(i=1;i<=n;i++) { printf("\nProcess %d\t%d\n", i, a[i][2]); twt=twt+a[i][2]; } awt=twt/n; printf("\n\n\naverage waiting time:%d", awt); getch();}

Page 27: Operating System Lab_manual

SAMPLE INPUT AND OUTPUT:

Round Robin Scheduling

Enter the No. of Process:3

Enter the Burst time for process1: 10Enter the Burst time for process2: 6Enter the Burst time for process3: 3

After Round Robin Scheduling

Process No. Burst Time waiting time Remaining1 10 0 52

6 5 13 3 10 01 5 13 02 1 18 0

Total Waiting time of processes

Process 1 13Process 2 23Process 3 10

Average waiting time:15

Page 28: Operating System Lab_manual

FIRST COME FIRST SERVE SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>void space(int n){ for(int i=1;i<=n;i++) { printf("-"); } printf("%d", n);}

void main(){ clrscr(); int sec, i, pos[50], head; int seek; float avg, sum;

printf("\n\t\tFCFS Disk Scheduling"); printf("\n\nenter the no. of Datas to be read:"); scanf("%d", &sec); for(i=1;i<=sec;i++) { printf("\n\nEnter the Sector Position of Data%d: ", i); scanf("%d",&pos[i]); } printf("\n\n\nEnter the Position of Head: "); scanf("%d", &head); clrscr(); seek=abs(pos[1]-head); sum=seek; printf("\n\t\t\tFCFS Disk Scheduling\n\n"); space(head); printf("\n\n\n"); for(i=1;i<=sec;i++) { space(pos[i]); if(i==1) { seek=abs(pos[i]-head); printf("[Seek Time=%d]\n\n\n", seek);

Page 29: Operating System Lab_manual

} else { seek=abs(pos[i-1]-pos[i]); sum=sum+seek; printf("[Seek Time=%d]\n\n\n", seek); } } avg= sum/sec; printf("\n\n\nAverage Seek time=%3.3f",avg); getch();}

SAMPLE INPUT AND OUTPUT

FCFS Disk Scheduling

Enter the no. of Datas to be read: 5Enter the Sector Position of Data1: 10Enter the Sector Position of Data2: 20Enter the Sector Position of Data3: 25Enter the Sector Position of Data4: 8Enter the Sector Position of Data5: 30Enter the Position of Head: 15

FCFS Disk Scheduling

---------------15----------10[Seek Time=5]--------------------20[Seek Time=10]-------------------------25[Seek Time=5]--------8[Seek Time=17]------------------------------30[Seek Time=22]

Page 30: Operating System Lab_manual

SHORTEST SEEK TIME FIRST SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>void space(int n){ for(int i=1;i<=n;i++) { printf("-"); } printf("%d", n);}

void main(){ clrscr(); int sec, pos[50], head; int min, cur, n; float sum=0, avg;

printf("\n\t\tSSTF Disk Scheduling"); printf("\n\nenter the no. of Datas to be read:"); scanf("%d", &sec); for(int i=1;i<=sec;i++) { printf("\n\nEnter the Sector Position of Data%d: ", i); scanf("%d",&pos[i]); } printf("\n\n\nEnter the Position of Head: "); scanf("%d", &head); clrscr(); printf("\n\t\t\tSSTF Disk Scheduling\n\n"); space(head); printf("\n\n\n"); for(i=1;i<=sec;i++) { n=1; min=abs(pos[1]-head); for(int j=2;j<=sec;j++) { if(min>abs(pos[j]-head)) { min=abs(pos[j]-head);

Page 31: Operating System Lab_manual

n=j; } } space(pos[n]); printf("[Seek Time=%d]\n\n\n", min); sum=sum+min; head=pos[n]; pos[n]=999; } avg=sum/sec; printf("\n\n\nAverage Seek Time=%f", avg); getch();}

SAMPLE INPUT AND OUTPUT

SSTF Disk Scheduling

Enter the no. of Data to be read:5Enter the Sector Position of Data1:12 Enter the Sector Position of Data2: 15Enter the Sector Position of Data3: 20Enter the Sector Position of Data4: 3Enter the Sector Position of Data5: 16Enter the Position of Head: 10

SSTF Disk Scheduling

----------10------------12[Seek Time=2]---------------15[Seek Time=3]----------------16[Seek Time=1]--------------------20[Seek Time=4]---3[Seek Time=17]

Average Seek Time=5.400000

Page 32: Operating System Lab_manual

SCAN SCHEDULING

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>void space(int n){ for(int i=1;i<=n;i++) { printf("-"); } printf("%d", n);}

void main(){ clrscr(); int sec, i, pos[50], head; int seek; float avg;

printf("\n\t\tSCAN Disk Scheduling"); printf("\n\nenter the no. of Datas to be read:"); scanf("%d", &sec); for(i=2;i<=sec+1;i++) { printf("\n\nEnter the Sector Position of Data%d: ", i-1); scanf("%d",&pos[i]); if(pos[i]>75 || pos[i]<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); i--; } } label1: printf("\n\n\nEnter the Position of Head: "); scanf("%d", &head); if(head>75 || head<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); goto label1; } pos[0]=0; pos[1]=head;

Page 33: Operating System Lab_manual

sec=sec+1; clrscr(); for(i=0;i<=sec;i++) { for(int j=i+1;j<=sec;j++) { if(pos[i]>pos[j]) { int temp; temp=pos[i]; pos[i]=pos[j]; pos[j]=temp; } } }

int hpos; for(i=0;i<=sec;i++) { if(pos[i]==head) { hpos=i; } } float sum=0; printf("\t\t\tSCAN SCHEDULING"); for(i=hpos;i>=0;i--) { printf("\n\n\n"); space(pos[i]); if(i!=hpos) { seek=abs(pos[i]-pos[i+1]); printf("[SEEK TIME=%d]", seek); sum=sum+seek; } }

for(i=hpos+1;i<=sec;i++) { printf("\n\n\n"); space(pos[i]); if(i==hpos+1) { seek=pos[i]; sum=sum+seek; printf("[SEEK TIME=%d]", seek); } else

Page 34: Operating System Lab_manual

{ seek=abs(pos[i]-pos[i-1]); sum=sum+seek; printf("[SEEK TIME=%d]", seek); }

} avg=sum/(sec-1); printf("\n\nAverage Seek Time = %f", avg); getch(); }

SAMPLE INPUT AND OUTPUT

SCAN Disk Scheduling

Enter the no. of Datas to be read:5Enter the Sector Position of Data1: 20Enter the Sector Position of Data2: 12Enter the Sector Position of Data3: 34Enter the Sector Position of Data4: 25Enter the Sector Position of Data5: 3Enter the Position of Head: 18

SCAN SCHEDULING------------------18------------12[SEEK TIME=6]---3[SEEK TIME=9]0[SEEK TIME=3]--------------------20[SEEK TIME=20]-------------------------25[SEEK TIME=5]----------------------------------34[SEEK TIME=9]

Average Seek Time = 10.400000

Page 35: Operating System Lab_manual

C- SCAN SCHEDULING

SOURCE CODE

#include<stdio.h>#include<conio.h>#include<stdlib.h>void space(int n){ for(int i=1;i<=n;i++) { printf("-"); } printf("%d", n);}

void main(){ clrscr(); int sec, i, pos[50], head; int seek; float avg;

printf("\n\t\tCSCAN Disk Scheduling"); printf("\n\nenter the no. of Datas to be read:"); scanf("%d", &sec); for(i=2;i<=sec+1;i++) { printf("\n\nEnter the Sector Position of Data%d: ", i-1); scanf("%d",&pos[i]); if(pos[i]>75 || pos[i]<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); i--; } } label1: printf("\n\n\nEnter the Position of Head: "); scanf("%d", &head); if(head>75 || head<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); goto label1; } pos[0]=0; pos[1]=head;

Page 36: Operating System Lab_manual

sec=sec+2; pos[sec]=75; clrscr(); for(i=0;i<=sec;i++) { for(int j=i+1;j<=sec;j++) { if(pos[i]>pos[j]) { int temp; temp=pos[i]; pos[i]=pos[j]; pos[j]=temp; } } }

int hpos; for(i=0;i<=sec;i++) { if(pos[i]==head) { hpos=i; } } float sum=0; printf("\t\t\tCSCAN SCHEDULING"); for(i=hpos;i<=sec;i++) { printf("\n\n\n"); space(pos[i]); if(i!=hpos) { seek=abs(pos[i]-pos[i-1]); printf("[SEEK TIME=%d]", seek); sum=sum+seek; } }

for(i=0;i<=hpos-1;i++) { printf("\n\n\n"); space(pos[i]); if(i==0) { sum=sum+75; printf("[SEEK TIME=%d]", seek); } else

Page 37: Operating System Lab_manual

{ seek=abs(pos[i]-pos[i-1]); sum=sum+seek; printf("[SEEK TIME=%d]", seek); } } avg=sum/(sec-1); printf("\n\nAverage Seek Time = %f", avg); getch(); }

SAMPLE INPUT AND OUTPUT

CSCAN Disk Scheduling

Enter the no. of Datas to be read:5Enter the Sector Position of Data1: 12Enter the Sector Position of Data2: 30Enter the Sector Position of Data3: 25Enter the Sector Position of Data4: 20Enter the Sector Position of Data5: 8Enter the Position of Head:

CSCAN DISK SCHEDULING

----------------------22-------------------------25[SEEK TIME=3]------------------------------30[SEEK TIME=5]---------------------------------------------------------------------------75[SEEK TIME=45]0[SEEK TIME=45]--------8[SEEK TIME=8]------------12[SEEK TIME=4]--------------------20[SEEK TIME=8]

Average Seek Time = 24.666666

Page 38: Operating System Lab_manual

C-LOOK SCHEDULING

SOURCE CODE

#include<stdio.h>#include<conio.h>#include<stdlib.h>void space(int n){ for(int i=1;i<=n;i++) { printf("-"); } printf("%d", n);}

void main(){ clrscr(); int sec, i, pos[50], head; int seek; float avg;

printf("\n\t\tCLOOK Disk Scheduling"); printf("\n\nenter the no. of Datas to be read:"); scanf("%d", &sec); for(i=1;i<=sec;i++) { printf("\n\nEnter the Sector Position of Data%d: ", i); scanf("%d",&pos[i]); if(pos[i]>75 || pos[i]<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); i--; } } label1: printf("\n\n\nEnter the Position of Head: "); scanf("%d", &head); if(head>75 || head<0) { printf("\nWarning!!! Sector Position Exeeded\nEnter Value between 0-75\n"); goto label1; } pos[0]=head; clrscr();

Page 39: Operating System Lab_manual

for(i=0;i<=sec;i++) { for(int j=i+1;j<=sec;j++) { if(pos[i]>pos[j]) { int temp; temp=pos[i]; pos[i]=pos[j]; pos[j]=temp; } } } int hpos; for(i=0;i<=sec;i++) { if(pos[i]==head) { hpos=i; } } float sum=0; printf("\t\t\tCLOOK SCHEDULING"); for(i=hpos;i<=sec;i++) { printf("\n\n\n"); space(pos[i]); if(i!=hpos) { seek=abs(pos[i]-pos[i-1]); printf("[SEEK TIME=%d]", seek); sum=sum+seek; } }

for(i=0;i<=hpos-1;i++) { printf("\n\n\n"); space(pos[i]); if(i==0) { seek=abs(pos[sec]-pos[0]); sum=sum+seek; printf("[SEEK TIME=%d]", seek); } else { seek=abs(pos[i]-pos[i-1]); sum=sum+seek;

Page 40: Operating System Lab_manual

printf("[SEEK TIME=%d]", seek); }

} avg=sum/(sec-1); printf("\n\nAverage Seek Time = %f", avg); getch(); }

SAMPLE INPUT AND OUTPUT

CLOOK Disk Scheduling

Enter the no. of Datas to be read:5Enter the Sector Position of Data1: 12Enter the Sector Position of Data2: 35Enter the Sector Position of Data3: 20Enter the Sector Position of Data4: 8Enter the Sector Position of Data5: 25Enter the Position of Head: 18

CLOOK SCHEDULING

------------------18--------------------20[SEEK TIME=2]-------------------------25[SEEK TIME=5]-----------------------------------35[SEEK TIME=10]--------8[SEEK TIME=27]------------12[SEEK TIME=4]

Average Seek Time = 12.000000

STUDENT, 03/29/11,
Page 41: Operating System Lab_manual

PRODUCER - CONSUMER PROBLEM

SOURCE CODE

#include<stdio.h>#include<conio.h>void main(){ clrscr(); int s, count=0; do { clrscr(); printf("\n\n\t\t\tPRODUCER - CONSUMER\n\n"); if(count<1) { printf("\n\nCURRENT BUFFER SIZE: EMPTY\n\n"); } else if(count>5) { printf("\n\nCURRENT BUFFER SIZE: FULL\n\n"); } else { printf("\n\nCURRENT BUFFER SIZE: %d\n\n", count); } printf("\nEnter the Next process:\n\n\t1: Producer\n\t2: Consumer\n\t3: Exit"); printf("\t\t\t:"); scanf("%d", &s); if(count<1 && s==2) { printf("\n\nSorry!!! Buffer Empty, cannot consume"); }

else if(count>5 && s==1) { printf("\n\nSorry!!! Buffer full, cannot produce"); }

else if(s==1) { count++; printf("\n\n1 Item added to buffer"); } else if(s==2) {

Page 42: Operating System Lab_manual

count--; printf("\n\n1 item consumed from buffer"); } else { printf("\nWRONG CHOICE!!!!"); } if(s!=3) { printf("\n\nPress ENTER"); getch(); } }while(s!=3);}

SAMPLE INPUT AND OUTPUT

PRODUCER - CONSUMER

CURRENT BUFFER SIZE: EMPTYEnter the Next process:

1: Producer2: Consumer3: Exit :2

Sorry!!! Buffer Empty, cannot consumePress ENTER

PRODUCER - CONSUMER

CURRENT BUFFER SIZE: EMPTYEnter the Next process:

1: Producer2: Consumer3: Exit :1

1 Item added to bufferPress ENTER

Page 43: Operating System Lab_manual

READER-WRITER PROBLEM

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<dos.h>void writer(int a[], int n){ int time; printf("\n\n\n\t\tWRITERS STARTED WRITING"); for(int i=1;i<=n;i++) { printf("\n\nwriter %d in progress...", i); time=a[i]*1000; delay(time); } if(n!=0)printf("\n\n\t\tUpdation Done...");}

void reader(int a[], int n){ int time, maxt=a[1]; for(int i=2;i<=n;i++) { if(a[i]>maxt) { maxt=a[i]; } } printf("\n\n\n\t\tREADERS STARTED READING"); for(i=1;i<=maxt;i++) { delay(1000); for(int j=1;j<=n;j++) { if(a[j]==i) { printf("\n\nReader %d finished Reading..", j); } } } printf("\n\n\n\t\tReading Done...");}void main(){

Page 44: Operating System Lab_manual

clrscr(); int n, time, r[20], w[20]; int rtemp=0, wtemp=0; char c; printf("\n\t\tREADERS-WRITERS PROBLEM(WRITERS PRIORITY)"); printf("\n\nEnter the no of Readers & Writers going to Access the file:"); scanf("%d", &n); printf("\n\n"); for(int i=1;i<=n;i++) { fflush(stdin); printf("\n%d : Reader(r) or Writer(w) :", i); scanf("%c", &c); if(c!='r' && c!='w') { printf("\nWrong choice:Press Reader(r) or Writer(w)\n\n"); i--; } else { printf("\n Enter the time needed:"); scanf("%d", &time); } if(c=='r') { rtemp++; r[rtemp]=time; } else if(c=='w') { wtemp++; w[wtemp]=time; } }clrscr();printf("\n\t\tREADERS-WRITERS PROBLEM(WRITERS PRIORITY)");printf("\n\n%d Readers", rtemp);for(i=1;i<=rtemp;i++) { printf("\n\n\tReader %d : %d seconds", i, r[i]); }printf("\n\n%d Writers", wtemp); for(i=1;i<=wtemp;i++) { printf("\n\n\twriter %d : %d seconds", i, w[i]); } getch(); writer(w, wtemp); reader(r, rtemp);

Page 45: Operating System Lab_manual

getch();}

SAMPLE INPUT AND OUTPUT:

READERS-WRITERS PROBLEM (WRITERS PRIORITY)

Enter the no of Readers & Writers going to Access the file:4

1 : Reader(r) or Writer(w) :r Enter the time needed:102 : Reader(r) or Writer(w) :w Enter the time needed:53 : Reader(r) or Writer(w) :r Enter the time needed:84 : Reader(r) or Writer(w) :w Enter the time needed:4

READERS-WRITERS PROBLEM(WRITERS PRIORITY)

2 ReadersReader 1 : 10 secondsReader 2 : 8 seconds

2 Writerswriter 1 : 5 secondswriter 2 : 4 seconds

WRITERS STARTED WRITING

writer 1 in progress...writer 2 in progress...

Updation Done...

READERS STARTED READING

Reader 2 finished Reading..Reader 1 finished Reading..

Reading Done...

Page 46: Operating System Lab_manual

DINING PHILOSOPHERS PROBLEM

SOURCE CODE

#include<stdio.h>#include<conio.h>#define MAX 50struct philosopher{ int left; int right; int status;}p[MAX];int n;void display(int n){ int i=1; printf("\n\n\t\tDINING PHILOSOPHERS PROBLEM\n\n"); printf("\n\nStatus of the philosophers"); printf("(In order from 1 to %d)\n\n\n", n); for(i=1;i<=n;i++) { if(p[i].status==1) printf("Hungry "); else if(p[i].status==2) printf("Eating "); else if(p[i].status==3) printf("Thinking "); } printf("\n");}

void takefork(int i, int n){ if(i==1) { if(p[i].status==1&&p[i+1].status!=2&&p[n].status!=2) p[i].status=2; else printf("\n\nPhilosopher %d cannot eat\n",i); } else if(i==n) { if(p[i].status==1&&p[i-1].status!=2&&p[1].status!=2) p[i].status=2; else

Page 47: Operating System Lab_manual

printf("\n\nPhilosopher %d cannot eat\n",i); } else { if(p[i].status==1&&p[i-1].status!=2&&p[i+1].status!=2) p[i].status=2; else printf("\n\nPhilosopher %d cannot eat\n",i); } return;}void putfork(int i){ p[i].status=3;}void main(){ int i; int count=0; int k; clrscr(); printf("\nEnter the total number of philosophers:\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(i==1) { p[i].left=n; p[i].right=i+1; p[i].status=3; } if(i==n) { p[i].left=i-1; p[i].right=1; p[i].status=3; } else { p[i].left=i-1; p[i].right=i+1; p[i].status=3; } } clrscr(); display(n); do { count=0;

Page 48: Operating System Lab_manual

for(i=1;i<=n;i++) { if(p[i].status==2) count++; } if(count<(n/2)) { printf("\n\n\nEnter the philosopher who wants to eat(Enter 0 to quit):\n"); scanf("%d",&k); if(p[k].status==3) { clrscr(); p[k].status=1; } else if(p[k].status==2) { clrscr(); printf("\n\n\nThe philosopher is already in eating state only\n"); } else { clrscr(); takefork(k, n); } display(n); } else { printf("\n\n\nEnter the philosopher who wants to put his fork down(Enter 0 to

quit):\n"); scanf("%d",&k); if(p[k].status==2) { clrscr(); putfork(k); } display(n); } }while(k!=0); getch();}

SAMPLE INPUT AND OUTPUT

Enter the total number of philosophers:5

DINING PHILOSOPHERS PROBLEM

Page 49: Operating System Lab_manual

Status of the philosophers(In order from 1 to 5)

Thinking Thinking Thinking Thinking Thinking

Enter the philosopher who wants to eat(Enter 0 to quit):3

DINING PHILOSOPHERS PROBLEM

Status of the philosophers(In order from 1 to 5)

Thinking Thinking Hungry Thinking Thinking

Enter the philosopher who wants to eat(Enter 0 to quit):2

DINING PHILOSOPHERS PROBLEM

Status of the philosophers(In order from 1 to 5)

Thinking Hungry Hungry Thinking Thinking

Enter the philosopher who wants to eat(Enter 0 to quit):3

DINING PHILOSOPHERS PROBLEM

Status of the philosophers(In order from 1 to 5)

Thinking Hungry Eating Thinking Thinking

Enter the philosopher who wants to eat(Enter 0 to quit):2

Philosopher 2 cannot eat

Page 50: Operating System Lab_manual

STORAGE PLACEMENT

SOURCE CODE:

#include<stdio.h>#include<conio.h>#define MAX 50struct blockdetails{ int bno; int ospace; int mspace;}block[MAX];struct jobdetails{ int jno; int space; int status;}job[MAX];int m,n;void allocateblock(){ int i; printf("Enter the total number of blocks:\n"); scanf("%d",&n); for(i=0;i<n;i++) { block[i].bno=i+1; printf("Block number = %d\n",block[i].bno); printf("Enter block space:\n"); scanf("%d",&block[i].ospace); }}void allocatejob(){ int i; printf("Enter the total number of jobs:\n"); scanf("%d",&m); for(i=0;i<m;i++) { job[i].jno=i+1; printf("Job number = %d\n",job[i].jno); printf("Enter job space:\n"); scanf("%d",&job[i].space); job[i].status=0;

Page 51: Operating System Lab_manual

} clrscr(); printf("Block number\tBlock Space\n"); for(i=0;i<n;i++) printf("%d\t\t%d\n",block[i].bno,block[i].ospace);

printf("\n\nJob number\tJob Space\n"); for(i=0;i<m;i++) printf("%d\t\t%d\n",job[i].jno,job[i].space);}void firstfit(){ int i,j; for(i=0;i<n;i++) block[i].mspace=block[i].ospace; for(j=0;j<m;j++) { for(i=0;i<n;i++) { if(job[j].space<=block[i].mspace) { printf("Job %d is allocated in Block %d\n",job[j].jno,block[i].bno); job[j].status=1; block[i].mspace=block[i].mspace-job[j].space; break; } } } for(j=0;j<m;j++) { if(job[j].status==0) printf("Job %d is not allocated in any block\n",job[j].jno); }}void bestfit(){ int i,j; struct blockdetails temp;

for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(block[i].ospace>block[j].ospace) { temp=block[i]; block[i]=block[j]; block[j]=temp; }

Page 52: Operating System Lab_manual

} } firstfit();}void worstfit(){ int i,j; struct blockdetails temp;

for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(block[i].ospace<block[j].ospace) { temp=block[i]; block[i]=block[j]; block[j]=temp; } } } firstfit();}void main(){ clrscr(); allocateblock(); allocatejob(); printf("\n\nFirst fit\n"); firstfit(); printf("\n\nBest fit\n"); bestfit(); printf("\n\nWorst fit\n"); worstfit(); getch();}

SAMPLE INPUT AND OUTPUT

Enter the total number of blocks:3Block number = 1Enter block space:6Block number = 2Enter block space:4Block number = 3Enter block space:8Enter the total number of jobs:3Job number = 1

Page 53: Operating System Lab_manual

Enter job space:4Job number = 2Enter job space:6Job number = 3Enter job space:8Block number Block Space

1 62 43 8

Job number Job Space1 42 63 8

First fitJob 1 is allocated in Block 1Job 2 is allocated in Block 3Job 3 is not allocated in any block

Best fitJob 1 is allocated in Block 2Job 2 is allocated in Block 1Job 3 is allocated in Block 3

Worst fitJob 1 is allocated in Block 3Job 2 is allocated in Block 1Job 3 is not allocated in any block

Page 54: Operating System Lab_manual

PAGING REPLACEMENT

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<malloc.h>struct frame{ int frameno; int usagerate; struct frame *next; struct frame *prev;}*head,*tail,*n,*t;int max;int m=0;void fifo();void lru();void allocateframe(int num){ int i,count; int ch; for(i=0;i<num&&m<max;i++) { if(m==max) break; n=(struct frame *)malloc(sizeof(struct frame)); m++; n->frameno=m; printf("Frame number = %d\n",n->frameno); printf("Enter frame usage rate:\n"); scanf("%d",&n->usagerate); if(head==NULL) { head=n; tail=n; head->prev=NULL; tail->next=NULL; } else { tail->next=n; n->prev=tail; tail=n;

Page 55: Operating System Lab_manual

tail->next=NULL; } count=i+1; printf("Count = %d\n",count); } printf("Frame details\n"); for(t=head;t!=NULL;t=t->next) printf("%d\t\t%d\n",t->frameno,t->usagerate); if(m==max) { printf("Page frame cannot be added further\n"); printf("Need replacement\n"); do { label: printf("Specify one of the following options:\n"); printf("1.FIFO Replacement\n"); printf("2.LRU Replacement\n"); scanf("%d",&ch); switch(ch) { case 1: fifo(); break; case 2: lru(); break; default: printf("Invalid choice\n"); goto label; } count++; }while(count<num); } printf("Frame details\n"); for(t=head;t!=NULL;t=t->next) printf("%d\t\t%d\n",t->frameno,t->usagerate);}void fifo(){ struct frame *temp; if(head!=NULL) { temp=head; head=head->next; head->prev=NULL; free(temp); n=(struct frame *)malloc(sizeof(struct frame)); m++;

Page 56: Operating System Lab_manual

n->frameno=m; printf("Frame number = %d\n",n->frameno); printf("Enter usage rate of the frame:\n"); scanf("%d",&n->usagerate); tail->next=n; n->prev=tail; tail=n; tail->next=NULL; printf("Frame details\n"); for(t=head;t!=NULL;t=t->next) printf("%d\t\t%d\n",t->frameno,t->usagerate); } else printf("FIFO Replacement not possible\n"); return;}void lru(){ int pos,min=head->usagerate; struct frame *temp; if(head!=NULL) { for(t=head;t!=NULL;t=t->next) { if(min>t->usagerate) min=t->usagerate; } printf("Minimum = %d\n",min); for(t=head;t!=NULL;t=t->next) { if(min==t->usagerate) { if(t==head) { printf("Deleted frame = %d\n",t->frameno); temp=head; head=head->next; head->prev=NULL; free(temp); } else if(t==tail) { printf("Deleted frame = %d\n",t->frameno); temp=tail; tail=tail->prev; tail->next=NULL; free(temp); } else

Page 57: Operating System Lab_manual

{ printf("Deleted frame = %d\n",t->frameno); temp=t; t->prev->next=t->next; t->next->prev=t->prev; free(temp); } } } n=(struct frame *)malloc(sizeof(struct frame)); m++; n->frameno=m; printf("Frame number = %d\n",n->frameno); printf("Enter usage rate of the frame:\n"); scanf("%d",&n->usagerate); tail->next=n; n->next=tail; tail=n; tail->next=NULL; printf("Frame details\n"); for(t=head;t!=NULL;t=t->next) printf("%d\t\t%d\n",t->frameno,t->usagerate); } return;}void main(){ int choice; char ans; int number; clrscr(); printf("Enter the maximum number of frames:\n"); scanf("%d",&max); do { label: printf("Specify any one of the following choices:\n"); printf("1.Allocate frames\n"); printf("2.FIFO Replacement\n"); printf("3.LRU Replacement\n"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the number of frames added\n"); scanf("%d",&number); allocateframe(number); break; case 2:

Page 58: Operating System Lab_manual

fifo(); break; case 3: lru(); break; default: printf("Invalid choice\n"); goto label; } printf("Do you want to continue y/n:\n"); fflush(stdin); scanf("%c",&ans); }while(ans=='y'||ans=='Y'); getch();}

SAMPLE INPUT AND OUTPUT

Enter the maximum number of frames:

Specify any one of the following choices:1.Allocate frames2.FIFO Replacement3.LRU Replacement

Enter the number of frames addedFrame number = 1Enter frame usage rate:Count = 1Frame number = 2Enter frame usage rate:Count = 2Frame number = 3Enter frame usage rate:Count = 3Frame details1 62 53 7Frame details1 62 53 7

Do you want to continue y/n:y

Specify any one of the following choices:1.Allocate frames2.FIFO Replacement

Page 59: Operating System Lab_manual

3.LRU ReplacementFrame number = 4Enter usage rate of the frame:Frame details2 53 74 3Do you want to continue y/n:y

Specify any one of the following choices:1.Allocate frames2.FIFO Replacement3.LRU ReplacementMinimum = 3Deleted frame = 4Frame number = 5

Enter usage rate of the frame:2Frame details2 53 75 2

Do you want to continue y/n:n

Page 60: Operating System Lab_manual

BANKERS ALGORITHM

SOURCE CODE:

#include<stdio.h>#include<conio.h>#include<dos.h>#include<stdlib.h>#define MAX 50int m,n;int finish[MAX];int allocation[MAX][MAX];int maximum[MAX][MAX];int need[MAX][MAX];int available[MAX];int work[MAX];void resourcerequest();void getdetails(){ int i,j; printf("Enter the number of processes:\n"); scanf("%d",&n); printf("Enter the number of resources:\n"); scanf("%d",&m); printf("Enter the allocation matrix:\n"); for(i=0;i<n;i++) { finish[i]=0; for(j=0;j<m;j++) scanf("%d",&allocation[i][j]); } printf("Enter the maximum allocation matrix:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) scanf("%d",&maximum[i][j]); } printf("Need matrix\n"); for(i=0;i<n;i++) {

Page 61: Operating System Lab_manual

for(j=0;j<m;j++) { need[i][j]=maximum[i][j]-allocation[i][j]; printf("%d ",need[i][j]); } printf("\n"); } printf("Enter the available matrix:\n"); for(i=0;i<m;i++) { scanf("%d",&available[i]); work[i]=available[i]; }}void safety(){ int i,j,k; int safe=0; printf("Safety Sequence\n"); label:

for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(finish[i]==0&&need[i][j]<=work[j]) continue; else break; } if(j==m) { for(j=0;j<m;j++) work[j]=work[j]+allocation[i][j]; finish[i]=1; printf("%d\n",i); goto label; } else { for(k=0;k<n;k++) { if(finish[k]==1) continue;

Page 62: Operating System Lab_manual

else break; } if(k==n) { printf("System is in safe state\n"); { safe=1; resourcerequest(); } } } } if(safe==0) { printf("System is in unsafe state\n"); delay(2000); exit(0); }}void resourcerequest(){ int request[MAX]; int pno; int i; printf("Enter the process number:\n"); scanf("%d",&pno); printf("Enter the resource vector for the process:\n"); for(i=0;i<m;i++) scanf("%d",&request[i]); for(i=0;i<m;i++) { if(request[i]<=need[pno][i]) { if(request[i]<=available[i]) { available[i]=available[i]-request[i]; allocation[pno][i]=allocation[pno][i]+request[i]; need[pno][i]=need[pno][i]-request[i]; } else break; }

Page 63: Operating System Lab_manual

else { printf("Process Request has exceeded the maximum claim\n"); printf("Erroneous - May result in unsafe state\n"); delay(2000); exit(0); } } for(i=0;i<n;i++) finish[i]=0; for(i=0;i<m;i++) work[i]=available[i]; safety();}void main(){ clrscr(); getdetails(); safety(); getch();}

SAMPLE INPUT AND OUTPUT

Enter the number of processes:2Enter the number of resources:2Enter the allocation matrix:3 33 3

Enter the maximum allocation matrix:4 44 4

Need matrix1 1 1 1

Enter the available matrix: 8 8

Safety Sequence01

Page 64: Operating System Lab_manual

System is in safe state

Enter the process number:1Enter the resource vector for the process:1 1

Safety Sequence01System is in safe state

Enter the process number:2

Enter the resource vector for the process:1 1

Process Request has exceeded the maximum claimErroneous - May result in unsafe state

Page 65: Operating System Lab_manual

SIMULATION OF FILE SYSTEMS.

SOURCE CODE

#include<stdio.h>#include<conio.h>#include<string.h>char fname[20], str[100], ch=0, ch1;FILE *fp;int handle;int m, pos2, k, pos1;

void fcreate(){ printf("\n\nEnter the File Name with extension: "); scanf("%s", fname); fp = fopen(fname, "w"); fflush(stdin); printf("\nEnter the Data:\n"); gets(str); fprintf(fp, "%s", str); handle = fileno(fp); int len=strlen(fname); int flag=0; printf("\n File created Sucessfully\n\n"); printf("\n\n\t\tFile Attributes\n\n"); for(int i=0;i<=len;i++) { if(fname[i]=='.') { flag = i; } }printf("\nFile Name :");for(i=0;i<=flag-1;i++){ printf("%c", fname[i]);}printf("\nFile Type :");for(i=flag+1;i<=len;i++)

Page 66: Operating System Lab_manual

{ printf("%c", fname[i]);} printf("\nFile Identifier:%d", handle);fclose(fp);}

void fwrite(){ fp=fopen(fname, "a"); printf("\nFile Opened for writing:\n"); fflush(stdin); printf("\nEnter the Data:\n"); gets(str); fprintf(fp, "%s", str); fclose(fp);}

void fread(){ fp=fopen(fname, "r"); printf("\nEnter your choice of Acess Methods:\n\t1: Sequential \n\t2: Random :"); scanf("%d", &m); if(m==1) { printf("\n Enter the Position till you want to read:"); scanf("%d", &pos2); rewind(fp); while(pos2!=ftell(fp)) { char c=getc(fp); printf("%c", c); } } else if(m==2) { printf("\n Enter the Start Position you want to read:"); scanf("%d", &pos1); fseek(fp,pos1, 0); printf("\n Enter the Position till you want to read:"); scanf("%d", &pos2); while(pos2!=ftell(fp)) {

Page 67: Operating System Lab_manual

char c=getc(fp); printf("%c", c); } } fclose(fp);}

void delfile(){if (remove(fname) == 0){ printf("Removed %s.\n",fname);}else{ perror("remove");}}

void main(){clrscr();printf("\n\tFile Creation");fcreate();getch();clrscr();

do{if(ch==3){fcreate();}printf("\n\nFile Operations\n\n");fflush(stdin);printf("\n\n1:Write Data\n2:Read Data\n3:Delete File");printf("\nEnter your Choice: ");scanf("%d", &ch);

switch(ch){ case 1: fwrite();

Page 68: Operating System Lab_manual

break; case 2: fread(); break; case 3: delfile(); break; default: printf("\nWrng Choice");}fflush(stdin);printf("\nDo you Want to Continue(y/n):");scanf("%c", &ch1);}while(ch1=='y');getch();}

SAMPLE INPUT AND OUTPUT

File Creation

Enter the File Name with extension: file1.txtEnter the Data:Hello world

File created Sucessfully

File Attributes

File Name :file1File Type :txt File Identifier:5

File Operations

1:Write Data2:Read Data3:Delete FileEnter your Choice: 1File Opened for writing:Enter the Data: How are you

Do you Want to Continue(y/n):y

Page 69: Operating System Lab_manual

File Operations

1:Write Data2:Read Data3:Delete FileEnter your Choice: 2Enter your choice of Access Methods:

1: Sequential 2: Random :2

Enter the Start Position you want to read:5 Enter the Position till you want to read:10

worldhow

Do you Want to Continue(y/n):y

File Operations

1:Write Data2:Read Data3:Delete FileEnter your Choice:3

Removed file1.txt.

Do you Want to Continue(y/n):n