CS 5/7343 2021年春季
编程作业2
截止日期:4/3(周六)晚上11:59(无延期)。如果您在3/31(星期三)晚上11:59之前交出5%的奖金
该计划的目的是提供使用同步工具(mutex)的经验,并获得一些
服务器/线程池编程方面的经验。
应用程序–拍卖
该程序将模拟拍卖。主要过程将是一个拍卖师,这将有不同的
要拍卖的商品类型(由1到k的整数表示)。可能有多个项目可供选择
每种类型的拍卖。
客户和桌子
拍卖行里有一组桌子。 (每个表都应有一个ID,与您的操作类似
与程序1)中的线程。每张桌子将坐一个客户。到达的客户将被放入
等待队列(队列的长度没有限制)。对于没有客户的任何桌子,它将进行扫描
等待队列以查看是否有客户。如果有的话,桌子会让顾客坐在那里。
客户将按照先到先得的原则分配给表。
每个客户都有三个属性:
ID(作为整数)
客户拥有的总金额(正整数)
他/她感兴趣的项目列表,以整数列表表示。 (请注意,
客户可能对某种类型的多个商品感兴趣,因此整数列表可以
有重复项)。您可以假设该列表只能是1到k之间的整数。
客户可以在任何一轮拍卖结束后离开。但是,当他/她还剩0美元时,
他/她必须离开。另外,如果他/她获得了他/她感兴趣的所有物品,则必须离开
立即地。
每个表都由在程序开头创建的线程表示。每个线程一次
创建后,客户离开时不会加入。而是,线程将查看是否有任何客户
在等待队列中可用,并在可用的情况下坐在其中之一。该线程仅在主线程退出时退出
过程告诉它。
每轮拍卖
当拍卖师(主要过程)宣布要拍卖的下一个物品时,拍卖开始。
在拍卖师这样做之前,他/她会检查哪个桌子被占用,并且只会允许那些桌子
当时被占领的人参加那轮拍卖。
然后,每个客户将检查他/她是否对该商品感兴趣。然后他/她将随机出价
金额(不超过他/她拥有的金额)。如果客户没有出价,则出价为0
对项目感兴趣。任何出价金额都必须是整数。
如果没有人出价,则拍卖结束,该物品被丢弃。
如果只有一个客户出价,则拍卖将立即结束,并且出价的客户将赢得该项目。
但是,如果一个以上的客户出价,那么任何出价的客户都将被允许再出价一个
时间。他/她必须保持相同的金额或出价更高的金额。注意客户
不知道任何客户的出价金额。对于这种程序,您可以假设在这种情况下
客户将有50%的机会维持相同的出价金额,并有50%的机会增加出价
随机出价。此后,拍卖师将确定出价最高的人,然后
宣布获胜者。请注意,如果有平局,则没人会赢得拍卖,并且该物品将被丢弃。
一旦回合结束,拍卖师需要告知每张桌子该回合已完成,并告知
每个赢得拍卖的人(或没有人赢得)。
输入
您的程序应读取一个输入文件。输入文件具有以下内容:
第一行包含六个整数
o第一个数字是可用表的数量。至少应为2。
o第二个数字是项目类型的数量(描述中为k)
o第三个数字是输入文件中的客户数量。至少应为2。
o第四个数字是拍卖的物品的最大数量。如果所有
拍卖物品,主要过程应告知所有顾客它已经退出(并且
请大家离开)。另一方面,如果没有客户,则该客户
应该退出)。
o第五个数字表示是否要生成要拍卖的物品
随机地。如果为1,则将以1、2、3,...,k,1的循环序列生成项目。
2,3…..任何其他数字表示该项目将随机生成。
o如果要运行额外的积分,第六个数字为1,否则将被忽略。
随后的每一行代表一个客户。每行具有以下格式:
o第一个数字是客户的ID(整数)
o第二个数字是客户开始时拥有的金额(正数)
整数)
o第三个数字表示客户感兴趣的商品数量
o该行的其余部分表示客户感兴趣的每个商品的类型(可以
有重复)代做CS 5/7343程序
您可以假定输入文件遵循该格式。如果文件输入格式为inc
CS 5/7343 Spring 2021
Programming Homework 2
Due Date: 4/3 (Sat) 11:59 pm (No extensions). 5% bonus if you hand in by 3/31 (Wed) 11:59pm
The goal of the program is to provide experience on using synchronization tools (mutex) and gain some
experience on server/threadpool programming.
Application – Auctions
This program will simulate auctions. The main process will be an auctioneer which will have different
type of items (denoted by integers 1 to k) up for auction. There may be more than one item available for
auction for each type.
Customers and tables
There are a set of tables in the auction house. (Each table should have an id, similar for what you do
with the threads in program 1). Each table will sit one customer. Customers arriving will be put into a
wait queue (there is no limit on the length of the queue). For any table without a customer, it will scan
the wait queue to see if there is a customer. If there is, the table will get the customer to sit there.
Customer are assigned to tables on a first-come-first-serve basis.
Each customer has three attributes:
An ID (as an integer)
The total amount of money the customer has (as a positive integer)
A list of items that he/she is interested in, represented by a list of integers. (notice that the
customer may be interested in more than one item for a certain type, so the list of integers can
have duplicates). You can assume the list will only be of integers between 1 and k.
A customer can leave any time a round of auction has finished. However, when he/she has 0 dollars left,
he/she must leave. Also, if he/she has obtained all the items he/she is interested in, he/she must leave
immediately.
Each table is represented by a thread that is created in the beginning of the program. Once each thread
is created, it is NOT joined when a customer leaves. Rather, the thread will see if there is any customer
available on the wait queue and sit one of them if available. The thread only quits when the main
process tells it to.
Each round of auction
A round of auction starts when the auctioneer (main process) announce the next item to be auctioned.
Before the auctioneer do that, he/she check which table is occupied, and will only allow those tables
that are occupied at that time to take part in that round of auction.
Each customer will then check whether he/she is interested in the item. Then he/she will bid a random
amount (not more than the amount of money he/she has). A customer will bid 0 if he/she has no
interest in the item. Any bidding amount must be an integer.
If no one bid, the auction ends and the item is discarded.
If only one customer bids, the auction ends immediately and the customer that bid will win the item.
However, if more than one customer bids, then any customer that bids will be allowed to bid one more
time. He/she must either maintain the same amount or bid a higher amount. Notice that the customer
does NOT know the amount being bid by any customers. For this program, you can assume in this case
the customer will have a 50% chance of maintaining the same bid amount, and 50% chance of increasing
the bidding amount randomly. After that, the auctioneer will determine who get the highest bid, and
declare the winner. Notice that if there is a tie, no one win the auction and the item is discarded.
Once the round finishes, the auctioneer need to inform every table that the round is done, and inform
everyone who has won the auction (or nobody wins).
Input
Your program should read an input file. The input file have the following content:
The first line consists of six integers
o The first number is the number of tables available. It should be at least 2.
o The second number is the number of types of items (k in the description)
o The third number is the number of customers in the input file. It should be at least 2.
o The fourth number is the maximum numbers of items that are auctioned. If all the
items are auctioned, the main process should tell all customers that it is quitting (and
ask everyone to leave). On the other hand, if there is no customer left, the customer
should quit).
o The fifth number denote whether the items to be auctioned is to be generated
randomly. If it is 1, then the items are generated in a cyclic sequence of 1, 2, 3, …, k, 1,
2, 3 ….. Any other number denotes that the items is to be generated randomly.
o The sixth number is 1 if you want to run the extra credit, otherwise it is ignored.
Each subsequent line represents a customer. Each line has the following format:
o The first number is the ID of the customer (integer)
o The second number is the amount of money the customer has in the beginning (positive
integer)
o The third number denote the number of items that the customer is interested
o The rest of the line denote the type of each item that the customer is interested in (can
have duplicates)
You can assume the input file to follow the format. If the file input format is incorrect and your program
crash, you are not responsible.
Program Structure
Your program should have the following structure:
Main Process
Read the input file
Pre-process and initialization
Create the tables (threads)
Sit the initial customers
Repeat
Determine who can participate in the next round of auction
Select an item to auction
Auction the item
Send message to threads to denote who win/lose
Until all auction ends or all customers left
Print final information
Tell all threads to quit
Join all threads
Exit the program
Foe each thread
Initialization
Repeat
If there is no customer
Find a customer to sit join this table
Repeat
Going through each round of auction
Until customer leaves
Until told by the main program to quit
Print final information
Quit
Output Requirements
Your program should print the following statements. The output is considered a major part of this
project, so please follow direction closely.
When a customer is added to the customer queue, you should print the following statement
“Customer <ID of the customer> added to the queue”
When a customer is assigned to a table, you should print “Customer <ID of the customer>
assigned to table <table ID>”.
When a customer leaves, you should print “Customer <ID of the customer> leaves, winning <the
number of auction won> auctions, amount of money spent = <amount of money spent>,
amount of money left = <amount of money left>
For each round of auction:
o After determining who is eligible for this round, the program should print “the following
customers are eligible for this round of auction : <list of ID of customers eligible>”
o After the item is selected, the program should print “Item of type <item type> up for
auction”
o When anyone makes a bid (at any round), the program should print “Customer <ID of
the customer> at table <table ID> bids <amount being bid>”.
o When a second round of bidding is required, the program should output “Second round
of bidding needed”
o At the end of each auction, the program should print “Customer <ID> won the auction,
with amount <bid amount>”. IF no one win the auction, it should print “No one won the
auction”
o At the end of the program, you should print: “total number of item auctioned : <# of
auctioned> item, total number of item successfully auctioned: <# of item that have
actually been won by someone>, total amount: <sum of the total amount paid for the
items>”
Bonus (20 points, Required for 7343 students, optional for 5343 students)
Your program should create an extra thread that add new (randomly generated) customer. Your extra
thread structure should be like that
Repeat
Int Delay = 100000;
Int Dtry = 1;
Int x;
For (int I = 0; I < Dtry; i++)
For (int j = 0; j < Delay; j++)
X = 1;
Generate a random number between 0 and 4
If (number generated == 4)
Generate a random customer and add it to the queue
Dtry = Dtry + 1
Else
If (Dtry >: 2) then Dtry = Dtry / 2; else Dtry = 1
until received a message from the main process to quit
quit
Comment bonus
You will get a maximum of 5% bonus for comments in your program. To get that 5% you need to:
For each function, put comment to describe each parameter, and what is the expected output
For each mutex/semaphore used, denote what is it used for
For each critical section, denote when is the start and end of that critical section (you can name
your critical section in various ways to distinguish among them (if necessary))
For loops and conditional statements that do non-trivial work (you decide what is non-trivial)
put comment before it describing what it does.
What to hand in
You should put all you source code into a zip file and upload the zip file to Canvas
请加QQ:99515681 或邮箱:99515681@qq.com WX:codehelp