os lab completed

52
CS 2257 OPERATING SYSTEMS LAB  #include<stdio. 1A. IMPLEMENTATION OF FORK 1

Upload: justina-praveen

Post on 06-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 1/52

CS 2257 OPERATING SYSTEMS

LAB

 

#include<stdio.

1A. IMPLEMENTATION OF FORK 

1

Page 2: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 2/52

CS 2257 OPERATING SYSTEMS

LAB

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

1B. IMPLEMENTATION OF STAT

2

Page 3: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 3/52

CS 2257 OPERATING SYSTEMS

LAB

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

3

Page 4: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 4/52

CS 2257 OPERATING SYSTEMS

LAB

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

4

Page 5: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 5/52

CS 2257 OPERATING SYSTEMS

LAB

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

}

5

Page 6: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 6/52

CS 2257 OPERATING SYSTEMS

LAB

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

6

Page 7: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 7/52

CS 2257 OPERATING SYSTEMS

LAB

2. IMPLEMENTATION OF I/O SYSTEM CALLS

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

IMPLEMENTATION OF I/O SYSTEM CALL

OUTPUT :

Enter the TEXT to File:SBC Engineering College

Content of the File:SBC Engineering College

7

Page 8: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 8/52

CS 2257 OPERATING SYSTEMS

LAB

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

}

8

Page 9: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 9/52

CS 2257 OPERATING SYSTEMS

LAB

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 text3.txt

arasua.c gg.c index.c ls2.c new.c sys.c

a.txt gr.c io.c ls.c second test.txt

first grep.c link.c music.dat soce.c text1.txt

[ivcsea@localhost karthik]$

9

Page 10: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 10/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 11: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 11/52

CS 2257 OPERATING SYSTEMS

LAB

 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.outEnter names to stop type keyword ENDcar war tar nar war END

car war tar nar war Enter the name you want to searchwar 

string foundwar war 

Enter the first character to searchc

sentence started withcar 

Enter the last character to searchr 

sentence end withcar war tar nar war [ivcsea@localhost karthik]$

11

Page 12: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 12/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 13: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 13/52

CS 2257 OPERATING SYSTEMS

LAB

FIRST COME FIRST SERVE

OUTPUT:

Enter the number of process: 3

Enter the time for 1 process: 3Enter the time for 2 process: 4Enter the time for 3 process: 5

PROCESS CPU TIME WAITING TIME TAR TIME

1 3.000000 0.000000 3.0000002 4.000000 3.000000 7.0000003 5.000000 7.000000 12.00000

The avg waiting time is:3.333333The avg turn around time is:7.333333

13

Page 14: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 14/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 15: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 15/52

CS 2257 OPERATING SYSTEMS

LAB

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: 6Enter the time for 2 process: 3Enter the time for 3 process: 5Enter the time for 4 process: 7

PROCESS CPU TIME WAITING TIME TAR TIME

2 3.000000 0.000000 3.0000003 5.000000 3.000000 8.0000001 6.000000 8.000000 14.0000004 7.000000 14.000000 21.000000

The avg waiting time is:6.250000The avg turn around time is: 6.250000

15

Page 16: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 16/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 17: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 17/52

CS 2257 OPERATING SYSTEMS

LAB

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 4Enter thr process number,priority,Execution time:Process 1Priority 3

Time 4

Process 2Priority 2Time 1

Process 3Priorrity 1Time 5

Process 4Priorrity 4Time 2

pno priority execution_time waiting_time turnaround_time3 1 5 0 52 2 1 5 61 3 4 6 104 4 2 10 12

average waiting time 5.250000average turn around time is 8.250000

17

Page 18: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 18/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 19: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 19/52

CS 2257 OPERATING SYSTEMS

LAB

 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

Page 20: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 20/52

CS 2257 OPERATING SYSTEMS

LAB

OUTPUT:

ROUND ROBIN SCHEDULING

Enter the number of process:3

Enter the time slice:2

Enter the burst time for 3 process643

PROCESS SYCHRONISATIONprocess 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. waiting time turn around time

1 7 132 6 103 8 11

Total average time =21average waiting time=7.000000Total turn around time =34Average turn around time =11.333333

20

Page 21: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 21/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 22: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 22/52

CS 2257 OPERATING SYSTEMS

LAB

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;

}

OUTPUT:

SHARED MEMORY

PARENT WRITING:

abcdefghij

CHILD IS READING:

abcdefghij

22

Page 23: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 23/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 24: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 24/52

CS 2257 OPERATING SYSTEMS

LAB

INTERPROCESS COMMUNICATION USING

PIPES

OUTPUT:

PIPES:

Enter the string to enter into the pipe:computer 

The string retrieved from the pipe is:computer 

24

Page 25: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 25/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 26: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 26/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 27: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 27/52

CS 2257 OPERATING SYSTEMS

LAB

  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

Page 28: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 28/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 29: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 29/52

CS 2257 OPERATING SYSTEMS

LAB

OUTPUT:

FIRST FIT

Enter the no of memory process 3Enter the required no of slots36755

Enter the job size 5555 is allocated to slot 2 of size 67

Remaining space of slot 2 is 121.Enter the job

2.Exit

Enter your choice 1

Enter the job size 2323 is allocated to slot 3 of size 55Remaining space of slot 3 is 321.Enter the job2.ExitEnter your choice 2

29

Page 30: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 30/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 31: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 31/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 32: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 32/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 33: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 33/52

CS 2257 OPERATING SYSTEMS

LAB

OUTPUT:

BEST FIT ALGORITHM

Enter the number of allocated process3Enter the unallocated space in KB122334Enter the space for required process20

The best fit for required space is 23

33

Page 34: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 34/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 35: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 35/52

CS 2257 OPERATING SYSTEMS

LAB

 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

Page 36: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 36/52

CS 2257 OPERATING SYSTEMS

LAB

{

 page_frame[i][0]=-1;

 page_frame[i][1]=count;

}

replace(2);

return 0;

}

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 -1 -1

7 0 -1

7 0 1

2 0 1

 NO page fault

2 3 -1

2 3 0

4 3 0

4 2 0

4 2 3

0 2 3

 NO page fault

NO page fault

0 1 3

0 1 2

 NO page fault

 NO page fault

36

Page 37: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 37/52

CS 2257 OPERATING SYSTEMS

LAB

7 1 2

7 0 2

7 0 1

 Number of page fault is : 15

LRU ALGORITHM

7 -1 -1

7 0 -1

2 0 1

 NO page fault

2 0 3

 NO page fault

4 0 3

4 0 2

4 3 2

0 3 2

 NO page fault

 NO page fault

1 3 2

 NO page fault

1 0 2

 NO page fault

1 0 7

 NO page fault

 NO page fault

 Number of page fault is: 12

37

Page 38: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 38/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 39: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 39/52

CS 2257 OPERATING SYSTEMS

LAB

}

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

Page 40: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 40/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 41: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 41/52

CS 2257 OPERATING SYSTEMS

LAB

 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

Page 42: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 42/52

CS 2257 OPERATING SYSTEMS

LAB

{

 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

Page 43: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 43/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 44: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 44/52

CS 2257 OPERATING SYSTEMS

LAB

 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

Page 45: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 45/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 46: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 46/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 47: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 47/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 48: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 48/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 49: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 49/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 50: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 50/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 51: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 51/52

CS 2257 OPERATING SYSTEMS

LAB

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

Page 52: Os Lab Completed

8/3/2019 Os Lab Completed

http://slidepdf.com/reader/full/os-lab-completed 52/52

CS 2257 OPERATING SYSTEMS

LAB

OUTPUT:

adding 5 to list. Last value was 2

adding 7 to list. Last value was 5

2

5

7