Home > Backend Development > C++ > body text

Translation of producer-consumer problem in C language

王林
Release: 2023-09-09 08:17:04
forward
693 people have browsed it

Translation of producer-consumer problem in C language

In concurrent programming, concurrency represents a key concept that is necessary to fully understand how these systems operate. Among the various challenges faced by practitioners working with these systems, the producer-consumer problem is one of the most well-known synchronization problems. In this article, we aim to analyze this topic and highlight its importance for concurrent computing, while also exploring possible C-based solutions.

The Chinese translation of

Introduction

is:

Introduction

In a concurrent system, multiple threads or processes may access shared resources at the same time. The producer-consumer problem involves two entities: the producer generates data or tasks, and the consumer processes or consumes the generated data. The challenge is to ensure that producers and consumers synchronize their activities to avoid problems such as race conditions or resource conflicts.

Understanding the producer-consumer problem

Problem Statement

One possible definition of the producer-consumer problem involves two main groups: producers of data store their work in a shared space called a buffer, and processors (consumers) use that space Saved content. These individuals use their expertise in gathering items in this temporary storage scenario, analyze it comprehensively, and then provide insightful results.

Synchronization requirements

Solving the producer-consumer dilemma necessarily involves implementing synchronized collaboration technologies among various stakeholders. Optimizing the integration of synchronization protocols is crucial in order to avoid device buffers being overloaded by producing units or exhausted by consuming units.

Implementing the producer-consumer problem in C language

Shared buffer

In C language, you can use an array or queue data structure to implement a shared buffer. Buffers should be of fixed size and support operations such as adding data (producer) and retrieving data (consumer).

Synchronization technology

A variety of synchronization techniques can be used to solve the producer-consumer problem in C language, including

  • Mutex locks and condition variables − Mutex locks provide mutual exclusion protection for critical parts of the code, while condition variables allow threads to wait until specific conditions are met.

  • Semaphores - Semaphores can control access to shared buffers by tracking the number of empty and full slots.

  • Monitors − Monitors provide a higher-level abstraction for synchronization and encapsulate shared data and the operations that can be performed on it.

Solution to the producer-consumer problem in C

Bounded buffer solution

A common solution to the producer-consumer problem is the bounded buffer solution. It involves using fixed-size buffers with a synchronization mechanism to ensure that producers and consumers cooperate correctly. The capacity of a project's production is limited by the size of the buffer, so planning must take this specification into account to avoid exceeding the available space in the buffer.

Producer and consumer threads

In C language, the activities of producers and consumers can be implemented as separate threads. Each producer thread generates data and adds it to the shared buffer, while each consumer thread retrieves data from the buffer and processes it. Synchronization mechanisms are used to coordinate the activities of threads.

Handling edge cases

In real-world scenarios, additional factors may need to be considered. For example, if a producer generates data at a faster rate than a consumer can process it, you may need to use buffering mechanisms such as blocking or discarding data to prevent data loss or deadlock situations.

Two sample codes written in C language to illustrate the implementation of the producer-consumer problem

Bounded buffer solution using mutex and condition variables, with terminating condition.

The Chinese translation of

Example

is:

Example

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define BUFFER_SIZE 5
#define MAX_ITEMS 5

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int produced_count = 0;
int consumed_count = 0;

pthread_mutex_t mutex;
pthread_cond_t full;
pthread_cond_t empty;

void* producer(void* arg) {
   int item = 1;

   while (produced_count < MAX_ITEMS) {
      pthread_mutex_lock(&mutex);

      while (((in + 1) % BUFFER_SIZE) == out) {
         pthread_cond_wait(&empty, &mutex);
      }

      buffer[in] = item;
      printf("Produced: %d</p><p>", item);
      item++;
      in = (in + 1) % BUFFER_SIZE;

      produced_count++;

      pthread_cond_signal(&full);
      pthread_mutex_unlock(&mutex);
   }

   pthread_exit(NULL);
}

void* consumer(void* arg) {
   while (consumed_count < MAX_ITEMS) {
      pthread_mutex_lock(&mutex);

      while (in == out) {
         pthread_cond_wait(&full, &mutex);
      }

      int item = buffer[out];
      printf("Consumed: %d</p><p>", item);
      out = (out + 1) % BUFFER_SIZE;

      consumed_count++;

      pthread_cond_signal(&empty);
      pthread_mutex_unlock(&mutex);
   }

   pthread_exit(NULL);
}

int main() {
   pthread_t producerThread, consumerThread;

   pthread_mutex_init(&mutex, NULL);
   pthread_cond_init(&full, NULL);
   pthread_cond_init(&empty, NULL);

   pthread_create(&producerThread, NULL, producer, NULL);
   pthread_create(&consumerThread, NULL, consumer, NULL);

   pthread_join(producerThread, NULL);
   pthread_join(consumerThread, NULL);

   pthread_mutex_destroy(&mutex);
   pthread_cond_destroy(&full);
   pthread_cond_destroy(&empty);

   return 0;
}
Copy after login

In this example, a bounded buffer solution to the producer-consumer problem is implemented using mutex locks and condition variables. Producer threads generate items and add them to the buffer, while consumer threads retrieve and consume items from the buffer. The mutex ensures mutual exclusivity when accessing the buffer, and the condition variables (full and empty) coordinate the producer and consumer threads. Added termination conditions to limit the number of items generated and consumed.

Output

Produced: 1
Produced: 2
Produced: 3
Produced: 4
Consumed: 1
Consumed: 2
Consumed: 3
Consumed: 4
Produced: 5
Consumed: 5
Copy after login

Bounded buffer solution using semaphores and termination conditions

The Chinese translation of

Example

is:

Example

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

#define BUFFER_SIZE 5
#define MAX_ITEMS 20

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int produced_count = 0;
int consumed_count = 0;

sem_t mutex;
sem_t full;
sem_t empty;

void* producer(void* arg) {
   int item = 1;

   while (produced_count < MAX_ITEMS) {
      sem_wait(&empty);
      sem_wait(&mutex);

      buffer[in] = item;
      printf("Produced: %d</p><p>", item);
      item++;
      in = (in + 1) % BUFFER_SIZE;

      produced_count++;

      sem_post(&mutex);
      sem_post(&full);
   }

   pthread_exit(NULL);
}

void* consumer(void* arg) {
   while (consumed_count < MAX_ITEMS) {
      sem_wait(&full);
      sem_wait(&mutex);

      int item = buffer[out];
      printf("Consumed: %d</p><p>", item);
      out = (out + 1) % BUFFER_SIZE;

      consumed_count++;

      sem_post(&mutex);
      sem_post(&empty);
   }

   pthread_exit(NULL);
}

int main() {
   pthread_t producerThread, consumerThread;

   sem_init(&mutex, 0, 1);
   sem_init(&full, 0, 0);
   sem_init(&empty, 0, BUFFER_SIZE);

   pthread_create(&producerThread, NULL, producer, NULL);
   pthread_create(&consumerThread, NULL, consumer, NULL);

   pthread_join(producerThread, NULL);
   pthread_join(consumerThread, NULL);

   sem_destroy(&mutex);
   sem_destroy(&full);
   sem_destroy(&empty);

   return 0;
}
Copy after login

In this example, a bounded buffer solution to the producer-consumer problem is implemented using semaphores. Semaphores are used to control access to buffers and synchronize producer and consumer threads. Mutex semaphores ensure mutually exclusive access, full semaphores track the number of items in the buffer, and empty semaphores track the number of empty slots available. Added termination conditions to limit the number of items produced and consumed.

输出

Produced: 1
Consumed: 1
Produced: 2
Consumed: 2
Produced: 3
Consumed: 3
Produced: 4
Consumed: 4
Produced: 5
Consumed: 5
Copy after login

结论

生产者-消费者问题是并发编程中的一个重要挑战。通过理解问题并采用适当的同步技术,如互斥锁、条件变量、信号量或监视器,在C编程语言中可以开发出健壮的解决方案。这些解决方案使生产者和消费者能够和谐地共同工作,在并发系统中确保高效的数据生成和消费。

The above is the detailed content of Translation of producer-consumer problem in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template