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

Post on 17-Dec-2015

219 views 3 download

Tags:

Transcript of 2010/12/12 ayao1917@gmail.com wei.karmao@gmai.com.

2010/12/12ayao1917@gmail.com

wei.karmao@gmai.com

fruit store’s stock system

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.

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.

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.

Input Command format

apple orange grape

1.txt 2.txt 3.txt

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

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

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

Requirement- Part III

Basket #1

Basket #2

Basket #3

Output #1

Output #2

Output #3

Odd-EvenSorting

Input files format(1.txt)Tag(Type) date amount Price

Example

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

Example

apple orange grape

1.txt 2.txt 3.txt

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

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

Example- Output

apple.txt

orange.txt

grape.txt

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.

Odd-Even Transposition Sort1st sort(even):

2nd sort(odd):

Odd-Even Transposition SortAfter sorting

origin

step1

step2

step3

step4

step5

step6

Example - Input

1.txt

2.txt

3.txt

Example- Output

apple.txt

orange.txt

grape.txt

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%)

Notice:Individual workDemo date( deadline as well) 1/17-1/19,

depends on the time you register for demo.Needs to demo on workstation.

Reminder(1/3)apple, orange, grape是指不同的水果 (籃子 ),跟

input裡頭的名稱會對應且一致,也就是分放到不同籃子的依據。

input檔案沒有硬性規定數量,根據 input的檔案數量,就產生多少的 producer來處理 input檔案 (每個producer只處理一個 input file,把所有資訊丟到同一個 buffer 中 )

Reminder(2/3)整體流程大致上是:

1. 主執行緒main()讀入參數

2. 主執行緒根據 input files個數產生對應數量的producer,每個 producer擁有其中一個檔案 (指標或名稱 ),並讀取該檔案的內容,將每一個水果資料放到共用的 Buffer中

Reminder(3/3)3. 主執行緒根據水果種類個數產生對應數量的 consumer,每個 consumer負責取一種水果到籃子裡 (也就是從共用的 Buffer讀取資料,若是 B該筆水果資料屬於自己負責,就從 Buffer取出,放到籃子裡,這裡的籃子,可以是每個 consumer的區域變數 )

4. producer 和 consumer執行期間, producer從檔案取資料丟到 buffer , consumer 從 buffer取資料出來丟到籃子裡,直到 Buffer中的水果都處理完。

5. Buffer的水果都取出之後,才開始進行排序。

ReferenceChapter 6 Project- Producer & ConsumerOdd-Even Sort

http://en.wikipedia.org/wiki/Odd-even_sorthttp://en.wikipedia.org/wiki/Bubble_sort