Tin học
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Dùng Semaphore giải quyết bài toán Sản xuất — Tiêu dùng

Go down

Dùng Semaphore giải quyết bài toán Sản xuất — Tiêu dùng Empty Dùng Semaphore giải quyết bài toán Sản xuất — Tiêu dùng

Bài gửi  Phamvantoan(I22A) 31/3/2013, 14:36

Trong hệ thống có 2 loại phần tử :
ƒ Sản xuất chuyên tạo sản phẩm mới và để vào kho chứa.
ƒ Tiêu dùng chuyên lấy sản phẩm từ kho chứa ra để sử dụng.

#define N 100 /*number of slots in the buffer*/
typedef int semaphone; /*semaphone are a special kind of int*/
semaphone mutex=1; /*controls access to critical region*/
semaphone empty=N; /*count empty buffer slots*/
semaphone full; /*count full buffer slots*/
void producer(void)
{
int item;
while(true) /*true is the constant 1*/
{
produce_item(&item);/* generate something to put in buffer*/
down(&empty);/* decrement empty count*/
dow(&mutex); /*enter critical region*/
enter_item(&item); /*put new item in buffer*/
up(&mutex); /*leave critical region*/
up(&full); /*increment count of full slots*/
}
}
void consumer(void)
{
int item;
while(true) /*infinite loop*/
{
down(&full);/* decrement full count*/
dow(&mutex); /*enter critical region*/
remove_item(&item); /*take item from buffer*/
up(&mutex); /*leave critical region*/
up(&empty); /*increment count of full slots*/
consumer_item(item); /*do something with the item*/
}
}

Phamvantoan(I22A)

Tổng số bài gửi : 13
Join date : 13/03/2013
Age : 33
Đến từ : Thanh Hóa

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết