Bonjour,
j'ai besoin d'échanger entre deux processus (1 writer, 1 reader), je teste les files de messages.
Le code du writer:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
struct Msgbuf {
    long mtype;       /* message type, must be > 0 */
    char mtext[128];    /* message data */
};
 
int main()
{
    int msgq;   //message queue identifier
    key_t cle;
 
    cle = ftok(PATH, PROJ_ID);
    if(cle < 0) {
        perror("ftok");
        exit(EXIT_FAILURE);
    }
 
    msgq = msgget(cle, 0660 | IPC_CREAT);
    if(msgq < 0) {
        perror("msgget");
        exit(EXIT_FAILURE);
    }
 
    struct Msgbuf msgbuf;
    msgbuf.mtype = 1;
 
    strcpy(msgbuf.mtext, "Hello");
    msgsnd(msgq, &msgbuf, sizeof(struct Msgbuf), 0);
    printf("Sent %s\n", msgbuf.mtext);
    usleep(1000000);
 
    msgbuf.mtype = 2;
    strcpy(msgbuf.mtext, "You !!!");
    msgsnd(msgq, &msgbuf, sizeof(struct Msgbuf), 0);
    printf("Sent %s\n", msgbuf.mtext);
 
    printf("Enter key to continue: ");
    getchar();
    msgctl(msgq, IPC_RMID, NULL);
    exit(EXIT_SUCCESS);
}
Le code du reader:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
struct Msgbuf {
    long mtype;       /* message type, must be > 0 */
    char mtext[128];    /* message data */
};
 
int main()
{
    int msgq;   //message queue identifier
    key_t cle;
    struct Msgbuf msgbuf;
    int nb_octets;
 
    cle = ftok(PATH, PROJ_ID);
    if(cle < 0) {
        perror("ftok");
        exit(EXIT_FAILURE);
    }
 
    msgq = msgget(cle, 0660 | IPC_CREAT);
    if(msgq < 0) {
        perror("msgget");
        exit(EXIT_FAILURE);
    }
 
    printf("Waiting for message...\n");
    do {
        nb_octets = 0;
        nb_octets = msgrcv(msgq, &msgbuf, sizeof(struct Msgbuf), 0, MSG_NOERROR);
        printf("%d %s\n", nb_octets, msgbuf.mtext);
        if(nb_octets < 0) {
            perror("msgrcv");
            break;
        }
    }while(msgbuf.mtype > 0);
 
    printf("Fin du lecteur.\n");
    exit(EXIT_SUCCESS);
}
Côté reader, 136 "Hello" est affiché puis plus rien jusqu'à ce que côté writer on appuie sur une touche du clavier.
Le reader répond par -1 "Hello" encore une fois. Apparemment "You !!!" n'arrive jamais puis "msgrcv: Identifier removed" puisque le reader clôt la file.
Que manque t-il à mon code?
Merci pour votre aide...