···11+# **Assignment 02: Threads and Scheduling**
22+33+University of Puerto Rico at Rio Piedras
44+Department of Computer Science
55+CCOM4017: Operating Systems
66+77+## **Introduction**
88+99+In this project the student will simulate a **Shortest Job First** scheduling
1010+algorithm for a distributed system that consists of one embedded device and a
1111+central computer cluster.
1212+1313+The student will assume that we have one embedded device that generates
1414+computational problems that are too heavy to be performed in their hardware,
1515+either because the device's battery will drain, they do not have enough memory,
1616+nor computational resources to perform them in a timely manner.
1717+1818+We will simulate a computer cluster that will have a head node that receives
1919+requests for computing time from the embedded device, will put the jobs in a
2020+queue of processes, and then two different compute nodes will “execute”
2121+them.
2222+2323+## **Objectives**
2424+2525+* Practice threads implementations
2626+* Practice inter process communication using sockets (TCP) and a shared buffer
2727+* Identify critical regions
2828+* Implementation of mutual exclusion and semaphores
2929+3030+**REMARK:** Your assignment MUST only use the primitive Locks and Semaphores
3131+objects. The queue must also be implemented by the student using a regular
3232+python list. **NO other synchronization, or synchronized object library may be
3333+used in this assignment.** The use of these libraries will immediately
3434+invalidate the assignment, and it will not be graded.
3535+3636+## **Prerequisites**
3737+3838+* Python:
3939+ * [www.python.org](http://www.python.org/)
4040+* Python threads:
4141+ * [https://docs.python.org/3/library/threading.html](https://docs.python.org/3/library/threading.html)
4242+ * [https://pymotw.com/3/threading/](https://pymotw.com/3/threading/) (good
4343+ tutorial with examples)
4444+* Python sockets (**TCP**):
4545+ * [https://docs.python.org/3/library/socket.html](https://docs.python.org/3/library/socket.html)
4646+* Threaded sockets (**You are NOT allowed to use this one for this assignment**)
4747+ * [https://docs.python.org/3/library/socketserver.html](https://docs.python.org/3/library/socketserver.html)
4848+4949+## **Instructions**
5050+5151+### **The embedded device:**
5252+5353+The embedded device will consist of **one process (not threads)** that will:
5454+5555+1. Get the processes running in the computer where the simulation is running.
5656+2. For each process obtained in step 1, send a different message to the cluster
5757+head node, with a random number that will simulate the time it takes for the
5858+process to complete.
5959+ * sleep a **random** **short** (1 to 5 seconds) random period of time
6060+ between sends.
6161+6262+For instance if the embedded device gets the processes: top, ls, and cut, then
6363+the embedded device will send to the head node the messages:
6464+6565+* `top:3`
6666+* `ls:1`
6767+* `cut:5`
6868+6969+where 3, 1, and 5 are random numbers generated to simulate the time it took to
7070+execute each process. Note that in this example the syntax is
7171+`Process Name:cpu time`.
7272+7373+Example of how to run the embedded device:
7474+7575+```sh
7676+python edevice.py <server address> <server port>
7777+```
7878+7979+where `<server address>` is the IP address of the cluster, and `<server port>`
8080+is the port number of the cluster.
8181+8282+**Remark**: Your implementation of the edevice MUST be a process. You MUST use
8383+big port numbers less than 65,000
8484+8585+### **The cluster:**
8686+8787+The cluster will consist of three threads:
8888+1. One thread that will simulate the head node:
8989+ * Receives the messages generated by the embedded device and
9090+ * puts the "processes" in the **Shortest Job First** Scheduler queue,
9191+2. and two threads to
9292+ * extract the processes from the queue and "execute" them.
9393+9494+The first thread will act as a **producer to the scheduler** by:
9595+9696+1. Listening to the embedded device messages.
9797+2. Extract the time and process name out of the message and store them in a
9898+ shared queue.
9999+100100+The other two threads will act as **consumers** by:
101101+102102+1. Picking a message from the shared queue.
103103+2. Keeping a table of the processes attended by each consumer and the total
104104+ time consumed by each process.
105105+3. Sleep the time extracted from the message picked from the queue.
106106+107107+For instance, let's assume the producer thread receives messages `top:3`,
108108+`ls:1`, `cut:5`, and puts them in the queue, and that the consumer 1 extracts
109109+`top:3`, and consumer 2 extracts `ls:1`, `cut:5`.
110110+111111+Consumer 1 must have a list:
112112+```python
113113+[("top",3)] # total = 3
114114+```
115115+and consumer 2 must have a list:
116116+```python
117117+[("ls",1), ("cut", 5)] # total = 6
118118+```
119119+120120+Example of how to run the cluster:
121121+122122+```sh
123123+python cluster.py <server port>
124124+```
125125+126126+**Note:** The consumers must not poll for messages in the shared queue
127127+(repeatedly ask for messages in the queue). The thread **MUST** block
128128+(Semaphores) until there is a message in the queue.
129129+130130+Example output of the cluster:
131131+132132+Consumer 1 consumed 3 seconds of CPU time
133133+Consumer 2 consumed 6 seconds of CPU time
134134+135135+### **Other notes**
136136+137137+Make every configuration variable a global variable. Example:
138138+139139+1. Range of sleep times
140140+2. Range of process CPU time
141141+142142+## **Rubric**
143143+144144+1. Programs that do not run (can’t be tested) will automatically obtain 0
145145+ points.
146146+2. Documentation of the code and the README (10 pts)
147147+3. Use of an unsynchronized Data Structure for the messages (5 pts)
148148+ 1. **Remember that the use of a synchronized data structure will invalidate
149149+ your project**.
150150+4. Implementation of Semaphores for blocking and not polling or busy waiting
151151+ (10 pts)
152152+5. Implementation of Mutexes to protect the critical regions. (Only the
153153+ critical regions) (10 pts)
154154+6. Use of the sleep libraries (5 pts)
155155+7. Implementation of TCP sockets for message communication(10 pts)
156156+8. Working implementation of the cluster server (35 pts)
157157+9. Working implementation of the embedded device. (15 pts)
158158+159159+## **Deliverables**
160160+161161+* The source code of the programs (well documented)
162162+* A README file with:
163163+ * Good description of the program
164164+ * How to use your program, how to execute the processes.
165165+ * Any additional reference used to perform the project and the names of
166166+ students who helped perform the project.
167167+ * Verbal collaborations are allowed, however any form of code sharing is
168168+ not allowed.
169169+170170+## **Bonus**
171171+172172+Implement the embedded devices (edevice.py) such that it sends the jobs to the
173173+compute server and waits for a response of the compute server instead of just
174174+randomly sleep to send the next job.