Uploaded by yukwokyao2

code from GPT Assignment A

advertisement
//compile successful and correct
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_LOADING_BAYS 2
#define NUM_TRAILERS 5
#define NUM_SHIPS 3
#define NUM_CARGOS_PER_SHIP 8
sem_t dock_mutex;
sem_t loading_bay_mutex[NUM_LOADING_BAYS];
sem_t trailer_mutex;
sem_t cargo_mutex;
int num_cargos_in_storage = 0;
void* ship(void* arg){
int ship_num = *(int*)arg;
printf("Ship %d arrived at port.\n", ship_num);
// Wait for available dock
sem_wait(&dock_mutex);
printf("Ship %d docked at port.\n", ship_num);
// Unload all cargos
for(int i = 0; i < NUM_CARGOS_PER_SHIP; i++){
printf("Ship %d is unloading cargo %d.\n", ship_num, i+1);
// Wait for trailer to be ready at loading bay
sem_wait(&loading_bay_mutex[i % NUM_LOADING_BAYS]);
printf("Ship %d found a trailer at loading bay %d.\n", ship_num, i % NUM_LOADING_BAYS + 1);
// Signal trailer to start unloading cargo
sem_post(&trailer_mutex);
// Wait for trailer to finish unloading cargo
sem_wait(&cargo_mutex);
printf("Ship %d unloaded cargo %d.\n", ship_num, i+1);
// Release loading bay
sem_post(&loading_bay_mutex[i % NUM_LOADING_BAYS]);
}
// Undock and leave the port
printf("Ship %d has left the port.\n", ship_num);
sem_post(&dock_mutex);
pthread_exit(NULL);
}
void* trailer(void* arg){
int trailer_num = *(int*)arg;
printf("Trailer %d is ready at the port.\n", trailer_num);
while(1){
// Wait for available loading bay
sem_wait(&trailer_mutex);
printf("Trailer %d is waiting for cargo at a loading bay.\n", trailer_num);
// Signal ship to indicate readiness to load a cargo
sem_post(&trailer_mutex);
// Wait for signal from ship to load a cargo
sem_wait(&trailer_mutex);
// Load a cargo from ship
sleep(2);
printf("Trailer %d loaded a cargo from a ship.\n", trailer_num);
// Depart from loading bay to storage field and then return to loading bay
sleep(3);
printf("Trailer %d delivered the cargo to the storage field and returned to the loading bay.\n", trailer_num);
// Signal cargo transfer complete
sem_post(&cargo_mutex);
}
pthread_exit(NULL);
}
int main(){
// Initialize semaphores
sem_init(&dock_mutex, 0, 1);
for(int i = 0; i < NUM_LOADING_BAYS; i++){
sem_init(&loading_bay_mutex[i], 0, 1);
}
sem_init(&trailer_mutex, 0, 1);
sem_init(&cargo_mutex, 0, 0);
// Create trailer threads
pthread_t trailer_thread[NUM_TRAILERS];
int trailer_id[NUM_TRAILERS];
for(int i = 0; i < NUM_TRAILERS; i++){
trailer_id[i] = i + 1;
if(pthread_create(&trailer_thread[i], NULL, trailer, &trailer_id[i])){
fprintf(stderr, "Error creating trailer thread\n");
return 1;
}
}
// Create ship threads
pthread_t ship_thread[NUM_SHIPS];
int ship_id[NUM_SHIPS];
for(int i = 0; i < NUM_SHIPS; i++){
ship_id[i] = i + 1;
if(pthread_create(&ship_thread[i], NULL, ship, &ship_id[i])){
fprintf(stderr, "Error creating ship thread\n");
return 1;
}
// Sleep for 4 seconds to simulate ship arrival every 4 seconds
sleep(4);
}
// Wait for all ships to depart
for(int i = 0; i < NUM_SHIPS; i++){
pthread_join(ship_thread[i], NULL);
}
// Cancel all trailer threads after all ships had departed from dock
for(int i = 0; i < NUM_TRAILERS; i++){
pthread_cancel(trailer_thread[i]);
}
// Destroy semaphores
sem_destroy(&dock_mutex);
for(int i = 0; i < NUM_LOADING_BAYS; i++){
sem_destroy(&loading_bay_mutex[i]);
}
sem_destroy(&trailer_mutex);
sem_destroy(&cargo_mutex);
return 0;
}
Download