리눅스 데브코스[7주차 - 1]<스레드 실습>

심우열·2023년 6월 3일
0

1. 스레드 코드 분석

1. tlpi-dist/threads/simple_thread.c

1. 코드

/* simple_thread.c

   A simple POSIX threads example: create a thread, and then join with it.
*/
#include <pthread.h>
#include "tlpi_hdr.h"

static void *
threadFunc(void *arg)
{
    char *s = arg;

    printf("%s", s);

    return (void *) strlen(s);
}

int
main(int argc, char *argv[])
{
    pthread_t t1;
    void *res;
    int s;

    s = pthread_create(&t1, NULL, threadFunc, "Hello world\n");
    if (s != 0)
        errExitEN(s, "pthread_create");

    printf("Message from main()\n");
    s = pthread_join(t1, &res);
    if (s != 0)
        errExitEN(s, "pthread_join");

    printf("Thread returned %ld\n", (long) res);

    exit(EXIT_SUCCESS);
}

2. 동작 모습

3. 디버깅 및 분석

2. tlpi-dist/threads/detached_attrib.c

1. 코드

/* detached_attrib.c

   An example of the use of POSIX thread attributes (pthread_attr_t):
   creating a detached thread.
*/
#include <pthread.h>
#include "tlpi_hdr.h"

static void *
threadFunc(void *x)
{
    return x;
}

int
main(int argc, char *argv[])
{
    pthread_t thr;
    pthread_attr_t attr;
    int s;

    s = pthread_attr_init(&attr);       /* Assigns default values */
    if (s != 0)
        errExitEN(s, "pthread_attr_init");

    s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if (s != 0)
        errExitEN(s, "pthread_attr_setdetachstate");

    s = pthread_create(&thr, &attr, threadFunc, (void *) 1);
    if (s != 0)
        errExitEN(s, "pthread_create");

    s = pthread_attr_destroy(&attr);    /* No longer needed */
    if (s != 0)
        errExitEN(s, "pthread_attr_destroy");

    s = pthread_join(thr, NULL);
    if (s != 0)
        errExitEN(s, "pthread_join failed as expected");

    exit(EXIT_SUCCESS);
}

2. 동작 모습

3. 디버깅 및 분석

2. 토이 프로젝트

1. System server와 input process 확장

  • 6개 thread 추가
    -> Watchdog, disk service, monitor, camera service, command, sensor

2. Command thread

  • 키보드 입력을 받아서 등록된 함수 수행하도록 수정.
  • 향후 메시지 테스트 용도로 활용.

3. 동작 모습

4. git

"V9 (add Multi-Thread)"
https://github.com/w10sim/FIleServer_Linux_SystemProgramming

profile
Dev Ops, "Git, Linux, Docker, Kubernetes, ansible, " .

0개의 댓글