As you are aware, we have lab assignments due every week. We also will have several homework assignments this semester, and this is one of those. While the labs are directed, the homework assignments present larger problems that you should break down into smaller problems. You should spend some time thinking about and planning your homework solutions before coding them.
Purpose:
This assignment will help you conceptualize, utilize, and implement
inter-process communication (IPC).
Understanding how a program can be forked into multiple processes, and
how those processes can communicate with each other,
form a cornerstone of system-level programming.
Getting Started
Page 503 in your Unix book begins a section on pipes.
Refer to the "talk.c" program on page 505. You should try it out yourself,
then use it as a template to build your solution.
Task:
Create a program that sets up three processes.
Two of the processes will play the Iterated Prisoner's Dilemma
(a "tournament").
The other process, the parent, will coordinate the tournament, like a
referee.
Upon start-up, the parent will:
The Prisoner's Dilemma is a classic game-theory model for interactions. In it, two players must choose between cooperate (with each other) and defect (against the other). Since the choice is simultaneous, each one does not know what the other will choose.
The following table shows a typical pay-off matrix. The pay-offs, or scores, are what each player gets at the end of the round. For example, if player 1 cooperates and player 2 defects, player 1 scores 0 while player 2 scores 5.
Player 2 Cooperates | Player 2 Defects | |
Player 1 Cooperates | 3/3 | 0/5 |
Player 1 Defects | 5/0 | 1/1 |
Create a program that forks twice. The two child processes will communicate with the parent process via pipes. Each child process needs 2 pipes, one to send data to the parent, and the other to receive data from the parent. Thus, your program will need to implement a minimum of 4 pipes. The child processes will play the Iterated Prisoner's Dilemma, with the parent process coordinating the tournament, and keeping track of the scores. Each message will be a single character.
The parent will send one of the following to the child processes.
Char | Meaning | Result |
R | get Ready | Child process replies with Y |
P | Play | Child process replies with move (C or D) |
Q | Quit | Child process exits |
0 | Zero | The child process scored 0 that round |
1 | One | The child process scored 1 that round |
3 | Three | The child process scored 3 that round |
5 | Five | The child process scored 5 that round |
How should the players decide what move to make?
You can determine this. Your players could base their next moves on the
score(s) from the last round(s), or they could ignore the score.
(They are required to read it, however.)
See strategies for a list of strategies that you can use.
According to your first and last name, implement the strategies below.
Name starts with, strategy to implement for one side
A-D, Unconditional Cooperator
E-K, Unconditional Defector
L-O, Random
P-R, Probability p Cooperator
S-T, Tit for Tat
U-Y, Tit for Two Tats
Z, One of the others on the list
i.e. a student named David Smith should implement Unconditional Cooperator
for one player, and Tit for Tat for the other.
When running your program, the user might pass "-h" as a command-line argument. If so, your program should print a "help" message about what the program does, and quit. It does not matter which argument is "-h"; if it is there, your program should print a "help" message about what the program does, and quit.
When running your program, the user might pass a number as a command-line argument. If so, your program should use that as the number of iterations. You should use "strtoimax" to convert the number from a string.
Any unusual command-line argument should be noted. Your program should indicate that the argument does not make sense and repeat the argument back, (i.e. "Argument "YOLO" does not make sense") print a "help" message about what the program does, and quit.
Criteria:
The ultimate goal is to have a program that solves the problem.
Each task should have a test associated with it, to verify that it works.
When all tasks have working solutions, they should all work together to
solve the problem. Keep in mind that we will check to see what tasks
are completed, and if your program does not solve the problem, it will still
receive partial credit.