"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Producer/Consumer

Producer/Consumer

Published on 2024-07-31
Browse:981

Definition

We consider two processes, called “producer” and “consumer”, respectively. The producer is a cyclical process and each time it goes through its cycle it produces a certain portion of information, which must be processed by the consumer. The consumer is also a cyclical process and each time it goes through its cycle it can process the next piece of information, as it was produced by the producer. A simple example is given by a computational process, which produces as “portions of information” images of punched cards to be punched by a punched card, which plays the role of the consumer.[1]

Producer/Consumer (Produtor/Consumidor)

Explanation

A producer creates items and stores them in a data structure, while a consumer removes items from that structure and processes them.

If consumption is greater than production, the buffer (data structure) empties, and the consumer has nothing to consume
If consumption is less than production, the buffer fills up, and the producer cannot add more items. This is a classic problem called limited buffer.

Contextualization of the Problem

Suppose we have a producer that publishes an email in the buffer, and a consumer that consumes the email from the buffer and displays a message stating that an email was sent with the new access password for the email. email provided.

Go implementation

package main

import (
    "fmt"
    "os"
    "strconv"
    "sync"
    "time"
)

type buffer struct {
    items []string
    mu    sync.Mutex
}

func (buff *buffer) add(item string) {
    buff.mu.Lock()
    defer buff.mu.Unlock()
    if len(buff.items) 



Explaining the implementation

  • First, we create a structure called buffer, which contains an array of strings called items and a mutex-like control mechanism, called mu, to manage concurrent access.
  • We have two functions: one called add, which basically adds an item to the buffer, as long as there is space available, since the buffer capacity is only 5 items; and another get call, which, if there are items in the buffer, returns the first element and removes that element from the buffer.
  • The Producer basically takes the index from the loop and concatenates it into a string called str, which contains the index and a dummy email domain, and adds it to the buffer. A time interval has been added to simulate a delay.
  • Consumer requests an item from the buffer, if it has at least one item. The Consumer then displays a message on the screen informing that an email was sent with the new access password for the item that was published in the buffer.

Code link: https://github.com/jcelsocosta/race_condition/blob/main/producerconsumer/buffer/producerconsumer.go

Reference

  1. https://www.cs.utexas.edu/~EWD/transcriptions/EWD01xx/EWD123.html#4.1. Typical Uses of the General Semaphore.

Bibliography

https://www.cin.ufpe.br/~cagf/if677/2015-2/slides/08_Concorrencia (Jorge).pdf

Release Statement This article is reproduced at: https://dev.to/celso/producerconsumer-produtorconsumidor-1jok?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3