You are on page 1of 37

INDEX

S.No.
1

TOPIC
Write program for process creation and termination for Unix operating system. (Fork, wait(), exit(), etc.) Implement producer-consumer problem using bounded and unbounded buffer. WAP for illustrating interprocess communication. Implement all the CPU scheduling algorithm. (FCFS, SJFS, priority scheduling, Round-Robin, Multilevel queue scheduling etc.) WAP for illustrating all the algorithms of critical-section problem. WAP for bounded buffer problem, readerwriters problem, Dining philosopher's problem using semaphores. Implement Banker's algorithm. WAP to illustrate page replacement algorithm.( FIFO, optimal, LRU, ERU approximation) WAP for file operations (open(), close(), read(), append()) Implement all the disk -scheduling algorithms.( FCFS, SSTF, SCAN,C-SCAN, LOOK, C-LOOK)

SIGNATURE

2 3 4

5 6

7 8

9 10

1. Write a routine for process creation and termination for unix operating systems.
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: perror("fork"); /* something went wrong */ exit(1); /* parent exits */ case 0: printf(" CHILD: This is the child process!\n"); printf(" CHILD: My PID is %d\n", getpid()); printf(" CHILD: My parent's PID is %d\n", getppid()); printf(" CHILD: Enter my exit status (make it small): "); scanf(" %d", &rv); printf(" CHILD: Goodbye!\n"); exit(rv); default: printf("PARENT: This is the parent process!\n"); printf("PARENT: My PID is %d\n", getpid()); printf("PARENT: My child's PID is %d\n", pid); printf("PARENT: I'm now waiting for my child to exit()...\n"); wait(&rv); printf("PARENT: My child's exit status is: %d\n", WEXITSTATUS(rv)); printf("PARENT: Goodbye!\n"); } return 0; }

Output:

3. Implement a program to illustrate shared memory inter process communication


#include <iostream> #include <cstdio> #include <sys/shm.h> #include <sys/stat.h> using namespace std; const int size = 2048; int main(void) { // identifier for shared memeory int segment_id; segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR); /* S_IRUSR - read permission, owner | S_IWUSR -write permission, owner */ // a pointer to shared memeory char *shared_mem; shared_mem = (char *)shmat(segment_id, NULL, 0); // write to shared memory sprintf(shared_mem, "Hi, There!"); // print out the string from shared memeory printf("%s\n", shared_mem); // detach shared memeory shmdt(shared_mem); // remove the shared memory segment shmctl(segment_id, IPC_RMID, NULL); return 0; } Output:

2. Implement producer consumer problem


#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> int buffer[5],f,r; sem_t mutex, full, empty; void *produce(void *arg) { int i; for(i=0;i<6;i++) { sem_wait(&empty); sem_wait(&mutex); printf("produced item is %d\n",i); buffer[(++r)%5]=i; usleep(1); sem_post(&mutex); sem_post(&full); printf("full %u\n",full); } } void *consume(void *arg) { int item,i; for(i=0;i<6;i++) { sem_wait(&full); printf("full %u\n",full); sem_wait(&mutex); item = buffer[(++f)%5]; printf("consumed item is %d\n",item); usleep(1); sem_post(&mutex); sem_post(&empty); } } int main() { pthread_t tid1,tid2; sem_init(&mutex,0,1); sem_init(&full,0,1); sem_init(&empty,0,5);

pthread_create(&tid1,NULL,produce,NULL); pthread_create(&tid2,NULL,consume,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; }

Output:

4. Implement process scheduling algorithms


#include <stdio.h> #include <iostream> int i,j,n; float totwt,totserv; class pr { public: char name[5]; float tservice; int priority; float twait; float trnd; }; pr temp; pr pr1[20];

void prioritysort() { for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) { if(pr1[j].priority>pr1[j+1].priority) { temp=pr1[j]; pr1[j]=pr1[j+1]; pr1[j+1]=temp; } } } void sjfsort() { for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) { if(pr1[j].tservice>pr1[j+1].tservice) { temp=pr1[j]; pr1[j]=pr1[j+1]; pr1[j+1]=temp; } } } void calculation() { pr1[0].twait=0;totwt=0;totserv=pr1[0].trnd=pr1[0].tservice; for(i=1;i<n;i++)

{ //printf("\nwait= %f",totwt); pr1[i].twait = pr1[i-1].twait + pr1[i-1].tservice; totwt +=pr1[i].twait; pr1[i].trnd = pr1[i].twait+pr1[i].tservice; totserv += pr1[i].trnd; } } void printing() { printf("Name \tPriority \tServiceTime \t WaitTime \t TurnAroundTime\n"); for(i=0;i<n;i++){ printf("%s \t %d \t\t %f \t %f \t %f\n",pr1[i].name,pr1[i].priority,pr1[i].tservice,pr1[i].twait,pr1[i].trnd); } printf("\nAverage wait:%f \tAverage turnaround:%f \n",totwt/n,totserv/n); } int main() { // initialization code printf("\nEnter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the details of process %d:\n1.Name: ",i+1); scanf("%s",pr1[i].name); printf("2.Service time: "); scanf("%f",&pr1[i].tservice); printf("3.Priority: "); scanf("%d",&pr1[i].priority); } int ch,exit=0; while(exit==0) { printf("\n---MENU---\n1.FIRST COME FIRST SERVED (FCFS)\n2.PRIORITY SCHEDULING\n3.SHORTEST JOB NEXT (sjf)\n5.Exit\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1:printf("FCFS SCHEDULING>>>\n"); break; case 2:printf("PRIORITY SCHEDULING>>>\n"); prioritysort(); break; case 3:printf("sjf SCHEDULING>>>\n"); sjfsort(); break;

case 5: printf("\nExiting...\n"); default: exit=1; break; } if(exit==0) { calculation(); printing(); } } }

ROUND ROBIN SCHEDULING #include<stdio.h> #define true 1 #define false 0 int n,tq,totwt=0,tottrnd=0; struct pr { int srvst,wt; int wt; int trndt; int flag; int temp; }prc[10]; void printpr() { printf("\nProcess_id\tServicetime\tWaitingtime\tTurnarndtime\n");int i; for(i=0;i<n;i++){ printf("\n%d \t\t%d \t\t%d \t\t%d\t\n",i,prc[i].srvst,prc[i].wt,prc[i].trndt); } printf("Average waiting time=%f\nAverage turnaroundtime=%f\n\n",(float)totwt/n,(float)tottrnd/n); } void rschedule() { int trnd=0,i=0,t1; while(completed()==false) { if(prc[i].flag==false) { if(prc[i].temp==0||prc[i].temp<=tq) { prc[i].flag=true;

trnd+=prc[i].temp; tottrnd+=prc[i].trndt=trnd; prc[i].temp=0; } else{ trnd+=tq; prc[i].temp-=tq; } } i=(i+1)%n; } } int completed() { int sum=0,i; for(i=0;i<n;i++) { if(prc[i].flag==true) sum++; } if(sum==n) return true; return false; } main() { int i; printf("\n\t\t<<<ROUND ROBIN SCHEDULING>>>\nEnter the timequantum: "); scanf("%d",&tq); printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { //printf("\nEnter the details for process %d:\nService time:",i); printf("\n Enter process %d Service time:",i); scanf("%d",&prc[i].srvst); prc[i].flag=false; prc[i].temp=prc[i].srvst; } prc[0].wt=0;int wtprmtr=0; for(i=0;i<n-1;i++) { if(prc[i].srvst<tq) wtprmtr+=prc[i].srvst; else wtprmtr+=tq; prc[i+1].wt=wtprmtr; totwt+=prc[i+1].wt; }

rschedule(); printpr(); } MULTILEVEL QUEUE SCHEDULING #include<stdio.h> float avg_wt,avg_tt; int i = 0,ttl_wt=0,ttl_tt=0,qt; struct process { int prn; char type; int bt; int wt; int tt; struct process *nxt; }*stfor,*stbck,*np,*endfor,*endbck,*temp; void ins_node(struct process *np) { if(np->type == 'f') { if(stfor == NULL) stfor = endfor = np; else { endfor->nxt = np; endfor = np; } } else { if(stbck == NULL) stbck = endbck = np; else { endbck->nxt = np; endbck = np; } } } void del_node(struct process *np,int choice) { if(np->type == 'f') { if(stfor == endfor) stfor = NULL; else stfor = stfor->nxt;

if(choice == 1) delete np; } else { if(stbck == endbck) stbck = NULL; else stbck = stbck->nxt; delete np; } } void ins_dat() { char ch; int j=0; stfor = endfor = stbck = endbck = NULL; do { np = new process; np->prn = ++j; printf("\n Enter the Burst time of Process #%d : ",np->prn); scanf("%d",&np->bt); printf("\n Enter the type of Process #%d (f-foreground or b-background): ",np->prn); np->type = getche(); if(np->type == 'f') np->nxt = stfor; else np->nxt = NULL; np->wt = np->tt = 0; ins_node(np); printf("\n\n Continue ?? : "); ch = getche(); }while(ch == 'y' || stfor == NULL); }

Output:

Shortest job first

Priority scheduling

5. Write a program to implement critical section problems

#include <stdio.h> #include <stdlib.h> #include <pthread.h>

#define COUNT 2 static int Entering[ COUNT ], Number[ COUNT ];

int max ( int* arr ) { int i, max = arr[ 0 ]; for ( i = 1 ; i < COUNT ; i++ ) if ( Number[ i ] > max ) return Number[ i ]; return Number[ 0 ]; }

static void* T1 ( void* args ) { pthread_detach( pthread_self() ); int i = 0, j; while ( 1 ) {

Entering[ i ] = 1; Number[ i ] = 1 + max( Number ); Entering[ i ] = 0; for ( j = 0 ; j < COUNT ; j++ ) { while ( Entering[ j ] ) {} while ( Number[ j ] != 0 && ( Number[ j ] < Number[ i ] || ( Number[ j ] == Number[ i ] && j < i ) ) ) {} }

printf( "Thread ID: %d START!\n", i ); for ( j = 0 ; j < 0xFFFFFF ; j++ ) {} printf( "Thread ID: %d END!\n", i );

Number[ i ] = 0; } return NULL; }

static void* T2 ( void* args ) { pthread_detach( pthread_self() ); int i = 1, j; while ( 1 ) {

Entering[ i ] = 1; Number[ i ] = 1 + max( Number ); Entering[ i ] = 0; for ( j = 0 ; j < COUNT ; j++ ) { while ( Entering[ j ] ) {} while ( Number[ j ] != 0 && ( Number[ j ] < Number[ i ] || ( Number[ j ] == Number[ i ] && j < i ) ) ) {} }

printf( "Thread ID: %d START!\n", i ); for ( j = 0 ; j < 0xFFFFFF ; j++ ) {} printf( "Thread ID: %d END!\n", i );

Number[ i ] = 0; } return NULL; }

int main () { int i = 0; pthread_t t1;

for ( i = 0 ; i < COUNT ; i++ ) Number[ i ] = Entering[ i ] = 0; if ( pthread_create( &t1, NULL, T1, NULL ) ) exit( -1 ); if ( pthread_create( &t1, NULL, T2, NULL ) ) exit( -1 ); // while ( 1 ) {} return EXIT_SUCCESS; } #include<stdio.h> int main() { int i,j,k,turn=0; for(i=0;i<5;i++) { if(turn==i) printf("In Critical Section of %d\n",i); turn=i+1; } return 0;

} #include<stdio.h> int main() { int i,j,k,turn=0,flag[5]; for(i=0;i<5;i++) { flag[i]=1; if(turn==i&&flag[i]==1) printf("In Critical Section of %d\n",i); turn=i+1; flag[i]=0; } return 0; } #include<stdio.h> int main() { int i,j,k,flag[5]; for(i=0;i<5;i++) { flag[i]=1; if(flag[i]==1) printf("In Critical Section of %d\n",i); flag[i]=0; } return 0; }

6. Write a program for readers-writers and Dining philosophers problem


#include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; unsigned int usecs = 1000000; void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); usleep(usecs); sem_wait(&mutex); rcount = rcount - 1; if(rcount==0) sem_post(&writeblock); sem_post(&mutex); } void *writer(void *arg) { int f; f = ((int) arg); sem_wait(&writeblock); data++; printf("Data writen by the writer%d is %d\n",f,data); usleep(usecs); sem_post(&writeblock); } int main() { printf("\n"); int i,b; pthread_t rtid[5],wtid[5]; sem_init(&mutex,0,1); sem_init(&writeblock,0,1); for(i=0;i<=2;i++) { pthread_create(&wtid[i], NULL, writer, (void *)i); pthread_create(&rtid[i], NULL, reader, (void *)i);

} for(i=0;i<=2;i++) { pthread_join(wtid[i], NULL); pthread_join(rtid[i], NULL); } printf("\n"); return 0; }

DINING PHILOSOPHERS PROBLEM


#include<stdio.h> #include<semaphore.h> #include<fcntl.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<pthread.h> struct{ int data[5]; sem_t *mutex[5]; }sh; char *name[]={"p1","p2","p3","p4","p5"}; void *work(void *); main() { pthread_t tid[5]; int i; for(i=0;i<5;i++) { sh.mutex[i]=sem_open(name[i],O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,1); } pthread_setconcurrency(6); setbuf(stdout,NULL); for(i=0;i<5;i++) pthread_create(&tid[i],NULL,work,&i); for(i=0;i<5;i++) pthread_join(tid[i],NULL); for(i=0;i<5;i++) sem_unlink(name[i]); } void *work(void *arg) { int p,i=1; p=*((int *)arg); while(i++<2) { sem_wait(sh.mutex[p]); sem_wait(sh.mutex[(p+1)%5]);

sh.data[p]=1; printf("p%d[%d %d%d]\n",p+1,sh.data[0],sh.data[1],sh.data[2],sh.data[3],sh.data[4]); sleep(1);//eating sh.data[p]=0; sem_post(sh.mutex[p]); sem_post(sh.mutex[(p+1)%5]); sleep(2); } }

Output:

7. Write a program to implement bankers algorithm


#include <stdio.h> #include <cstdlib> #define true 1 #define false 0 int m,n,i,j,count=0,process; int max[10][10],alloc[10][10],need[10][10],c[10],avail[10],finish[10]; void readtable(int t[10][10]) { for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&t[i][j]); } void printtable(int t[10][10]) { for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d",t[i][j]); printf("\n"); } } void readvector(int v[10]) { for(j=0;j<n;j++) scanf("%d",&v[j]); } void printvector(int v[10]) { for(j=0;j<n;j++) printf("\t%d",v[j]); } void init() { printf("enter the number of process\n"); scanf("%d",&m); printf("enter the number of resources\n"); scanf("%d",&n); printf("enter the claim table\n"); readtable(max); printf("enter the allocation table\n"); readtable(alloc); printf("enter the max units of each resource\n"); readvector(c);

for(i=0;i<n;i++) finish[i]=false; } void findavail() { int sum; for(j=0;j<n;j++) { sum=0; for(i=0;i<m;i++) { sum=sum+alloc[i][j]; } avail[j]=c[j]-sum; } } void findneed() { for(i=0;i<m;i++) { for(j=0;j<n;j++) { need[i][j]=max[i][j]-alloc[i][j]; } } } void selectprocess() { int flag; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(need[i][j]<=avail[j]) flag=1; else { flag=0; break; } } if((flag==1)&&(finish[i]==false)) { process=i; count++; break; }

} printf("current status is\n"); printtable(alloc); if(flag==0) { printf("system is in unsafe state\n"); exit(1); } printf("system is in safe state"); } void executeprocess(int p) { printf("excuting process is %d",p); printtable(alloc); } void releaseresource() { for(j=0;j<n;j++) avail[j]=avail[j]+alloc[process][j]; for(j=0;j<n;j++) { alloc[process][j]=0; need[process][j]=0; } } int main() { init(); findavail(); findneed(); do { selectprocess(); finish[process]=true; executeprocess(process); releaseresource(); }while(count<m); printf("\n all proces executed correctly"); return 0; }

Output:

8. Write a program to implement page replacement algorithms


#include<stdio.h> int main() { int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\n ENTER THE NUMBER OF PAGES:\n"); scanf("%d",&n); printf("\n ENTER THE PAGE NUMBER :\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\n ENTER THE NUMBER OF FRAMES :"); scanf("%d",&no); for(i=0;i<no;i++) frame[i]= -1; j=0; printf("\tref string\t page frames\n"); for(i=1;i<=n;i++) { printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++) if(frame[k]==a[i]) avail=1; if (avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]); } printf("\n"); } printf("Page Fault Is %d",count); return 0; }

LRU Page Replacement #include<stdio.h> main() { int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20]; printf("Enter no of pages:"); scanf("%d",&n); printf("Enter the reference string:"); for(i=0;i<n;i++) scanf("%d",&p[i]); printf("Enter no of frames:"); scanf("%d",&f); q[k]=p[k]; printf("\n\t%d\n",q[k]); c++; k++; for(i=1;i<n;i++) { c1=0; for(j=0;j<f;j++) { if(p[i]!=q[j]) c1++; } if(c1==f) { c++; if(k<f) { q[k]=p[i]; k++; for(j=0;j<k;j++) printf("\t%d",q[j]); printf("\n"); } else { for(r=0;r<f;r++) { c2[r]=0; for(j=i-1;j<n;j--) { if(q[r]!=p[j]) c2[r]++; else break;

} } for(r=0;r<f;r++) b[r]=c2[r]; for(r=0;r<f;r++) { for(j=r;j<f;j++) { if(b[r]<b[j]) { t=b[r]; b[r]=b[j]; b[j]=t; } } } for(r=0;r<f;r++) { if(c2[r]==b[0]) q[r]=p[i]; printf("\t%d",q[r]); } printf("\n"); } } } printf("\nThe no of page faults is %d",c); }

OPTIMAL PAGE REPLACEMENT ALGORITHM #include<stdio.h> #define max 25 int main() { int frame[10],length[10],index,highest; int i,j,k,nf,np=0,page[max],temp; int flag=0,pf=0,found=0; printf("Enter no. of Frames:"); scanf("%d",&nf); for(i=0;i<nf;i++) frame[i]=-1;

printf("Enter pages (press -999 to exit):\n"); for(i=0;i<max;i++) { scanf("%d",&temp); if(temp==-999) break; page[i]=temp; np++; } for(i=0;i<np;i++) { flag=0; for(j=0;j<nf;j++) { if(frame[j]==page[i]) { printf("\n\t"); flag=1;break; } } if(flag==0) { for(j=0;j<nf;j++) { for(temp=i+1;temp<np;temp++) { length[j]=0; if (frame[j]==page[temp]) {length[j]=temp-i;break;} } } found=0; for(j=0;j<nf;j++) { if(length[j]==0) { index=j; found=1; break; } } if(found==0) { highest=length[0]; index=0; for(j=1;j<nf;j++)

{ if(highest<length[j]) { highest=length[j]; index=j; } } } frame[index]=page[i]; printf("\nFault: "); pf++; } for(k=0;k<nf;k++) printf("%d\t",frame[k]); } printf("\nNumber of page faults is: %d ",pf); return 0; }

9.Write a program to implement file operations


#include <stdio.h> #include <stdio.h> int main( ) { FILE *fp,*fp2; char stuff[25]; int i,c; fp = fopen("new.txt","a+"); strcpy(stuff,"This is an example line."); for (i = 1; i <= 10; i++) fprintf(fp,"%s Line number %d\n", stuff, i); fclose(fp); /* close the file before ending program */ fp2 = fopen("new.txt","r"); c = getc(fp2) ; while (c!= EOF) { putchar(c); c = getc(fp); } fclose(fp2); return 0; }

10. Write a program to implement disk scheduling algorithms


#include<stdio.h> #include<math.h> void fcfs(int noq, int qu[10], int st) { int i,s=0; for(i=0;i<noq;i++) { s=s+abs(st-qu[i]); st=qu[i]; } printf("\n Total seek time :%d",s); } void sstf(int noq, int qu[10], int st, int visit[10]) { int min,s=0,p,i; while(1) { min=999; for(i=0;i<noq;i++) if (visit[i] == 0) { if(min > abs(st - qu[i])) { min = abs(st-qu[i]); p = i; } } if(min == 999) break; visit[p]=1; s=s + min; st = qu[p]; } printf("\n Total seek time is: %d",s); } void scan(int noq, int qu[10], int st, int ch) { int i,j,s=0; for(i=0;i<noq;i++) { if(st < qu[i]) { for(j=i-1; j>= 0;j--) { s=s+abs(st - qu[j]); st = qu[j];

} if(ch == 3) { s = s + abs(st - 0); st = 0; } for(j = 1;j < noq;j++) { s= s + abs(st - qu[j]); st = qu[j]; } break; } } printf("\n Total seek time : %d",s); } int main() { int n,qu[20],st,i,j,t,noq,ch,visit[20]; printf("\n Enter the maximum number of cylinders : "); scanf("%d",&n); printf("enter number of queue elements"); scanf("%d",&noq); printf("\n Enter the work queue"); for(i=0;i<noq;i++) { scanf("%d",&qu[i]); visit[i] = 0; } printf("\n Enter the disk head starting posision: \n"); scanf("%d",&st); while(1) { printf("\n\n\t\t MENU \n"); printf("\n\n\t\t 1. FCFS \n"); printf("\n\n\t\t 2. SSTF \n"); printf("\n\n\t\t 3. SCAN \n"); printf("\n\n\t\t 4. EXIT \n"); printf("\nEnter your choice: "); scanf("%d",&ch); if(ch > 2) { for(i=0;i<noq;i++) for(j=i+1;j<noq;j++) if(qu[i]>qu[j]) { t=qu[i]; qu[i] = qu[j]; qu[j] = t; }

} switch(ch) { case 1: printf("\n FCFS \n"); printf("\n*****\n"); fcfs(noq,qu,st); break; case 2: printf("\n SSTF \n"); printf("\n*****\n"); sstf(noq,qu,st,visit); break; case 3: printf("\n SCAN \n"); printf("\n*****\n"); scan(noq,qu,st,ch); break; case 4: exit(0); } } } LOOK #include<stdio.h> #include<conio.h> #include<math.h> #define max 20 #define cymax 199 int i,j,req,ttl_tracks=0,cp,np,cposn,nposn; int cyposn[max],temp; void input() { do { clreol(); printf("\n Enter the current header position : "); scanf("%d",&cposn); }while(cposn>cymax || cposn <=0); printf("\n Enter the %d I/O Requests : ",req); cyposn[0] = cposn; for(i=1;i<=req;i++) scanf("%d",&cyposn[i]); } void LOOK() { int ind = 0; for(i=0;i<=req;i++) {

for(j=0;j<req-i;j++) { if(cyposn[j] > cyposn[j+1]) { temp = cyposn[j]; cyposn[j] = cyposn[j+1]; cyposn[j+1] = temp; } } } cp=0; do { if(cyposn[cp] == cposn) break; cp++; }while(cp!=req); int tmp = cp; printf("\nS.No. Current Position Next Position Displacement \n"); printf("---------------------------------------------------------- \n\n"); i=0; cposn = cyposn[cp]; do { if(ind == 0) { if(cp == 0) { cp = tmp; nposn = cyposn[++cp]; ind = 1; } else nposn = cyposn[--cp]; } else nposn = cyposn[++cp]; printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn)); ttl_tracks += (abs(cposn-nposn)); cposn = nposn; }while(nposn!=cyposn[req]); printf("---------------------------------------------------------- \n\n"); printf(" Total Tracks Displaced : %d",ttl_tracks); } int main() { do { clrscr(); printf("\n Enter the number of requests : ");

scanf("%d",&req); }while(req>max || req <=0); input(); LOOK(); return 0; } CSCAN #include<stdio.h> #include<conio.h> #include<math.h> #define max 20 #define cymax 199 int i,j,req,ttl_tracks=0,cp,np,cposn,nposn; int cyposn[max],temp; void input() { do { clreol(); printf("\n Enter the current header position : "); scanf("%d",&cposn); }while(cposn>cymax || cposn <=0); printf("\n Enter the %d I/O Requests : ",req); cyposn[0] = cposn; for(i=1;i<=req;i++) scanf("%d",&cyposn[i]); } void CSCAN() { for(i=0;i<=req;i++) { for(j=0;j<req-i;j++) { if(cyposn[j] > cyposn[j+1]) { temp = cyposn[j]; cyposn[j] = cyposn[j+1]; cyposn[j+1] = temp; } } } cp=0; do { if(cyposn[cp] == cposn) break;

cp++; }while(cp!=req); printf("\nS.No. Current Position Next Position Displacement \n"); printf("---------------------------------------------------------- \n\n"); i=0,j=cp; cposn = cyposn[cp]; do { if(cposn == cyposn[req]) { nposn = 199; cp = -1; } else nposn = cyposn[++cp]; printf(" %d\t\t%d\t\t%d\t\t%d\n",++i,cposn,nposn,abs(cposn-nposn)); ttl_tracks += (abs(cposn-nposn)); cposn = nposn == 199 ? 0 : nposn; }while(nposn != cyposn[j-1]); printf("---------------------------------------------------------- \n\n"); printf(" Total Tracks Displaced : %d",ttl_tracks); } int main() { do { clrscr(); printf("\n Enter the number of requests : "); scanf("%d",&req); }while(req>max || req <=0); input(); CSCAN(); return 0; }

You might also like