Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a multithreaded car park simulator program in C as follows: 1. Five drive-

ID: 3734546 • Letter: W

Question

Write a multithreaded car park simulator program in C as follows:

1. Five drive-in threads (simulating in-valets) should randomly move cars into the car park 2. Two drive-out threads (simulating out-valets) should randomly take cars out of the car park 3. A monitor thread should periodically print out a map of the car park showing the parking slots and the identities of the currently parked cars in the occupied slots. You can do this by using one monitoring thread, 5 producer threads (simulating arrivals) and 2 consumer threads (simulating departures) with a bounded buffer (simulating the car park) of integers (simulating cars and car identities). The capacity of the car park (the buffer size) should be supplied to your program as a command line parameter.

Requirements:

• Use Ptread Locks for both synchronization and mutual exclusion. • Implement mutual exclusion where appropriate. • Do not remove cars from an empty car park. • Do not add cars to a full car park. • Avoid busy-waiting, starvation and deadlocks. • Each producer/consumer thread must pause for a random period (up to 1s) between parking and/or removing a car. • Each producer/consumer thread must pause for a random period (up to 0.2s) in its critical section. • The monitor thread should periodically print out the current contents of the car park. • Use pthread_barrier_init and pthread_barrier_wait to ensure that all producer and consumer threads begin producing/consuming at the same time. • Your simulator shall continue working until it is interrupted from the keyboard by control-c. The output from the monitor thread must clearly show that your simulator is working as specified above. Study the output of your program to check it is operating properly, i.e. cars are continuously parking and leaving, consistently, no deadlock, no starvation, and no busy-waiting. What to turn in: 1. Your program design document in .pdf form. 2. The fully documented, commented and working C program, in .c file(s). 3. A meaningful sample of your program’s output in .pdf form. 4. A statement report of your observations and comments about your program output in .pdf. 5. All the above in one .zip file, that also includes your cover page and the executable file(s)

Explanation / Answer

CODE:

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

#define MAX_CARS 1000 // as an example we assume max cars as 1000

// Definition of the variables

int total_Slots;

int levels;

int parking_Spaces;

int** parking;

FILE *file;

int identifierMax=0;

pthread_cond_t parkingFull = PTHREAD_COND_INITIALIZER;

pthread_cond_t leaveSpace = PTHREAD_COND_INITIALIZER;

pthread_mutex_t mutexin = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t mutexout = PTHREAD_MUTEX_INITIALIZER;

void assignSpace(void threadid){

// When thread goes in, it checks if the parking is full.

pthread_mutex_lock(&mutexin);

if(totalSlots <= 0){

printf("Parking full. Thread %d will wait ",threadid);

//printf("Parking full. Thread %d will wait ",pthread_self());

pthread_cond_wait(&leaveSpace, &mutexin);

}

total_Slots--;

if ((fprintf(file,"[%d] in the parking, %d vacancies ", threadid, total_Slots))<0) {

perror("write error");

}

//printf("%d in the parking, %d vacancies ",pthread_self(), total_Slots);

int i,j, ib,jb,k, counter = 0;

// The car must look for a place in p floor.. then p+1 etc..

for( i = 0; i < levels; i++){

for( j = 0; j < parking_Spaces; j++){

if(parking[i][j] == 0){

printf("Level %d. Position %d is free. Thread %d is taking it...",i,j,threadid);

parking[i][j]=1;

printf("TAKEN ");

ib=i; jb=j;

for(k = 0 ; k < parking_Spaces; k++)

if(parking[i][k] == 0)

counter++;

if ((fprintf(file,"[%d] arrives at level %d, %d free spaces on the level ", threadid, i, counter))<0) {

perror("write error");

}

j=parking_Spaces;

i=levels; // This forces the program to go out of the loop

}

}

}

pthread_mutex_unlock(&mutexin);

pthread_mutex_lock(&mutexout);

sleep(random()%10);

//pthread_mutex_unlock(&mutexout);

//printf("%d leaves parking, %d vacancies ",pthread_self(), total_Slots);

counter=0;

parking[ib][jb]=0;

for(k = 0 ; k < parking_Spaces; k++)

if(parking[ib][k] == 0)

counter++;

if ((fprintf(file,"[%d] leaves level %d, %d free spaces on the level ",threadid, ib, counter))<0) {

perror("write error");

}

total_Slots++;

printf("%d leaves parking, %d vacancies ",threadid, total_Slots);

if ((fprintf(file,"[%d] leaves the parking, %d vacancies ", threadid, total_Slots))<0) {

perror("write error");

}

// As soon as the thread finishes a signal is emitted to the threads waiting.

pthread_cond_signal(&leaveSpace);

pthread_mutex_unlock(&mutexout);

pthread_exit(0);

}

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

if (argc != 4){

printf("Sorry, at least three parameters, number of levels, number of spaces"

" and file name are required to run the program. You wrote %d parameters"

" Exiting... ", argc);

exit(1);

}

printf("Assigning levels...");

levels = atoi(argv[1]);

printf("DONE ");

printf("Assigning parking spaces per level...");

parking_Spaces = atoi(argv[2]);

printf("DONE ");

total_Slots = levels * parking_Spaces;

printf("Initializing parking spots...");

int i;

parking = malloc(levels*sizeof(int *));

if(parking == NULL){

perror("out of memory ");

exit(1);

}

for( i = 0; i < levels; i++){

parking[i] = malloc(parking_Spaces*sizeof(int));

if(parking[i] == NULL){

perror("out of memory ");

exit(1);

}

}

// Convention is : 0 empty spot, 1 occupied spot.

printf("DONE ");

printf("The parking has %d total slots. All of them are empty ",totalSlots);

printf("Creating output file...");

const char *textfilename = argv[3];

if ((file = fopen(textfilename,"a+")) == NULL) {

perror("Error when creating text file");

return 1;

}

printf("successfully done ");

int nofCars;

int t, rc;

int stillCarsIn = 1;

pthread_t threads[MAX_CARS];

while(stillCarsIn){

printf("How many cars are trying to enter the parking? ");

scanf("%d",&noOfCars);

if(noOfCars < 0){

printf("Sorry, you cannot have a negative number of cars. Exiting...");

fclose(file);

exit(1);

}

if(noOfCars == 0){

printf("Wait until the cars exit the parking... ");

//Wait for the cars to exit

while(total_Slots != levels * parking_Spaces);

printf("All the cars are out. Parking is empty. ");

printf("Exiting...");

fclose(file);

exit(1);

}

if(noOfCars > 0){

//Assign a thread to each car trying to enter.

for(t=0;t < noOfCars;t++){

printf("Creating thread for car %d ", identifierMax+t);

// Assign a car to a position if possible in assignSpace

rc = pthread_create(&threads[t], NULL, assignSpace, (void *)identifierMax+t);

if (rc){

printf("ERROR; return code from pthread_create() is %d ", rc);

exit(-1);

}

}

identifierMax=noOfCars;

//If not possible, car waits.

}

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote