Os Lab Completed
Os Lab Completed
OPERATING SYSTEMS
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT : Fork domain the parent ( Pid=4124) I am the child (cid=4125)of(pid=4124) 0 Parent waiting here for child 1 2 3 4 5 6 7 8 9 Child finished parent quiting too
CS 2257 LAB
OPERATING SYSTEMS
PROGRAM #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> main() { int fd1,fd2,n; char source[30],ch[5]; struct stat s,t,w; fd1=creat("test.txt",0644); printf("Enter the file to be copied\n"); scanf("%s",source); fd2=open(source,0); if(fd2==-1) { perror("file doesnot exist"); exit(0); } while((n=read(fd2,ch,1))>0) write(fd1,ch,n); close(fd2); stat(source,&s); printf("Source file size=%d\n",s.st_size); fstat(fd1,&t); printf("Destination File size before closing=%d\n",t.st_size); close(fd1); fstat(fd1,&w); printf("Destination File Size after closing=%d\n",w.st_size); }
CS 2257 LAB
OPERATING SYSTEMS
IMPLEMENTATION OF STAT OUTPUT: "soce.c" 30L, 614C written [ivcsea@localhost karthik]$ cc soce.c [ivcsea@localhost karthik]$ ./a.out Enter the file to be copied soce.c Source file size=614 Destination File size before closing=614 Destination File Size after closing=3
CS 2257 LAB
OPERATING SYSTEMS
1C. IMPLEMENTATION OF READ DIRECTORIES PROGRAM: #include<stdio.h> #include<sys/types.h> #include<dirent.h> #include<sys/stat.h> #define REQUEST_DIR "/" int main(int argc,char *argv[]){ FILE *fp; DIR *dirp; struct dirent *dp; struct stat buf; dirp=opendir(REQUEST_DIR); chdir(REQUEST_DIR); while((dp = readdir(dirp))!=NULL){ if(stat(dp->d_name,&buf)==-1) perror("stat\n"); if(S_ISDIR(buf.st_mode)) printf("%s is a directory \n",dp->d_name); else if(S_ISREG(buf.st_mode)) printf("%s is a regular file \n",dp->d_name); } (void)closedir(dirp); }
CS 2257 LAB
OPERATING SYSTEMS
IMPLEMENTATION OF READ DIRECTORIES OUTPUT: . is a directory .. is a directory lost+found is a directory dev is a directory proc is a directory .autofsck is a regular file var is a directory tmp is a directory etc is a directory root is a directory usr is a directory boot is a directory bin is a directory home is a directory initrd is a directory lib is a directory mnt is a directory opt is a directory sbin is a directory misc is a directory .automount is a directory tftpboot is a directory .fonts.cache-1 is a regular file
CS 2257 LAB
OPERATING SYSTEMS
PROGRAM: #include<stdio.h> int main() { char c; FILE *f; f=fopen("a.txt","w"); printf("\nEnter the TEXT to File:\n"); while((c=getchar())!='\n') fprintf(f,"%c",c); fclose(f); printf("\n\nContent of the File:\n\n"); f=fopen("a.txt","r"); while(fscanf(f,"%c",&c)!=EOF) printf("%c",c); fclose(f); return(0); }
OUTPUT : Enter the TEXT to File: SBC Engineering College Content of the File: SBC Engineering College
CS 2257 LAB
OPERATING SYSTEMS
3A. IMPLEMENTATION OF LS COMMAND PROGRAM: #include<sys/wait.h> #include<unistd.h> int fatal(char str[]); int main(int argc,char* argv[]) { int pid; switch(pid=fork()) { case -1: fatal("fork error 1"); case 0: execl("/bin/ls","ls",argv[1],NULL); fatal("exec call 1"); } while(wait((int *)0) !=pid); } int fatal(char str[]) { printf("%s\n"); exit(1); }
CS 2257 LAB
OPERATING SYSTEMS
IMPLEMENTATION OF LS COMMAND
OUTPUT: "ls2.c" 22L, 331C written [ivcsea@localhost karthik]$ cc ls2.c [ivcsea@localhost karthik]$ ./a.out a.out fork.c grep.sh ls1.c music.txt start new.c sys.c text3.txt
second
test.txt text1.txt
[ivcsea@localhost karthik]$
CS 2257 LAB
OPERATING SYSTEMS
3B. IMPLEMENTATION OF GREP COMMAND PROGRAM: #include<stdio.h> main() { char names[5][10],temp[10],c; int i,j,k,n=0; char ch,cha; printf("Enter names to stop type keyword END \n"); scanf("%s",names[n]); while(strcmp(names[n],"END")>0) { n++; scanf("%s",names[n]); } printf("\n"); for(i=0;i<n;i++) printf("%10s",names[i]); printf("\n Enter the name you want to search \n"); scanf("%s",temp); printf("\n string found \n"); for(i=0;i<n;i++) { if(strcmp(names[i],temp)==0) { printf("\n%s\n",names[i]); } } printf("\n Enter the first character to search \n"); ch=getchar(); scanf("%c",&ch); printf("\n sentence started with \n"); for(i=0;i<5;i++) if(names[i][0]==ch) { printf("\n %s \n",names[i]); break; }
10
CS 2257 LAB
OPERATING SYSTEMS
printf("\nEnter the last character to search \n"); cha=getchar(); scanf("%c",&cha); printf("\n sentence end with \n"); for(i=0;i<5;i++) for(j=0;j<10;j++) if(names[i][j]==cha) { printf("\n %s \n",names[i]); break; } OUTPUT: "gg1.c" 50L, 887C written [ivcsea@localhost karthik]$ cc gg1.c [ivcsea@localhost karthik]$ ./a.out Enter names to stop type keyword END car war tar nar war END car war tar nar war Enter the name you want to search war string found war war Enter the first character to search c sentence started with car Enter the last character to search r sentence end with car war tar nar war [ivcsea@localhost karthik]$
11
CS 2257 LAB
OPERATING SYSTEMS
4A. FIRST COME FIRST SERVE SCHEDULING ALGORITHM PROGRAM: #include<stdio.h> int main() { int n,i,j; float s,t[20],w[20],tar[20],avgw,avgtr; printf("\n Enter the number of process"); scanf("%d",&n); for (i=1;i<=n;i++) { printf("\n Enter the time for %d process ",i); scanf("%f",&t[i]); } printf("\n\n"); w[1]=0; for(i=2;i<=n;i++) w[i]=w[i-1]+t[i-1]; for(i=1;i<=n;i++) tar[i]=w[i]+t[i]; printf("\n process no \t cpu time \t waiting time \t tar time \n"); for(i=1;i<=n;i++) printf("\n\t %d\t %f\t%f\t%f\t\n",i,t[i],w[i],tar[i]); s=0; for(i=1;i<=n;i++) s+=w[i]; avgw=s/n; printf("\n The avg waiting time is %f",avgw); s=0; for(i=1;i<=n;i++) s+=tar[i]; avgtr=s/n; printf("\n The avg turn around time is %f\n",avgtr); }
12
CS 2257 LAB
OPERATING SYSTEMS
FIRST COME FIRST SERVE OUTPUT: Enter the number of process: 3 Enter the time for 1 process: 3 Enter the time for 2 process: 4 Enter the time for 3 process: 5 PROCESS 1 2 3 CPU TIME 3.000000 4.000000 5.000000 WAITING TIME 0.000000 3.000000 7.000000 TAR TIME 3.000000 7.000000 12.00000
The avg waiting time is:3.333333 The avg turn around time is:7.333333
13
CS 2257 LAB
OPERATING SYSTEMS
4B. SHORTEST JOB FIRST SERVE SCHEDULING PROGRAM: #include<stdio.h> main() { int n,i,j,p[20],temp1; float s,t[20],w[20],tar[20],avgw,avgtr,temp; printf("\n enter the no of process\t"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n enter the time for %d process\t",i); scanf("%f",&t[i]); p[i]=i; } printf("\n\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(t[i]<t[j]) { temp=t[i]; temp1=p[i]; t[i]=t[j]; p[i]=p[j]; t[j]=temp; p[j]=temp1; } } w[1]=0; for(i=2;i<=n;i++) w[i]=w[i-1]+t[i-1]; for(i=1;i<=n;i++) tar[i]=w[i]+t[i]; printf("\n process no \t cpu time \t waiting time \t tar time \t\n"); for(i=1;i<=n;i++) printf("\n\t%d\t%f\t%f\t%f\t\n",p[i],t[i],w[i],tar[i]); s=0; for(i=1;i<=n;i++) s+=w[i]; avgw=s/n;
14
CS 2257 LAB
OPERATING SYSTEMS
printf("\n the average waiting time is %f",avgw); s=0; for(i=1;i<=n;i++) s+=tar[i]; avgtr=s/n; printf("\n the avg time around time %f,avgtr"); }
OUTPUT: Enter the number of process: 4 Enter the time for 1 process: 6 Enter the time for 2 process: 3 Enter the time for 3 process: 5 Enter the time for 4 process: 7 PROCESS 2 3 1 4 CPU TIME 3.000000 5.000000 6.000000 7.000000 WAITING TIME 0.000000 3.000000 8.000000 14.000000 TAR TIME 3.000000 8.000000 14.000000 21.000000
The avg waiting time is:6.250000 The avg turn around time is: 6.250000
15
CS 2257 LAB
OPERATING SYSTEMS
5A. PRIORITY SCHEDULING PROGRAM: #include<stdio.h> struct process { int pno,btime,prior; } p[100],temp; int main() { int i,j,wt=0,n; float awt=0,ata=0; printf("\n\n Enter the number of process\t"); scanf("%d",&n); printf("\nEnter thr process number,priority,Execution time:"); for(i=0;i<n;i++) { printf("\n Process\t"); scanf("%d",&p[i].pno); printf("\nPriorrity\t"); scanf("%d",&p[i].prior); printf("\nTime\t"); scanf("%d",&p[i].btime); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(p[j].prior<p[i].prior) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } printf("\n pno\tpriority\t execution_time\t waiting_time\tturnaround_time"); for(i=0;i<n;i++) { printf("\n%10d\t%20d\t%10d\t%10d\t%10d",p[i].pno,p[i].prior,p[i].btime,wt,wt+p[i ].btime); awt=awt+wt; ata=ata+wt+p[i].btime;
16
CS 2257 LAB
OPERATING SYSTEMS
wt=wt+p[i].btime; } printf("\n average waiting time %f",awt/n); printf("\n average turn around time is %f",ata/n); exit(0); getch(); return 0; } OUTPUT: ------------Enter the number of process 4 Enter thr process number,priority,Execution time: Process 1 Priority 3 Time 4 Process Priority Time Process Priorrity Time Process Priorrity Time 2 2 1 3 1 5 4 4 2
pno priority execution_time waiting_time turnaround_time 3 1 5 0 5 2 2 1 5 6 1 3 4 6 10 4 4 2 10 12 average waiting time 5.250000 average turn around time is 8.250000
17
CS 2257 LAB
OPERATING SYSTEMS
5B. ROUND ROBIN SCHEDULING PROGRAM: #include<stdio.h> typedef struct { int pno,btm,sbtm,wtm,lst; }spcb; int main() { int pp=-1,ts,flag,count,pno,ptm=0,i,n,twt=0,tttm=0; spcb proc[100]; printf("\n \t\t\t ROUND ROBIN SCHEDULING\t\t\t"); printf("\n Enter the number of process:"); scanf("%d",&n); printf("\n Enter the time slice:"); scanf("%d",&ts); printf("\n Enter the burst time for %d process \n",n); for(i=0;i<n;i++) { scanf("%d",&proc[i].btm); proc[i].wtm=proc[i].lst=0; proc[i].pno=i+1; proc[i].sbtm=proc[i].btm; } printf("\n \t\t\t PROCESS SYCHRONISATION \t\t\t"); do { flag=0; for(i=0;i<n;i++) if((count=proc[i].btm)>0) { flag=1; count=(count>=ts)?ts:count; printf("\n process %d from %d",proc[i].pno,ptm); printf("t0 %d \n",(ptm+=count)); proc[i].btm-=count; if(pp!=i) { proc[i].wtm+=ptm-proc[i].lst-count; proc[i].lst=ptm; } } } while(flag);
18
CS 2257 LAB
OPERATING SYSTEMS
ROUND ROBIN SCHEDULING printf("\t\t process no. \t waiting time \t turn around time\n"); for(i=0;i<n;i++) { twt+=proc[i].wtm; tttm+=proc[i].wtm+proc[i].sbtm; printf("\n \t\t%d\t\t%d\t\t",proc[i].pno,proc[i].wtm); printf("%d",proc[i].wtm+proc[i].sbtm); } printf("\n Total average time =%d",twt); printf("\n average waiting time=%f",(float)twt/n); printf("\n Total turn around time =%d",tttm); printf("\n Average turn around time =%f",(float)tttm/n); } "nimmi1.c" 56L, 1412C written [ivcsea@localhost ivcse]$ cc nimmi1.c [ivcsea@localhost ivcse]$ ./a.out
19
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: ROUND ROBIN SCHEDULING Enter the number of process:3 Enter the time slice:2 Enter the burst time for 3 process 6 4 3 PROCESS SYCHRONISATION process 1 from 0t0 2 process 2 from 2t0 4 process 3 from 4t0 6 process 1 from 6t0 8 process 2 from 8t0 10 process 3 from 10t0 11 process 1 from 11t0 13 process no. 1 2 3 waiting time 7 6 8 turn around time 13 10 11
Total average time =21 average waiting time=7.000000 Total turn around time =34 Average turn around time =11.333333
20
CS 2257 LAB
OPERATING SYSTEMS
6A. INTERPROCESS COMMUNICATION USING SHARED MEMORY PROGRAM: #include<stdio.h> #include<sys/shm.h> #include<sys/ipc.h> int main() { int child,shmid,i; char*shmptr; child=fork(); if(!child) { shmid=shmget(2041, 32, 0666 | IPC_CREAT); shmptr=shmat(shmid, 0, 0); printf("\n\n SHARED MEMORY\n\n"); printf("\nPARENT WRITING\n"); for(i=0;i<10;i++) { shmptr[i]='a' + i; putchar(shmptr[i]); } printf("\n\n%s",shmptr); wait(NULL);
21
CS 2257 LAB
OPERATING SYSTEMS
INTERPROCESS COMMUNICATION USING SHARED MEMORY else { shmid=shmget(2041, 32, 0666); shmptr=shmat(shmid, 0, 0); printf("\nCHILD IS READING:\n"); for(i=0;i<10;i++) putchar(shmptr[i]); shmdt(NULL); shmctl(shmid,IPC_RMID,NULL); } return 0; }
22
CS 2257 LAB
OPERATING SYSTEMS
6B. INTERPROCESS COMMUNICATION USING PIPES PROGRAM: #include<stdio.h> int main() { int fd[2],child; char a[10]; printf("\nPIPES:\n"); printf("\nEnter the srting to enter into the pipe:"); scanf("%s",a); pipe(fd); child=fork(); if(!child) { close(fd[10]); write(fd[1],a,5); wait(0); } else { close(fd[1]); read(fd[0],a,5); printf("\nThe string retrieved from the pipe is %s\n",a); } return 0; }
23
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: PIPES: Enter the string to enter into the pipe:computer The string retrieved from the pipe is:computer
24
CS 2257 LAB
OPERATING SYSTEMS
7. PRODUCER-CONSUMER PROBLEM
PROGRAM
#include<stdio.h> #define eof '\n' int main() { int n; char buf[10],item[10]; int i,full=0,empty=5,mutex=1; for(i=0;i<5;i++) { printf("\n produce an item in buffer"); while((item[i]=getchar())!=eof) if((mutex==1)&&(empty!=0)) { buf[i]=item[i]; full=full+1; empty=empty-1; } printf("\n\t\tfull=%d",full); printf("\n\t\tempty=%d",empty); printf("\nthe item moved in the buffer \n"); } if((mutex==1)&&(empty==5)) printf("\n the buffer is full \n"); for(i=0;i<5;i++) { if((mutex==1)&&(full!=0))
25
CS 2257 LAB
OPERATING SYSTEMS
PRODUCER-CONSUMER PROBLEM
{ printf("\n %c",buf[i]); full=full-1; empty=empty+1; } printf("full=%d",full); printf("empty=%d",empty); } if((mutex==1)&&(empty==5)) printf("\n the buffer is empty"); } OUTPUT: produce an item in buffer5 full=1 empty=4 the item moved in the buffer produce an item in buffer3 full=2 empty=3 the item moved in the buffer produce an item in buffer4 full=3
26
CS 2257 LAB
OPERATING SYSTEMS
PRODUCER-CONSUMER PROBLEM
empty=2 the item moved in the buffer produce an item in buffer7 full=4 empty=1 the item moved in the buffer produce an item in buffer2 full=5 empty=0 the item moved in the buffer 5full=4empty=1 3full=3empty=2 4full=2empty=3 7full=1empty=4 2full=0empty=5
27
CS 2257 LAB
OPERATING SYSTEMS
8A. FIRST FIT ALGORITHM PROGRAM: #include<stdio.h> main() { int a,b[50],i,s,c,n; printf("\n\t\t\t FIRST FIT\n"); printf("\n Enter the no of memory process\t"); scanf("\n %d",&a); printf("\n Enter the required no of slots \n"); for(i=1;i<=a;i++) { scanf("\n %d",&b[i]); } do { printf("\n Enter the job size \t"); scanf("\n %d",&s); for(i=1;i<=a;i++) { if(s<=b[i]) { printf("\n %d is allocated to slot %d of size %d\n",s,i,b[i]); b[i]=b[i]-s; printf("\n Remaining space of slot %d is %d\n",i,b[i]); n=1; break; } else continue; } if(n!=1) printf("\n %d is not allocated to any slot",s); n=0; printf("\n 1.Enter the job\n2.Exit\n"); printf("\n Enter your choice\t"); scanf("\n %d",&c); } while(c==1); }
28
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: FIRST FIT Enter the no of memory process 3 Enter the required no of slots 3 67 55 Enter the job size 55 55 is allocated to slot 2 of size 67 Remaining space of slot 2 is 12 1.Enter the job 2.Exit Enter your choice 1
Enter the job size 23 23 is allocated to slot 3 of size 55 Remaining space of slot 3 is 32 1.Enter the job 2.Exit Enter your choice 2
29
CS 2257 LAB
OPERATING SYSTEMS
8B. WORST FIT ALGORITHM PROGRAM: #include<stdio.h> main() { int a[50],n,i,j,p,t; printf("\n WORST FIT ALGORITHM"); printf("\n\n Enter Unallocated space \n"); scanf("\n %d",&n); printf("\n Enter the memory space \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\n Enter the time memory required space"); scanf("%d",&p); for(i=1;i<n;i++) for(j=i+1;j<=n;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } printf("\n THE WORST FIT PROCESS %d\n",a[i]); }
30
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: WORST FIT ALGORITHM Enter Unallocated space 3 Enter the memory space 34 43 23 Enter the time memory required space20 THE WORST FIT PROCESS 43
31
CS 2257 LAB
OPERATING SYSTEMS
8C. BEST FIT ALGORITHM PROGRAM: #include<stdio.h> main() { int a,b[50],i,j,temp,s; printf("\n BEST FIT ALGORITHM"); printf("\n Enter the number of allocated process"); scanf("%d",&a); printf("\n Enter the unallocated space in KB"); for(i=1;i<=a;i++) scanf("%d",&b[i]); for(j=j+1;j<=a;j++) if(b[i]>b[j]) { temp=b[j]; b[j]=b[i]; b[i]=temp; } printf("\n Enter the space for required process\n"); scanf("%d",&s); for(i=1;i<a;i++) { if(s<=b[i]) { printf("\n \n The best fit for required space is %d \n",b[i]); break; } } }
32
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: BEST FIT ALGORITHM Enter the number of allocated process3 Enter the unallocated space in KB 12 23 34 Enter the space for required process 20 The best fit for required space is 23
33
CS 2257 LAB
OPERATING SYSTEMS
9. MEMORY ARRANGEMENT-II PAGE REPLACEMENT PROGRAM: #include<stdio.h> int m,n,i,j,k,flag,count=0,refer[100],page_frame[100][2],fault=0,min,no_frames; void replace(int z) { for (i=0;i<n;i++) { flag=1; for(j=0;j<no_frames;j++) if(refer[i]==page_frame[j][0]) { m=j; flag=0; } if(flag) { fault++; min=32000; for(j=0;j<no_frames;j++) if(page_frame[j][1]<min) { min=page_frame[j][1]; k=j; } page_frame[k][0]=refer[i]; page_frame[k][1]=++count; for(j=0;j<no_frames;j++) printf("\n %d",page_frame[j][0]);
34
CS 2257 LAB
OPERATING SYSTEMS
printf("\n"); } else { printf("No page fault\n"); if(z==2) page_frame[m][1]=++count; } } printf("Number of page fault is: %d \n",fault); } int main() { printf("\n Enter the number of reference: "); scanf("%d",&n); printf("\n Enter the number of frames: "); scanf("%d",&no_frames); printf("\n Enter the reference string: "); for(i=0;i<n;i++) scanf("%d",&refer[i]); printf("\n FIFO ALGORITHM\n"); for(i=0;i<no_frames;i++) { page_frame[i][0]=-1; page_frame[i][1]=count; } replace(1); fault=0; count=0; printf("\n\t\t\tLRU ALGORITHM\n"); for(i=0;i<no_frames;i++)
35
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: Enter the number of reference: 20 Enter the number of frames: 3 Enter the reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 FIFO ALGORITHM 7 7 7 2 2 2 4 4 4 0 -1 0 0 0 3 3 3 2 2 2 -1 -1 1 1 -1 0 0 0 3 3
NO page fault
36
CS 2257 LAB
OPERATING SYSTEMS
7 7 7
1 0 0
2 2 1
NO page fault NO page fault 1 1 1 3 0 0 2 2 7 NO page fault NO page fault NO page fault NO page fault Number of page fault is: 12
37
CS 2257 LAB
OPERATING SYSTEMS
IMPLEMENTATION OF FILE ALLOCATION TECHNIQUE USING INDEX PROGRAM: #include<stdio.h> #include<stdlib.h> typedef struct student { int rno; char name[20]; int age; int marks; }ss; typedef struct index { int irno; long offset; int flag; }is; FILE *ptr=NULL; FILE *iptr=NULL; void main() { int ch; int status; int rno; char op1='y'; ptr=fopen("std.dat","wb"); if(ptr==NULL) { printf("file not found"); exit(0);
38
CS 2257 LAB
OPERATING SYSTEMS
} iptr=fopen("std1.dat","wb"); if(iptr==NULL) { printf("file not found"); exit(0); } fclose(ptr); fclose(iptr); while((op1='y')||(op1='Y')) { printf("\n 1.add"); printf("\n 2.delete"); printf("\n 3.modify"); printf("\n 4.search"); printf("\n 5.display"); printf("\n 6.exit \n"); printf("enter your option (1-6):"); scanf("%d",&ch); switch(ch) { case 1: status=ADD_RECORD(); if(status==1) printf("record successfully added"); else printf("record could not be added"); break; case 2: printf("enter the rollno of the record to delete"); scanf("%d",&rno);
39
CS 2257 LAB
OPERATING SYSTEMS
status=DELETE_RECORD(rno); if(status==1) printf("record deleted successfully"); else printf("record could not be deleted"); break; case 3: printf("enter rollno of the record to modify:"); scanf("%d",&rno); status=MODIFY_RECORD(rno); if(status==1) printf("record modified successfully"); else printf("record could not be modified"); break; case 4: printf("enter the rollno to search:"); scanf("%d",&rno); status=SEARCH_RECORD(rno); if(status==1) printf("record found"); else printf("record not found"); break; case 5: DISPLAY_ALL_RECORD(); break; case 6: exit(0); } fflush(stdin);
40
CS 2257 LAB
OPERATING SYSTEMS
printf("\n do you want to continue(Y/N):"); scanf("%c",&op1); if((op1=='n')||(op1=='N')) break; } } int ADD_RECORD() { ss stud; is ind; long offset; ptr=fopen("std.dat","rb+"); if(ptr==NULL) { printf("file not opened"); return -1; } while(fread(&stud,sizeof(stud),1,ptr)); offset=ftell(ptr); printf("rollno:"); scanf("%d",&stud.rno); printf("name of the student:"); scanf("%s",&stud.name); printf("age:"); scanf("%d",&stud.age); printf("marks:"); scanf("%d",&stud.marks); fwrite(&stud,sizeof(stud),1,ptr); fclose(ptr); iptr=fopen("std1.dat","ab+"); if(iptr==NULL)
41
CS 2257 LAB
OPERATING SYSTEMS
{ printf("file not opened"); return -1; } ind.irno=stud.rno; ind.offset=offset; ind.flag=1; fwrite(&ind,sizeof(ind),1,ptr); fclose(iptr); SORT_FILE(); return 1; } int DELETE_RECORD(int rno) { is ind; int flag=0; iptr=fopen("std1.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; } while(fread(&ind,sizeof(is),1,iptr)); { if(ind.irno==rno) { flag=1; } } if(flag==1) {
42
CS 2257 LAB
OPERATING SYSTEMS
ind.flag=-1; fseek(iptr,-(long) sizeof(is),SEEK_CUR); fwrite(&ind,sizeof(is),1,iptr); fclose(iptr); return 1; } fclose(iptr); return -1; } int MODIFY_RECORD(int rno) { is ind; ss stud; int flag=0; iptr=fopen("std1.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; } while(fread(&ind,sizeof(is),1,iptr)) { if(ind.irno==rno) { flag=1; break; } } fclose(iptr); if(flag==1) {
43
CS 2257 LAB
OPERATING SYSTEMS
ptr=fopen("std.dat","rb+"); if(ptr==NULL) { printf("file not opened"); return -1; } fseek(ptr,ind.offset,SEEK_SET); fread(&stud,sizeof(stud),1,ptr); printf("enter name:"); scanf("%s",stud.name); printf("age:"); scanf("%d",&stud.age); printf("marks:"); scanf("%d",&stud.marks); fseek(ptr,-(long) sizeof(stud),SEEK_CUR); fwrite(&stud,sizeof(stud),1,ptr); fclose(ptr); return 1; } return -1; } int SEARCH_RECORD(int rno) { is ind; ss stud; iptr=fopen("std1.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; }
44
CS 2257 LAB
OPERATING SYSTEMS
while(fread(&ind,sizeof(is),1,iptr)) { if(ind.irno==rno && ind.flag==1) { fclose(iptr); return 1; } } fclose(iptr); return -1; } int DISPLAY_ALL_RECORD() { is ind; ss stud; iptr=fopen("std1.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; } ptr=fopen("std.dat","rb+"); if(ptr==NULL) { printf("file not opened"); return -1; } while(fread(&ind,sizeof(is),1,iptr)) { if(ind.flag==1) {
45
CS 2257 LAB
OPERATING SYSTEMS
fseek(ptr,ind.offset,SEEK_SET); fread(&stud,sizeof(stud),1,ptr); printf("\n\n rollno:%d",stud.rno); printf("\n name:%s",stud.name); printf("\n age:%d",stud.age); printf("\n marks:%d",stud.marks); } } fclose(iptr); fclose(ptr); return 1; } int SORT_FILE() { int size; int i,j,flag=0; is ind,ind_temp; ss stud; iptr=fopen("std1.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; } ptr=fopen("std.dat","rb+"); if(ptr==NULL) { printf("file not opened"); return -1; } size=0;
46
CS 2257 LAB
OPERATING SYSTEMS
while(fread(&ind,sizeof(is),1,iptr)) size++; fclose(iptr); iptr=fopen("std.dat","rb+"); if(iptr==NULL) { printf("file not opened"); return -1; } for(i=0;i<size;i++) { flag=0; for(j=0;j<size-(i+1);j++) { fseek(iptr,j*sizeof(is),SEEK_SET); fread(&ind,sizeof(is),1,iptr); fread(&ind_temp,sizeof(is),2,iptr); if(ind.irno>ind_temp.irno) { fseek(ptr,j*sizeof(is),SEEK_SET); fwrite(&ind_temp,sizeof(is),1,ptr); fseek(iptr,(j+1)*sizeof(is),SEEK_SET); fwrite(&ind,sizeof(is),1,iptr); flag=1; } if(flag==0) break; } fclose(iptr); return 1; }
47
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: 1. Add 2. Delete 3. Modify 4. Search 5. Display 6. Exit Enter your option(1-6):1 Rollno:01 Name of the student:muni Age:25 Marks:78 Record Successfully Added Do You want to continue(Y/N):y 1. Add 2. Delete 3. Modify 4. Search 5. Display 6. Exit Enter your option(1-6):1 Rollno:02 Name of the student:raj Age:25 Marks:72 Record Successfully Added Do You want to continue(Y/N):y Enter your option(1-6):2 Enter the rollno of the record to delete02 Record could not be deleted Do You want to continue(Y/N):y
48
CS 2257 LAB
OPERATING SYSTEMS
Enter your option(1-6):3 Enter roll no of the record to modify:01 Enter name:muni Age:30 Marks:80 Record modified successfully Do You want to continue(Y/N):y 1. Add 2. Delete 3. Modify 4. Search 5. Display 6. Exit Enter your option(1-6):5 Rollno:01 Name:muni Age:30 Marks:80 Do You want to continue(Y/N):6
49
CS 2257 LAB
OPERATING SYSTEMS
IMPLEMENTATION OF FILE ALLOCATION TECHNIQUE USING LINKED PROGRAM #include<stdio.h> #include<conio.h> typedef struct list { int val; struct list *next; }item; item *additem(item *entry, int value); void printitems(void); item *head=NULL; item *tail=NULL; main(int argc,char *argv[]) { clrscr(); tail=additem(tail,5); tail=additem(tail,7); tail=additem(tail,2); printitems(); } item *additem(item *entry, int value) { item *newitem; newitem=(item*)malloc(sizeof(item)); if(entry==NULL) { head=newitem; printf("first list_item in list\n"); }
50
CS 2257 LAB
OPERATING SYSTEMS
else { entry->next=newitem; printf("adding %d to list. Last value was %d \n",value,entry->val); } newitem->val=value; newitem->next=NULL; return newitem; } void printitems(void) { item *ptritem; for(ptritem=head; ptritem!=NULL; ptritem=ptritem->next){ printf("Value is %d\n",ptritem->val); getch(); } }
51
CS 2257 LAB
OPERATING SYSTEMS
OUTPUT: adding 5 to list. Last value was 2 adding 7 to list. Last value was 5 2 5 7
52