-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarketParentChild.cpp
161 lines (110 loc) · 3.06 KB
/
MarketParentChild.cpp
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
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include "List.h"
#include "Stock.h"
#include "Market.h"
using namespace std;
int main(){
//declare variables needed to create the pipes and fork off the child
int fd_top[2];
int fd_bottom[2];
pid_t pid;
char line[80];
// create two pipes and check for errors
...
// fork off child process
// check for errors based on return value
...
/*
* block of code for parent
*/
// declare variables
//Market object using default parameters
//money variable set to 0.0
//any other variables needed ...
...
// close unused ends of the pipes
...
//display current balance, read in the stockfile, display list of stocks
...
// wait for child process to send purchase request through the pipe
...
// parse out parameters
...
// echo the child's request to the screen
...
// try to find stock by symbol (use Market class methods); report error
...
// if not found, send "[email protected]:NULL:No such stock" to child and exit
...
// if found, use stored price, quantity and cost to check for value client request
// Does the market have enough shares? Limit sale based on
// - number available
// - client funds
...
// remove stocks from market
...
// update money
...
// display confirmation to screen
...
// send confirmation to child
...
// construct string with purchase confirmation
...
// send purchase request to child via pipe
...
// wait for child to exit; display error if appropriate
...
//then display the updated balance and stocklist
...
// and write out the updated stockfile
...
// and then parent exits
...
/*
* block of code for parent
*/
// Wait 1 second to allow parent display to occur first
sleep(1);
//close unused ends of the pipes
...
// declare variables:
// child maintains a list of stocks ; the list is initially empty
// and a money balance, initialized to $10,000
// and other variables as needed
...
// Child displays initial account balance and list of stocks owned
...
// prompt the user to enter a stock symbol and a quantity
...
// build a C++ string that contains sym, quantity and money
...
// use the string functions str() to turn your C++ string
// object into a string, and then apply c_str() to make
// it a null-terminated string, suitable for sending through the pipe
...
// send the purchase request string to the parent/server through one pipe
...
// get the response from the parent/server through the other pipe
...
// parse out the elements of the returned string
// check for exceptions thrown by std::stoi and std::stod
...
// If the return value from the parent is successful
// make a new stock object based on the return string
...
// insert it into the list of stocks owned
...
// and update the account balance
...
// display updated balance and stock list
...
// and then exit
..
}