나는 두 개의 생산자와 두 명의 소비자를 만들었고 producer1은 consumer1에 두 개의 정수를 보내고 consumer1은 숫자의 합을 출력하고 또 다른 소비자와 생산자는 producer2에게 consumer2에게 폴더의 경로를 보내고 consumer2는 모든 파일을 디렉토리에서 출력했다. (리눅스에서 ls 명령). 이제는 이것을 병합하고 싶습니다. 예를 들어 모든 제작자와 소비자가 동일한 메시지 대기열을 사용하기를 원합니다. //IPC_msgq_send.c더 많은 소비자와 생산자에게 동일한 FIFO를 사용하는 방법은 무엇입니까?
#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 :
이것은 producer1 내 코드입니다
#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;
}
동일한 키를 사용하려면 동일한 대기열이 필요하지만이를 만드는 방법은 무엇입니까?
감사합니다.
그건 나에게 적절하지 않습니다. 일반적인 대기열은 소비자 중 하나라도 메시지를 처리 할 수 있다면 의미가 있습니다. 이는 사용자의 경우에는 해당하지 않습니다. 큐 2에서 다음 메시지가 실제로 소비자 1에 대한 것이고 반대의 경우에도 소비자 2는 무엇을 할 것입니까? – Aconcagua
@Aconcagua : 그것이 메시지 유형입니다. – Matthias