2017-04-12 3 views
0

は、私は2つの整数をconsumer1すると数字の合計を印刷するconsumer1送っproducer1、2つの生産者と2つのコンシューマを作成し、私は別の消費者と生産者は、フォルダのパスをconsumer2に送られproducer2およびディレクトリからすべてのファイルを印刷consumer2持っています(linuxのlsコマンド)。今私はこれを一緒にマージしたいと思います。たとえば、すべてのプロデューサとコンシューマが同じメッセージキューを使用したいとします。より多くのコンシューマとプロデューサに同じfifoを使用するには?

これはproducer1のための私のコードです://IPC_msgq_send.c

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

#define MAXSIZE  128 

struct msgbuf 
{ 
    long mtype; 
    char mtext[MAXSIZE]; 
}; 

int main() { 
    int msqid; 
    // int msgflg = IPC_CREAT | 0666; 
    key_t key; 
    struct msgbuf sbuf; 
    size_t buflen; 

    key = ftok(".", 'g'); 

    //Get the message queue ID for the given key 
    if ((msqid = msgget(key, IPC_CREAT | 0666)) < 0) { 
     perror("Could not get message queue\n"); 
     exit(1); 
    } 
    printf("MSQID value: %d\n", msqid); 
    //Message Type 
    sbuf.mtype = 1; 
    printf("Enter a path for a folder : "); 
    scanf("%[^\n]",sbuf.mtext); 

    // getchar(); 

    buflen = strlen(sbuf.mtext) + 1 ; 

    if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) 
    { 
     printf ("%d, %ld, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, (int)buflen); 
     perror("Could not send message!\n"); 
     exit(1); 
    } 
    else 
     printf("Message Sent\n"); 

    exit(0); 
} 

Consumer1:

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

#define MAXSIZE 128 

struct msgbuf 
{ 
    long mtype; 
    char mtext[MAXSIZE]; 
}; 
int main() { 
    int msqid; 
    key_t key; 
    struct msgbuf rcvbuffer; 
    char pathForPrint[1024] = ""; 

    // key = 1234; 
    key = ftok(".", 'g'); 

    if ((msqid = msgget(key, 0666)) < 0) { 
    perror("Could not get message queue\n"); 
    exit(1); 
    } 
    printf("MSQID value: %d\n", msqid); 
    // Receive an answer of message type 1. 
    if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) { 
    perror("Could not receive message!\n"); 
    exit(1); 
    } 

    //printf("%s\n", rcvbuffer.mtext); 
    strcat(pathForPrint, "ls "); 
    strcat(pathForPrint, rcvbuffer.mtext); 
    system(pathForPrint); 
return 0; 
} 

Producer2:

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

#define VECTOR_SIZE 2 

struct msgbuf 
{ 
    long mtype; 
    int  vector[VECTOR_SIZE]; 
}; 

int main() { 
    int msqid; 
    // int msgflg = IPC_CREAT | 0666; 
    key_t key; 
    struct msgbuf sbuf; 
    size_t buflen; 
    int i; 

    key = ftok(".", 'g'); 

    //Get the message queue ID for the given key 
    if ((msqid = msgget(key, IPC_CREAT | 0666)) < 0) { 
     perror("Could not get message queue\n"); 
     exit(1); 
    } 
    printf("MSQID value: %d\n", msqid); 
    //Message Type 
    sbuf.mtype = 1; 
    while(1){ 
     printf("Enter a message to add to message queue : "); 
     for(i = 0; i < 2; i++){ 
      scanf("%d",&(sbuf.vector[i])); 

      buflen = sizeof(sbuf.vector[i]) + 1 ; 
     } 

    // getchar(); 

     if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0) 
     { 
      printf ("%d, %ld, %d, %d, %d\n", msqid, sbuf.mtype, sbuf.vector[0], sbuf.vector[1], (int)buflen); 
      perror("Could not send message!\n"); 
      exit(1); 
     } 
     else 
      printf("Message Sent\n"); 

     sleep(3); 
    } 

    exit(0); 
} 

Consumer2:

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

#define MAXSIZE 128 

#define VECTOR_SIZE 2 

struct msgbuf 
{ 
    long mtype; 
    int  vector[VECTOR_SIZE]; 
}; 
int main() { 
    int msqid; 
    key_t key; 
    struct msgbuf rcvbuffer; 

    // key = 1234; 
    key = ftok(".", 'g'); 
    while(1){ 
     if ((msqid = msgget(key, 0666)) < 0) { 
     perror("Could not get message queue\n"); 
     exit(1); 
     } 
     printf("MSQID value: %d\n", msqid); 
     // Receive an answer of message type 1. 
     if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0) { 
     perror("Could not receive message!\n"); 
     exit(1); 
     } 

     printf("%d\n", (rcvbuffer.vector[0] + rcvbuffer.vector[1])); 
    } 
    return 0; 
} 
使用する場合

は同じキューは、同じキーを持つことが必要であるが、どのようにそれを作るために?

ありがとうございました!

+1

。共通のキューは、コンシューマのいずれかがどのメッセージも処理できる場合は意味がありますが、これはあなたの例では当てはまりません。キュー2内の次のメッセージが消費者1のもので、その逆の場合、消費者2は何をしますか? – Aconcagua

+1

@Aconcagua:それはメッセージタイプが何のためにあるのかです。 – Matthias

答えて

1

キーは、ランダムに選択整数です。あなたはftok()でキーを生成する場合は、限り、あなたは、同じパスと同じidを参照を参照してください。同じキーを取得しますman page同じキーが必要な場合は、プログラムが異なるディレクトリを持つ可能性があるため、相対パスを使用することは悪い考えです。絶対パスを使用します。

しかし、アコンカグアの発言を考えてください。共通キューを使用する場合は、異なるタイプIDが必要です(両方にid = 1を使用します)。私には適切に見えない

関連する問題