Good day, I caught the eye of the code that implements this task, but I can not understand how it is performed and why. In particular, how producer () and consumer () are called and executed. As I see, the memory itself is allocated using System V functions, when the semaphores are used by POSIX. That's actually the main ():

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> #include <semaphore.h> #include <sys/shm.h> #include <time.h> #define N 10 int L = 0; struct sharedmem { sem_t mutex; sem_t buffer_full; sem_t buffer_empty; FIFO buff; // тут наша очередь, куда наш производитель кладёт буквы //для очереди реализованны банальные функции типо // Pop(FIFO & val) - достать элемент из очереди val // Push(FIFO & val, char c) - положить элемент в очередь val }; struct sharedmem *shared; int main() { srand((unsigned int)time(NULL)); int semid; semid = semget(1024, 3, IPC_CREAT | 0600); int shmid; shmid = shmget(1024, sizeof(shared), IPC_CREAT | 0600); shared = (struct sharedmem*) shmat(shmid, NULL, 0); shared->count = 0; sem_init(&shared->mutex, 1, 1); sem_init(&shared->buffer_full, 1, 0); sem_init(&shared->buffer_empty, 1, N); pid_t children[2]; children[0] = fork(); if(children[0] == 0) consumer(); children[1] = fork(); if(children[1] == 0) producer(); waitpid(children[0], NULL, 0); waitpid(children[1], NULL, 0); return 0; } 

This is a manufacturer function.

 void producer() { char let; while(1) { let = random_letter(); sem_wait(&shared->buffer_empty); sem_wait(&shared->mutex); Push(&shared->buff, let); sem_post(&shared->mutex); sem_post(&shared->buffer_full); printf("Producer added: %s\n", &let); Sleep();// засыпает на рандомное время } } 

This is a consumer

 void consumer() { char let; while(1) { sem_wait(&shared->buffer_full); sem_wait(&shared->mutex); let = Pop(&shared->buff); sem_post(&shared->mutex); sem_post(&shared->buffer_empty); printf("Consumer read and deleted: %s\n", &let); Sleep();// засыпает на рандомное время } } 

approximate result of the program: enter image description here

  • What's the question ? The program works correctly! - Yaroslav
  • I do not understand how the functions of the producer and the consumer are called - koshachok
  • The fork function creates a parallel thread (thread, CreateThread is often used instead of fork in win, 0 means that you are in another thread), you have two. One calls only the producer, and the other only the consumer. For those who write under Linux, there is slang "forking")) creating a stream with the fork function. - nick_n_a
  • @nick_n_a, and where does fork() know what to create for the current thread? - Koshachok
  • See, the flow is divided into two. One gives no zero, the other gives zero. If it gives zero, you enter the procedure with a blind cycle. Fork always creates another thread, one thread at the input, two at the output. One with zero, the other without. Often they even do if (fork()) { /*первый поток*/ } else { /*второй поток*/} - nick_n_a

0