C语言代写|COMP2017 9017 Assignment 3



Task description

SPX (Systems Programming Exchange) is a marketplace that enables highly sought after computer components to be traded amongst seasoned computer programmers. Traditionally, trading is inperson, however due to COVID-19 restrictions trading must now be done remotely.

Your task is to implement a new system for SPX that allows trading to take place electronically. This consists of two parts, the exchange (handles orders) and an auto-trader (a program which trades based on predefifined conditions).

You are encouraged to ask questions on Ed using the assignments category. As with any assignment,make sure that your work is your own, and you do not share your code or solutions with other students.

To complete this assignment, you should be familiar with:

  • Dynamic memory allocation: malloc(), realloc(), free(), etc
  • open(), read(), write() system calls
  • Processes: fork(), exec(), waitpid(), etc
  • Signals: sigaction(), kill(), etc
  • Named Pipes (FIFOs): mkfififo()

High Level Overview


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: 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:


Product 1

Product 2

Product N

For example:




Basic Example

  • 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 fulfifilled. Trader 0 has 5 CPUs remaining on the market for sale.

Implementation details

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 fifiles 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 fifile to fifind 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.

After both pipes are created for a trader, the exchange shall launch that trader binary as a new child process, assigning it 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.