OS Bounded Buffer

Chihhh Linnn 香菇頭

INTRO

  1. 共享記憶體的解決方案 (Share memory Solution)
    使用一個共享的Buffer(bounded buffer),這個Buffer有固定大小,生產者將數據放入Buffer,消費者從中取出數據。為了保證生產者和消費者不會同時訪問Buffer,使用互斥鎖 (mutex) 和兩個條件變量 (condition variables),一個用於通知Buffer未滿 (not full),另一個用於通知Buffer非空 (not empty)。

  2. 生產者 (Producer)
    生產者的工作是將數據放入Buffer中,當Buffer滿時,它會等待消費者取走一些數據以騰出空間。

  3. 消費者 (Consumer)
    消費者的工作是從Buffer中取出數據,當Buffer為空時,它會等待生產者放入新的數據。

  4. Main Funtion

    1. 初始化 mutex
    2. prod_thread, cons_thread 保存新建 Thread ID , 創建生產者跟消費者 Thread
    3. 等待 Thread 完成
    4. 釋放它們佔用的資源 (destory)
  5. LIB Function (pthread.h)

    1. pThread_t: typedef __darwin_pthread_t pthread_t;
    2. pthread_mutex_lock , pthread_mutex_unlock , pthread_cond_wait , pthread_cond_signal

IMPL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define BUFFER_SIZE 5 // 緩衝區大小

int buffer[BUFFER_SIZE]; // share memory buffer
int count = 0; // data amount in buffer
int in = 0; // producer write position
int out = 0; // consumer read position

pthread_mutex_t mutex; // mutex lock for buffer
pthread_cond_t not_full; // not full condition variable for buffer
pthread_cond_t not_empty; // not empty condition variable for buffer


void* producer(void* param){
int item;
while(1){
item = rand() % 100; //隨機生成item
pthread_mutex_lock(&mutex); // lock
while(count == BUFFER_SIZE){
//如果緩衝區已滿,等待not_full條件
pthread_cond_wait(&not_full, &mutex);
}
// to buffer
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
count++;
printf("Producer produced: %d\n", item);

// buffer not empty signal
pthread_cond_signal(&not_empty);
pthread_mutex_unlock(&mutex); // unlock
sleep(1);
}
}

void* consumer(void* param){
int item;
while (1) {
pthread_mutex_lock(&mutex); // lock
while (count == 0) {
// if buffer is empty, wait for not_empty condition
pthread_cond_wait(&not_empty, &mutex);
}
// get data from buffer
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
printf("Consumer consumed: %d\n", item);

// notify producer buffer not full
pthread_cond_signal(&not_full);
pthread_mutex_unlock(&mutex); // unlock

sleep(1); // simulate consumption time
}
}


int main(void){
pthread_t producer_thread, consumer_thread;

// intialize mutex and condition variables
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&not_full, NULL);
pthread_cond_init(&not_empty, NULL);

// create producer and consumer threads
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);

// wait for threads to finish
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);

// destroy mutex and condition variables
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&not_full);

return 0;
}
  • Title: OS Bounded Buffer
  • Author: Chihhh Linnn
  • Created at : 2024-08-26 13:57:36
  • Updated at : 2024-08-26 13:57:36
  • Link: https://chihhhs.github.io/2024/08/26/os-bbuff/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
OS Bounded Buffer