세마포어 메세지큐 공유메모리 IPC객체

----------------------------------------26-4.c읽기 파일-----------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>

#define SIZE 124
#define PRIOR 10   /* 우선순위 최대값 */

struct {
    long type;
    char data[SIZE];
} msg_data;

main()
{
    int msqid, data_len;

    /* 1234 키의 메시지 큐가 있으면 접근해서 식별자 받음 */
    if((msqid = msgget((key_t)1234, IPC_CREAT|0666)) == -1) {
       perror("msgget failed");
       exit(1);
    }

    while(1) {
       /* (-1*PRIOR)에 의해 메시지 큐에서 메시지 형식 값이 작은 메시지부터 읽음.
       IPC_NOWAIT에 의해 메시지 없으면 실패로 -1 반환하는데 이때의 errno는 ENOMSG */
       if((data_len=msgrcv(msqid, &msg_data, SIZE, (-1*PRIOR), IPC_NOWAIT)) == -1) {
          if(errno == ENOMSG) {
             printf("no message\n");
             break;
          }
          perror("msgrcv failed");
          break;
       }
       msg_data.data[data_len]='\0';
       printf("data : %s [%ld]\n", msg_data.data, msg_data.type);
    }

    /* msqid 메시지 큐 삭제 */
    if(msgctl(msqid, IPC_RMID, 0) == -1) {
       perror("msgctl failed");
       exit(1);
    }

    exit(0);
}
----------------------------------------26-3.c쓰기 파일-----------------------------------------
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define SIZE 124
/* 메시지 형식을 저장하는 type 멤버를 우선순위를 저장하는 용도로 사용한다. */
struct {
    long type;
    char data[SIZE];
} msg_data;

main()
{
    int msqid, data_len;
    char buffer[SIZE];

    /* 1234 키의 메시지 큐 생성 */
    if((msqid = msgget((key_t)1234, IPC_CREAT|0666)) == -1) {
       perror("msgget failed");
       exit(1);
    }

    while(1) {
       /* 표준입력장치로부터 데이터 입력 */
       printf("input data => ");
       scanf("%s", buffer);
       /* 입력받은 내용이 ‘quit’이면 while 문 벗어남 */
       if(!strcmp(buffer, "quit"))
          break;
       /* 표준입력장치로부터 우선순위 입력 */
       printf("input priority => ");
       scanf("%ld", &(msg_data.type));
       strcpy(msg_data.data, buffer);
       /* msg_data 메시지를 msqid 메시지 큐에 전송 */
       if(msgsnd(msqid, &msg_data, strlen(msg_data.data), 0) == -1) {
          perror("msgsnd failed");
          exit(1);
       }
    }
    exit(0);
}

      c 언어  |  2007. 9. 18. 11:21



sokoban's Blog is powered by Daum & Tattertools