Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
The purpose of the SPX is to allow trading to happen in an effificient and orderly manner. It receives orders from traders and matches them, allowing the buying and selling of computer components.
The exchange also tracks the cash balance of each trader (which starts at $0 for each trading session).
For providing such trading avenue, SPX collects a 1% transaction fee on all successful orders.
The exchange communicates with trader processes using a combination of named pipes (FIFOs) and signals. All messages are followed by the delimiter ; and signal SIGUSR1.
All messages sent through FIFOs are highlighted in this document:
EXAMPLE;
Traders carry out their buying and selling activities by placing orders on the exchange.
The following commands may be sent from traders to the exchange:
BUY <ORDER_ID> <PRODUCT> <QTY> <PRICE>;
SELL <ORDER_ID> <PRODUCT> <QTY> <PRICE>;
AMEND <ORDER_ID> <QTY> <PRICE>;
CANCEL <ORDER_ID>;
Order ID is unique per Trader (i.e. Trader 0 and 1 can both have their own Order ID 0). Order IDs are not reused (with the exception of Invalid orders, which can be fifixed and re-sent with the same ID, given the next ID is not yet used).
Products traded on the exchange are provided through a text fifile of the following structure:
n_items
Product 1
Product 2
…
Product N
For example:
2
GPU
Motherboard
SELL 0 CPU 15 300;
BUY 0 CPU 10 300;
Write programs in C that implement SPX as shown in the examples.
You are guaranteed not to have NULL returned from malloc() or realloc() in this context.
Your submission must be contained in the following files and produce no errors when built and run on Ed C compiler.
Read / write with FIFOs and/or write to stdout as instructed.
Your program output must match the exact output format shown in the examples and on Ed. You are encouraged to submit your assignment while you are working on it, so you can obtain feedback.
You may modify any of the existing scaffold fifiles, or make your own.
No external code outside the standard C library functions should be required.
In order to obtain full marks, your program must free all of the dynamic memory it allocates.
Exchange: Start Up
The exchange accepts command line arguments as follows:
./spx_exchange [product file] [trader 0] [trader 1] … [trader n]
The following example uses a product fifile named products.txt and trader binaries trader_a and trader_b:
./spx_exchange products.txt ./trader_a ./trader_b
Upon start up, the exchange should read the product file to find out about what it will trade. It should then create two named pipes for each trader:
/tmp/spx_exchange_<Trader ID>
/tmp/spx_trader_<Trader ID>
The spx_exchange_* named pipes are for the exchange write to each trader and spx_trader_* named pipes are for each trader to write to the exchange.
Afterwards, the exchange shall launch each of the trader binaries as a new child process, assigning each binary a trader ID starting from 0, in the Command Line Argument order. The trader ID shall be passed to the trader binary as a command line argument.
For the example above, the trader binaries should be launched like so:
./trader_a 0
./trader_b 1
Upon launching each binary, the exchange and trader should attempt to connect to both named pipes.
After all binaries are launched and pipes are connected, the exchange should send each trader (lowest trader IDs fifirst) the following message using the spx_exchange_* named pipes; afterwards, notify each trader using the SIGUSR1 signal.
MARKET OPEN;
Exchange: Placing Orders
Traders may place orders with the exchange by sending the appropriate commands through their spx_trader_<Trader ID> named pipe. Once the whole command is written to the pipe, it shall notify the exchange with the SIGUSR1 signal.
Once the exchange receives the SIGUSR1 signal from a trader, it would read the command from the spx_trader_<Trader ID> named pipe and processed the order appropriately. Depending on whether the order was accepted (for new buy/sell orders), amended or canceled, or if the command is invalid, it would write one of the following messages to the spx_exchange_<Trader ID> named pipe and notify the trader using the SIGUSR1 signal.
ACCEPTED <ORDER_ID>;
AMENDED <ORDER_ID>;
CANCELLED <ORDER_ID>;
INVALID;
The exchange should also message all other traders (lowest trader IDs first) using the spx_exchange_*named pipes, and notify them using the SIGUSR1 signal, with the following message:
MARKET <ORDER TYPE> <PRODUCT> <QTY> <PRICE>;
N.B.: In case of a cancelled order, QTY = 0 and PRICE = 0.