Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *car_arrive(void *arg) {
- struct lane* l = arg;
- while (1) {
- pthread_mutex_lock(&l->lock);
- // When the process is finished.
- if ((l->inc <= 0) || (l->in_cars == NULL)) {
- pthread_cond_signal(&l->consumer_cv);
- pthread_mutex_unlock(&l->lock);
- return NULL; // Stopping the infinite loop under the correct conditions.
- }
- // If the buffer is full.
- while (l->in_buf == l->capacity) {
- pthread_cond_wait(&l->consumer_cv, &l->lock);
- }
- // Putting the car in the buffer.
- l->buffer[l->tail] = l->in_cars;
- l->tail++;
- if (l->tail >= l->capacity) {
- l->tail = 0;
- }
- l->in_buf++;
- l->in_cars = l->in_cars->next;
- pthread_cond_signal(&l->producer_cv);
- pthread_mutex_unlock(&l->lock);
- }
- }
- /**
- * TODO: Fill in this function
- *
- * Moves cars from a single lane across the intersection. Cars
- * crossing the intersection must abide the rules of the road
- * and cross along the correct path. Ensure to notify the
- * arrival thread as room becomes available in the lane.
- *
- * Note: After crossing the intersection the car should be added
- * to the out_cars list of the lane that corresponds to the car's
- * out_dir. Do not free the cars!
- *
- *
- * Note: For testing purposes, each car which gets to cross the
- * intersection should print the following three numbers on a
- * new line, separated by spaces:
- * - the car's 'in' direction, 'out' direction, and id.
- *
- * You may add other print statements, but in the end, please
- * make sure to clear any prints other than the one specified above,
- * before submitting your final code.
- */
- void *car_cross(void *arg) {
- struct lane *l = arg;
- while (1) {
- pthread_mutex_lock(&l->lock);
- // When the process is finished.
- if (l->inc <= 0) {
- pthread_mutex_unlock(&l->lock);
- return NULL; // Stopping the infinite loop under the correct conditions.
- }
- // While the buffer is empty.
- while (l->in_buf == 0) {
- pthread_cond_wait(&l->producer_cv, &l->lock);
- }
- // Get the car from the buffer.
- struct car *cur_car = l->buffer[l->head];
- l->head++;
- if (l->head >= l->capacity) {
- l->head = 0;
- }
- l->in_buf--;
- l->inc--;
- pthread_cond_signal(&l->consumer_cv);
- pthread_mutex_unlock(&l->lock);
- // Print statement for testing purposes.
- printf("in_dir: %d || out_dir: %d || ID: %d\n", cur_car->in_dir, cur_car->out_dir, cur_car->id);
- // Find the path.
- int* path = compute_path(cur_car->in_dir, cur_car->out_dir);
- int x = 0;
- while (x < (sizeof(path)/sizeof(int))) {
- pthread_mutex_lock(&isection.quad[path[x]]);
- x++;
- }
- struct lane* exit = &isection.lanes[cur_car->out_dir];
- pthread_mutex_lock(&exit->lock);
- cur_car->next = exit->out_cars;
- exit->out_cars = cur_car;
- exit->passed++;
- pthread_mutex_unlock(&exit->lock);
- int y = 0;
- while (y < (sizeof(path)/sizeof(int))) {
- pthread_mutex_unlock(&isection.quad[path[y]]);
- y++;
- }
- // Free up the path for the next loop.
- free(path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement