2010/12/12 [email protected] [email protected]
TRANSCRIPT
![Page 2: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/2.jpg)
fruit store’s stock system
![Page 3: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/3.jpg)
PurposeTo practice the “producer & consumer”
concept, which requires students to use pthread and at the same time use mutual exclusion. –Chapter 6
To understand why we need to design multi-threads program by practicing odd-even algorithm.
Key words: pthread, producer & consumer, Key words: pthread, producer & consumer, mutual exclusion, odd-even algorithm.mutual exclusion, odd-even algorithm.
![Page 4: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/4.jpg)
IntroductionThis program simulate a fruit store’s stock
system.First, purchase different kinds of fruits and
record all the orders.Second, classify them into different
department.At last, sort them by given properties.
In this project, you need to use pthread to implement a program with producer & consumer concepts and the odd-even algorithm.
![Page 5: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/5.jpg)
RequirementGiven different input files with tagged data,
students have to output the correct order by sorting.
Needs to implement the pthread with producer & consumer concepts and odd-even algorithm.
During your demonstration, students needs to fix their program instructed by given requirements.
![Page 6: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/6.jpg)
Input Command format
apple orange grape
1.txt 2.txt 3.txt
![Page 7: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/7.jpg)
buffer
Input #1
Input #2
Input #3
Basket #1
Basket #2
Basket #3
Output
command
P1
P2
P3
C1
C2
C3
Odd-EvenSorting
Date flow
![Page 8: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/8.jpg)
Requirement- Part I Mutual
Exclusion
buffer
Apple
Orange
Appele
Grape
Grape
Orange
Apple
Orange
Input #1
Input #2
Input #3
Input #4
Producer #1
Producer #2
Producer #3
Producer #4
![Page 9: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/9.jpg)
Requirement-Part IIMutual Exclusion
buffer
Apple
Orange
Appele
Grape
Grape
Orange
Apple
Orange
Basket #1
Basket #3
Basket #2
Consumer #1
Consumer #2
Consumer #3
Consumer #3
![Page 10: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/10.jpg)
Requirement- Part III
Basket #1
Basket #2
Basket #3
Output #1
Output #2
Output #3
Odd-EvenSorting
![Page 11: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/11.jpg)
Input files format(1.txt)Tag(Type) date amount Price
![Page 12: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/12.jpg)
Example
![Page 13: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/13.jpg)
buffer
Input #1
Input #2
Input #3
Basket #1
Basket #2
Basket #3
Output
command
P1
P2
P3
C1
C3
Odd-EvenSorting
O
C2
OutputOutput #1
![Page 14: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/14.jpg)
Example
apple orange grape
1.txt 2.txt 3.txt
![Page 15: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/15.jpg)
Each consumer need to sort the fruit by assign properties(date 、 amount 、 price), and write to the output file
Use Odd-even transposition sort
Sorting
![Page 16: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/16.jpg)
buffer
Input #1
Input #2
Input #3
Basket #1
Basket #2
Basket #3
command
P1
P2
P3
C1
C2
C3
Odd-EvenSorting
OutputOutput
Output #1
O O O O
![Page 17: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/17.jpg)
Example- Output
apple.txt
orange.txt
grape.txt
![Page 18: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/18.jpg)
Odd-Even Transposition SortOdd-Even Transposition Sort functions by
comparing all indexed pairs of adjacent elements in the list and, if a pair is in the wrong order the elements are switched. The next step repeats this for indexed pairs Then it alternates between and steps until the list is sorted. It can be thought of as using parallel processors, each using bubblesort but starting at different points in the list.
![Page 19: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/19.jpg)
Odd-Even Transposition Sort1st sort(even):
2nd sort(odd):
![Page 20: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/20.jpg)
Odd-Even Transposition SortAfter sorting
origin
step1
step2
step3
step4
step5
step6
![Page 21: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/21.jpg)
Example - Input
1.txt
2.txt
3.txt
![Page 22: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/22.jpg)
Example- Output
apple.txt
orange.txt
grape.txt
![Page 23: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/23.jpg)
GradesProgram(40%)
Compile and Run(10%)Correctness(30%)
Code(50%)Producer & consumer(20%)Mutual Exclusion(10%)Buffer ‘s structure(10%)Odd-even transposition sort(10%)Bonus
Report (10%)
![Page 24: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/24.jpg)
Notice:Individual workDemo date( deadline as well) 1/17-1/19,
depends on the time you register for demo.Needs to demo on workstation.
![Page 25: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/25.jpg)
Reminder(1/3)apple, orange, grape是指不同的水果 (籃子 ),跟
input裡頭的名稱會對應且一致,也就是分放到不同籃子的依據。
input檔案沒有硬性規定數量,根據 input的檔案數量,就產生多少的 producer來處理 input檔案 (每個producer只處理一個 input file,把所有資訊丟到同一個 buffer 中 )
![Page 26: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/26.jpg)
Reminder(2/3)整體流程大致上是:
1. 主執行緒main()讀入參數
2. 主執行緒根據 input files個數產生對應數量的producer,每個 producer擁有其中一個檔案 (指標或名稱 ),並讀取該檔案的內容,將每一個水果資料放到共用的 Buffer中
![Page 27: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/27.jpg)
Reminder(3/3)3. 主執行緒根據水果種類個數產生對應數量的 consumer,每個 consumer負責取一種水果到籃子裡 (也就是從共用的 Buffer讀取資料,若是 B該筆水果資料屬於自己負責,就從 Buffer取出,放到籃子裡,這裡的籃子,可以是每個 consumer的區域變數 )
4. producer 和 consumer執行期間, producer從檔案取資料丟到 buffer , consumer 從 buffer取資料出來丟到籃子裡,直到 Buffer中的水果都處理完。
5. Buffer的水果都取出之後,才開始進行排序。
![Page 28: 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com](https://reader030.vdocuments.site/reader030/viewer/2022032722/56649ce65503460f949b48ca/html5/thumbnails/28.jpg)
ReferenceChapter 6 Project- Producer & ConsumerOdd-Even Sort
http://en.wikipedia.org/wiki/Odd-even_sorthttp://en.wikipedia.org/wiki/Bubble_sort