Write a C program that accepts 3 parameters. Each parameter indicates the quantity of product to be
produced. Each product will be produced in different production line. Ready products will be placed
in a buffer area located at the end of each production line. Packaging workers will pack the products
into boxes. Information of each production line as follow:
Production line A: product ready in 1-2 minutes, buffer capacity: 12 units
Production line B: product ready in 2-3 minutes, buffer capacity: 6 units
Production line C: product ready in 1-2 minutes, buffer capacity: 24 units
There are currently 2 packaging workers available. Each worker needs 2 minutes to pack 6 units of
product into a box. The production line will be temporary suspended if the buffer area is full of
product. The operation will continue when the worker has taken 1 unit of product from the buffer
area.
Simulate the operation of production line and the packaging workers by using threads and
appropriate semaphores. There are at least 5 threads, but you may use additional thread if it is
necessary. You have to decide how the workers select the product to pack. Assume that 1 second in
your program is equivalent to 1 minute.
Sample output:
...
Buffer A: 11
Buffer C: 4
Worker X packing B: 3
Buffer B: 8
Worker Y packing C: 2
Buffer A: 12
Production line A suspended.
...

#anybody know how 2 do this question ?i really no idea on it ...can anybody help ?

Dani AI

Generated

: this is a classic producer/consumer concurrency problem. As noted, it belongs to software/concurrency design; below is a compact, practical design and implementation guidance that focuses on synchronization, worker selection, and clean termination without restating the assignment numbers.

A clean architecture uses one producer thread per production line and a fixed number of packaging worker threads. Give each production line its own bounded ring buffer (capacity per line). Protect each buffer with a mutex plus two semaphores: slots (free slots) and items (available items). Producers block on slots then enqueue under the mutex and post items. Consumers block on items, dequeue under the mutex, then post slots. Keep printing serialized with a print mutex to avoid interleaved output.

Workers can pick which buffer to service using several policies: first-available (scan buffers and take the first non-empty), fullness-priority (pick the buffer with most items to reduce suspensions), or round-robin (fairness). A practical non-blocking selection loop uses sem_trywait on each buffer’s items to find available work; if none found, fall back to blocking sem_wait on a chosen buffer. Use thread-safe random functions (e.g., per-thread RNG or rand_r) for simulated delays.

A minimal behavior sketch:

void *producer(void *arg) {
    Buffer *b = arg;
    while (more_to_produce) {
        simulate_production_delay();
        sem_wait(&b->slots);
        pthread_mutex_lock(&b->mtx);
        enqueue(b, item);
        pthread_mutex_unlock(&b->mtx);
        sem_post(&b->items);
    }
    return NULL;
}

void *worker(void *arg) {
    for (;;) {
        int i = choose_buffer_index(); // try sem_trywait loop, else sem_wait
        sem_wait(&buffers[i].items);
        pthread_mutex_lock(&buffers[i].mtx);
        item = dequeue(&buffers[i]);
        pthread_mutex_unlock(&buffers[i].mtx);
        sem_post(&buffers[i].slots);
        simulate_packing(item);
        if (shutdown_condition()) break;
    }
    return NULL;
}

Termination: track active producers (atomic counter). When a producer finishes, decrement the counter. Workers should exit when active producers is zero and all buffers are empty. Alternatively push sentinel "poison" items equal to worker count. Test with small quotas first. Avoid using non-thread-safe RNG or printing without a mutex; use pthread_join to wait for threads and sem_destroy/pthread_mutex_destroy to clean up.

You better ask this type of question in the Software Development forums...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.