1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kondor19780726 [428]
4 years ago
9

OBJECTIVE This project will familiarize you with the use of pthreads, pthread mutexes and pthread condition variables. Your prog

ram will earn you no credit if it uses semaphores instead of mutexes and condition variables. THE PROBLEM A car tunnel is so poorly ventilated that it has become

Computers and Technology
1 answer:
zysi [14]4 years ago
8 0

Answer:

Check the explanation

Explanation:

/*

This program will simulate a tunnel with a limit of how many cars may move through it,

and will also simulate the cars going north and south-bound through the tunnel. We will

use pthreads, pthread mutexes, and pthread condition variables to simulate this.

*/

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

#include <string.h>

#include <signal.h>

#include <sys/types.h>

using namespace std;

#define MAXTHREADS 128

static int maxCarsInTunnel;

static int maxNCarsInTunnel;

static int maxSCarsInTunnel;

static int totalNCars;

static int totalSCars;

static int currentCarsInTunnel;

static int currentNCarsInTunnel;

static int currentSCarsInTunnel;

static int numCarsWaiting;

static pthread_mutex_t lockAccess = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *northCar(void *arg);

void *southCar(void *arg);

struct car {

int carNo;

int travelTime;

bool waited;

};

int main() {

pthread_t cartid[MAXTHREADS];

int storage = 0;

cin >> maxCarsInTunnel;

cin >> maxNCarsInTunnel;

cin >> maxSCarsInTunnel;

cout << "Maximum number of cars in the tunnel: " << maxCarsInTunnel << endl;

cout << "Maximum number of northbound cars: " << maxNCarsInTunnel << endl;

cout << "Maximum number of southbound cars: " << maxSCarsInTunnel << endl;

unsigned int arrival_time;

char direction;

unsigned int traversal_time;

struct car argList;

while (cin >> arrival_time >> direction >> traversal_time) {

sleep(arrival_time);

 

argList.travelTime = traversal_time;

argList.waited = false;

if (direction == 'N') {

totalNCars++;

argList.carNo = totalNCars;

pthread_create(&cartid[storage], NULL, northCar, (void *) &argList);

}

else if (direction == 'S') {

totalSCars++;

argList.carNo = totalSCars;

pthread_create(&cartid[storage], NULL, southCar, (void *) &argList);

}

storage++;

}

for (int i = 0; i < storage; i++) {

pthread_join(cartid[i], NULL);

}

cout << totalNCars << " northbound car(s) crossed the tunnel." << endl;

cout << totalSCars << " southbound car(s) crossed the tunnel." << endl;

cout << numCarsWaiting << " car(s) had to wait." << endl;

return 0;

}

void *northCar(void* arg) {

int tunnelTime;

int carNum;

bool wait;

struct car *argptr;

argptr = (struct car *) arg;

tunnelTime = argptr->travelTime;

carNum = argptr->carNo;

wait = argptr->waited;

pthread_mutex_lock(&lockAccess);

cout << "Northbound car # " << carNum << " arrives at the tunnel." << endl;

while (currentNCarsInTunnel >= maxNCarsInTunnel || currentCarsInTunnel >= maxCarsInTunnel) {

if (wait == false) {

wait = true;

numCarsWaiting++;

}

pthread_cond_wait(&cond, &lockAccess);

}

 

currentNCarsInTunnel++;

currentCarsInTunnel++;

cout << "Northbound car # " << carNum << " enters the tunnel." << endl;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lockAccess);

sleep(tunnelTime);

pthread_mutex_lock(&lockAccess);

cout << "Northbound car # " << carNum << " exits the tunnel." << endl;

currentNCarsInTunnel--;

currentCarsInTunnel--;

 

pthread_cond_broadcast(&cond);

pthread_mutex_unlock(&lockAccess);

}

void *southCar(void* arg) {

int tunnelTime;

int carNum;

bool wait;

struct car *argptr;

argptr = (struct car *) arg;

tunnelTime = argptr->travelTime;

carNum = argptr->carNo;

wait = argptr->waited;

pthread_mutex_lock(&lockAccess);

cout << "Southbound car # " << carNum << " arrives at the tunnel." << endl;

while (currentSCarsInTunnel >= maxSCarsInTunnel || currentCarsInTunnel >= maxCarsInTunnel) {

if (wait == false) {

wait = true;

numCarsWaiting++;

}

pthread_cond_wait(&cond, &lockAccess);

}

currentSCarsInTunnel++;

currentCarsInTunnel++;

cout << "Southbound car # " << carNum << " enters the tunnel." << endl;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lockAccess);

sleep(tunnelTime);

pthread_mutex_lock(&lockAccess);

cout << "Southbound car # " << carNum << " exits the tunnel." << endl;

currentSCarsInTunnel--;

currentCarsInTunnel--;

pthread_cond_broadcast(&cond);

pthread_mutex_unlock(&lockAccess);

}

Kindly check the attached images below to see the code screenshot and code output.

You might be interested in
What the repeal of online privacy protections means for you?
kvv77 [185]
Online Privacy is well, our privacy while on the internet. If they have repealed that, then we have no privacy while on the internet. I feel like now a days there is no privacy at all anywhere. Everywhere you go, there are cameras watching you. So for them to take away online privacy is pretty messed up.
7 0
4 years ago
Explain the differences between copyright, fair use, and trademark?
ValentinkaMS [17]

A trademark is an easily recognizable symbol, phrase, or word that denotes a specific product. It legally differentiates a product or service from all others of its kind and recognizes the source company's ownership of the brand.

"Copyright" literally means the right to copy but has come to mean that body of exclusive rights granted by law to copyright owners for protection of their work. Copyright protection does not extend to any idea, procedure, process, system, title, principle, or discovery.

Fair use is a doctrine in United States law that permits limited use of copyrighted material without having to first acquire permission from the copyright holder.

7 0
3 years ago
Dione has created a PowerPoint presentation that has several common nouns, names of products, etc. He is
svetlana [45]

Answer:

D. Add

Explanation:

PowerPoint application can be defined as a software application or program designed and developed by Microsoft, to avail users the ability to create various slides containing textual and multimedia informations that can be used during a presentation. Some of the features available on Microsoft PowerPoint are narrations, transition effects, custom slideshows, animation effects, formatting options etc.

In this scenario, Dione has created a PowerPoint presentation that has several common nouns, names of products, etc. He is running Spell Checker and does not want to be notified in regard to these words in this presentation or in any other presentation created on this computer. Hence, the option he should choose is Add. This would be used to automatically add a list of all the common nouns.

3 0
3 years ago
Read 2 more answers
A loop that will output every other name in the names list.
Nuetrik [128]

Answer:

names = ['Peter', 'Bruce', 'Steve', 'Tony', 'Natasha', 'Clint', 'Wanda', 'Hope', 'Danny', 'Carol']

numbers = [100, 50, 10, 1, 2, 7, 11, 17, 53, -8, -4, -9, -72, -64, -80]

for index, element in enumerate(names):

if index % 2 == 0:

 print(element)

for num in numbers:

 if num >= 0:

   print(num, end = " ")

count = 0

for i in numbers:

 count += i

avg = count/len(numbers)

print("sum = ", count)

print("average = ", avg)

for num in numbers:

 if num % 2 != 0:

   print(num, end = " ")

Explanation:

I'm stuck on the last two.. I have to do those too for an assignment.

4 0
3 years ago
What is a data source in OLE?​
babunello [35]

Answer:

OLE DB Driver for SQL Server uses the term data source for the set of OLE DB interfaces used to establish a link to a data store, such as SQL Server. Creating an instance of the data source object of the provider is the first task of an OLE DB Driver for SQL Server consumer.

Explanation:

hope it helps you and give me a brainliest

6 0
2 years ago
Other questions:
  • Which connector is most commonly used to connect printers to desktop pc systems?
    10·1 answer
  • While designing web pages for mobiles, the page content should be extensive such that the readers get the opportunity to explore
    5·2 answers
  • A database has a built-in capability to create, process and administer itself.
    14·1 answer
  • List 5 anti-virus products currently in use
    15·1 answer
  • Write a function called simulate_several_key_strikes. It should take one argument: an integer specifying the number of key strik
    7·1 answer
  • Paul has been working long hours. He is tired and distracted by family issues,
    10·1 answer
  • , 13 dB correspond to a power ratio of ....?
    14·1 answer
  • Another term for the plot structure of the hero journey
    13·1 answer
  • Which computers accuracy is much higher(i.e.analog &amp; digital )​
    14·1 answer
  • Which of the following statements is TRUE? A. A name error occurs if the variable is referenced before it is declared. B. A name
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!