Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a c program to answer this question Laboratory Exercise You have created n child processes from a parent and to distribute simulation tasks

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please write a c program to answer this question

Laboratory Exercise You have created n child processes from a parent and to distribute simulation tasks to those child processes in Lab 4 , by having the child getting the necessary data from the argument list or from variables. In real applications, the parent may not know the data before-hand, until provided by the user. It is also possible that the parent would need to decide what data to be passed to which child on demand, e.g. pass the next available task to the child which just completes its current task in the presence of many tasks to be assigned. It would be more efficient for the simulation study in Lab 4 when there are many tasks but a limited number of child processes. It is thus important that a parent is able to pass data to child processes after they are created, similar to lab6C.c, which makes use of the pipe. More importantly, child processes often need to convey results back to the parent, since child processes are created by a parent usually with a goal to help handling some of the workload of the parent. The use of exit status could only return a single value between 0 to 255 , and only when a child process terminates. Passing data back to parent when the child processes are still executing is therefore needed. You are to extend the program lab6C.c to play a simple version of the Hearts game, to allow communication between parent and child processes. The parent first creates all the necessary pipes (two pipes for each child, one from parent to child and the other from child to parent), and then forks the child processes. Note that each pipe is associated with two file descriptors, one for read and one for write. The parent and each child will close the excessive ends of the respective pipes. This is very important to help program development. Failure to close excessive pipes could often result in an incorrect program. The processes will communicate via the pipes to carry out the necessary computation. Finally, everyone closes all the pipes and the parent will wait for all the child processes to terminate at the end of the game. Effectively, a star communication topology is adopted and the parent is playing the key role. You would create 4 child processes to play the Hearts game. Input is received from the keyboard, but input redirection from a file is expected, so end-of-file testing should be adopted for the input. The input file contains a collection of cards. Each card is of the form S (* or spade), H ( or heart), C ( or club) or D( or diamond), and rank is drawn from the set {A,K,Q,J, T,9,8,7,6,5,4,3,2} in descending order, where T means 10 . You can assume that there is no error in the input lines. Parent reads in the cards until EOF. The cards are dealt to the child processes in a roundrobin manner and the game will start. In the Hearts game, the start player plays a card. This is called the lead card. Each player will take turn to play another card. Here are the game rules. Rule 1: each player must play a card of the same suit as the lead card, unless the player has no card of the same suit. In that case, he/she is allowed to play any card. We call such a case a "discard" of a card in a different suit. Rule 2: the player playing the highest card of the same suit as the lead card will "win" all 4 cards played in the round. A card of a different suit than the lead card always loses, even if it is an A. For example, if the lead card is 2, the card A will still lose to it. Rule 3: the winner of a round will be the start player in the next round. The scoring rule in Hearts is unconventional. A player winning a card of will be given one point. A player "winning" the very special card of Q will be given 13 points. So after a hand is played, a total of 26 points would normally be given for all players. There is one single and very important exception. If a particular player wins all 13 cards and also the Q, then he/she will be the big winner and all the other three players will be given 26 points instead (so there is a total of 78 points given out). The player with the least number of points will be the champion after the game is played the pre-determined number of times. A strategy to play is to avoid winning a round and attempt not to win the Q by accident. So the simple approach is to play low cards whenever possible. Note that if there is already a high card played, a player could try to play a high card that is just smaller than the highest played card to reduce the risk of winning in the upcoming tricks (SI. If a player is the last player and will win the cards even with the lowest card, he/she will play the highest card to win instead, so as to keep the low cards in hand for future use (S2). However, we would not implement these strategies in order to simplify the program logic. In other words, you simply play the lowest card in the required suit of the lead card. In the future, you may consider to introduce artificial intelligence into your program in order to make it play like a competent player. We assume that each child process is quite nave in playing the game. The start player will always select the lowest card in his/her hand to play. Since no one wants to get points, each player will try to avoid winning Q and cards. Thus, one will always play the lowest card in the suit of the lead card to avoid winning, or at least to reduce the chance of winning. If there is a chance of "discard" (i.e., no card with the same suit as the lead card) during playing, $Q will be chosen for discard. If there is no Q, then the highest card will be discarded. If there is no card, the highest card in the remaining hand will be discarded. If there are more than one highest cards in different suits for discard, just discard before before (a simple tie-breaking rule). In this program, the parent will act like the table for holding the cards, as well as the arbitrator to tell each child what card(s) have been played. A child will play a card by sending its card via a pipe to the parent, who will then relay the cards played in the round to the next child via a pipe for its consideration. The parent actually serves as the overall controller of the whole program, prompting the child processes for card and informing cards played to them. Here is a simple arrangement: each child always tries to read from the pipe about request from parent. Parent starts the game by "writing" tolead to the first child and then "reads" from that child. Child "reads" from parent and "writes" its card to be played with play. Parent then "writes" p1 ayed with all cards played in this round to the next child. When the game is finished, parent will compute and print the score for each child, since it knows all cards being played and the "winner" for each round. It then "writes" done to all child processes and concludes the game. Note that the sequence of messages printed by child processes at the beginning may occur in different orders and you do not need to try to control their order. Do not forget to wait for all the child processes to complete and close the pipes in the end of the game. Please provide proper comments and check your program before submission. Your program must run on apollo or apollo2. Here are some sample executions, with content of data file card. txt shown Sample execution (input data are stored inside a text file, e.g. card. txt): hearts

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions