Presentation is loading. Please wait.

Presentation is loading. Please wait.

2010/12/12

Similar presentations


Presentation on theme: "2010/12/12"— Presentation transcript:

1 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com

2 fruit store’s stock system

3 Purpose To 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, mutual exclusion, odd-even algorithm. Key words: pthread, producer & consumer, mutual exclusion, odd-even algorithm.

4 Introduction This 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.

5 Requirement Given 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.

6 Input Command format apple orange grape1.txt 2.txt 3.txt

7 buffer Input #1 Input #2 Input #3 Basket #1 Basket #2 Basket #3 Output command P1 P2 P3 C1 C2 C3 Odd-Even Sorting Date flow

8 Requirement- Part I Mutual Exclusion buffer Apple Orange Appele Grape Orange Apple Orange Input #1 Input #2 Input #3 Input #4 Producer #1 Producer #2 Producer #3 Producer #4

9 Requirement-Part II Mutual Exclusion buffer Apple Orange Appele Grape Orange Apple Orange Basket #1 Basket #3 Basket #2 Consumer #1 Consumer #2 Consumer #3

10 Requirement- Part III Basket #1 Basket #2 Basket #3 Output #1 Output #2 Output #3 Odd-Even Sorting

11 Input files format(1.txt) Tag(Type)dateamountPrice

12 Example

13 buffer Input #1 Input #2 Input #3 Basket #1 Basket #2 Basket #3 Output command P1 P2 P3 C1 C3 Odd-Even Sorting O C2 Output Output #1

14 Example apple orange grape1.txt 2.txt 3.txt

15 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

16 buffer Input #1 Input #2 Input #3 Basket #1 Basket #2 Basket #3 command P1 P2 P3 C1 C2 C3 Odd-Even Sorting Output Output #1 OOOO

17 Example- Output apple.txt orange.txt grape.txt

18 Odd-Even Transposition Sort Odd-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.

19 Odd-Even Transposition Sort 1 st sort(even): 2 nd sort(odd):

20 Odd-Even Transposition Sort After sorting origin step1 step2 step3 step4 step5 step6

21 Example - Input 1.txt 2.txt 3.txt

22 Example- Output apple.txt orange.txt grape.txt

23 Grades Program(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%)

24 Notice: Individual work Demo date( deadline as well) 1/17-1/19, depends on the time you register for demo. Needs to demo on workstation.

25 Reminder(1/3) apple, orange, grape 是指不同的水果 ( 籃子 ) ,跟 input 裡 頭的名稱會對應且一致,也就是分放到不同籃子的依據 。 input 檔案沒有硬性規定數量,根據 input 的檔案數量, 就產生多少的 producer 來處理 input 檔案 ( 每個 producer 只處理一個 input file ,把所有資訊丟到同一個 buffer 中 )

26 Reminder(2/3) 整體流程大致上是: 1. 主執行緒 main() 讀入參數 2. 主執行緒根據 input files 個數產生對應數量的 producer , 每個 producer 擁有其中一個檔案 ( 指標或名稱 ) ,並讀取 該檔案的內容,將每一個水果資料放到共用的 Buffer 中

27 Reminder(3/3) 3. 主執行緒根據水果種類個數產生對應數量的 consumer , 每個 consumer 負責取一種水果到籃子裡 ( 也就是從共用 的 Buffer 讀取資料,若是 B 該筆水果資料屬於自己負責, 就從 Buffer 取出,放到籃子裡,這裡的籃子,可以是每 個 consumer 的區域變數 ) 4. producer 和 consumer 執行期間, producer 從檔案取資料 丟到 buffer , consumer 從 buffer 取資料出來丟到籃子裡 ,直到 Buffer 中的水果都處理完。 5. Buffer 的水果都取出之後,才開始進行排序。

28 Reference Chapter 6 Project- Producer & Consumer Odd-Even Sort http://en.wikipedia.org/wiki/Odd-even_sort http://en.wikipedia.org/wiki/Bubble_sort


Download ppt "2010/12/12"

Similar presentations


Ads by Google