Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

CS3841 Operating Systems Lab 7: Pong – The Game of Champions “I kind of played Ping Pong in my mind with what to do." - Herb Sendek 1. Introduction Pong is a high paced, action packed game. In fact, it has even been considered as a potential Olympic sport1. In this lab, you will be creating a multi-threaded game of pong. The player will play against the computer, using the up and down arrow keys to manipulate the players paddle. The game will have sounds, graphics, scores, and other features. Plus, you will be working with a lab partner to develop the program. 2. Objectives • Construct software which uses POSIX Threads to manage multiple task concurrently. • Properly synchronize all shared resources as are necessary to prevent race conditions and to protect critical sections. • Use existing system calls to solve meaningful software development problems. • Use independent learning to learn about the curses API. • Practice good software development techniques in a environment. 3. Reference Reading Topic Book Pages

Programmer's Guide to NCurses

http://msoe.summon.serialssolutions.com/#!/search?bookMark=ePnHCXMw42LgTQStzc4rAe_hSmGEHnJjD mnKA3vXXHAOMCVxBzqam- kBkT_40jMjYO0NbLKYWHAgugSWxhYGRpwMsgGQBUu5qUXqxQrppZkpqQol-Qp- zuDFDjwMLCVFpcASVcrNNcTZQze3OD81HjoCEg- so0HLP7gZ1CCSxdmZOTnFwGIuHtSSLTYyqTCPT8oGnagErO- MQXPQmhCFicXZwKIGWAyVFMeX5YCnL4vjUbwDVKsEUQusaYCuS8lMB1_2UQA5KyIeWP-

NCurses NCurses agy5eVoYogkyFwmVxGAUaz0jJh6uCcE0sTM3B8-N4fAgA0zls0Q NCURSES Programming HOWTO http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

NCurses NCurses Library Writing Programs with NCURSES

http://invisible-island.net/ncurses/ncurses-intro.html

NCurses NCurses Library

4. VM Updates In order for this lab to function properly, you will need to make an update to the virtual machine image you have been provided. To do this, you will need to make certain your VM is connected to the network, preferably via

1 Well, maybe not. But it might be the next time the Olympics is in the US if we start working now... We were oh so close to having it in Chicago this year…

Page 1 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems cable, and issue the following command: sudo -get install libncurses5-dev

The sudo command increases your operating privileges within the shell to be an administrator and invokes the command which follows. The first command will install the curses library, which is used to manipulate the screen.

NCurses applications will not be directly runnable in Eclipse. To make things be runnable, you will need to follow a few different instructions to setup your development environment. You can find details of how to do this available here: http://damienlearnsperl.blogspot.com/2015/03/experimenting-with-ncurses-on-linux-in.html. When finished your, external tool configuration should match that which is shown below.

5. Lab Details For this lab, you will work with a partner. Between the two of you, you will only need to support one lab report and source code set.

In the repository, there is a baseline code set. This code establishes three threads for the game of pong, and provides a starting point for your game. The goal of this lab is to extend the project and incorporate additional features. A UML diagram for this source code is shown in Figure 1. (Notice that it is rather spartan...)

Page 2 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

Figure 1: Basic starting UML diagram.

The code as it is provided is somewhat crippled. First and foremost, there is no competition. The computer does not have a paddle. And, beyond that, the ball never goes beyond the end of the screen even if the paddle is missing. You will need to correct these things. Most importantly, the ncurses library is not thread safe! To ensure proper operation, you will need to synchronize all calls into the ncurses library using an appropriate synchronization mechanism.

When the game is finished, at a minimum, you will be required to support the following functionality: 1. One player that always plays against the computer, and the computer shall automatically manipulate its paddle. 2. The program shall keep track of the elapsed time since the game began.2 3. The game must implement a pause key whereby the game suspends operation until the pause key is pressed again. In pause mode, the user still can move their paddle, but the ball will not move.3 4. The user shall have the ability to set a difficulty level. The difficulty level shall adjust how fast the ball moves.4 5. The program must display the names of the players on the screen. Names will be provided as command line parameters to the program at startup. 6. The program shall keep score. How this is done is up to you. 7. The program shall play background music while the game is active.5 8. Volume level adjustment for sounds.

In addition, you program must implement at least two features not described above. Suggested ideas include

2 Hint: This may be best done with another thread.

3 Hint: This may require a new global variable and some way to suspend the threads when this variable is set into the appropriate state.

4 Hint: Adjust the usleep in the ball controller to handle this. It might involve a multiplication, division, or some other adjustment to this based on the game level 5 Hint: It is recommended that you have a thread which plays music continuously for this feature, reading from the file and starting back at the beginning when the end is reached. You might find it useful to read the entire file into memory to make this work better. Page 3 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

1. Pretty up the screen - add top and bottom borders, a net, etc. 2. Make the ball flash when it collides 3. The game shall play collision sounds when the ball impacts either the edge of the screen or a paddle.6 4. Display the authors of the program, the course, and the date as a pseudo splash screen before the program starts operating 5. Playback, whereby a log of the game is stored to disk and can be played back automatically (hint: hard and requires an extra thread) 6. Ability to store high scores to a file and display a log when high scores are exceeded 7. Computer versus computer play 8. Switching sides so the player can manipulate either the left or the right paddle 9. Two player version using different keys on the keyboard. 10. Two player version across the network (Hard).

While you should always use good programming practices and design, the emphasis for this lab is on working with threads and various system calls. With this being the case, your program must follow the following implementation guidelines:

1. Your program must add additional threads which are not present in the baseline code. 2. You may use global variables for inter-thread communication, but variables must be protected with appropriate mutex or semaphore synchronization constructs when accessed. It would be preferred, however, to encapsulate variables with appropriate accessor methods. 3. Each thread shall be implemented in a separate source file, and each c file shall include proper function prototypes in a .h file. 4. All global, shared variables shall be defined in headers, but you should really try to minimize the number of them that are in your program. 5. All methods which only require object scope shall be declared static. 6. The source code shall compile and link using an included build script without errors or warnings. 7. The source code shall be commented, and each function shall have documentation similar in format to that given in the downloaded file. 8. The ncurses library must be properly synchronized, as the library is not thread safe. 9. All keyboard input shall be handled by a single thread and dispatched based on input provided.7

6. Development Process Your instructor will provide you with a baseline file that contains some starting code. This starting code includes a basic ball movement algorithm thread, a basic paddle controller, and an empty thread for the opponent. This will get you started.

Since you will be working as a team with GIT repository access, you are free to use all of the configuration management techniques you have learned in previous courses, specifically SE2030 Software Tools and Practices. You may work with

6 Hint: You may find it best to develop an API that has a solid background music and then allows one to “mix” in a one shot track. Mixing can be accomplished by summing the values of two different audio tracks on a 16 bit sample by sample basis. 7 Note: This is not true in the initially provided code.

Page 4 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems your partner on different branches, merging the code into the trunk as you both reach working code. Your final deliverable should be merged onto the trunk of the repository for easy checkout by the instructor for grading purposes.

It is probably advisable to have a notebook with a sketch of the UML for your project, showing mainly the files you are creating and who will be responsible for implementing them. This is a great visualizer for your instructor as to how you are approaching your design. 7. Debugging this program Because of the terminal usage, debugging this program may be a bit challenging. However, there is a feature within Eclipse that allows you to connect to an executing process and debug the executing process. To do this, do the following: 1. In your eclipse project, open up the debug configurations menu and select C/C++ Attach to Application and click on the button with the + sign on it to create a new instance.

Figure 2: The Debug Configuration Display.

2. On the resulting window, enter the name of the project and other information on the configuration tab.

Page 5 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

Figure 3: The setup for attaching to an executing process.

3. After clicking on apply, open up a terminal window and start your program running on the console. With the programming then running, click on the Debug button for the debug profile and select the executing process that you would like to attach to from the select processes window. You can type the name in the box to filter.

Page 6 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

Figure 4: The select process dialog box for selecting and attaching to a running process.

4. After you click on OK, the debug perspective will open in Eclipse and you will be able to debug your program, setting and adjusting breakpoints as is desired. You may find that the debugger starts up a fashion such that the source code is not available. This will occur if the debugger is deep into OS System code. To get into your code, switch perspectives to C/C++, place a breakpoint at the location you would like the code to stop at, and then switch back to the debug perspective. You can then press F8 to resume executing and your program will stop at the desired location.

Page 7 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

Figure 5: Debugging when in system code.

8. Deliverables This will essentially be a two-week lab. However, in order to prevent extreme procrastination on the lab, certain interim deliverables and demonstrations are expected to be made in class.

8.1. Interim Demonstration During the next lab, you must demonstrate your program running with the following features 1. Computer control of one paddle 2. Game suspension when pause button is pressed 3. Elapsed time displaying on the screen 4. Anything else you have working by this time

When your final submission is made, you must submit a working makefile which will compile on the instructors machine.

9. Final Submission 1. GIT repository including all of your source code, makefile, etc. Your source code must compile without any warnings or errors using the makefile you provide. 2. Lab report, submitted in pdf format, with the following (a) Name, partners name (if one), date, and lab title, and repository name (b) Introduction. i. Summarize how your team approached this problem. This should be one paragraph describing the approach. (c) Time analysis i. An analysis of how long you spent on this lab. This does not need to be tremendously formal. ii. How much time did each person put into this lab? iii. What areas required the most effort?

Page 8 of 9 Department of Electrical and Computer Engineering and Computer Science Milwaukee School of Engineering Fall Quarter, 2018-2019 CS-3841 Operating Systems

iv. Who was responsible for what aspects of the lab assignment? Much of the time analysis section can be summarized in a table with appropriate explanation. (d) Design explanation. i. How did you structure the design of your program? ii. Where are the critical sections and how did you manage synchronization of the critical sections? iii. What does your UML class diagram look like. (Note: UML can be neatly hand drawn and then captures with a scanner or camera. It does not need to be machine generated.) (e) Users guide i. Describe from a users standpoint how one plays the game, how one accesses any special features you have added, etc. This should not be overly elaborate, possibly a table or two and paragraph or so (f) What things went right and what things went wrong with this lab. (g) What did you learn from this lab. (h) Conclusions

The lab report is to be uploaded into Blackboard.

Page 9 of 9