Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Project 4: Bankers Algorithm For this project, you will write a multithreaded program. ========================================================= Bankers Algorithm For this project, you will write a multithreaded

Programming Project 4:

Bankers Algorithm For this project, you will write a multithreaded program.

=========================================================

Bankers Algorithm

For this project, you will write a multithreaded program that implements the bankers algorithm discussed in Section 7.5.3. Several customers request and release resources from the bank. The banker will grant a request only if it leaves the system in a safe state. A request that leaves the system in an unsafe state will be denied. This programming assignment combines three separate topics: (1) multithreading, (2) preventing race conditions, and (3) deadlock avoidance.

The Banker The banker will consider requests from n customers for m resources types. as outlined in Section 7.5.3. The banker will keep track of the resources using the following data structures:

/* these may be any values >= 0 */ #define NUMBER OF CUSTOMERS 5 #define NUMBER OF RESOURCES 3

/* the available amount of each resource */ int available[NUMBER OF RESOURCES];

/*the maximum demand of each customer */ int maximum[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

/* the amount currently allocated to each customer */ int allocation[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

/* the remaining need for each customer */ int need[NUMBER OF CUSTOMERS][NUMBER OF RESOURCES];

The Customers Create n customer threads that request and release resources from the bank. The customers will continually loop, requesting and then releasing random numbers of resources. The customers requests for resources will be bounded by their respective values in the need array. The banker will grant a request if it satis?es the safety algorithm outlined in Section 7.5.3.1. If a request does not leave the system in a safe state, the banker will deny it. Function prototypes for requesting and releasing resources are as follows:

int request resources(int customer num, int request[]); int release resources(int customer num, int release[]);

These two functions should return 0 if successful (the request has been granted) and 1 if unsuccessful. Multiple threads (customers) will concurrently access shared data through these two functions. Therefore, access must be controlled through mutex locks to prevent race conditions. Both the Pthreads and Windows APIs provide mutex locks. The use of Pthreads mutex locks is covered in Section 5.9.4; mutex locks for Windows systems are described in the project entitled Producer-Consumer Problem at the end of Chapter 5. Implementation

You should invoke your program by passing the number of resources of each type on the command line. For example, if there were three resource types, with ten instances of the first type, five of the second type, and seven of the third type, you would invoke your program follows:

./a.out 10 5 7

The available array would be initialized to these values. You may initialize the maximum array (which holds the maximum demand of each customer) using any method you find convenient.

==========================

You can find this information Internet and textbook Page 345-346.

This is an individual programming project NOT Group Project.

1. You need to work on the server

2. You need to submit your document about what you have done for this project more than 2 pages single space 11 points.

HERE I HAVE ATTACHED THE CODE:

Buffer.c

#include "buffer.h"

#include

#include "buffer.h"

#include

#include

#include

#include

#include

#define BUFFER_SIZE 5

#define RAND_DIVISOR 100000000

#define TRUE 1

sem_t empty, full;

pthread_mutex_t mutex;

int in = 0;

int out = 0;

int insert_item(buffer_item item) {

if(sem_wait(&empty) != 0)

return -1;

if(pthread_mutex_lock(&mutex) != 0)

return -1;

buff[in % BUFFER_SIZE] = item;

printf("producer produced %d at %d ", item, in % BUFFER_SIZE);

in++;

if(pthread_mutex_unlock(&mutex) != 0)

return -1;

if(sem_post(&full) != 0)

return -1;

return 0;

}

int remove_item(buffer_item *item) {

if(sem_wait(&full) != 0)

return -1;

if(pthread_mutex_lock(&mutex) != 0)

return -1;

*item = buff[out % BUFFER_SIZE];

printf("consumer consumed %d at %d ", *item, out % BUFFER_SIZE);

out++;

if(pthread_mutex_unlock(&mutex) != 0)

return -1;

if(sem_post(&empty) != 0)

return -1;

return 0;

}

void init_buffer() {

int i = 0;

for(;i

buff[i] = 0;

sem_init(&full, 0, 0);

sem_init(&empty, 0, BUFFER_SIZE);

pthread_mutex_init(&mutex, NULL);

}

/* For testing only void print_buffer() {

int i = 0;

for(;i

printf("buff[%i] = %i ", i, buff[i]);

}

*/

void *producer(void *param) {

FILE * fp;

fp = fopen ("file.txt", "w+");

buffer_item item;

param = NULL;

while(1)

{

sleep(rand());

// fill item with a random value

item = rand_r(&junk);

if(insert_item(item) < 0)

fprintf(fp,"Report Error Condition ");

else

printf("producer produced %d ", item);

}

}

void *consumer(void *param) {

FILE * fp;

fp = fopen ("file.txt", "w+");

buffer_item item;

param = NULL;

while(1)

{

sleep(rand());

// fill item with a random value

item = rand_r(&junk);

if(insert_item(item) < 0)

fprintf(fp,"Report Error Condition ");

else

printf("producer produced %d ", item);

}

}

void *consumer(void *param) {

FILE * fp;

fp = fopen ("file.txt", "w+");

buffer_item item;

param = NULL;

while(1)

{

sleep(rand());

if(remove_item(&item) < 0)

fprintf(fp,"Report Error Condition ");

else

printf("consumer consumed %d ", item);

}

}

Main.c

#include

#include

#include

#include

#include "buffer.h"

void print_instr() {

printf("Please provide \

\t1. How long to sleep before terminating. \

\t2. The number of producer threads. \

\t3. The number of consumer threads. ");

}

int main(int argc, char *argv[]) {

if(argc <= 3) { // To make sure the proper input is there, prompt the

user if not

print_instr();

exit(0);

}

int sleep_duration = atoi(argv[1]);

int num_producers = atoi(argv[2]);

int num_consumers = atoi(argv[3]);

init_buffer(); //print_buffer(buffer); For testing, currently commented out in buffer.h

int i = 0;

for(;i

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_create(&tid, &attr, producer, NULL);

}

i = 0;

for(;i

pthread_t tid;

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_create(&tid, &attr, consumer, NULL);

}

sleep(sleep_duration);

//print_buffer(buffer);

return 0;

}

Buffer.h

#ifndef BUFFER_H

#define BUFFER_H

typedef int buffer_item;

#define BUFFER_SIZE 5

unsigned int junk;

buffer_item buff[BUFFER_SIZE];

int insert_item(buffer_item);

int remove_item(buffer_item *);

void init_buffer(); //void print_buffer(); for testing only

void *producer(void *param);

void *consumer(void *param);

#endif

Makefile

UNAME := $(SHELL uname)

CC=gcc

CFLAGS=-c -Wall -Wextra -Wno-parentheses -pedantic -std=gnu99

LDFLAGS=-pthread

SOURCES=main.c buffer.c

OBJECTS=$(SOURCES:.c=.o)

EXECUTABLE=proj3

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)

$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.m.o:

$(CC) $(CFLAGS) $< -o $@

clean:

rm -rf *.o $(EXECUTABLE)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Identify the types of informal reports.

Answered: 1 week ago

Question

Write messages that are used for the various stages of collection.

Answered: 1 week ago