The document provides code for a C program implementing a roller coaster simulation with passenger and car threads. The code is receiving a segmentation fault error from one of the functions. The passenger thread code boards the car and rides the roller coaster for a random number of iterations before exiting. The car thread allows boarding for 2 seconds or until full capacity, then rides for 5 seconds before allowing passengers to exit and the next group to board. The error is believed to be in the car thread or conditional statements.
This is a roller coaster C file program Please correct my c.pdf
1. This is a roller coaster C file program. Please correct my code, I am receiving a segmentation fault
from something in one of the functions! Here is the code I have right now: I believe the error is
coming from the car thread function. The instructions are below this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <stdbool.h>
sem_t rideEmpty, rideTurn, rideExit;
int passengerInPark = 0;
int passengerInSeat = 0;
int passengerIsDone = 0;
char i = 0, n = 0, c = 0;
//Passenger Thread Function
void *passenger(void *arg)
{
int id = *(int*) arg;
free(arg);
int j = 0;
int max_iter = rand() % i+1;
for (int j = 0; j < max_iter+1; j++)
{
int wait = rand() % 11;
sleep(wait);
sem_wait(&rideEmpty);
sem_wait(&rideTurn);
printf("033[0;32m Thread %d: Wooh! I'm about to ride the roller coaster for the %d th time! I have
%d iterations left. 033[0mn", id, j, max_iter - j);
passengerInSeat++;
sem_post(&rideTurn);
}
passengerIsDone++;
sem_wait(&rideExit);
printf("033[0;32m Thread %d: completed %d iterations on the roller coaster. Exiting. 033[0mn", id,
j);
passengerInPark--;
return NULL;
//pthread_exit(NULL);
}
2. //Car Thread Function
void *car(void *arg)
{
int rideCounter = 0;
while (passengerInPark > 0)
{
int time = 0;
while (time != 2 && passengerInSeat != c)
{
sleep(1);
time++;
}
sem_wait(&rideTurn);
printf("033[0;31 Car: %d passengers are riding the roller coaster. Off we go on the %d ride!
033[0mn", passengerInSeat, ++rideCounter);
sleep(5);
printf("033[0;31m Car: ride %d completed. 033[0mn", rideCounter);
int emptySeats = 0;
while(emptySeats < passengerInPark && emptySeats < c)
{
sem_post(&rideEmpty);
emptySeats++;
}
passengerInSeat = 0;
while (passengerIsDone > 0)
{
sem_post(&rideExit);
passengerIsDone = 0;
}
sem_post(&rideTurn);
}
printf("033[0;31m Car: Roller coaster shutting down.033[0mn");
return NULL;
//pthread_exit(NULL);
}
//Main Function
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("Usage: -n <count> -c <count> -i <count>nn");
return EXIT_SUCCESS;
3. }
char options;
while ((options = getopt(argc, argv, ":n:c:i:")) != -1)
{
switch (options)
{
case 'n':
n = atol(optarg);
if (n <= 0)
{
printf("./roller: invalid value - %snn", optarg);
return EXIT_SUCCESS;
}
break;
case 'c':
c = atoi(optarg);
if (c <= 0)
{
printf("./roller: invalid value - %snn", optarg);
return EXIT_SUCCESS;
}
break;
case 'i':
i = atoi(optarg);
if (i <= 0)
{
printf("./roller: invalid value - %snn", optarg);
return EXIT_SUCCESS;
}
break;
default:
printf("./roller: invalid option - %cnn", argv[optind-1][1]);
return EXIT_SUCCESS;
}
}
if ((c == 0) || (n == 0) || (i == 0)) return EXIT_SUCCESS;
if ((c >= n) || (n > 100))
{
printf("n (>c ) and n (<= 100) arguments requirednn");
return EXIT_SUCCESS;
}
//int passengerInSeat = atoi(argv[1]);
4. //rideNum = atoi(argv[2]);
n = atoi(argv[2]);
c = atoi(argv[4]);
i = atoi(argv[6]);
pthread_t passenger[passengerInSeat];
pthread_t car;
sem_init(&rideEmpty, 0, 0);
sem_init(&rideTurn, 0, 0);
sem_init(&rideExit, 0, 1);
for (int i = 0; i < passengerInSeat; i++)
{
int *id = malloc(sizeof(int));
*id = i;
pthread_create(&passenger[i], NULL, passenger, id);
}
pthread_create(&car, NULL, car, NULL);
for (int i = 0; i < passengerInSeat; i++)
{
pthread_join(passenger[i], NULL);
}
pthread_join(car, NULL);
sem_destroy(&rideEmpty);
sem_destroy(&rideTurn);
sem_destroy(&rideExit);
printf("main past semaphore destroysn");
return 0;
}
Here are the instructions:
Implement the Roller Coaster problem in C (not C++) ensuring the constraints are met and critical
sections are properly protected. The program must compile and execute under Ubuntu 22.04 LTS.
The Roller Coaster scenario is a concurrent programming problem that can be solved using
semaphores1 and is summarized below. This code requires multi-threading, semaphores, and
possibly overcoming deadlocks.
Suppose there are n passenger threads and one roller coaster car thread. The car can hold at
most c passengers, where c < n. Each passenger would ride the roller coaster for a random
number of times, and the random number is in the range [0, i]. After each ride, the passenger will
wait for a random time in the range [0s, 10s] to brace themselves. After completing their
predetermined number of iterations, the passenger will exit the park (i.e., the thread terminates).
Upon deciding to ride a roller coaster, the passenger must wait for an open seat to ride the roller
coaster car. The car waits for passengers for a maximum of 2s. Upon completing 2s or reaching
the maximum capacity, the car goes around the track for 5s. The roller coaster shuts down (the
car thread exits) after all passengers exit the park.
5. You will create one thread for each user and one thread for the car. The car thread will allow
passenger boarding for 2s or until the car is full. Once the boarding closes, the car goes on a ride
for 5s, represented by a sleep function call. The pseudo code for the car thread can be found
below:
Each user thread will wait for a random time in the rage [0, 10s] and then attempt to board the car.
During the boarding process, the user checks to see if the car is boarding passengers. If the car is
not boarding, the user thread will have to wait until the car is boarding. If the car is boarding
passengers, the user can check to see if a seat is available. If the seat is available, the user can
take the seat. However, if the seat is unavailable, the user will have to wait in the queue until the
car finishes a loop and there are empty seats. The Pseudo code for the passenger thread can be
found on the next page.
Pseudo code for the car and passenger:
The total number of passengers, n, the number of passengers per car, c, and the upper bound on
the maximum number of iterations of a passenger, i, will be accepted as command line arguments.
The accepted arguments should meet the following constraints: c < n, n < 100, and i <= 20. All
values must be > 0.
Include Files:
In order to use threading functions and semaphores in C, you will need the below include files:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <stdbool.h>
Compilation options: user the following compilation options:
gcc -Wall -g -pedantic -pthread -o roller roller.c
Example Output: the following are some execution examples showing error handling and an
example execution. Successive execution may vary. The $ is the prompt.
Correct output showing program execution:
The error checking in the command line argument works properly, but the program will not execute
and will print out a segmentation fault (core dumped). I have commented out main and there is
nothing wrong with the main function, I believe it may be something in the semaphores or the
conditionals in the car thread function. The output of the code must match the green and red text
above when inputting: ./roller -n 5 -c 2 -i 5 in the command line.
Car:Passenger: max i ter = random (0,i); while (j<=maxiter): wait(random (0,10s)); boardCar ();
print(" 033[0;32m Thread ID > : Wooh! I'm about to ride the roller coaster for the jth time! I have
max_iter j iterations left. 033[0mn);