You are on page 1of 19

/* A program for First Come First Served*/ #include<stdio.h> #include<conio.

h> void main() { int btime[20],n,i,j,ttime=0,wtime=0; float avgtime; clrscr(); printf("\n enter the no:of jobs:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n enter the burst time of job %d:",i); scanf("%d",&btime[i]); } printf("\n\torder in which job is processed:\n"); printf("\n\tjob\tburst time\twaiting time\tturnaround time\n"); for(i=1;i<=n;i++) { wtime=0; for(j=1;j<=i;j++) { ttime=ttime+btime[j]; wtime=wtime+btime[j]; } printf("\n\tjob %d\t\t%d\t\t%d\t\t%d\n",i,btime[i],wtime-btime[i],wtime); } printf("\n\ttotal turnaroundtime is %d",ttime); avgtime=(float)(ttime/n); printf("\n\tthe average turn around time is:%f",avgtime); getch(); } Output: enter the no:of jobs:3 enter the burst time of job 1:4 enter the burst time of job 2:6 enter the burst time of job 3:2 order in which job is processed: job burst waiting time job 1 4 0 job 2 6 4 job 3 2 10 total turnaroundtime is:26 the average turn around time is:8.000000

turnaround time 4 10 12

/*A program for Shortest Job First*/ #include<stdio.h>

#include<conio.h> void main() { int btime[20],b[20],a[20],n,i,j,tt=0,k,w[10],small; float att; clrscr(); printf("\n\tenter the no:of jobs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\t enter burst time of job %d:",i+1); scanf("%d",&btime[i]); b[i]=btime[i]; a[i]=1; } printf("\n\t order in which jobs are processed:"); printf("\n\t job name \t burst time \t waiting time\t turnaround time"); for(i=0;i<=n;i++) { small=btime[i]; k=i; for(j=i+1;j<n;j++) if(btime[j]<small) { small=btime[j]; k=j; } btime[k]=btime[i]; btime[i]=small; j=a[i]; a[i]=a[k]; a[k]=j; } tt=0; for(i=0;i<n;i++) { k=0; for(j=0;j<=i;j++) { tt+=btime[j]; k+=btime[j]; } printf("\n\t job %d\t\t%d\t\t%d\t\t%d",a[i]+1,btime[i],k-btime[i],k); } printf("\n\t total turnaround time is:%d",k); att=k/n; printf("\n\t average turnaround time is:%.2f",att); getch(); } Output: enter the no:of jobs:4 enter burst time of job 1:5 enter burst time of job 2:3 enter burst time of job 3:6 enter burst time of job 4:4 order in which jobs are processed: job name burst time waiting time

turnaround time

job 2 3 job 4 4 job 1 5 job 3 6 total turnaround time is:18 average turnaround time is:4.00

0 3 7 12

3 7 12 18

/*A program for Priority job scheduling* #include<stdio.h> #include<conio.h> void main() { int btime[20],b[20],a[20],n,j,tt=0,k,i,small,pp[20],si; float att; clrscr();

printf("\n\t enter the number of jobs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\t enter the burst time and priority of job %d:",i+1); scanf("%d%d",&btime[i],&pp[i]); b[i]=a[i]; a[i]=i; } printf("\n\t order in which jobs are processed:"); printf("\n\t job name\t burst time\t priority\t waiting time\t turnaround time"); for(i=0;i<n;i++) { small=pp[i]; si=btime[i]; k=i; for(j=i+1;j<n;j++) if(pp[j]>small) { small=pp[j]; si=btime[j]; k=j; } pp[k]=pp[i]; pp[i]=small; btime[k]=btime[i]; btime[i]=si; j=a[i]; a[i]=a[k]; a[k]=j; } tt=0; for(i=0;i<n;i++) { k=0; for(j=0;j<=i;j++) { tt+=btime[j]; k+=btime[j]; } printf("\n\t job %d\t\t%d\t\t%d\t\t%d\t\t%d",a[i]+1,btime[i],pp[i],k-btime[i],k); } printf("\n\t total turnaround time is:%d",k); att=(float)k/n; printf("\n\t average turnaround time is:%.2f",att); getch(); } Output: enter the number of jobs:4 enter the burst time and priority of job 1:6 3 enter the burst time and priority of job 2:5 4 enter the burst time and priority of job 3:10 1 enter the burst time and priority of job 4:8 2 order in which jobs are processed: job name burst time priority waiting time job 2 5 4 0 job 1 6 3 5

turnaround time 5 11

job 4 8 2 job 3 10 1 total turnaround time is:29 average turnaround time is:7.25

11 19

19 29

FIRST IN FIRST OUT PAGE REPLACEMENT ALGORITHM Program: # include < Stdio.h> Void Main ( ) { int a [20], c[10], i. k. ps, nop, npf = 0, nps = 0 ; clrscr ( ) ; printf (\n\tEnter no of pages : ) ; scanf (%d , & nop ) ; printf (\n\tEnter pages : ) ; for ( i = 0 ; i <nop ; i ++ ) {scanf ( % d , & a [ i ] ) ;} printf ( \ n \ tEnter no of page frames : ) ; scanf ( % d , & ps ) ; for ( i = 0 ; i < nop ; i ++ )

for ( k = 0 ; c [ k ] ! = a [ i ] & & k <nps ; k++ ) ; if (k = = nps) { npf++ ; if (nps = = ps) for (k = 0 ; k < nps 1 ; k++ ) c[k]=c[k+1]; c[k]=a[i]; if ( nps<ps ) nps++ ; } printf ( \n ) ; for (k = 0 ; k<nps ; k++ ) printf (%d , c [ k ] ) ; }printf (\ n \ tNo of page faults occurred are %d, npf ) ; getch ( ) ; } SAMPLE INPUT AND OUTPUT FORMAT : Enter no of pages : 20 Enter pages : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter no of pages frames : 3 7 70 701 012 012 123 230 304 042 423 230 230 230 301 012 012 012 127 270 701 No of page faults occurred are 15 \ \ LEAST RECENTLY USED PAGE REPLACEMENT ALGORITHM Program: # include < Stdio.h> void main ( ) { int a [50], c[10], I , j , k . ps, nop, npf = 0, nps = 0 ; clrscr ( ) ; printf ( \ n\ tEnter no of pages : ) ; scanf (%d , & nop ) ; printf ( \ n\ tEnter pages : ) ; for ( i = 0 ; i <nop ; i ++ ) { scanf ( % d , & a [ i ] ) ; } printf ( \ n \ tEnter no of page frames : ) ; scanf ( % d , & ps ) ;

for ( i = 0 ; i < nop ; i ++ ) { for ( k = 0 ; c [ k ] ! = a [ i ] && k <nps ; k++ ) ; if (k = = nps) { npf++ ; if (nps = = ps) for (k = 0 ; k < nps 1 ; k++ ) c[k]=c[k+1]; c[k]=a[i]; if ( nps<ps ) nps++ ; } else { for (j = k; j<nps - 1 ; j++ ) c[j]=c[j+1]; c[j]=a[i]; } printf ( \ n \ tNo of page faults occurred are %d, npf ) ; getch ( ) ; } SAMPLE INPUT AND OUTPUT FORMAT : Enter no of pages : 20 Enter pages : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter no of pages frames : 3 7 70 701 012 120 203 230 042 423 230 203 032 312 120 201 170 701 No of page faults occurred are 12

OPTIMAL PAGE REPLACEMENT ALGORITHM Program: # include < Stdio.h> void main ( )

{ int a [20], c[20], i , j , k . ps, nop, npf = 0, nps = 0 , m,n; clrscr ( ) ; printf ( \ nenter no of pages : ) ; scanf (%d , & nop ) ; printf ( \ n enter pages : ) ; for ( i = 0 ; i <nop ; i ++ ) { scanf ( % d , & a [ i ] ) ; } printf ( \ n enter no of page frames : ) ; scanf ( % d , & ps ) ; for ( i = 0 ; i < nop ; i ++ ) { for ( k = 0 ; c [ k ] ! = a [ i ] && k <nps ; k++ ) ; if (k = = nps) { npf ++ ; if (nps = = ps) {m=I+1; n=0 for ( j = 0 ; j < nps 1 ; j ++ ) { for ( k = i + 1; c [ j ] ! = a [ k ] && k < nop ; k ++ ) ; If ( m < k ) {m=k; n=j; } } for (k = n ; k<nps - 1 ; k++ ) c[k]=c[k+1]; }c[k]=a[i]; if ( nps < ps ) nps ++ ; } printf ( \ n ) ; for ( k = 0 ; k < nps ; k ++ printf ( % d , c [ k ] ) ; } Printf ( \ n no of page faults occurred are : % d , npf ) ; } Output : Enter no. of pages : 20 Enter pages : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter no of page frames : 3 7 70 701 012 012 023 023 234 234 304 230 230 201 201 201 201

017 017 017 No of page faults occurred are 9

FILE MANAGEMENT Program : # include<stdio.h> # include<conio.h> # include<process.h> # include<dir.h> Void main (void) { int stat, ch, I ; Char name [ 100 ] ; int handle ; char buf [100 ] = C was implemented on UNIX operating system. ; clrscr ( ) ; printf ( \ n \ t1. Create file. ) ; printf ( \ n \ t2. Delete file. ) ; printf ( \ n \ tChoose ur choice : ) ; scanf (%d, & ch ) ; Switch (ch) { Case 1 : { Printf ( \ n \ tEnter file name : ) ; Scanf (%s, & name ) ; handle = Creat new (name, 0 ) ; if ( handle = = - 1 ) printf (%s already exists. \ n, name ) ; else { Write (handle, buf, strlen (buf) ) ; Close (handle) ; Printf ( \ n \ tFile has been created. ) ; } break ; } case 2 : { printf (File to delete: ); scanf ( % S , name ) ; if ( remove (name) = = 0 ) printf (Removed % S. \ n , name ) ; else printf ( remove) ; break ; } default : { Printf ( \ n \ twrong choice.) ;

} } getch ( ) ; } SAMPLE INPUT AND OUTPUT FORMAT : Create file. Delete file. Choose ur Choice : 1 Enter file name : Kiet File has been created. Create file. Delete file. Choose ur Choice : 1 Enter file name : Kiet File has been created. Displaying File content C : \ software \ TC \ BIN > type kiet C was implemented on UNIX operating system. C : \ software \ TC \ BIN > 1. Create file. 2. Delete file. Choose ur Choice : 2 File to delete : kiet Removed kiet. C : \ software | TC \ BIN > type kiet File not found kiet DIRECTORY MANAGEMENT Program: # include <stdio.h> # include <conio.h> # include <process.h> # include <dir.h> Void main (void) { int stat, ch, i ; char name [ 100 ] ; struct ffblk ffblk ; int done ; printf ( \ n \ t1. Create Directory.) ; printf ( \ n \ t2. Delete Directory.) ; printf ( \ n \ t3. Display files in the Directory.) ; printf ( \ n \ tChoose ur choice :) ; scanf ( % d, & ch) ; switch (ch) { Case 1 :

{ printf ( : \ n \ tEnter Directory name :) ; scanf ( % S, & name ) ; stat = mkdir (name) ; if ( !stat ) { printf (Directory created \ n) ; } else } printf ( Unable to create directory \ n) ; } break ; } Case 2 : { printf ( \ n \ tEnter existed directory name :) ; scanf ( % S , & name ) ; stat rmdir (name) ; if ( !stat ) printf ( \ n Directory deleted \ n ) ; else { printf ( \ n Unable to delete directory \ n ) ; } break ; } Case 3 : { printf ( Directory listing of *.* \ n ) ; done = find first ( *.* , & ffblk, 0 ) ; i=0; while ( !done ) { if ( I < = 20 ) { printf ( % S \ n , ffblk.ff_name ) ; done = find next ( & ffblk ) ; i ++ ; } else { getch ( ) ; i=0 } break ; } default : printf ( : \ n \ twrong choice. ) ; } getch ( ) ; } SAMPLE INPUT AND OUTPUT FORMAT : Create Directory. Delete Directory. Display files in the Directory.

Choose ur choice : 1 Enter Directory name : hai 2 Directory Created Create Directory. Delete Directory. Display files in the Directory. Choose ur choice : 2 Enter existed directory name : hai 2 Directory deleted Create Directory. Delete Directory. Display files in the Directory. Choose ur choice : 3 Directory listing of *.* AA.C AA.CPP AA.OBJ AAA.TXT ABC.CPP ABC.TXT ASSIGN1.CPP ASSIGN1.EXE ASSIGN1.OBJ BUILTINS.MAK CH24_2.BAK CH24_2.CPP TC0034.SWP B10-DLL.OBJ B10-DLL.EXE KINTOPOS.OBJ KINTOPOS.EXE KIFTOPF.OBJ KIFTOPF.EXE INTOPOST.OBJ INTOPOST.EXE PREDICTI.OBJ PREDICTI.EXE TC0035.SWP OPREP.OBJ OPREP.EXE PREP.OBJ PREP.EXE FCFSPAGE.OBJ FCFSPAGE.EXE FIFOPAGE.OBJ FIFOPAGE.EXE LRUPAGE.OBJ LRUPAGE.EXE DIRMANG.OBJ DIRMANG.EXE ROUNDROBIN CPU SCHEDULING

#include<conio.h> #include<stdio.h> int a[20] [20]; int completed(int[],int); void main() { int I,j,q,c=0,btime[10],n; float att; clrscr(); printf(Enter no of jobs:); scanf(%d,&n); for(i=0;i<n;i++) { printf(\n Enter burst time of job %d:,i+1); scanf(%d,&btime[i]); } printf(\n Enter the length of the time quantum:); sacnf(%d,&q); j=0; i=0; while(!completed(btime,n)) {if(btime[i]<q&&btime[i]!=0) { a[j] [0]=i+1; a[j++][1]=btime[i]; btime[i]=0; } else { if(btime[i]!=0) { a[j] [0]=i+1; a[j++] [1]=q; btime[i]-=q; } } i++; if(i= =n) i=0; } printf(\n Job Name allocated timeslice is:\n); for(i=0;i<j;i++) { printf( \n Job %d \t\t %d\n,a[i] [0],a[i] [1]); } for(i=0;i<n;i++) { printf(\n Turn around time of Job %d,i+1,printc(i+1,j)); c+=printc(i+1,j); } printf(\n Total turn around time is : %d,c); att=(float)c/n; printf(\n Average turn around time is %f,att); getch();

} int completed(int bt[10],int x) { int I; for(i=0;i<x;i++) { if(bt[i]!=0) return 0; } return 1; } Int printc(int,int m) { int k,l,c=0; for(k=0;k<m;k++) { if(a[k] [0]= =i) { c=0; for(l=0;l<=k;l++) c+=a[l] [1]; } } return c; } SAMPLE INPUT AND OUTPUT: Enter no of jobs: 3 Enter burst time of job 1: 5 Enter burst time of job 2: 4 Enter burst time of job 3: 3 Enter the length of time quantum: 4 JOBNAME ALLOCATED TIMESLICE Job1 Job2 Job3 Job1 4 4 3 1

Turn aroundtime of job1: 12 Turn aroundtime of job2: 8 Turn aroundtime of job3: 11 Total turnaroundtime : 31 Average Turnaroundtime: 10.333333

PROGRAM: CREATION OF THREADS WITH SYNCHRONIZATION import java.lang.Thread;

import java.lang.System; import java.lang.Math; import java.lang.Interrupted Exception; class ThreadSynchronization { public static void main(String args[]) { MyThread thread1=new MyThread(Thread1:); MyThread thread2=new MyThread(Thread2:); thread1.start(); thread2.start(); boolean thread1IsAlive=true; boolean thread2IsAlive=true; do { if(thread1IsAlive && !thread1.isAlive()) { thread1IsAlive=false; System.out.println(Thread1 is dead); } if(thread2IsAlive && !thread2.isAlive()) { thread2IsAlive=false; System.out.printLn(Thread2 is dead); } }while(thread1IsAlive thread2IsAlive); } } class MyThread extends Thread { static String message[]={Java,is,hot,aromatic,and,invigorating.}; public MyThread(String id) { super(id); } public void run() { Synchronized Output.display List(getName(),message); } void random Wait() { try { sleep(3000); } catch(Interrupted Exception x) { System.out.println(Interrupted); } } } class Synchronized Output { publicstatic synchronized void display List(String name,String list[]) {

for(int i=0;i<list.length;++i) { MyThread t=(MyThread)Thread.currentThread(); t.random Wait(); System.out.println(name + list[i]); } } } SAMPLE INPUT AND OUTPUT: C:\DOCUME ~ 1\Srinivas>javac threadsync.java C:\DOCUME ~ 1\Srinivas>java Thread Synchronization Thread1:Java Thread1:is Thread1:hot Thread1:aromatic Thread1:and Thread1:invigorating. Thread1 is dead Thread2:Java Thread2:is Thread2:hot Thread2:aromatic Thread2:and Thread2:invigorating. Thread2 is dead C:\DOCUME ~ 1\Srinivas> PROGRAM: CREATION OF THREADS WITHOUT SYNCHRONIZATION import java.lang.Thread; import java.lang.System; import java.lang.math; import java.lang.Interrupted Exception; class nosynchronization { public static void main(String args[]) { MyThread thread1=new MyThread(Thread1:); MyThread thread2=new MyThread(Thread2:); thread1.start(); thread2.start(); boolean thread1IsAlive=true; boolean thread2IsAlive=true; do { if(thread1IsAlive && !thread1.isAlive()) { thread1IsAlive=false; System.out.println(Thread1 is Dead); } if(thread2IsAlive && !thread2.isAlive()); { thread2IsAlive=false; System.out.println(Thread2 is Dead);

} }while(thread1IsAlive thread2IsAlive); } } class MyThread extends Thread { static String message[]={Java,is,hot,aromatic,and,invigorating}; public MyThread(String id) { super(id); } public void run() { Synchronized Output.displayList(getName(),message); } void random Wait() { try { sleep(long)(3000*Math.random())); } catch(Interrupted Exception x) { System.out.println(Interrupted); } } } class SynchronizedOutput { public static void display List(String name,String list[]) { for(int i=0;i<list.length;++i) { MyThread t=(MyThread)Thread.currentThread(); t.random Wait(); System.out.println(name + list[i]); } } SAMPLE INPUT AND OUTPUT: C:\Documents and Settings\Srinivas>java nosynchronization Thread1:Java Thread2:Java Thread1:is Thread1:hot Thread1:aromatic Thread2:is Thread2:hot Thread1:and Thread1:invigorating Thread1 is Dead Thread2:aromatic Thread2:and Thread2:invigorating Thread2 is Dead

C:\Documents and Settings\Srinivas>

PROGRAM: DEADLOCK OCCURRENCE BETWEEN TWO PROCESSES Class Deadlock implements Runnable { Public static void main(String[] args) { Deadlock d1=new Deadlock(); Deadlock d2=new Deadlock(); Thread t1\new Thread(d1); Thread t2=new Thread(d2); d1.grabIt=d2; d2.grabIt=d1; t1.start(); t2.start(); try { t1.join(); t2.join(); } catch(Interrupt Exception e) { System.out.println(e); } System.exit(0); } Deadlock grabIt; public synchronized void run0 { try { Thread.sleep(2000); } catch(Interrupted Exception e) { System.out.printIn(e); } grabIt.sync_method(); } public synchronized void sync_method() { try { Thread.sleep(2000); } catch(Interrupted Exception e) { System.out.printIn(e); } System.out.printIn(in sync_method); } }

SAMPLE INPUT AND OUTPUT: This program doesnt print anything because two processes are entered into deadlock.

You might also like