본문 바로가기

IT

[ 리눅스 - IPC ] 리눅스 IPC 통신 예제

반응형
[ 리눅스 IPC 통신예제 ]
 

리눅스 IPC 통신방식 중 하나인 메시지 큐 방식에 대해서 알아보던 중 괜찮은 예제가 있어서 
블로그에 남깁니다.

예제를 찾으시는 분들에게 도움이 되었으면 좋겠습니다.

참고로 만약에 1:N 프로세스간의 통신을 해야 한다면,

N개의 키값과
N개의 큐가 필요합니다. 


 




IPC 예제 : 두 프로세스 간에 메세지 보내기

 

The following two programs should be compiled and run at the same time to illustrate basic principle of message passing:

message_send.c
 
-- 메시지큐 생성하기
-- 메시지큐에 한개의 메시지 던지기
 
message_rec.c
 
-- 큐로부터 메시지 읽기

message_send.c -- creating and sending to a simple message queue

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

#define MSGSZ     128


/*
 * Declare the message structure.
 */

typedef struct msgbuf {
         long    mtype;
         char    mtext[MSGSZ];
         } message_buf;

main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    message_buf sbuf;
    size_t buf_length;

    /*
     * Get the message queue id for the
     * "name" 1234, which was created by
     * the server.
     */
    key = 1234;

(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n",
key, msgflg);


// IPC_CREATE 를 인자로 넣으면 메시지 큐를 생성한다.
    if ((msqid = msgget(key, msgflg )) < 0) {
        perror("msgget");
        exit(1);
    }
    else 
     (void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);


    /*
     * We'll send message type 1
     */
     
    sbuf.mtype = 1;
    
    (void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
    
    (void) strcpy(sbuf.mtext, "Did you get this?");
    
    (void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
    
    buf_length = strlen(sbuf.mtext) + 1 ;
    
    

    /*
     * Send a message.
     */
    if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
       printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
        perror("msgsnd");
        exit(1);
    }

   else 
      printf("Message: \"%s\" Sent\n", sbuf.mtext);
      
    exit(0);
}


The essential points to note here are:

  • The Message queue is created with a basic key and message flag msgflg = IPC_CREAT | 0666 -- create queue and make it read and appendable by all.
  • A message of type (sbuf.mtype) 1 is sent to the queue with the message ``Did you get this?''
     

message_recv.c 

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

#define MSGSZ     128


/*
 * Declare the message structure.
 */

typedef struct msgbuf {
    long    mtype;
    char    mtext[MSGSZ];
} message_buf;


main()
{
    int msqid;
    key_t key;
    message_buf  rbuf;

    /*
     * Get the message queue id for the
     * "name" 1234, which was created by
     * the server.
     */
    key = 1234;

    if ((msqid = msgget(key, 0666)) < 0) {
        perror("msgget");
        exit(1);
    }

    
    /*
     * Receive an answer of message type 1.
     */
    if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
        perror("msgrcv");
        exit(1);
    }

    /*
     * Print the answer.
     */
    printf("%s\n", rbuf.mtext);
    exit(0);
}

The essential points to note here are:

  • The Message queue is opened with msgget (message flag 0666) and the same key as message_send.c.
  • A message of the same type 1 is received from the queue with the message ``Did you get this?'' stored in rbuf.mtext.
반응형