23 lines
1.0 KiB
Plaintext
23 lines
1.0 KiB
Plaintext
Assignment name: producer_consumer
|
|
Expected files: producer_consumer.c
|
|
Allowed functions: memset, printf, malloc, free, write, pthread_create,
|
|
pthread_detach, pthread_join, pthread_mutex_init, pthread_mutex_destroy,
|
|
pthread_mutex_lock, pthread_mutex_unlock, usleep, gettimeofday
|
|
---------------------------------------------------------------------------
|
|
|
|
Implement the classic Producer-Consumer problem:
|
|
- 2 Producer threads: produce items (numbers from 1 to 100) and place them in a buffer
|
|
- 2 Consumer threads: consume items from the buffer and process them
|
|
- Shared buffer with limited capacity (10 items)
|
|
|
|
Rules:
|
|
- Producers must wait if buffer is full
|
|
- Consumers must wait if buffer is empty
|
|
- There should be no race conditions
|
|
- Print when items are produced/consumed
|
|
|
|
Usage: `./producer_consumer`
|
|
|
|
Hint: This exercise teaches you coordination between multiple threads with limited resources.
|
|
It's similar to the forks problem in Philosophers.
|
|
Use mutexes to protect the buffer and condition variables or sleep for coordination. |