message queue 추가

EEEFFEE·2023년 12월 2일
0

kdt system-project note

목록 보기
8/15

23.12.03 최초 작성

  • 사용자가 명령어를 입력하면 이를 해석해 각 메시지 큐에 명령어를 저장하고 특정 스레드에 명령을 전달

  • /toy_message.h : 메시지 큐로 보낼 메시지의 toy_msg_t 자료구조를 정의해놓은 헤더파일

#ifndef _TOY_MESSAGE_H_
#define _TOY_MESSAGE_H_

#include <unistd.h>

typedef struct {
    unsigned int msg_type;
    unsigned int param1;
    unsigned int param2;
    void *param3;
} toy_msg_t;

#endif /* _TOY_MESSAGE_H_ */

  • /main.c : 메시지 큐를 최초로 생성하는 코드 추가

#define NUM_MESSAGES 10
...
#include <mqueue.h>
#include <toy_message.h>
...

static mqd_t watchdog_queue;
static mqd_t monitor_queue;
static mqd_t disk_queue;
static mqd_t camera_queue;

static void create_msq(mqd_t *msq, const char *name, int max_num, int max_size);

int main(){
	
    ...
    
    // 메시지 큐 생성
    create_msq(&watchdog_queue, "/watchdog_queue", NUM_MESSAGES, sizeof(toy_msg_t));
    create_msq(&monitor_queue, "/monitor_queue", NUM_MESSAGES, sizeof(toy_msg_t));
    create_msq(&disk_queue, "/disk_queue", NUM_MESSAGES, sizeof(toy_msg_t));
    create_msq(&camera_queue, "/camera_queue", NUM_MESSAGES, sizeof(toy_msg_t));
}

...

// 메시지 큐 생성하는 함수
static void create_msq(mqd_t *msq, const char *name, int max_num, int max_size){
    struct mq_attr msqinfo;
    int flags = O_CREAT | O_RDWR | O_CLOEXEC;


    msqinfo.mq_maxmsg = max_num;
    msqinfo.mq_msgsize = max_size;
    *msq = mq_open(name, flags, 0777, &msqinfo);
    if ( *msq < 0){
        perror("Message queue open error");
        exit(0);
    }
}

  • /system/system_server.c : 메시지 큐에서 메시지를 받아 메시지 내용과 그에 따른 동작을 하는 코드 추가

#define CAMERA_TAKE_PICTURE 1
#include <mqueue.h>
#include <toy_message.h>

...

static mqd_t watchdog_queue;
static mqd_t monitor_queue;
static mqd_t disk_queue;
static mqd_t camera_queue;

...

void print_msq(toy_msg_t *msg);

...

int system_server(){
	...
    //	메시지 큐 open
    watchdog_queue = mq_open("/watchdog_queue", O_RDWR);
    monitor_queue = mq_open("/monitor_queue", O_RDWR);
    disk_queue = mq_open("/disk_queue", O_RDWR);
    camera_queue = mq_open("/camera_queue", O_RDWR);
	
    ...
    
}

...
// toy_msg_t 구조체의 메시지 내용을 출력하는 함수
void print_msq(toy_msg_t *msg){
    printf("msg_type : %d\n", msg->msg_type);
    printf("param1 : %d\n", msg->param1);
    printf("param2 : %d\n", msg->param2);
}

...

void *watchdog_thread(){
    ...
    toy_msg_t msg;
    while(1){
        ...
        if(mq_receive(watchdog_queue, (void *)&msg, sizeof(toy_msg_t), 0) < 0){
            perror("Message queue recevie error : watchdog");
            exit(0);
        }
        printf("Watchdog msq arrived\n ");
        print_msq(&msg);

    }
}

...

/* 메시지 큐의 내용을 받고 toy_msg_t 구조체의 msg_type이 
	1일 시 toy_camera_take_picture() 수행*/
void *camera_service_thread(){
    ...
    toy_msg_t msg;

    while(1){
        ...
        if(mq_receive(camera_queue, (void *)&msg, sizeof(toy_msg_t), 0) < 0){
            perror("Message queue recevie error : camera");
            exit(0);
        }
        printf("Camera msq arrived\n");

        print_msq(&msg);
        if(msg.msg_type == CAMERA_TAKE_PICTURE){
            toy_camera_take_picture();
        }
    }
}

  • /ui/input.c : 사용자가 입력한 내용을 바탕으로 메시지 큐에 메시지 저장

#include <mqueue.h>
#include <toy_message.h>

...

static mqd_t watchdog_queue;
static mqd_t monitor_queue;
static mqd_t disk_queue;
static mqd_t camera_queue;

int toy_message_queue(char **args);

char *builtin_str[] = {
    "send",
    "mu",
    "sh",
    "mq",
    "exit"
};

int (*builtin_func[]) (char **) = {
    &toy_send,
    &toy_mutex,
    &toy_shell,
    &toy_message_queue,
    &toy_exit
};

...

int input(){
	
    ...
    
    watchdog_queue = mq_open("/watchdog_queue", O_RDWR);
    monitor_queue = mq_open("/monitor_queue", O_RDWR);
    disk_queue = mq_open("/disk_queue", O_RDWR);
    camera_queue = mq_open("/camera_queue", O_RDWR);
    
    ...
    
}

...

// mq camera 1 입력을 받을 시 camera_queue 메시지 큐에 메시지 저장
int toy_message_queue(char **args)
{
    int mqretcode;
    toy_msg_t msg;

    if (args[1] == NULL || args[2] == NULL) {
        return 1;
    }

    if (!strcmp(args[1], "camera")) {
        msg.msg_type = atoi(args[2]);
        msg.param1 = 0;
        msg.param2 = 0;
        mqretcode = mq_send(camera_queue, (char *)&msg, sizeof(msg), 0);
        assert(mqretcode == 0);
    }

    return 1;
}



0개의 댓글

관련 채용 정보