Banker

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

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++)

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

comp[prc]=1; } } } while(count!=p&&prc!=0); if(count==p) printf("\nThe system is in a safe state!!"); else printf("\nThe system is in an unsafe state!!"); getch(); } ---------------------------------------------------------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,writer
#include #include #include #include <pthread.h> <semaphore.h> <stdio.h> <unistd.h>

#define MAXTHREAD 4 readers */

/* define #

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

sem_wait(&mutex); rc++; if(rc == 1) sem_wait(&db); sem_post(&mutex); access_database(); sem_post (&q); printf("Thread %d reading\n",index); sleep(index); sem_wait(&mutex); rc--; if(rc == 0) sem_post(&db); sem_post(&mutex); non_access_database(); } return 0; } ; void* writer(void*arg) /* writer's function to write */ { while(1){ non_access_database(); sem_wait (&q); sem_wait(&db); access_database(); sem_post (&q); printf("Writer is now writing...Number of readers: %d \n",rc); sleep(1); sem_post(&db); } return 0; } void access_database() { } void non_access_database() {

Share

jalex39 is offline 03-06-2009, 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. It works fine for me (though under Cygwin).

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.h> <sched.h> < semaphore.h> <stdio.h> <unistd.h> /* define #

#define MAXTHREAD 10 readers */

void access_database(); void non_access_database(); void* reader(void*); void* writer(void*); sem_t q; /* establish que */ int rc = 0; /* number of processes reading or wanting to */ int wc = 0; int write_request = 0; int main() { pthread_t readers[MAXTHREAD],writerTh; int index; int ids[MAXTHREAD]; /* readers and initialize mutex, q and db-set them to 1 */ sem_init (& q,0,1); for(index = 0; index < MAXTHREAD; index ++) { ids[index]=index+1; if(pthread_create(&readers[index],0 ,reader,&ids[index])!=0){ perror("Cannot create reader!"); exit(1); } } if(pthread_create(&writerTh,0,write r,0)!=0){ perror("Cannot create writer"); /* create writers and error check */ exit(1); } pthread_join(writerTh,0); sem_destroy (&q);

 

 

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

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

You might also like