C Program For SJF CPU Scheduling Algorithm: #Include #Include #Include

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 24

C program for SJF CPU Scheduling Algorithm

#include<stdio.h> #include<conio.h> #include<process.h> void main() { char p[10][5],temp[5]; int tot=0,wt[10],pt[10],i,j,n,temp1; float avg=0; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:\n",i+1); scanf("%s",&p[i]); printf("enter process time"); scanf("%d",&pt[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pt[i]>pt[j]) { temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; tot=tot+wt[i]; } avg=(float)tot/n; printf("p_name\t P_time\t w_time\n"); for(i=0;i<n;i++) printf("%s\t%d\t%d\n",p[i],et[i],wt[i]); printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

OUTPUT: enter no of processes: 5

enter process1 name: aaa enter process time: 4 enter process2 name: bbb enter process time: 3 enter process3 name: ccc enter process time: 2 enter process4 name: ddd enter process time: 5 enter process5 name: eee enter process time: 1 p_name P_time w_time eee 1 0 ccc 2 1 bbb 3 3 aaa 4 6 ddd 5 10 total waiting time=20 avg waiting time=4.00

C program for Round Robin CPU Scheduling Algorithm

#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> void main() { char p[10][5]; int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m; float avgwt; clrscr(); for(i=0;i<n;i++) { printf("enter the process name : "); scanf("%s",&p[i]); printf("enter the processing time : "); scanf("%d",&pt[i]); } m=n; wt[0]=0; i=0; do

{ if(pt[i]>timer) { rt=pt[i]-timer; strcpy(p[n],p[i]); pt[n]=rt; et[i]=timer; n++; } else { et[i]=pt[i]; } i++; wt[i]=wt[i-1]+et[i-1]; }while(i<n); count=0; for(i=0;i<m;i++) { for(j=i+1;j<=n;j++) { if(strcmp(p[i],p[j])==0) { count++; found=j; } } if(found!=0) { wt[i]=wt[found]-(count*timer); count=0; found=0; }

} for(i=0;i<m;i++) { totwt+=wt[i]; } avgwt=(float)totwt/m; for(i=0;i<m;i++) { printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]); } printf("\ntotal waiting time %d\n",totwt); printf("total avgtime %f",avgwt); }

INPUT : enter the process name : aaa enter the processing time : 4 enter the process name : bbb enter the processing time : 3 enter the process name : ccc

enter the processing time : 2 enter the process name : ddd enter the processing time : 5 enter the process name : eee enter the processing time : 1 OUTPUT : p_name p_time w_time aaa 4 9 bbb 3 3 ccc 2 6 ddd 5 10 eee 1 11 total waiting time : 39 average waiting time : 7.8000

C Program for FCFS CPU Scheduling Algorithm


#include<stdio.h> #include<conio.h> #include<process.h> void main() { char p[10][5]; int tot=0,wt[10],i,n; float avg=0; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:\n",i+1); scanf("%s",&p[i]); printf("enter process time"); scanf("%d",&pt[i]); } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; tot=tot+wt[i]; } avg=(float)tot/n; printf("p_name\t P_time\t w_time\n"); for(i=0;i<n;i++) printf("%s\t%d\t%d\n",p[i],et[i],wt[i]); printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch();

C Program for Priority CPU Scheduling Algorithm


#include<stdio.h> #include<conio.h> void main() { char p[10][5],temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j]) { temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]); } printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

producer consumer simulation


1. 2. 3. 4. 5. 6. 7. 8. //header file #include<stdio.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #include<sys/sem.h> 9. #define MUTEX 0 10. #define FULL 1 11. #define EMPTY 2 12. #define KEY1 2019 13. #define KEY2 6000 14. struct sembuf sop; 15. int n; 16. union semun 17. {

18. 19. 20. 21. 22. 23. 24.

int val; struct semid_ds *buf; unsigned short *array; struct seminfo *_buf; }s; void wait(int sem_num,int semid); void signal(int sem_num,int semid);

25. 26. //implementation file 27. 28. #include"pro_cons.h" 29. void wait(int sem_num,int semid) 30. {

31. 32. 33. 34.


35. }

sop.sem_op=-1; sop.sem_num=sem_num; sop.sem_flg=SEM_UNDO; semop(semid,&sop,1);

36. void signal(int sem_num,int semid)


37. {

38. 39. 40. 41.

sop.sem_op=1; sop.sem_num=sem_num; sop.sem_flg=SEM_UNDO; semop(semid,&sop,1);

42. } 43. 44. //Producer 45. 46. #include"pro_cons.h"

47. main()
48. { 49.

50. 51. 52. 53.


54.

int *data,c,sid,semid,i=0,a,b,in=0; printf("\nEnter the number of elements"); scanf("%d",&n); sid=shmget(KEY1,5*sizeof(int),IPC_CREAT|IPC_EXCL|00666);

55. if(sid==-1)
56. {

57. printf("Error in shared memory\n"); 58. exit(1);


59. }

60. printf("got shared memory\n");


61.

62. data=shmat(sid,0,00400); 63. semid=semget(KEY2,3,00666);


64.

65. 66. 67. 68. 69. 70.

s.val=1; semctl(semid,MUTEX,SETVAL,s); s.val=0; semctl(semid,FULL,SETVAL,s); s.val=5; semctl(semid,EMPTY,SETVAL,s);

71. /* printf("\n\nEnter the values:");*/ 72. do 73. { 74. wait(EMPTY,semid); 75. wait(MUTEX,semid); 76. printf("Enter the %dst value",i+1); 77. scanf("%d",&data[in]); 78. signal(MUTEX,semid); 79. signal(FULL,semid); 80. in=(in+1)%5; 81. i++; 82. }while(i<n); 83. a=shmdt(data); 84. b=shmctl(sid,IPC_RMID,0); 85. } 86. /*CONSUMER*/ 87. #include"pro_cons.h" 88. main() 89. {

90. int *data,sid,temp,i=0,semid,out=0;printf("initialized\n"); 91. sid=shmget(KEY1,5*sizeof(int),00666);printf("getting shared mem\n"); 92. if(sid==-1)


93. {

94. printf("Error in shared memory\n");

95. exit(1);
96. }

97. 98. 99. 100.

printf("got shared memory%d\n",sid); data=shmat(sid,0,00400); semid=semget(KEY2,3,00666); printf("semid%d",semid);

101. do 102. { 103. wait(FULL,semid); 104. wait(MUTEX,semid); 105. printf("In critical section%d\n",data[out]*data[out]); 106. signal(MUTEX,semid); 107. signal(EMPTY,semid); 108. out=(out+1)%5; 109. i++; 110. }while(i<n); 111. }

#include<stdio.h> //global variables. int Pcurr[3][3]; //max of 3 processes, 3 resources int int int int Pmax[3][3]; avl[]={6,4,7}; avltemp[]={6,4,7}; maxres[]={6,4,7};

int running[3]; //Which processes are running (this appears to be used as a boolean) int i,j, safe=0,count=0;; main() { for(i=0;i<3;i++) running[i]=1; //set all the processes to "running" = true (1) int ch; initresources(); while(1) //loop forever {

system("clear"); //calls a command prompt command, this looks like unix clear screen to manage the output. count=0; running. //extremely excessive logic to determing that you have a process

//should be if(!(running[0]+running[1]+running[2])) to replace 8 lines here for(i=0;i<3;i++) { if(running[i]) count++; } if(count==0) { printf("\n\n\n\n\nCongratulations! We have completed execution of all processes successfully without any deadlock!"); getchar(); break; } //end excessive logic section, begin menu section. //The following is just a menu for the user to see what is going one at each iteration. else { printf("\nDeadlock Prevention using Banker's Algorithm:\n"); viewresources(); printf("\n1. Request resource(s) for a process\n"); printf("\n2. View Allocated Resources\n"); printf("\n3. Exit\n"); printf("\nEnter your choice:\n"); scanf("%d",&ch); if(ch==1) { requestresource(); getchar(); } else if(ch==2) { viewresources(); getchar(); } else if(ch==3) break; else printf("\nInvalid Choice, please try again!\n");

} } } //initialization routine, this defines the current "problem" to be tested. //I do not really understand what values go where. initresources() { //for each process, get curr. requirement and max. requirement->check if max. req.... Pmax[0][0]=3; Pcurr[0][0]=1; avl[0]=3; Pmax[0][1]=3; Pcurr[0][1]=2; avl[1]=1; Pmax[0][2]=2; Pcurr[0][2]=2; avl[2]=1; Pmax[1][0]=1; Pcurr[1][0]=1; Pmax[1][1]=2; Pcurr[1][1]=0; Pmax[1][2]=3; Pcurr[1][2]=3; Pmax[2][0]=1; Pcurr[2][0]=1; Pmax[2][1]=1; Pcurr[2][1]=1; Pmax[2][2]=5; Pcurr[2][2]=1; } // this routine mimics an OS resource request which basiclly checks if a resource is busy, //if not gives it to your process, and then marks it busy. If it is busy to begin with some //strategy is used to deny the request. Here, he deadlocks if the request cannot be done -//I think that means that the processes cannot complete because of lack of resources, no matter //what you do (Besides runnign them one at a time). requestresource() { //check if it is allocated, will it go to deadlock int proc, res[3]; printf("\nFor which Process, you need resources?(1-3):\n"); scanf("%d",&proc); proc--; if(running[proc]) { printf("\nCurrently this process needs the foll. resources:\n"); printf("R1\tR2\tR3\n"); for(i=0;i<3;i++) printf("%d\t",Pmax[proc][i]-Pcurr[proc][i]); for(i=0;i<3;i++) {

loop_3: printf("\nEnter no. of Resource %d to Allocate to Process %d:\n",i+1,proc+1); scanf("%d",&res[i]); if((res[i]>(Pmax[proc][i]-Pcurr[proc][i]))||(res[i]>avl[i])) { printf("\nInvalid Value!, try again!"); goto loop_3; } } getchar(); if(allocate(proc,res)) { printf("\nResources successfully allocated.\n"); if(checkcompletion(proc)) printf("\nProcess %d has completed its execution and its resources have been released.\n",proc+1); } else printf("\nResouces cannot be allocated as it may lead to Deadlock!\n"); } else { printf("\nInvalid Process no.\n"); getchar(); } } ///allocate a resource to a process, used in the above routine. T //his is just management code to mark the appropriate stuff when an allocation is allowed. int allocate(int proc, int res[3]) { for(i=0;i<3;i++) { Pcurr[proc][i]+=res[i]; avl[i]-=res[i]; } if(!checksafe()) { for(i=0;i<3;i++) { Pcurr[proc][i]-=res[i]; avl[i]+=res[i]; } return 0; } return 1; } int checkcompletion(int proc) {

if((Pcurr[proc][0]==Pmax[proc][0])&&(Pcurr[proc][1]==Pmax[proc] [1])&&(Pcurr[proc][2]==Pmax[proc][2])) { for(i=0;i<3;i++) { avl[i]+=Pmax[proc][i]; } running[proc]=0; return 1; } return 0; } //print the state of the resources for the user to study. viewresources() { printf("\n----Current Snapshot of the system----\n"); printf("\nMax. resources in the system:\n"); printf("R1\tR2\tR3\n"); for(i=0;i<3;i++) printf("%d\t",maxres[i]); printf("\nCurrent resources available in the system:\n"); printf("R1\tR2\tR3\n"); for(i=0;i<3;i++) printf("%d\t",avl[i]); printf("\n\nMax. resources required for Completion of each process:\n"); printf("\tR1\tR2\tR3\n"); for(i=0;i<3;i++) { if(running[i]) { printf("P%d\t",i+1); for(j=0;j<3;j++) printf("%d\t",Pmax[i][j]); printf("\n"); } } printf("\n\nCurr. resources allocated for each process:\n"); printf("\tR1\tR2\tR3\n"); for(i=0;i<3;i++) { if(running[i]) { printf("P%d\t",i+1); for(j=0;j<3;j++) printf("%d\t",Pcurr[i][j]); printf("\n"); }

} } // this is what you want: the bankers algorithm portion of the code! It uses the algorithm to generate a true or false (safe or unsafe) value //which is used to see if a resource can be allocated to a given process. int checksafe() { //Check if atleast one process can get all resources it needs --> Banker's algorithm safe=0; for(i=0;i<3;i++) { avltemp[i]=avl[i]; } for(i=0;i<3;i++) { if(running[i]) { if((Pmax[i][0]-Pcurr[i][0]<=avltemp[0])&&(Pmax[i][1]-Pcurr[i] [1]<=avltemp[1])&&(Pmax[i][2]-Pcurr[i][2]<=avltemp[2])) { for(j=0;j<3;j++) avltemp[j]+=Pcurr[i][j]; safe=1; } } } return safe; }

Optimal Page replacement algorithm


#include< stdio.h > #include< conio.h > int fr[3]; void main() {void display(); int p[12]={5,6,8,7},i,j,fs[3]; int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3;

clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<3;i++) { if(fr[i]==p[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<3;i++) { if(fr[i]==-1)

{ fr[i]=p[j]; flag2=1; break; } } } if(flag2==0) { for(i=0;i<3;i++) lg[i]=0; for(i=0;i { for(k=j+1;k<12;k++){ if(fr[i]==p[k]) { lg[i]=k-j; break; } } } found=0; for(i=0;i

{ if(lg[i]==0) { index=i; found=1; break; } } if(found==0) { max=lg[0]; index=0; for(i=1;i { if(max { max=lg[i]; index=i; } } } fr[index]=p[j]; pf++; }

display(); } printf("\nno of page faults %d",pf); getch(); } void display() { int i; printf("\n"); for(i=0;i<3;i++) printf("\t%d",fr[i]); }

---------------------------------------------------------OUT PUT: ----------------------------------------------------------

2 2 2 2 2 2

-1 3 3 3 3 3

-1 -1 -1 1 5 5

4 4 4 2 2 2

3 3 3 3 3 3

5 5 5 5 5 5

No of page faults : 4
LRU page replacement algorithm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include<stdio.h> #include<conio.h> int fr[3]; void main() { void display(); int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3]; int index,k,l,flag1=0,flag2=0,pf=0,frsize=3; clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0,flag2=0; for(i=0;i<3;i++) { if(fr[i]==p[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<3;i++) { if(fr[i]==-1) { fr[i]=p[j]; flag2=1; break; }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

} } if(flag2==0) { for(i=0;i<3;i++) fs[i]=0; for(k=j-1,l=1;l<=frsize-1;l++,k--) { for(i=0;i<3;i++) { if(fr[i]==p[k]) fs[i]=1; } } for(i=0;i<3;i++) { if(fs[i]==0) index=i; } fr[index]=p[j]; pf++; } display(); } printf("\n no of page faults :%d",pf); getch(); } void display() { int i; printf("\n"); for(i=0;i<3;i++) printf("\t%d",fr[i]); }

OUTPUT : 2 -1 -1 2 3 -1 2 3 -1 2 3 1 2 5 1 2 5 1 2 5 4 2 5 4 3 5 4 3 5 2 3 5 2 3 5 2 no of page faults : 4

Dining Philosopher's Problem #include char state[10],self[10],spoon[10]; void test(int k) { if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k=1)]!='e')) { state[k]='e'; self[k]='s'; spoon[k]='n'; spoon[(k+4)%5]='n'; } } void pickup(int i) { state[i]='h'; test(i); if(state[i]=='h') self[i]='w'; } void putdown(int i) { state[i]='t'; spoon[i]='s'; spoon[i-1]='s'; test((i+4)%5); test((i+1)%5); } main() { int ch,a,n,i; system("clear"); printf("\n Dining Philosopher's Problem:"); printf("\n...........................................\n"); for(i=0;i<5;i++) { state[i]='t'; self[i]='s'; spoon[i]='s'; } printf("\n Initial state of each philososphers:"); printf("\n Phil No : \t Think/Eat \t Status \t\t Spoon"); for(i=0;i<5;i++) printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]); printf("\n 1.Exit \n 2.Hungry \n 3.Thinking"); printf("\n Enter your choice :");

scanf("%d",&ch); while(ch!=1) { switch(ch) { case 1: return 0; case 2: printf("\n Enter wchich philosophers is Hungry :"); scanf("%d",&n); pickup(n-1); break; case 3: printf("\n Enter which Philosopher is Thinking :"); scanf("%d",n); putdown(n-1); break; } printf("\n State of each Philosepher :"); printf("\n Phil no: \t Think/Eat \t Status \t\t Spoon"); for(i=0;i<5;i++) printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]); printf("\n Exit \n 2.Hungry \n 3. Thinking"); scanf("%d",&ch); } }

Implementing indexed File allocation using C Program


// Indexed Allocation #include #include #include struct node { int file_name; int data; int is_free; int size; int directory; int cnt; struct node* link; struct node* inner_link[30]; }; struct node* defaultFile() {

struct node* temp = (struct node*)malloc( sizeof(struct node) ); temp->link = NULL; temp->is_free=0; temp->data='a'; temp->file_name=99; temp->directory = 1; return temp; } struct node* insert(struct node *rt, int size, int fname) { struct node* temp = rt; struct node* first=NULL,*last=NULL,*tt=NULL; int flag=0,act_size=0, inner_fname=100,cnt=0; last=first; act_size = size; if ( size>temp->size ) { printf("There is not enough space on the disk to write that file\n"); return rt; } while( temp->link ) { temp = temp->link; } first = defaultFile(); while( act_size>0 ) { tt = defaultFile(); if (act_size>50) tt->size = 50; else tt->size = act_size; tt->file_name = inner_fname; first->inner_link[cnt] = tt; tt->directory = 0; tt->is_free = 0; act_size -= 50; inner_fname += 1; cnt += 1; } temp->link = first; first->is_free = 1; first->cnt = cnt; first->size = size; first->file_name = fname; act_size = rt->size; rt->size = act_size-size; return rt; } void printFiles(struct node* rt) { struct node *temp = rt, *tt; int first=0,cnt=0; printf("format is (File name, size)\n"); printf("\t(%d,%d)\n",rt->file_name,rt->size); while( temp ) { if ( temp->is_free ) {

printf("\t(%d,%d)\n",temp->file_name,temp->size); first = 0; while( cntcnt ) { tt = temp->inner_link[cnt]; printf("\t\t(%d,%d)\n",tt->file_name, tt->size); cnt += 1; } printf("\n"); } temp = temp->link; } } struct node* combine(struct node *rt,int fname) { struct node *temp=rt,*nt=NULL,*temp1=temp->link; int size=0; if ( rt->file_name==fname ){ printf("You cannot that file as thats just to show that that much amount of space is left in the disk\n"); return rt; } while( temp1 ) { if (temp1->is_free==0 && temp1->file_name==fname ) { size = temp1->size; temp->link = temp1->link; temp1 = temp->link; } else { temp = temp1; temp1 = temp1->link; } } rt->size += size; return rt; } struct node* deleteFiles(struct node* rt, int fname) { struct node *temp = rt,*nt=NULL; int flag=0; while( temp && flag==0 ) { if (temp->file_name==fname) { temp->is_free=0; flag=1; } temp = temp->link; } if( flag==0 ){ printf("There doesnt exist any file with that name\n"); } return combine(rt,fname); } int main() { int flag,no,size,data;

struct node *root; root = defaultFile(); root->size=1000; data=100; flag=no=size=0; while( flag==0 ) { printf("Enter no's \n1.insert\n 2.Delete\n 3.Print files \n 4.Exit\n"); scanf("%d",&no); printf(" no is %d\n",no); switch(no) { case 1: printf("Enter file size\n"); scanf("%d",&size); root = insert(root, size, data); data = data+1; break; case 2: printf("Enter file name to delete\n"); scanf("%d",&size); root = deleteFiles(root, size); break; case 3: printFiles(root); break; case 4: flag=1; printf("Quitting from loop\n"); break; default: printf("Enter a valud no \n"); break; } } }

You might also like