$29
Multi-Threading
Programming
Example - Hello Thread!
hello_thread.c
Output
vvvvvvvvdddv
x
<pthread.h> library info
Ref:
Tools for showing threads
• NCTU Workstation > top
(THR means the num of threads under the process)
Tools for showing threads
• Ubuntu -> sudo apt-get install htop
◦ - - press <F2> to enter htop setup menu.
◦ - - Choose "Display option" under "Setup" column, and toggle on "Three view" and "Show custom thread names" options.
◦ - - Presss <F10> to exit the setup.
Tools for showing threads
• Mac -> Enter the terminal > top
Problem: Sum Checker-input
• Input file: test.txt (it contains a 2D array.) Ex. (-1 means the (x=0 or y=0) of the 2D array. )
Problem: Sum Checker-rules
• Given a 9x9 2D-array and each item of the array must be the digits 1 to 9.
• Sum of each row, column, and each of the nine sub-grids must be the same and every digits only show once.
• Hint:
1. A thread to check that each sum of the rows.
2. A thread to check that each sum of the cols.
3. Nine threads to check each sum of the 3x3 sub-grids.
Example
Row
s
Colum
ns
3x3
subgrids
Problem: Sum
Checker-Output
Code output
Check num of the threads under your process
Problem: Sum Checker- Time
Command: time ./exe
Multi-thread Single thread
V.S
Requirements
1. You can write codes in C/C++.
2. You need to hand in one single-thread version and the other multi-thread version. Put all of *.c or *.cpp source files and OS_hw2_report.pdf into same compressed file. The type of compressed file must be
“studentID_hw2.zip” (ex. 010101_hw2.zip)
3. Use NCTU workstation as your environment
Deadline: 2018/10/28(日) PM 11:59
• Filename format sample
Grade
◦ Total score: 100 pts: COPY WILL GET A 0 POINT!
A. Input sample +10 pts
B. Two testing data + 10 pts (5 pts for each)
C. multithreading +40 pts
( report Q3: speed up= Ts/Tm ;
Ts:Time of single thread code ; Tm:Time of multi-thread code)
D. OS report (format OS_report.pdf) + 40 pts
E. Incorrect filename format will get -5 pts
◦ Incorrect output format will get -5 pts
DELAYED SUBMISSION WILL GET 0 POINT!