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:
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>;
- BUY: An order to buy a product at or below the specifified price, up to the specifified quantity.
- SELL: An order to sell a product at the specifified price, up to the specifified quantity.
- AMEND: Update the quantity or price of an existing order, that has yet to be fully fifilled.
- CANCEL: Cancel an existing order, that has yet to be fully fifilled.
Data Types and Ranges
- ORDER_ID: integer, 0 – 999999 (incremental)
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).
- PRODUCT: string, alphanumeric, case sensitive, up to 16 characters
- QTY, PRICE: integer, 1 – 999999
Products traded on the exchange are provided through a text fifile of the following structure:
- Trader 0 places a SELL order for 15 CPUs at $300 each
SELL 0 CPU 15 300;
- Trader 1 places a BUY order for 10 CPUs at $300 each
BUY 0 CPU 10 300;
- SPX matches these orders, Trader 1 buys 10 CPUs from Trader 0 for $3,000 and pays $30 transaction fee.
- Trader 1’s order is now fulfilled. Trader 0 has 5 CPUs remaining on the market for sale.
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.
- spx_common.h: Common constants and structures
- spx_exchange.c, spx_exchange.h: Part 1 (Exchange, compiles to spx_exchange)
- spx_trader.c, spx_trader.h: Part 2 (Trader, compiles to spx_trader)
- Test cases in tests directory
- README.md: Code Description
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:
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:
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.
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.
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.