-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrainLine.cpp
More file actions
178 lines (133 loc) · 5.61 KB
/
trainLine.cpp
File metadata and controls
178 lines (133 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*-------------------------------------------------------------------------*
*--- ---*
*--- trainLine.cpp ---*
*--- ---*
*--- This file defines code the implements 2 Station instances, ---*
*--- and some number of Train objects that transit between them. ---*
*--- ---*
*--- ---- ---- ---- ---- ---- ---- ---- ---- ---*
*--- ---*
*--- Version 2.0 Joseph Phillips 2016 May 5 ---*
*--- ---*
*-------------------------------------------------------------------------*/
// Compile with: $ g++ trainLine.cpp -o trainLine -lpthread -g
/*
Student Name: Abdulaziz Alqulaysh
B- What do you notice?
It works with no problems but it crashes at the 3rd try.
———————————————————————————————————————————————————————————————
D- What do you notice?
It works fine with no problems but in the 5th try it produced infinite results! I had to force quite the program to stop it.
Because it has a lock and nothing can get inside the methods arrive() and leave().
———————————————————————————————————————————————————————————————
F- What is the minimum number of conditions that you need? How does it behave?
The minimum number of conditions is one. It works fine with no crashes and no infinite results.
*/
#include <cstdlib>
#include <string>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include "Train.h"
#include "Station.h"
const int NUM_TRAINS = 4;
const int NUM_NORTHERNLY_TRAINS = NUM_TRAINS / 2;
const int NUM_SOUTHERNLY_TRAINS = NUM_TRAINS - NUM_NORTHERNLY_TRAINS;
const int NUM_LOOPS = 16;
const char* TRAIN_NAME_ARRAY[NUM_TRAINS]
= { "Thomas the Tank-Engine",
"Engine No. 9",
"California Zephyr",
"Tokaido Shinkansen"
};
const int TRAIN_TRANSIT_TIME_MSECS[NUM_TRAINS]
= { 10000, // Thomas
10000, // Engine no. 9
5000, // CA Zephyr
1000 // Bullet train
};
Station northTerminal("North Station");
Station southTerminal("South Station");
// PURPOSE: To make the Train object pointed to by 'vPtr' do 'NUM_LOOP' times:
// * arrive at 'northTerminal'
// * leave at 'northTerminal'
// * pause
// * arrive at 'southTerminal'
// * leave at 'southTerminal'
// * pause
// Returns a pointer to the Train it used.
void* initiallyNorthernly (void* vPtr)
{
Train* trainPtr = (Train*)vPtr;
for(int i = 0; i < NUM_LOOPS; i++)
{
northTerminal.arrive(trainPtr);
northTerminal.leave();
trainPtr->pause();
southTerminal.arrive(trainPtr);
southTerminal.leave();
trainPtr->pause();
}
return((void*)trainPtr);
}
// PURPOSE: To make the Train object pointed to by 'vPtr' do 'NUM_LOOP' times:
// * arrive at 'southTerminal'
// * leave at 'southTerminal'
// * pause
// * arrive at 'northTerminal'
// * leave at 'northTerminal'
// * pause
// Returns a pointer to the Train it used.
void* initiallySouthernly (void* vPtr)
{
Train* trainPtr = (Train*)vPtr;
for(int i = 0; i < NUM_LOOPS; i++)
{
southTerminal.arrive(trainPtr);
southTerminal.leave();
trainPtr->pause();
northTerminal.arrive(trainPtr);
northTerminal.leave();
trainPtr->pause();
}
return((void*)trainPtr);
}
int main (int argc,
char* argv[]
)
{
if (argc > 1)
srand(strtol(argv[1],NULL,10));
pthread_t tidArray[NUM_TRAINS];
Train* trainArray[NUM_TRAINS];
for (int i = 0; i < NUM_TRAINS; i++)
trainArray[i] = new Train(TRAIN_NAME_ARRAY[i],TRAIN_TRANSIT_TIME_MSECS[i]);
int trainInd = 0;
// Please make 'NUM_NORTHERNLY_TRAINS' threads (tidArray[0] to
// tidArray[NUM_NORTHERNLY_TRAINS-1]) run 'initiallyNorthernly()'.
// Each thread should get a pointer to its own Train instance
// (trainArray[0] to trainArray[NUM_NORTHERNLY_TRAINS-1], this is
// an array of POINTERS to trains, not trains themselves).
for (trainInd = 0; trainInd < NUM_NORTHERNLY_TRAINS ; trainInd++)
pthread_create(&tidArray[trainInd],NULL,initiallyNorthernly,(void*)trainArray[trainInd]);
// Please make 'NUM_SOUTHERNLY_TRAINS' threads
// (tidArray[NUM_NORTHERNLY_TRAINS] to tidArray[NUM_TRAINS-1]) run
// 'initiallySouthernly()'. Each thread should get a pointer to its own
// Train instance (trainArray[NUM_NORTHERNLY_TRAINS] to
// trainArray[NUM_TRAINS-1], this is an array of POINTERS to trains, not
// trains themselves).
for ( ; trainInd < NUM_TRAINS ; trainInd++)
pthread_create(&tidArray[trainInd],NULL,initiallySouthernly,(void*)trainArray[trainInd]);
// Leave this loop here. I want to make sure you get the Train pointers
// back from initiallyNorthernly and initiallySouthernly().
for (int i = 0; i < NUM_TRAINS; i++)
trainArray[i] = NULL;
// Wait for all Train threads. Also, get the pointers to the Train objects
// and delete() them because they were created by 'new'
for (int i = 0; i < NUM_TRAINS ; i++)
{
pthread_join(tidArray[i],(void**)&trainArray[i]);
delete(trainArray[i]);
}
return(EXIT_SUCCESS);
}