watchdog, monitor, disk, camera 스레드 생성

EEEFFEE·2023년 12월 3일
0

kdt system-project note

목록 보기
4/15

23.12.03 최초 작성

  • 각 기능을 담당하는 스레드를 생성하는 코드 추가

  • /system/system_server.c : watchdog, monitor, camera, disk 스레드 생성하는 코드 추가

#include <assert.h>
#include <pthread.h>

void timer_sighandler();
int posix_sleep_ms(unsigned int overp_time, unsigned int underp_time);

void *watchdog_thread();
void *monitor_thread();
void *disk_service_thread();
void *camera_service_thread();

...

// 스레드 생성하는 코드 추가
int system_server()
{
	int retcode;
    pthread_t watchdog_thread_tid, monitor_thread_tid, disk_service_thread_tid, camera_service_thread_tid;
    
     retcode = pthread_create(&watchdog_thread_tid, NULL, watchdog_thread, NULL);
    if(retcode < 0){
        perror("Watchdog thread create error");
        exit(1);
    }

    retcode = pthread_create(&monitor_thread_tid, NULL, monitor_thread, NULL);
    if(retcode < 0){
        perror("Watchdog thread create error");
        exit(1);
    }

    retcode = pthread_create(&disk_service_thread_tid, NULL, disk_service_thread, NULL);
    if(retcode < 0){
        perror("Disk service thread create error");
        exit(1);
    }

    retcode = pthread_create(&camera_service_thread_tid, NULL, camera_service_thread, NULL);
    if(retcode < 0){
        perror("Camera service thread create error");
        exit(1);
    }
    
    ...
}

...

//	스레드에서 실행할 함수 코드
void *watchdog_thread(){
    printf("Watchdog thread started!\n");
    while(1){
        posix_sleep_ms(60, 0);
    }
}

void *monitor_thread(){
    printf("Monitor thread started!\n");
    while(1){
        posix_sleep_ms(60, 0);
    }

}

void *disk_service_thread(){
    printf("Disk service thread started!\n");
    while(1){
        posix_sleep_ms(60, 0);
    }
}

void *camera_service_thread(){
    printf("Camera service thread started!\n");
    while(1){
        posix_sleep_ms(60, 0);
    }

}

  • /ui/input.c : cmd 스레드 생성

#include <assert.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define TOY_TOK_BUFSIZE 64
#define TOY_TOK_DELIM " \t\r\n\a"

...

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

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

int input()
{
	...
    
     retcode = pthread_create(&command_thread_tid, NULL, command_thread, "command thread\n");
    if(retcode < 0){
        perror("Command thread error");
        return -1;
    }
    retcode = pthread_create(&sensor_thread_tid, NULL, sensor_thread, "sensor thread\n");
    if(retcode < 0){
        perror("Command thread error");
        return -1;
    }
    ...
}

...

int toy_num_builtins()
{
    return sizeof(builtin_str) / sizeof(char *);
}

int toy_send(char **args)
{
    printf("send message: %s\n", args[1]);

    return 1;
}

int toy_exit(char **args)
{
    return 0;
}

int toy_shell(char **args)
{
    pid_t pid;
    int status;

    pid = fork();
    if (pid == 0) {
        if (execvp(args[0], args) == -1) {
            perror("toy");
        }
        exit(EXIT_FAILURE);
    } else if (pid < 0) {
        perror("toy");
    } else
{
        do
        {
            waitpid(pid, &status, WUNTRACED);
        } while (!WIFEXITED(status) && !WIFSIGNALED(status));
    }

    return 1;
}

int toy_execute(char **args)
{
    int i;

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

    for (i = 0; i < toy_num_builtins(); i++) {
        if (strcmp(args[0], builtin_str[i]) == 0) {
            return (*builtin_func[i])(args);
        }
    }

    return 1;
}

char *toy_read_line(void)
{
    char *line = NULL;
    ssize_t bufsize = 0;

    if (getline(&line, &bufsize, stdin) == -1) {
        if (feof(stdin)) {
            exit(EXIT_SUCCESS);
        } else {
            perror(": getline\n");
            exit(EXIT_FAILURE);
        }
    }
    return line;
}

char **toy_split_line(char *line)
{
    int bufsize = TOY_TOK_BUFSIZE, position = 0;
    char **tokens = malloc(bufsize * sizeof(char *));
    char *token, **tokens_backup;

    if (!tokens) {
        fprintf(stderr, "toy: allocation error\n");
        exit(EXIT_FAILURE);
    }

    token = strtok(line, TOY_TOK_DELIM);
    while (token != NULL) {
        tokens[position] = token;
        position++;

        if (position >= bufsize) {
            bufsize += TOY_TOK_BUFSIZE;
            tokens_backup = tokens;
            tokens = realloc(tokens, bufsize * sizeof(char *));
            if (!tokens) {
                free(tokens_backup);
                fprintf(stderr, "toy: allocation error\n");
                exit(EXIT_FAILURE);
            }
        }

        token = strtok(NULL, TOY_TOK_DELIM);
    }
    tokens[position] = NULL;
    return tokens;
}

void toy_loop(void)
{
    char *line;
    char **args;
    int status;

    do {
        printf("TOY>");
        line = toy_read_line();
        args = toy_split_line(line);
        status = toy_execute(args);

        free(line);
        free(args);
    } while (status);
}

void *command_thread(void* arg)
{
    char *s = arg;

    printf("%s", s);

    toy_loop();

    return 0;
}

0개의 댓글

관련 채용 정보