Lab File
Lab File
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:
Output:
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;
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:
Priority scheduling
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 ]; }
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 );
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 );
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; }
Output:
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:
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; }
} 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; }