QuestionQuestion

Objective

This assignment will introduce you to the process creation mechanism in UNIX using the fork function.

Specifications

You must write a program to simulate a traffic light controlling how cars use a four-way intersection. Each road has a direction (northbound, southbound, eastbound, and westbound). Your program will receive from STDIN the information about the parameters of the traffic light and the cars that want to cross the intersection. After defining the order that the cars will follow to use the intersection, your program must create a child process per car. Each child process will print the information about the car and will sleep for the number of seconds specified in the input file.

Input Format: Your program should read its input from stdin (C++ cin) and use input redirection as in:

assignment1 < input1.txt

This input file will look like:

E // Initial direction
1 // Max number of cars per direction ABC N 2 // Car info (license plate, direction, time) BCD N 2
CDE S 3
DEF E 2
EFG S 1
FGH W 1

• The initial direction represents the starting direction (N, E, S, or W) of the simulation. The direction of the traffic light will change using a clockwise pattern.
• The max number of cars limits the number of cars that can use the intersection based on the current direction of the traffic light.
• The car information will have: (a) the license plate (only letters and numbers); (b) the direction (N=northbound, E=eastbound, S=southbound, W=westbound); and (c) the time (seconds) using the intersection. These parameters are separated by whitespace.
• The order in which the cars arrive at the intersection is defined by their position in the input file.
• Only one car can use the intersection at a time.

Using the example input file presented before, the order in which the cars will use the intersection is:

Direction, License Plate, Time
E DEF2
S CDE 3
W FGH 1
N ABC 2
S EFG 1
N BCD 2

Implementation

You will have two types of processes:

1. Parent process: is the process that reads the input file, defines the order that cars will follow to use the intersection, creates the child processes (one at a time), prints the current direction of the traffic light (per each direction change), and waits for all the child processes to complete before ending its execution.

2. Child processes: these are the processes created by the parent process. Each child process will perform the following operations: prints the information about the car (license plate, time), and sleeps for the number of seconds specified by the time parameter.

Based on the previous example, the corresponding output is:

Current direction: Eastbound
Car DEF is using the intersection for 2 sec(s). Current direction: Southbound
Car CDE is using the intersection for 3 sec(s). Current direction: Westbound
Car FGH is using the intersection for 1 sec(s). Current direction: Northbound
Car ABC is using the intersection for 2 sec(s). Current direction: Southbound
Car EFG is using the intersection for 1 sec(s). Current direction: Northbound
Car BCD is using the intersection for 2 sec(s).

HINTS:
• You can safely assume that the input files will always be in the proper format.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

class CarInfo
{
public:
string licPlate;
char direction;
int time;
bool traversed;
};

char getNextDirection(char currDir){
char nextDir = currDir;
switch(currDir){
case 'N': nextDir = 'E'; break;
case 'E': nextDir = 'S'; break;
case 'S': nextDir = 'W'; break;
case 'W': nextDir = 'N'; break;
}
return nextDir;
}

string getDirectionName(char currDir){
string name = "";
switch(currDir){
case 'E': name = "Eastbound"; break;
case 'S': name = "Southbound"; break;
case 'W': name = "Westbound"; break;
case 'N': name = "Northbound"; break;
}
return name;
}...

By purchasing this solution you'll be able to access the following files:
Solution.cpp.

$30.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats