banker #include< stdio.h > #include< conio.

h > void main() { int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5], comp[7]; int first,p,r,i,j,prc,count,t; clrscr(); count=0; for(i=1;i<=7;i++) comp[i]=0; printf("Enter the no of processes:\n"); scanf("%d",&p); printf("Enter the no of resources:\n"); scanf("%d",&r); printf("Enter the claim for each process:"); for(i=1;i<=p;i++) { printf("\nFor process %d",i); for(j=1;j<=r;j++) { scanf("%d",&clm[i][j]); } } printf("Enter the allocation for each process:\n"); for(i=1;i<=p;i++) { printf("\nFor process ",i); for(j=1;j<=r;j++) { scanf("%d",&alloc[i][j]); } } printf("Enter total no of each resource:"); for(j=1;j<=r;j++) scanf("%d",&rsrc[j]); for(j=1;j<=r;j++) { int total=0; avail[j]=0; for(i=1;i<=p;i++) {total+=alloc[i][j];} avail[j]=rsrc[j]-total; } do { for(i=1;i<=p;i++)

j<=r.prc. } printf("\nClaim matrix:\t\tAllocation matrix:\n").j++) { printf("%d". } } printf("\n\nAvailable resorces is:").j<=r.clm[i][j]). for(j=1.j++) { req[i][j]=clm[i][j]-alloc[i][j]."runs to completion!"). for(j=1. . alloc[prc][j]=0.{ for(j=1. count++. } } } if(prc!=0) break.j++) { if(avail[j] { prc=0.i<=p.j<=r. for(j=1.j<=r. } prc=0.j++) { printf("%d".i++) { for(j=1. clm[prc][j]=0.j++) { avail[j]+=alloc[prc][j].j++) { printf(" ".i++) { if(comp[i]==0)//if not completed { prc=i.i<=p.j<=r. } if(prc!=0) { printf("\nProcess ".j<=r. break. for(j=1. } printf("\t\t\t"). for(i=1. for(i=1.alloc[i][j]).avail[j]). } printf("\n").

else printf("\nThe system is in an unsafe state!!"). getch().comp[prc]=1. if(count==p) printf("\nThe system is in a safe state!!").writer #include #include #include #include <pthread. } } } while(count!=p&&prc!=0).h> <semaphore.h> <unistd.h> <stdio.h> . } ---------------------------------------------------------OUT PUT: ---------------------------------------------------------Enter the no of processes: 2 Enter the no of resources: 3 Enter the claim for each process: For process I : 2 4 5 For process II: 2 5 3 Enter the total no of each resource : 5 5 2 Available resource is : Claim matrix: 245 253 allocation matrix: 123 234 The system is in an unsafe state!! Reader.

exit(1).1). int ids[MAXTHREAD]. sem_t mutex. sem_destroy(&db).writer. } } if(pthread_create(&writerTh. for(index = 0. sem_init(&db. } void* reader(void*arg) /* readers function to read */ { int index = *(int*)arg. q and db -set them to 1 */ sem_init(&mutex. int index. sem_init (&q.0. .#define MAXTHREAD 4 readers */ /* define # void access_database(). void* writer(void*).1). while(1){ sem_wait(&q).&ids[index])!=0){ perror("Cannot create reader!").0) !=0){ perror("Cannot create writer"). sem_destroy (&q).writerTh.0). e readers and initialize mutex.1). index < MAXTHREAD.0. if(pthread_create(&readers[index ]. sem_destroy(&mutex). exit(1).0. /* controls access to db */ sem_t q. } pthread_join(writerTh. void* reader(void*). /* number of processes reading or wanting to */ int main() { pthread_t readers[MAXTHREAD]. /* controls access to rc */ sem_t db. return 0. /* prototypes */ void non_access_database().0.reader. index ++) { ids[index]=index+1.0. /* establish que */ int rc = 0.

sem_post (&q). sem_wait(&db). sem_post(&db). rc--. rc++.rc). access_database().. .sem_wait(&mutex). access_database(). sem_wait(&mutex). 08:10 PM tjb Registered User Join Date: Jan 2009 Posts: 31 #2 I haven't been able to find the issue with this code. if(rc == 0) sem_post(&db). sem_post(&mutex). sem_wait (&q). sleep(index). printf("Writer is now writing. } return 0. printf("Thread %d reading\n".index). sleep(1).. void* writer(void*arg) /* writer's function to write */ { while(1){ non_access_database(). } void access_database() { } void non_access_database() { Share jalex39 is offline 03-06-2009.Number of readers: %d \n". non_access_database(). } return 0. It works fine for me (though under Cygwin). if(rc == 1) sem_wait(&db). } . sem_post(&mutex). sem_post (&q).

void* writer(void*). /* establish que */ int rc = 0. exit(1).&ids[index])!=0){ perror("Cannot create reader!").0). if(pthread_create(&readers[index].h> <unistd. index ++) { ids[index]=index+1. index < MAXTHREAD. /* create writers and error check */ exit(1). /* readers and initialize mutex. int write_request = 0.h> <stdio. sem_destroy (&q). void non_access_database().0. } } if(pthread_create(&writerTh.0.h> <sched. int index. /* number of processes reading or wanting to */ int wc = 0. } pthread_join(writerTh.1).h> /* define # #define MAXTHREAD 10 readers */ void access_database(). int ids[MAXTHREAD].h> < semaphore.0 . q and db-set them to 1 */ sem_init (& q. int main() { pthread_t readers[MAXTHREAD]. void* reader(void*).reader. sem_t q.0)!=0){ perror("Cannot create writer").H 's s t t uses one semapho e ( th n it's a i tt e bit easier to anal ze): Co e: #include #include #include #include #include <pthread.write r.writerTh.     © ¨ ¡ ¢§ ¤¦¥ ¡ £ ¢   ¡  ¤ . for(index = 0.

sem_wait(&q). if(can_read) { access_database(). sem_post(&q). sem_wait(&q). sem_wait (&q). write_request = 0.Number of readers: %d\n". } sched_yield(). else { can_write = 0. } . wc--. while(1){ can_write = 1. } sem_post(&q). index). if(rc == 0) wc++. if(can_write) { access_database(). sem_wait(&q).return 0. non_access_database(). sem_post(&q). void* writer(void*arg) /* writer's function to write */ { int can_write.. printf("Writer is now writing. rc--. sleep(3). sleep(index). } . sem_post(&q). if(wc == 0 && write_request == 0) rc++. } return 0.rc).. write_request = 1. else can_read = 0. printf("Thread %d reading\n". } void* reader(void*arg) { int index = *(int*)arg. int can_read. while(1){ can_read = 1.

} return 0. } void access_database() { } void non_access_database() { } .sched_yield().