untitled

Pasted Dec 2, 2026, 12:46 PM1 file85 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgrqvqfg0z4xab9
untitled

const int N = 5;

int buffer[N];
int l = 0, o = 0;

semaphore a, L, O;

void producator(int rep)
{
    int i, m;

    for (i = 0; i < rep; i++)
    {
        // Produce mesajul
        m = which_proc() * 100 + i;

        // P(L)
        p(L);

        // P(a)
        p(a);

        // Regiunea critica: depunerea
        buffer[l] = m;
        l = (l + 1) % N;

        // V(a)
        v(a);

        // V(O)
        v(O);
    }
}

void consumator(int rep)
{
    int i, m;

    for (i = 0; i < rep; i++)
    {
        // P(O)
        p(O);

        // P(a)
        p(a);

        // Regiunea critica: extragerea
        m = buffer[o];
        o = (o + 1) % N;

        // V(a)
        v(a);

        // V(L)
        v(L);

        // Prelucrarea mesajului
        cout << "P " << which_proc()
             << ": consumat " << m << endl;
    }
}

void main()
{
    initialsem(a, 1);
    initialsem(L, N);
    initialsem(O, 0);

    cobegin
    {
        producator(5);
        producator(5);
        producator(5);
        producator(5);
        producator(5);

        consumator(5);
        consumator(5);
        consumator(5);
        consumator(5);
        consumator(5);
    }
}

1,187 chars · codepastes.com