943,590 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19715
  • C RSS
Oct 13th, 2008
0

banker's algorithm implementation

Expand Post »
we are given the assignment to implement the banker's algorithm in c. this implementation must run in the linux terminal and program must be utilized the os in the time of execution. this means that it must capable on real-time execution with the os. i really don't have any idea how to do this. aside from that i am not that proficient in c, i'm more into java. can someone help me with this. any help would much appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joelogs is offline Offline
8 posts
since Oct 2008
Oct 13th, 2008
1

Re: banker's algorithm implementation

>aside from that i am not that proficient in c, i'm more into java
Last I checked, homework assignments were meant to help you learn the subject matter. Thus, claiming that you aren't proficient isn't exactly a good reason not to try.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 15th, 2008
0

Re: banker's algorithm implementation

Click to Expand / Collapse  Quote originally posted by joelogs ...
we are given the assignment to implement the banker's algorithm in c. this implementation must run in the linux terminal and program must be utilized the os in the time of execution. this means that it must capable on real-time execution with the os. i really don't have any idea how to do this. aside from that i am not that proficient in c, i'm more into java. can someone help me with this. any help would much appreciated.
Start coding and give it a try... we'll guide you if you get stuck... we will help you if you can't proceed... we will do everything to get you moving... thats what we do at DANIWEB...
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Aug 30th, 2010
0
Re: banker's algorithm implementation
BANKER'S ALGORITHM PROGRAM IN C/C++
POSTED BY : NITIJ ADHWARYU

#include<stdio.h>
//global variables.
int Pcurr[3][3]; //max of 3 processes, 3 resources

int Pmax[3][3];
int avl[]={6,4,7};
int avltemp[]={6,4,7};
int 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;
//extremely excessive logic to determing that you have a process
running.

//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;
}
{//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())
{
Attached Files
File Type: cpp banker.cpp (3.2 KB, 1732 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NITIJ is offline Offline
1 posts
since Aug 2010
Aug 30th, 2010
0
Re: banker's algorithm implementation
Has anyone ever managed to figure out why it is that people feel the need to post solutions to homework problems? It seems quite pointless, especially when it's in a thread that's two years dead....
Reputation Points: 383
Solved Threads: 186
Posting Virtuoso
jon.kiparsky is offline Offline
1,837 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help me out on opencv
Next Thread in C Forum Timeline: Inline assembly query.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC