CS 307 Operating Systems

Homework 1

In this homework you will implement a program, which includes threads playing number guessing game. Number guessing game is as follows: There is a host who picks a number between 1 and 100 at the beginning of the game. Player makes a guess and host responds to the guess by specifying weather the actual number is equal to, greater than, or less than the guessed number. At each step player revises his/her guess and game continues until player makes a correct guess.

Your program should include two threads; one of them will act as the host and another will act as the player. At the beginning, host thread will pick a random number between 1 and 100 and print “I have picked a number between 1 and 100… Guess my number”. Player thread will guess a number and print it. Host thread will print the appropriate response. If player thread finds the correct number, host thread should print the number of guesses that player thread made. Below is the sample output of the program:

Thread1: I have picked a number between 1 and 100. Guess my number

Thread2: 30

Thread1: Try a higher number

Thread2: 70

Thread1: Try a lower number

Thread2: 50

Thread1: Try a higher number

Thread2: 60

Thread1: Try a higher number

Thread2: 65

Thread1: Try a higher number

Thread2: 68

Thread1: Try a lower number

Thread2: 67

Thread1: That is correct. It took you 7 guess(es) to get the correct number

Note that there will be no user input. User will only watch the conversation between threads. Player thread can use any algorithm to guess the number but it should increase/decrease its guess whenever host thread says it to do so(i.e. player thread shouldn’t guess a lower number when host thread says “Try a higher number”). Player thread should be able to find the correct number at the end.

You should submit your code that can be compiled and run on a Unix machine.

Working environment

Since you may not be familiar with Unix development environment we suggest a way to develop your code using Microsoft Visual C++. Following instructions are for user aliveli. Type your SU-Net username instead of aliveli.

Click Start > Run and type \\class\aliveli and hit enter.

You will be prompted to enter username and password. Enter your SU-net username and password. Create a directory called cs307. Enter cs307 directory and create a file called 1111-aliveli-hw1.cpp. Open this file using your favorite code editor (most probably Microsoft Visual C++ 6.0)

You can write your code using Visual Studio but you will not compile you code here. Instead you will connect to blum machine and compile and run your code there. To do this you can use an ssh client called putty. You can get it from

\\software\academic\Putty\putty.exe

Execute Putty program, write blum.sabanciuniv.edu in the hostname box and click open

Enter your SU-Net username and password. Now you are interacting with unix shell.

Type “cd cs307” (without quotes) to change current directory to cs307. You can type ls to list files in the current directory. In order to learn which directory you are in, type pwd (acronym for print working directory). When you type ls in cs307 directory you will see 1111-aliveli-hw1.cpp. You can compile this code using compiler called g++. In order to compile your code type the following

g++ 1111-aliveli-hw1.cpp –o hw1

Here -o hw1 option specifies name of the output executable file. If it is omitted then an executable called a.out is produced. In order to execute your program type hw1 and hit enter.

You may refer to the following link for more information:

Submission

Name your source code file as XXXX-namesurname-hw1.cpp where XXXX is your number and namesurname is your name and surname. Submit this cpp file via WebCT.