Operating System Lab - Manual
Operating System Lab - Manual
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 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)
COMPARE TWO STRINGS SOURCE CODE: clear echo "OUTPUT:" echo "Enter first string" read str1 echo "Enter second string" read str2 if [ $str1 = str2] then echo "Strings are equal" else echo"String are not equal" fi SAMPLE INPUT AND OUTPUT: Enter first string Operating Enter second string operating Strings are equal
REVERSE A STRING SOURCE CODE clear echo "OUTPUT:" echo "Enter the string" read str len='echo $str| wc -c' while [$len -gt 0 ] do temp='echo $str | cut -c $len' rev= 'echo $rev$temp' len='expr $len - 1' done echo "The reverse string is $rev"
SAMPLE INPUT AND OUTPUT: Enter the string Computer The reverse string retupmoc
FIBONACCI SERIES SOURCE CODE: clear echo "OUTPUT" echo "Enter the limit" read n echo "The fibonacci series" b=0 c=1 d=0 i=0 if [ $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)) done else echo "$b" fi SAMPLE INPUT AND OUTPUT: Enter the limit 5 The fibonacci series 0 1 1 2 3
SWAPPING TWO NUMBERS SOURCE CODE: clear echo "OUTPUT:" echo "Enter first number to be swapped" read a echo "Enter second number to be swapped' read b echo "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 12 Enter second number to be swapped 21 Before swapping A=12 B=21 After swapping A=21 B=12
SUM OF DIGITS SOURCE CODE: clear echo "OUTPUT:" echo "Enter an interger" read num sum=0 digit=0 while [ $num -gt 0 ] do a=$(($num%10)) digit=$(($digit+1)) sum=$(($sum+$a)) num=$(($num/10)) done echo "The no of digits in the integer is $digit" echo "The sum of digits is $sum" SAMPLE INPUT AND OUTPUT: Enter an integer 1234 The no of digits in the integer is 4 The sum of digits is 10
CONVERT LOWER TO UPPER SOURCE CODE: clear echo "OUTPUT:" echo "Enter a string" read str temp='echo $str | tr [a-z] [A-Z]' echo "The case changed string is $temp"
SAMPLE INPUT AND OUTPUT: Enter a string system The case changed string is SYSTEM
CURRENTLY LOGGED IN OR NOT SOURCE CODE: clear echo "OUTPUT:" echo "Enter the name of the user:" read name who>temp if ( 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: Anu Anu is currently logged in
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.out HELLO CHILD PROCESS IS EXECUTED THE ID NUMBER OF THE CHILD PROCESS IS 3150 HELLO PARENT PROCESS IS EXECUTED THE ID NUMBER OF PARENT PROCESS IS 3149
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.out HELLO CHILD PROCESS IS EXECUTED THE ID NUMBER OF THE CHILD PROCESS IS 3206 HELLO PARENT PROCESS ISRUNNING THE ID NUMBER OF PARENT PROCESS IS 2964
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"); } } }
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:..:
FILE SYSTEM CALL USING OPEN AND CLOSE SOURCE CODE: #include<stdio.h> #define size 10 main (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); }
SAMPLE INPUT AND OUTPUT: [iiiit01@localhost cpro]$ cc pro5.c [iiiit01@localhost cpro]$./a.out output peruout File completed [iiiit01@localhost cpro]$ vi peruout Hi how are u
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-r1 iiiit08 iiiit08 709 jan 1 1999 fc.c -rw-rw-r1 iiiit08 iiiit08 421 jan 1 00:46 fcfs.c -rw-rw-r1 iiiit08 iiiit08 128 jan 1 00:54 ls.c -rw-rw-r1 iiiit08 iiiit08 76 jan 1 1999 output.c -rw-rw-r1 iiiit08 iiiit08 0 jan 1 00:47 pi.c -rw-rw-r1 iiiit08 iiiit08 0 jan 1 00:47 pi.c -rw-rw-r1 iiiit08 iiiit08 1180 jan 1 00:49 rr.c -rw-rw-r1 iiiit08 iiiit08 766 jan 1 00:49 sjf.c -rw-rw-r1 iiiit08 iiiit08 178 jan 1 00:53 stf.c -rw-rw-r1 iiiit08 iiiit08 258 jan 21 2001 wait.c
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 IN Hello world, how are u?
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; printf("\n\n\t\aaverage waiting Time:\a\a%f", avg); getch();} SAMPLE INPUT AND OUTPUT: First come First Serve Enter Enter Enter Enter the the the the No of process: 3 Burst time for Process 1: 6 Burst time for Process 2: 3 Burst time for Process 3: 4 Before FCFS Scheduling Process 1 2 3 Burst Time 6 3 4
After FCFS Scheduling Process 1 2 3 Average waiting Time: 5 Burst Time 6 3 4 Waiting Time 0 6 9
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) { 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 Enter Enter Enter Enter Shortest Job First No of process:3 Burst time for Process 1: 6 Burst time for Process 2: 1 Burst time for Process 3: 2 Before SJF Scheduling Process Burst Time 1 6 2 1 3 2 After SJF Scheduling Process Burst Time Turn Around Time 2 1 0 3 2 1 1 6 3 the the the the Average waiting Time:1.0
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; } } } 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 Scheduling Enter Enter Enter Enter Enter Enter Enter the the the the the the the No of process: Burst time for Process 1: Priority for Process 1: Burst time for Process 2: Priority for Process 2: Burst time for Process 3: Priority for Process 3: Priority 2 1 3 Before Priority Scheduling Burst Time 4 5 6
Process 1 2 3
After Priority Scheduling Process Priority Burst Time Turn Around Time 2 1 5 0 1 2 4 5 3 3 6 9 Average waiting Time:4.0
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]); } 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(); }
SAMPLE INPUT AND OUTPUT: Round Robin Scheduling Enter the No. of Process:3 Enter the Burst time for process1: 10 Enter the Burst time for process2: 6 Enter the Burst time for process3: 3 After Round Robin Scheduling Process No. 1 2 3 1 2 Burst Time waiting time 10 0 6 3 5 1 5 10 13 18 Remaining 5 1 0 0 0
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); } 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 Enter Enter Enter Enter Enter Enter the the the the the the the no. of Datas to be read: 5 Sector Position of Data1: 10 Sector Position of Data2: 20 Sector Position of Data3: 25 Sector Position of Data4: 8 Sector Position of Data5: 30 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]
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); 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 Enter Enter Enter Enter Enter Enter the the the the the the the no. of Data to be read:5 Sector Position of Data1:12 Sector Position of Data2: 15 Sector Position of Data3: 20 Sector Position of Data4: 3 Sector Position of Data5: 16 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
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 075\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 075\n"); goto label1; } pos[0]=0; pos[1]=head; 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 { 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 Enter Enter Enter Enter Enter Enter the the the the the the the no. of Datas to be read:5 Sector Position of Data1: 20 Sector Position of Data2: 12 Sector Position of Data3: 34 Sector Position of Data4: 25 Sector Position of Data5: 3 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
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 075\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 075\n"); goto label1; } pos[0]=0; pos[1]=head; 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 { 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 Enter Enter Enter Enter Enter Enter the the the the the the the no. of Datas to be read:5 Sector Position of Data1: 12 Sector Position of Data2: 30 Sector Position of Data3: 25 Sector Position of Data4: 20 Sector Position of Data5: 8 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
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 075\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 075\n"); goto label1; } pos[0]=head; 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\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; 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 Enter Enter Enter Enter Enter Enter the the the the the the the no. of Datas to be read:5 Sector Position of Data1: 12 Sector Position of Data2: 35 Sector Position of Data3: 20 Sector Position of Data4: 8 Sector Position of Data5: 25 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
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) { 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: EMPTY Enter the Next process: 1: Producer 2: Consumer 3: Exit :2 Sorry!!! Buffer Empty, cannot consume Press ENTER PRODUCER - CONSUMER CURRENT BUFFER SIZE: EMPTY Enter the Next process: 1: Producer 2: Consumer 3: Exit :1 1 Item added to buffer Press ENTER
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() { 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); 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:10 2 : Reader(r) or Writer(w) :w Enter the time needed:5 3 : Reader(r) or Writer(w) :r Enter the time needed:8 4 : Reader(r) or Writer(w) :w Enter the time needed:4 READERS-WRITERS PROBLEM(WRITERS PRIORITY) 2 Readers Reader 1 : 10 seconds Reader 2 : 8 seconds 2 Writers writer 1 : 5 seconds writer 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...
DINING PHILOSOPHERS PROBLEM SOURCE CODE #include<stdio.h> #include<conio.h> #define MAX 50 struct 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 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; 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 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
STORAGE PLACEMENT SOURCE CODE: #include<stdio.h> #include<conio.h> #define MAX 50 struct 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; } 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; } } } 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:3 Block number = 1 Enter block space:6 Block number = 2 Enter block space:4 Block number = 3 Enter block space:8 Enter the total number of jobs:3 Job number = 1 Enter job space:4 Job number = 2 Enter job space:6 Job number = 3 Enter job space:8 Block number Block Space 1 6 2 4 3 8 Job number 1 2 3 Job Space 4 6 8
First fit Job 1 is allocated in Block 1 Job 2 is allocated in Block 3 Job 3 is not allocated in any block Best fit Job 1 is allocated in Block 2 Job 2 is allocated in Block 1 Job 3 is allocated in Block 3 Worst fit Job 1 is allocated in Block 3 Job 2 is allocated in Block 1 Job 3 is not allocated in any block
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; 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++; 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 { 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: 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 frames 2.FIFO Replacement 3.LRU Replacement Enter the number of frames added Frame number = 1 Enter frame usage rate: Count = 1 Frame number = 2 Enter frame usage rate: Count = 2 Frame number = 3 Enter frame usage rate: Count = 3 Frame details 1 6
2 5 3 7 Frame details 1 6 2 5 3 7 Do you want to continue y/n:y Specify any one of the following choices: 1.Allocate frames 2.FIFO Replacement 3.LRU Replacement Frame number = 4 Enter usage rate of the frame: Frame details 2 5 3 7 4 3 Do you want to continue y/n:y Specify any one of the following choices: 1.Allocate frames 2.FIFO Replacement 3.LRU Replacement Minimum = 3 Deleted frame = 4 Frame number = 5 Enter usage rate of the frame:2 Frame details 2 5 3 7 5 2 Do you want to continue y/n: n
BANKERS ALGORITHM SOURCE CODE: #include<stdio.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #define MAX 50 int 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++) { 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; 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; } 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:2 Enter the number of resources:2 Enter the allocation matrix: 33 33 Enter the maximum allocation matrix: 44 44
Need matrix 11 11 Enter the available matrix: 8 8 Safety Sequence 0 1 System is in safe state Enter the process number:1 Enter the resource vector for the process:1 1 Safety Sequence 0 1 System is in safe state Enter the process number:2 Enter the resource vector for the process:1 1 Process Request has exceeded the maximum claim Erroneous - May result in unsafe state
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++) { 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)) { 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(); 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.txt Enter the Data:Hello world File created Sucessfully File Attributes File Name :file1 File Type :txt File Identifier:5
File Operations 1:Write Data 2:Read Data 3:Delete File Enter your Choice: 1 File Opened for writing: Enter the Data: How are you Do you Want to Continue(y/n):y
File Operations 1:Write Data 2:Read Data 3:Delete File Enter your Choice: 2 Enter 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 Data 2:Read Data 3:Delete File Enter your Choice:3 Removed file1.txt. Do you Want to Continue(y/n):n