JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE NUSTULAPUR, KARIMNAGAR

1

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

LAB MANUAL OF
UNIX & OS LAB (IT) (III B.Tech. II SEM)

2

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

INDEX
SNO 1 2 3 4 5 6

PROGRAM NAME
Write a shell script to print the multiplication table for the given number. Write a shell script that copies multiple files into a directory. Write a shell script to find no. of words and characters in a given file. Write a shell script to display all files in a given directory. Write a shell script to perform simple calculator. Write a C program to count no.of blanks,characters a) Using standard i/o function. b) Using system calls. Write a C program to illustrate the following Command using system Calls a) cat b) ls c) mv Write a C program to illustrate the stat system call. Write a C program that illustrates the creation of child process using fork() system call. Write a C program that illustrates file locking. Write a C program that implements producer consumer problem using semaphore system calls. Write a C program to illustrate inter process communication using shared memory system calls. Write a C program to (a) create message queue (b) write to message queue (c) read from message queue

PAGE NO 1 2 3 4 5 6-7

7

8-10

8 9 10 11 12 13

11 12 13-14 15-16 17-18 19-20

3

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

O.S. PROGRAMMES
S.NO 1 2 3 4 5 6 7 8 9 10 11 12 13 PROGRAM NAME Write a C program for ROUND ROBIN CPU scheduling algorithm Write a C program for SJF CPU scheduling algorithm Write a C program for FCFS CPU scheduling algorithm Write a C Program for priority CPU scheduling algorithm. Write a C Program for sequential file allocation. Program for indexed file allocation strategy. Program for linked file allocation strategy. Write a C Program for MVT first fit. Write a C Program for MFT . Write a C Program for MVT best fit. Program for bankers algorithm Write a C program for FIFO page allocation algorithm. Write a C program for LRU page replacement algorithm. PAGE NO 20-21 22-23 24 25-26 27-28 29-30 31-32 33 34 35-36 37-39 40-41 42-44

4

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE

Program 1: AIM: Write a shell script to print the multiplication table for the given number.
Algorithm: 1.Start. 2.Read n. 3.i=1. 4.f = n * i. 5.i = i + 1. 6.Display f. 7.Repeat 4,5,6 steps until i = 10. 8.End.

Source Code:
#! /bin/sh echo “enter the number” read n for i in 1 2 3 4 5 6 7 8 9 10 do x= `expr $n \* $i` done

Input:
enter the number 6

Output:
6*1=6 6 * 2 = 12

5

c` do cp $i /root done echo “file are copied into root” Output: file are copied into root” Program 3 : AIM: Write a shell script to find no.End.Start. Source Code: #! /bin/sh for i in `ls *.Copy $i to the root directory.Read *.3 steps until all values of $i. Start. 4.. Algorithm: 1.c files into i. 5. Algorithm: 1.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 Program 2: AIM: Write a shell script that copies multiple files into a directory. 6 . of words and characters in a given file.Repeat 2. 2. 3.

6.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 2. give file name as commandline argument (i.of characters “ $cc echo “no. Repeat 4. 8.out sun Output: no.*”` cc = `expr $cc + $c` done echo “no. cc = 0. w = w + 1.w=0. c = expr “$i” : “*”.e $1). cc = cc + c. 6. . of words 11 7 . End. 9. 7. 5. 4./a. 3.7 steps until eof. of characters 56 no.5. Source Code: # /bin/sh w=0 cc=0 for i in ‘cat $1’ do j= $i echo $j w=`expr $w + 1` c=`expr “$j”:”. for i in `cat $1`.of words” $w Input:.

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 4: AIM: Write a shell script to display all files in a given directory.End. 4.txt ss.Read i value from ls sss.c 8 . Source Code: #! /bin/sh for i in `ls sss` do echo $i done Output: abc. 5. Algorithm: 1.Start. 3.Repeat above 2 steps until end of directory.Display i value . 2.

Depending on value of operator perform Operation.Read op 4.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 5: AIM: Write a shell script to perform simple calculator.Start 2.End case Source Code: #! /bin/sh echo ‘enter the value for a’ read a echo ‘enter the value for b’ read b 9 . 5. Algorithm: 1.Read a and b 3.

. characters. 3.. esac echo $c Input: enter the value for a 3 enter the value for b 6 enter the operator * Output: 18 Program 6a: AIM: Write a C program to count no.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE echo ‘enter operator’ read op case $op in +) c=`expr $a + $b`. open the file using fopen( ) function in “r” mode ch=fgetc(fp) (to read character by character) if ch = ‘ ‘ or ‘\n’ b=b+1. \*) c = `expr $a \* $b`.5&6 steps until ch = eof End Source Code: #include<stdio. /) c = `expr $a / $b . if ch = ‘\n’ l=l+1..h> int main( ) { 10 . 6.. 8. -) c = `expr $a -$b`. Algorithm: 1. Start. 4. 7.4. 2. lines using standard i/o function. 5. c=c+1. Repeat 3. of blanks.

txt”. 2. fp=fopen(“text. characters.buf.of lines 2 no.txt Output: no.b). char ch. lines using system calls. c=c+1. c++.”r”)./a. of blanks. 7.out sss.c). while(ch!=eof) { ch=fgetc(fp).of characters 36 Program 6b: AIM: Write a C program to count no. } printf(“no.of lines %d”. Start.nl). 3. 6. Repeat 3. printf(“no.of characters %d”. Algorithm: 1. if ch = ‘\n’ l=l+1. 5. } Input: .5) (to read characters ) if ch = ‘ ‘ or ‘\n’ w=w+1. printf(“no.c=0. 4.nl=0. open the file using open( ) system call in O_RDONLY rc=read(fd. 8.of blanks 5 n.of blanks %d”.5&6 steps until ch = eof End 11 . if(ch==’\n’) nl++.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE file *fp: int b=0. if(ch==’ ‘) b++.4.

of characters %d”. delete existed file using using unlink( ) system 12 .of lines %d”. printf(“no. read the contents from keyboard using read( ) 3. open one existed file and one new open file using open( ) system call 2.c).nl)./a.h> int main( ) { int fd: int b=0.ch.nl=0. printf(“no. c++.txt”. } Input: . repeat 2.txt Output: no. of blanks 5 no.3 steps until eof 5. of characters 36 Program 7a: AIM: Write a C program to illustrate the mv command using system Calls Algorithm: 1.O_RDONLY). if(ch==’\n’) nl++. char ch[1]. fd=open(“text. } printf(“no. close 2 file using fclose( ) system call 6.c=0. write these contents into file using write() 4.b).out sss.of blanks %d”. of lines 2 no. while((rc=read(fd.1))>0) { if(ch==’ ‘) b++.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include<stdio.

repeat above step until end of directory.rc. read the directory using readdir( ) system call. while((rc=read(fd1.h> #include<sys/types. 6. } Input: hai hello A friend in need is a friend in deed ^z Output: hai hello A friend in need is a friend in dee Program 7b : AIM: Write a program to illustrate “ls” command using system calls Algorithm: 1.h> int main() { int fd. close fd1.fd1. close fd1. fd1=open(“sss”. open directory using opendir( ) system call. End 13 .5))>0) write(fd1.h> #include<unistd.ch. char ch[5].rc). fd1=open(“sss”.O_RDONLY).name and dp. 2. 4.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include<stdio.ch.O_WRONLY | O_CREATE).inode . while((rc=read(stdin. 3. 5. print dp.5))>0) write(stdout.rc).ch.ch. Start.

c 12344 12345 12346 Program 7c: AIM: Write a C program to illustrate the mv command using system calls. if(((dp=opendir(“sss”)))==null) printf(“cannodt open\n”).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include<stdio.char *argv[]) { dir *dp.sh division. Algorithm: 1.h> int main(int argc. } closedir(dirp). Start 2.c process.d->d_ino). printf(“%d\n”. while((d=readdir(dp)!=null)) { printf(“%s\t”. exit(0). } output: mul.d->d_name).h> #include<dirent. struct dirent *d. open an existed file and one new open file using open() 14 .

ch. 6. fd=open(“sss”.fd1. fd1= open(“rrr”. unlink(fd). 4. 8. Source Code: #include<stdio. while((rc=read(fd. char ch[5]. close fd1. 5.O_RDONLY ).c file 15 .JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 3.h> int main() { int fd. system call read the contents from existed file using read( ) system call write these contents into new file using write system call using write( ) system call repeat above 2 steps until eof close 2 file using fclose( ) system call delete existed file using using unlink( ) system End.rc). Declare s and st of type struct stat. close fd.h> #include<unistd.h> #include<sys/types.O_WRONLY | O_CREATE).ch.5))>0) write(fd1. printf(“ file is copied “). 2. } Output: file is copied Program 8: AIM: Write a C program to illustrate the stat system calls Algorithm: 1. 7.rc. Use stat system call by specifying sss.

c.st_mode).c file 6.of links to sss. Source Code: #include<stdio.c file is 2 file permissions of sss.st_nlink ).st. Display information about sss.st.c file is %d“. s=stat(“sss.h> int main() { int s. st.h> #include<sys/types. printf(“no.st_size). if s<0 then display there is no file with the name sss. 7. Display last access time of file sss. printf(“size of sss. 8.c”.st.c.of links to sss.c. printf(“last access time of sss.st.c.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 3.c %d”. } Output: Output : inode number of sss.c file is “.h> #include<sys/stat. exit(0).st_atime ).c is %d“. stat returns s (s may be 0 or -1) 4. Display inode number of sss. Display size of the file sss.&st).h> #include<unistd.st_ino).c is %d“. 5.c file is 7:40 Program 9: AIM: Write a C program that that illustrates the creation of child process using fork( ) system call 16 . if(s<0) { printf(“use of stat() is unsuccessful “).c file is 666 size of sss. } printf(“inode number of sss.c file is 12345 no.c file is 8 last access time of sss. struct stat st. printf(“file permissions sss.

Start Declare pid create new process using fork( ) system call If pid!=0 then Display parent process getpid(). 8.getppid()). 3. 5.getppid(). else { sleep(5). 2. pid=fork(). getpid(). End Source code: #include<stdio. 17 . printf(“child process with pid %d ppid %d\n”. } printf(“ pid %d terminates “.getppid().h> int main( ) { printf(“original process with pid %d ppid %d\n”. getpid() . getpid(). 6. 7. 4. Else Display child process getpid().getppid()).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Algorithm: 1. if(pid!=0) printf(“parent process with pid %d ppid %d \n”.getppid()).getpid()). } Output: original process with pid 3456 and ppid 3525 child process with pid 3457 and ppid 3456 pid 3457 terminates parent process with pid 3456 and ppid 3525 pid 3456 terminates Program 10: AIM: Write a C program that illustrates file locking.

h> #include<fcntl. By pressing return it gets unlocked. 8. 6.seek_set.&f1)==-1) { perror(“fcntl”).0). 7.o_rdwr))==-1) { perror(“open”). l_type is read lock 5.h> #include<unistd. if((fd=open(‘rr. 3. } 18 . Opening rr.txt in read and write mode.f_setlkw. 9. printf(“press <return> to release lock:”). Next we are sending unlock type into l_type. 4.&f1)==-1) { perror(“fcntl”). exit(1). It gets the lock. getchar().l_type=f_unlck.h> #include<stdlib. Start 2. if(fcntl(fd. exit(1).h> int main(int argc. Flock is predefined data structure for locking techniques. f1.txt”. printf(“trying to get lock”): if(fnctl(fd. int fd.f_setlk. exit(1).0. End Source Code: #include<stdio.l_type=f_rdlck.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Algorithm: Al gorithm: 1. By pressing return it reads that character through getchar() function.0. getchar( ). f1. } printf(“got lock \n”).char* argv[]) { struct flock f1={f_wrlck. } printf(“press<return> to try to get lock:”).

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE printf(“unlocked\n”). close(fd). } Output: press<return> to try to get lock trying to get lock press <return> to release lock unlocked 19 .

20 .i. if(sem_set_id==-1) { perror(“main:semget”). sem_set_id).0600).h> #define num_loops 2 int main(int argc. exit(1). } printf(“semaphore set created.2. 3. 4.h> #include<sys/sem. 5.sem_val.semaphore setid‘%d’\n ”.h> #include<unistd.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 11: AIM: Write a C program that implements producer consumer problem using semaphore system calls Algorithm: 1. End.h> #include<stdlib. int rc. 2. Start create semaphore using semget( ) system call if successful it returns positive value create two new processes first process will produce until first process produces second process cannot consume 7. clrscr(). 6. int child_pid. struct sembuf sem_op. Source code: #include<stdio.char* argv[]) { int sem_set_id. struct timespec delay. sem_set_id=semget(ipc_private.h> #include<sys/types.h> #include<sys/ipc.

fflush(stdout). fflush(stdout).null).sem_num=0. { delay. semop(sem_set_id. sem_op. if(rand()>3*(rano_max14)). printf(“producer:’%d’\n”. sem_op.sem_op=1. exit(1). switch(child_pid) { case -1: perror(“fork”).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE child_pid=fork().1). sem_op.i++) { printf(“consumer:’%d’\n”.sem_num=0.sem_op=-1. case 0: for(i=0.i<num_loops.i). nanosleep(&delay. } Output: semaphore set created semaphore set id ‘327690’ producer: ‘0’ consumer:’0’ producer:’1’ 21 . semop(sem_set_id. default: for(i=0.&sem_op.&sem_op.i<num_loops.tv_nsec=10. } break.sem_flg=0.1). } return 0.i++) { sem_op. sem_op. } } break.tv_sec=0.i). sem_op. delay.sem_flg=0.

”usage:stdemo[data_to_writte]\n”). End . 2.h> #include<sys/shm.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE consumer:’1’ Program 12: AIM: Write a C program to illustrate inter process communication using shared memory system calls. 22 . exit(1).h> #include<sys/ipc.0644/ipc_creat))==-1) { perror(“shmget”). Source Code: #include<stdio.char * argv[]) { key_t key.h> #include<stdlib. if(argc>2) { fprintf(stderr. Algorithm: 1.h> #include<string. write to shared memory using shmsnd( ) system call 6. Start create shared memory using shmget( ) system call if success full it returns positive value attach the created shared memory using shmat( ) system call 5. } if((shmid=shmget(key. 3. int shmid. read the contents from shared memory using shmrcv( ) system call 7. 4. int mode. exit(1).h> #define shm_size 1024 int main(int argc. char *data.h> #include<sys/types.shm_size.

if(shmdt(data)==-1) { perror(“shmdt”). exit(1).out swarupa Output: writing to segment swarupa 23 .0). } if(argc==2) printf(writing to segment:\”%s”\”\n”. } return 0. exit(1)./a.(void *)0. if(data==(char *)(-1)) { perror(“shmat”). } Input: #.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE } data=shmat(shmid.data).

if successful rite into message queue using msgsnd() system call 6. char mtext[20]. Start 2. End. Source Code: #include<stdio. create message queue using msgget( ) system call 3. 24 . print the message queue id 5. read the contents from message queue using msgrcv( ) system call 7.h> #include<sys/msg.h> #include<sys/types.h> int main(int argc. if(q_id=msgget(ipc_private.0600)==-1) perror(msgget”).char* argv[]) { int q_id. } struct msgbuf* msg. struct msgbuf { long mtype. while creating message queue it returns id into file descriptor 4.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 13: AIM: Write a C program to ( a ) create message queue ( b ) write to message queue ( c ) read from message queue Algorithm: 1.h> #include<sys/ipc.

recv_msg->mtext). printf(“message placed on queue successfully\n”). exit(1).helloworld”). int rc. } Output: message queue created 0 message placed on the queue successfully msgrcv:received message: mtype: 0 mtext: hello world 25 . printf(“message queue created %d\n”. } printf(“msgrcv:received message:\nmtype’%d’\n. recv_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+ strlen(“hello world”)).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE struct msgbuf* recv_msg. return 0. msg->mtype=1. msg=(struct msgbuf*)malloc(sizeof(struct msgbuf) + strlen(“hello world”)). if(rc==-1) { perror(“main:msgrcv”). free(msg). mtext ’%s’\n”.q_id).recv_msg->mtype. strcpy(msg->mtext.

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE LAB MANUAL OF OPERATING SYSTEM LAB 26 .

tst=0.i. float aw at.t[10].&s[i]).j.wi=0.&n). 27 .n.tw=0. scanf("%d". st[10].&tq).&p[i].of process").w[10].i<n.p[10]. Source Code: #include<stdio. scanf("%d". int tt=0. printf("enter no. printf("\n enter time quanum"). for(i=0.h> main() { int s[10].JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 1: AIM: Write a C program for ROUND ROBIN CPU scheduling algorithm.i++) scanf("%d%d".tq. printf("\n enter process&service time").

aw).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE for(i=0. tst=tst+s[i]. } aw=tw/n. w1=w1+tq. t[i]=w1.i<n.at). } else if(s[i]!=0) { w1=w1+tq. tt=tt+t[i].st[i]. for(i=0.p[i].w[i]. s[i]=s[i]-tq.i++) printf("%d\t%d\t%d\t%d".j++) for(i=0. for(j=0.i<n.i<n. } } for(i=0. } Input: enter no of process 3 enter time quantum 2 enter process&service time 1 4 2 6 3 28 .t[i]). t[i]=w1.i++) st[i]=s[i]. w[i]=t[i]-st[i]. w[i]=t[i]-st[i]. printf("process\tst\twt\ttt"). at=tt/n. printf("att=%d".i++) { if(s[i]>tq) { s[i]=s[i]-tq.i++) { tw=tw+w[i]. printf("awt=%d".i<n.j<tst.

printf(“enter no of jobs”).h> main() { int i.w1=0.t.bt[10].000000 Program 2: AIM: Write a C program for SJF CPU scheduling algorithm Source Code: #include<stdio.i<n.j.j++) 29 .i<n.pt[10].n. for(i=0. scanf(“%d”. float at.j<n. for(i=0.tt[10]. printf(“enter burst time”).aw.i++) scanf”(%d”.&bt[i]).wt[10].000000 att = 8.k.i++) for(j=0.l.&n).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 2 Output: process 1 2 3 st 4 6 2 wt 4 6 4 tt 8 12 6 Awt = 4.t1=0.

tt[i+1]=tt[i]+bt[i]. } for(i=0.aw. bt[j]=t.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE if(bt[i]<bt[j]) { t=bt[i]. w1=w1+wt[i].i<n. at=t1/n. getch(). bt[i]=bt[j]. printf(“aw=%d\nat=%d”. t1=t1+tt[i].i++) { wt[i+1]=bt[i]+wt[i].bt[i].i<n.wt[i]. } aw=w1/n.tt[i]).at). for(i=0. printf(“\nbt\twt\ttt”).i++) printf(“%d\t%d\t%d\n”. } Input: Enter no of jobs 4 Enter burst time 5 12 8 20 Output: Bt 5 12 8 20 wt 0 5 13 25 tt 5 13 25 45 30 .

float at.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE aw=10.&n). scanf(“%d”.h> main() { int i.bt[10].75000 at=22.i<n.t1=0.i++) scanf”(%d”. printf(“enter no of jobs”).l.n.&bt[i]).pt[10].tt[10].j.w1=0. printf(“enter burst time”).t. for(i=0. 31 .aw.wt[10].000000 Program 3: AIM: Write a C program for FCFS CPU scheduling algorithm Source Code: include<stdio.k.

wt[10].j.aw.666670 at=24.i++) { wt[i+1]=bt[i]+wt[i]. getch().t1=0. } aw=w1/n. 32 . for(i=0.w1=0.bt[i].00000 Program 4: AIM: Program Write a C Progra m for priority CPU scheduling algorithm.bt[10]. t1=t1+tt[i].at).h> main() { int i. printf(“\nbt\twt\ttt”).tt[10]. Source Code: #include<stdio. } Input: enter no of jobs 3 enter bursttime 12 8 20 output: bt wt tt 12 0 12 8 12 20 20 20 40 aw=10. at=t1/n.wt[i]. tt[i+1]=tt[i]+bt[i].i++) printf(“%d\t%d\t%d\n”. printf(“aw=%d\nat=%d”.t. w1=w1+wt[i].i<n.i<n.l.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE for(i=0.k. printf(“enter no of jobs”).n.tt[i]).aw. float at.pt[10].

} aw=w1/n.i++) { wt[i+1]=bt[i]+wt[i].i++) for(j=0. printf(“enter burst time”).at). tt[i+1]=tt[i]+bt[i].bt[i]. pt[i]=pt[j].tt[i]). } Input: Enter no of jobs 4 Enter bursttime 10 2 4 7 Enter priority values 4 33 . k=bt[i]. t1=t1+tt[i].&bt[i]). at=t1/n.j++) if(pt[i]<pt[j]) { t=pt[i].&n).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE scanf(“%d”.i++) scanf”(%d”. getch(). printf(“\nbt\tprority\twt\ttt”).i<n. for(i=0. bt[i]=bt[j]. pt[j]=t. } for(i=0. w1=w1+wt[i]. printf(“enter priority values”).i<n.i++) printf(“%d\t%d\t%d\t%d\n”. for(i=0.i<n. bt[j]=k.wt[i]. for(i=0.pt[i].aw.&pt[i]).i++) scanf(“%d”.j<n. for(i=0. printf(“aw=%d\nat=%d”.i<n.i<n.

500000 Program 5: AIM: Write a C Program for sequential file allocation.h> #include<conio. Source Code: #include<stdio.h> main() { 34 .JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 2 1 3 Output: Bt 4 2 7 10 priority 1 2 3 4 wt 0 4 6 13 tt 4 6 13 23 aw=5.750000 at=12.

count=0. if(len==count) { for(j=st.c.st. getch(). if(c==1) goto X.j++) if(f[i]==0) { f[j]=1. if(j==(st+len-1)) printf("\n the file is allocated to disk").j<(st+len). for(k=st.&len).k.k++) if(f[k]==0) count++.&c). else exit().i++) f[i]=0.k<(st+len).count=0. scanf("%d%d". } Input: enter starting block & length of files 4 5 Output: file not allocated (y-1/n-0) 35 .len. printf("\n if u want to enter more files(y-1/n-0)"). printf("\n file not allocated(yes-1/no-0)").f[j]).. } } else printf("file is not allocated"). X: printf("\n enter starting block & length of files").&st.j.i<50. for(i=0. printf("\n%d\t%d".JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE int f[50].i. scanf("%d".j.

h> main() { int f[50]. 36 .k.c.index[50]. Source Code: #include<stdio.i.n.count=0.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 4 5 6 7 8 1 1 1 1 1 file is allocated to disk if u want to enter more files(y-1/n-0) 0 Program 6: AIM: Write a C Program for indexed file allocation strategy.h> #include<conio.j.

printf("\nallocated").&c).i++) f[i]=0.f[index[k]).&n). if(count==n) { for(j=0.k++) printf("\n%d->%d:%d". } else { printf("\n file in the index already allocation"). scanf("%d". } printf("\n index is already allocated"). printf("\n if u enter one more block(1/0)"). if(f[index[i]==0) count++.&index[i]). scanf("%d".k<n. if(f[i]!=1) { f[i]=1. getch( ). if(c==1) goto x.i. } y: for(i=0.index[k]. printf("\n file indexed"). printf("\nenter another file indexed").&i).i++) scanf("%d".i<n. printf("enter no of files on index"). X: printf("enter index block"). for(k=0.j<nj++) f[index[j]=1. goto y. } Input: enter index block 3 enter no of files on index 37 . scanf("%d". count=0.i<50.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE for(i=0.

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 4 5 6 7 8 Output: Allocated file indexed 4->5:1 4->6:1 4->7:1 4->8:1 index is already allocated if u enter one more block(1/0) 0 Program 7: AIM: Write a C Program for linked file allocation strategy.h> main() 38 . Source Code: #include<stdio.h> #include<conio.

p.f[j]).&st. if(c==1) goto X.j).a. scanf("%d".j++) { if(f[j]==0) { f[j]=1.len.i++) f[i]=0.i. printf("\n%d->%d".i++) { scanf("%d".j. k=len. for(i=0. } } } else printf("\nif u enter one more (yes-1/no-0)").&p).&c).i<p. } Input: enter how many blocks already allocated 5 39 . printf("\nenter the blocks nos"). f[a]=1. printf("enter how many blocks already allocated"). if(f[st]==0) { for(j=st.&a).st. scanf("%d%d".n.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE { int f[50]. scanf("%d".i<50.j<(k+st). k++. } X: printf("enter index sarting block & length"). getch( ). } else { printf("\n %d->file is already allocated". for(i=0.j. else exit().k.&len).

h> #include<conio.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE enter block nos 3 7 9 10 14 enter index starting block & length 4 10 Output: 4->1 5->1 6->1 7->file is already allocated 8->1 9->file is already allocated 10->file is already allocated 11->1 12->1 13->1 14->file is already allocated 15->1 16->1 17->1 Program 8: AIM: Write a C Program for MVT first fit.h> 40 . Source Code: #include<stdio.

j++) if(p[i]<=h[j]) { t[i]=h[j]=h[j]-p[i]. int p[10].k.i<k. for(i=0. break. printf(“enter no of blocks”). printf(“enter each process size”). } Input: Enter no of blocks3 Enter block size100 200 300 Enter no of process2 Enter process123 243 Output: Total fragmentation=134 Program 9: AIM: Write a C Program for MFT .j<n.j<n.&n). } for(i=0.h[10].i++) t1+=t[i].t1=0.i++) for(j=0.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE main() { int i. printf(“total fragmentation=%d”. scanf(“%d”. printf(“enter no of process”).t1). getch().j. static int t[10].&k).j++) scanf(“%d”. 41 .j++) scanf(“%d”. printf(“enter block sizes”).i<k.&h[j]).j<k.&p[i]). scanf(“%d”. for(j=0. for(j=0.n.

} Enter no of process3 Input: Enter size of process100 200 300 Enter size of memory900 Enter no of partitions3 output: Process memorysize processize fragmentation 1 300 100 200 2 300 200 100 3 300 300 0 Total fragmentation=300 Program 10: AIM: Write a C Program for MVT best fit. for(i=0.t=0. printf(“enter no of partition”). for(i=0.h> #include<conio.&p).&s). printf(“enter no of process”).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include<stdio.t). printf(“\nprocess\tmemorysize\tprocessizefragementation”).b[10].i++) b[i]=s/n.p. for(i=0. for(i=0. printf(“enter size of memory”).t.i++) c[i]=b[i]-a[i].i.n. 42 . scanf(“%d”. getch(). scanf(“%d”.i<p.c[10]. printf(“total fragementation=%d”.&a[i]). printf(“enter size of each process”).c[i]).b[i]. for(i=0.h> main( ) { int a[10].i<p.&n).i<n.i<p.j.i++) t=t+c[i].i++) scanf(“%d”.i++) printf(“%d\t%d\t%d\t%d\n”.i<p. scanf(“%d”.a[i].s.

printf(“enter size of each process”). for(i=1.&p[i]).&d).h> main() { int n.j++) if(p[i]<=b[j]) { b[j]=b[j]-p[i].i.i++) scanf(“%d”.ts. } sort(int b[10]. b[i]=b[j]. for(i=0.h> #include<conio. printf(“enter no of blocks”).t.i<=d.i<=n.&b[i]).j.&n).int n) { int i. sort(&b.d). b[j]=t. break.i++) scanf(“%d”.i++) for(j=0.i<n.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include<stdio. getch().i++) for(j=i+1.i++) ts=ts+b[i]. } for(i=1. for(i=0.i<d. scanf(“%d”. printf(“enter size of block”). printf(“total fragmentation=%d”. for(i=1. scanf(“%d”.b[10].p[10].j<=d.ts). printf(“\n enter no of process”).j.d.j++) if(b[i]>b[j]) { t=b[i].i<=n. } } Input: enter no of blocks 3 43 .j<=n.

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE enter size of block 43 43 21 enter no of process 3 enter size of each process 32 40 20 Output: total fragmentation 15 44 .

p.i<p.h> #include<conio.&p). printf(“\n enter no.j.j++) printf(“%d”.h> main() { int i.f[10]. } for(i=0.al[10][10]. int a1[10][10].of resources for each type”). n1.j<n1.i<p.c=0.j<n1.i++) { f[i]=0.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 11: AIM: Program for bankers algorithm Source Code: #include<stdio.a1[b][j]). printf(“\n enter the max resources for every process”).i<n1.a=0. for(i=0. scanf(“%d”.j++) { if(a1[i][j]<=t[b][j]) { t[b][j]+=a1[i][j]. for(i=0.n1). for(j=0.t[10][10]. continue.ta[10][10]. scanf(“%d”. for(j=0. printf(“\nenter the max no .i<p. for(i=0.of resources”). printf(“\nenter no . printf(“\nenter allocation resources”).of process”).&a1[i][j]).i++) 45 .k=0.i++) for(j=0. } else printf(“\n wrong resourcesallocation”). max[10][10].&t[b][i]). printf(“\n chance of deadlock occurrence after allocation”).j<n1.b=0.j++) scanf(“%d”.i++) scanf(“%d”. n[10][10].

i<p. for(i=0.n[i][j+2]). for(k=0. } } getch(). b=0.) for(i=0.k<n-1. n[i][j]=max[i][j]-a1[i][j].i++) { j=0. } j=0.n[i][j+1].JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE for(j=0. } else f[i]=0. for(a=0.j<n1. { scanf(“%d”.i<p.i+1).i++) printf(“\n%d %d%d”.a<(p-c). printf(“\n needed resources for every process to start execution”). printf(“\n safe sequence the sequence of process to compute their execution”). f[i]=1.&max[i][j]).j++). c++. if(f[i]==0) { if(n[i][j]<=a1[b][j]&&n[i][j+1]<=a1[b][j+1]&& n[i][j+2]<=a1[b][j+2]) { printf(“\n process %d execution started and completed”.n[i][j].k++) a1[b][k]+=a1[i][k]. } 46 .

of process 5 enter allocation of resources 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Output: total available resources after allocation 3 3 2 enter the max resources for every process 7 5 3 3 2 2 9 0 2 2 2 2 4 3 3 needed resources for every process to start execution 7 4 3 1 2 2 6 0 0 0 1 1 4 3 1 Safe sequence .the sequence of process to complete their execution Procee 2 execution started & completed Procee 4 execution started & completed Procee 5 execution started & completed Procee 1 execution started & completed Procee 3 execution started & completed 47 .of resources 3 enter the max no .JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Input: enter no.of resources for each type 10 5 7 enter the no .

scanf("%d".&a[i]). if(k<t) { for(x=0.i<n.x<k. } } } for(i=m. m=i.m.b[5]. if(x==3) { 48 .x. k++. for(i=0.h> #include<conio.h> main() { int a[10].n.i++) { scanf("%d". scanf("%d".j=0.c=0.i. printf("\nenter no of requests").x++) if(b[x]==a[i]) break.&n). printf("enter requests one by one").x++) if(b[x]==a[i]) break. Source Code: #include<stdio.i++) { for(x=0.k=0.t.i<n.x<k.&t).JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 12: AIM: Write a C program for FIFO page allocation algorithm. if(x==k) { b[k]=a[i]. printf("\nenter no of frames").

getch( ).c). j++. } } printf("no of pagefaults=%d". b[j]=a[i]. } Input: enter no of requests 13 enter no of frames 3 enter requests one by one 2 5 3 2 0 1 3 5 4 2 2 3 4 Output: no of pagefaults 6 49 .JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE c++. if(j==t) j=0.

k=0.i<n. printf("enter requests one by one"). scanf("%d".h> #include<conio.i++) { printf("%d". scanf("%d". m=i.h> main() { int a[50].y. printf("\nenter no of frames").JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Program 13: AIM: Write a C program for LRU page replacement algorithm.m.c[15].s. Source Code: #include<stdio.a[i]).&f). printf("\nenter no of requests").n.b[40].f.x<k. if(x==k) { b[k]=a[i].x++) if(b[x]==a[i]) break.x<f.p=0. c[k]=a[i]. if(k<f) { for(x=0.i. } } } for(i=m.i++) { scanf("%d". for(x=0.x. k++.&n).g. for(i=0.i<n.x++) 50 .&a[i]).l=0.

p).s<f. } printf("\n no of pagefaults=%d". l=0. } for(s=0.x++) if(a[g]==b[x]&&c[x]==b[x]) { y=c[x]. printf("\n"). printf("---------").s++) if(y==b[s]) b[s]=a[i].b[x]).s++) if(c[s]!=-1) { b[s]=a[i].x++) if(b[x]==a[i]) break.g--) for(x=0. if(x==f) { p++.s++) c[s]=b[s].s<f. } for(s=0.s++) printf("%4d". for(x=0.x<f.x<f&&l<=f.JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE printf("%4d". } if(l<f) for(s=0. for(g=i-1. c[x]=-1. l++.b[s]).s<f.s<f. getch(). } 51 . } if(l==f) { for(s=0. break.g>0.

JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Input: enter no of requests 13 enter no of frames 3 enter requests one by one 2 5 3 2 0 1 3 5 4 2 2 3 4 Output: 3 2 5 2 2 5 0 2 5 1 2 0 3 2 0 5 2 0 4 2 5 2 2 5 2 2 5 3 2 5 4 2 3 No of 3 -----------3 -----------3 -----------3 -----------1 -----------1 -----------1 -----------4 -----------4 -----------4 -----------4 -----------pagefaults=7 2 2 2 2 2 2 2 2 2 2 2 5 5 0 0 0 5 5 5 5 3 3 3 3 3 1 1 1 4 4 4 4 4 52 .