Witam

moim zadaniem jest stworzyć trzy procesy potomne, które będą konsumentami a proces-rodzic będzie producentem. Producent wysyła do wybranego przez użytkownika konsumenta wiadomość za pomocą kolejki komunikatów. I właśnie z tym wybieraniem adresata mam problem - konsumenci nie wykonują swojego kodu. Do wyboru konsumenta używam zmiennej number. Jak ustawie ją na stałe dla jakiegoś wybranego producenta to program działa.

Byłbym wdzięcny za pomoc.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <string.h>

#define SIZE 100            //maksymalna długość wiadomości

pid_t PID[3];               //tablica PID

struct mymsgbuf {
    long    mtype;          // typ wiadomości
    int     request;        // numer żądania danego działania
    char    string[SIZE];   //tablica przechowująca wiadomość
    int     number;         //numer wybranego procesu
} msg;

struct mymsgbuf buf;        //struktura do odbioru wiadomości

int open_queue( key_t keyval ) {
    int qid;
          
    if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
        return(-1);
          
    return(qid);
}

//obsługa wysyłania wiadomości
int send_message( int qid, struct mymsgbuf *qbuf ){
    int result, length;
  
    /* lenght jest rozmiarem struktury minus sizeof(mtype) */
    length = sizeof(struct mymsgbuf) - sizeof(long);        
  
    if((result = msgsnd( qid, qbuf, length, 0)) == -1)
          return(-1);
          
    return(result);
}

//obsługa usuwania kolejki
int remove_queue( int qid ){
    if(msgctl( qid, IPC_RMID, 0) == -1)
        return(-1);
          
    return(0);
}

//obsługa zytania wiadomości
int read_message( int qid, long type, struct mymsgbuf *qbuf ){
    int result, length;
  
    /* lenght jest rozmiarem struktury minus sizeof(mtype) */
    length = sizeof(struct mymsgbuf) - sizeof(long);        
  
    if((result = msgrcv( qid, qbuf, length, type,  0)) == -1)
        return(-1);
          
    return(result);
}
/*
 * 
 *
 *
*/
int main(void){
    fflush(stdin);
    
    int    qid;
    key_t  msgkey;
  
    //wartość klucza IPC
    msgkey = ftok(".", 'a');
  
    //otwarcie/utworzenie kolejki
    if((qid = open_queue(msgkey)) == -1) {
        perror("BŁĄD. Nie mogę otworzyć kolejki.");
        exit(1);
    }
    
    //ODBIERANIE
    buf.mtype   = 1;
    buf.request = 1;
    
    char bufor[SIZE];
    
    if (fork() == 0) {
        PID[0] = getpid();
        printf("Proces 1: %d\n", PID[0]);
        
        while (1) {
            if (msg.number == 1) {
                read_message(qid, msg.mtype, &buf);
                printf("Proces %d odebrał: %s\n", msg.number, buf.string);
            }
        }
        return 0;
    }
    else if (fork() == 0) {
        PID[1] = getpid();
        printf("Proces 2: %d\n", PID[1]);
        
        while (1) {
            if (msg.number == 2) {
                read_message(qid, msg.mtype, &buf);
                printf("Proces %d odebrał: %s\n", msg.number, buf.string);
            }
        }
        return 0;
    }
    else if (fork() == 0) {
        PID[2] = getpid();
        printf("Proces 3: %d\n", PID[2]);
        
        while (1) {
            if (msg.number == 3) {
                read_message(qid, msg.mtype, &buf);
                printf("Proces %d odebrał: %s\n", msg.number, buf.string);
            }
        }
        return 0;
    }
    else if (fork() == 0) {
        PID[3] = getpid();
        printf("Proces 4: %d\n", PID[3]);
        
        while (1) {
            if (msg.number == 4) {
                read_message(qid, msg.mtype, &buf);
                printf("Proces %d odebrał: %s\n", msg.number, buf.string);
            }
        }
        return 0;
    }
    else {
        printf("Rodzic: %d\n", getpid());
        
        while (1) {
            sleep(1);

            //odczyt z stdin
            printf("Podaj numer procesu (0 kończy program): \n");
            scanf("%d", &msg.number);

            //sprawdza czy wyjść z programu
            if (msg.number == 0 || msg.number >= 5) {
                printf("Kończę działanie programu\n");
                kill(PID[0], 9);
                kill(PID[1], 9);
                kill(PID[2], 9);
                kill(PID[3], 9);
                remove_queue(qid);
                return 0;
            }
            
            //odczyt z stdin
            printf("Podaj wiadomość: \n");
            scanf("%s", &bufor);
            strcpy(msg.string, bufor);
            
            //WYSYŁANIE
            msg.mtype   = 1;
            msg.request = 1;
            
            //wysyłanie wiadomości
            if((send_message( qid, &msg )) == -1) {
                perror("BŁĄD. Nie mogę wysłać wiadomości.");
                kill(PID[0], 9);
                kill(PID[1], 9);
                kill(PID[2], 9);
                kill(PID[3], 9);
                remove_queue(qid);
                exit(1);
            }
        }
    }  
    
    //usunięcie kolejki
    remove_queue(qid);
    return 0;
}