pthread_getattr_np(3) - wariua/manpages-ko GitHub Wiki

NAME

pthread_getattr_np - μƒμ„±λœ μŠ€λ ˆλ“œμ˜ 속성 μ–»κΈ°

SYNOPSIS

#define _GNU_SOURCE             /* feature_test_macros(7) μ°Έκ³  */
#include <pthread.h>

int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr);

-pthread둜 컴파일 및 링크.

DESCRIPTION

pthread_getattr_np() ν•¨μˆ˜λŠ” attr이 κ°€λ¦¬ν‚€λŠ” μŠ€λ ˆλ“œ 속성 객체λ₯Ό λ™μž‘ 쀑인 μŠ€λ ˆλ“œ threadλ₯Ό κΈ°μˆ ν•˜λŠ” μ‹€μ œ 속성 값듀을 담도둝 μ„€μ •ν•œλ‹€.

λ°˜ν™˜λ˜λŠ” 속성 값이 pthread_create(3)으둜 μŠ€λ ˆλ“œλ₯Ό 생성할 λ•Œ 썼던 attr 객체둜 μ „λ‹¬ν•œ λŒ€μ‘ 속성 κ°’κ³Ό λ‹€λ₯Ό μˆ˜λ„ μžˆλ‹€. 특히 λ‹€μŒ 속성듀이 λ‹€λ₯Ό 수 μžˆλ‹€.

  • 뢄리 μƒνƒœ. 생성 ν›„ ν•©λ₯˜ κ°€λŠ₯ μŠ€λ ˆλ“œκ°€ 슀슀둜λ₯Ό λΆ„λ¦¬ν–ˆμ„ 수 μžˆλ‹€.

  • μŠ€νƒ 크기. κ΅¬ν˜„μ—μ„œ μ λ‹Ήν•œ κ²½κ³„λ‘œ μ •λ ¬ν–ˆμ„ 수 μžˆλ‹€.

  • 방호 ꡬ역 크기. κ΅¬ν˜„μ—μ„œ νŽ˜μ΄μ§€ 크기의 배수둜 올림 ν•  수 있으며, λ˜λŠ” μ‘μš©μ—μ„œ 자체적으둜 μŠ€νƒμ„ ν• λ‹Ήν•˜λŠ” 경우 λ¬΄μ‹œν•  수 (즉 0으둜 λ‹€λ£° 수) μžˆλ‹€.

여기에 λ”ν•΄μ„œ, μŠ€λ ˆλ“œ 생성에 쓰인 μŠ€λ ˆλ“œ 속성 객체에 μŠ€νƒ μ£Όμ†Œ 속성이 섀정돼 μžˆμ§€ μ•Šμ•˜λ‹€λ©΄ κ΅¬ν˜„μ—μ„œ κ·Έ μŠ€λ ˆλ“œμ— 골라 μ€€ μ‹€μ œ μŠ€νƒ μ£Όμ†Œλ₯Ό λ°˜ν™˜λ˜λŠ” μŠ€λ ˆλ“œ 속성 객체가 μ•Œλ € 주게 λœλ‹€.

pthread_getattr_np()κ°€ λ°˜ν™˜ν•œ μŠ€λ ˆλ“œ 속성 객체가 더 이상 ν•„μš”ν•˜μ§€ μ•ŠμœΌλ©΄ pthread_attr_destroy(3)둜 νŒŒκΈ°ν•˜λŠ” 게 μ’‹λ‹€.

RETURN VALUE

성곡 μ‹œ 이 ν•¨μˆ˜λ“€μ€ 0을 λ°˜ν™˜ν•œλ‹€. 였λ₯˜ μ‹œ 0 μ•„λ‹Œ 였λ₯˜ 번호λ₯Ό λ°˜ν™˜ν•œλ‹€.

ERRORS

ENOMEM
λ©”λͺ¨λ¦¬ λΆ€μ‘±.

λ”λΆˆμ–΄ threadκ°€ 메인 μŠ€λ ˆλ“œλ₯Ό κ°€λ¦¬ν‚€λŠ” κ²½μš°μ—λŠ” μ—¬λŸ¬ 기반 ν˜ΈμΆœλ“€μ˜ 였λ₯˜ λ•Œλ¬Έμ— pthread_getattr_np()κ°€ μ‹€νŒ¨ν•  수 μžˆλ‹€. κ°€λ Ή fopen(3)이 /proc/self/mapsλ₯Ό μ—΄ 수 μ—†κ±°λ‚˜ getrlimit(2)κ°€ RLIMIT_STACK μžμ› μ œν•œμ„ μ§€μ›ν•˜μ§€ μ•ŠλŠ” κ²½μš°μ΄λ‹€.

VERSIONS

glibc 버전 2.2.3λΆ€ν„° 이 ν•¨μˆ˜κ°€ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.

ATTRIBUTES

이 μ ˆμ—μ„œ μ‚¬μš©ν•˜λŠ” μš©μ–΄λ“€μ— λŒ€ν•œ μ„€λͺ…은 attributes(7)λ₯Ό 보라.

μΈν„°νŽ˜μ΄μŠ€ 속성 κ°’
pthread_getattr_np() μŠ€λ ˆλ“œ μ•ˆμ „μ„± MT-Safe

CONFORMING TO

이 ν•¨μˆ˜λ“€μ€ λΉ„ν‘œμ€€ GNU ν™•μž₯이닀. κ·Έλž˜μ„œ 이름 뒀에 "_np"(nonportable: 이식성 μ—†μŒ)κ°€ λΆ™μ–΄ μžˆλ‹€.

EXAMPLE

μ•„λž˜ ν”„λ‘œκ·Έλž¨μ€ pthread_getattr_np() μ‚¬μš© 방식을 보여 μ€€λ‹€. ν”„λ‘œκ·Έλž¨μ—μ„œ μŠ€λ ˆλ“œλ₯Ό μƒμ„±ν•œ λ‹€μŒ pthread_getattr_np()λ₯Ό μ΄μš©ν•΄ 방호 ꡬ역 크기, μŠ€νƒ μ£Όμ†Œ, μŠ€νƒ 크기 속성을 가져와 ν‘œμ‹œν•œλ‹€. λͺ…λ Ήν–‰ 인자λ₯Ό μ΄μš©ν•΄ μŠ€λ ˆλ“œ 생성 μ‹œ κΈ°λ³Έκ³Ό λ‹€λ₯Έ κ°’μœΌλ‘œ 이 속성듀을 μ„€μ •ν•  수 μžˆλ‹€. μ•„λž˜ μ…Έ μ„Έμ…˜μ€ ν”„λ‘œκ·Έλž¨ μ‚¬μš© 방식을 보여 μ€€λ‹€.

x86-32 μ‹œμŠ€ν…œ μƒμ˜ 첫 번째 μ‹€ν–‰μ—μ„œλŠ” κΈ°λ³Έ μ†μ„±λ“€λ‘œ μŠ€λ ˆλ“œλ₯Ό λ§Œλ“ λ‹€.

$ ulimit -s       # μŠ€νƒ μ œν•œ μ—†μŒ ==> κΈ°λ³Έ μŠ€νƒ 크기 2MB
unlimited
$ ./a.out
Attributes of created thread:
        Guard size          = 4096 bytes
        Stack address       = 0x40196000 (EOS = 0x40397000)
        Stack size          = 0x201000 (2101248) bytes

λ‹€μŒ μ‹€ν–‰μ—μ„œλŠ” 방호 ꡬ역 크기λ₯Ό μ§€μ •ν•˜λ©΄ μ‹œμŠ€ν…œ νŽ˜μ΄μ§€ 크기(x86-32μ—μ„œ 4096λ°”μ΄νŠΈ)의 λ‹€μŒ 번 배수둜 올림 λ˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.

$ ./a.out -g 4097
Thread attributes object after initializations:
        Guard size          = 4097 bytes
        Stack address       = (nil)
        Stack size          = 0x0 (0) bytes

Attributes of created thread:
        Guard size          = 8192 bytes
        Stack address       = 0x40196000 (EOS = 0x40397000)
        Stack size          = 0x201000 (2101248) bytes

λ§ˆμ§€λ§‰ μ‹€ν–‰μ—μ„œλŠ” μŠ€λ ˆλ“œλ₯Ό μœ„ν•œ μŠ€νƒμ„ ν”„λ‘œκ·Έλž¨μ—μ„œ 직접 ν• λ‹Ήν•œλ‹€. 이 경우 방호 ꡬ역 크기 속성이 λ¬΄μ‹œλœλ‹€.

$ ./a.out -g 4096 -s 0x8000 -a
Allocated thread stack at 0x804d000

Thread attributes object after initializations:
        Guard size          = 4096 bytes
        Stack address       = 0x804d000 (EOS = 0x8055000)
        Stack size          = 0x8000 (32768) bytes

Attributes of created thread:
        Guard size          = 0 bytes
        Stack address       = 0x804d000 (EOS = 0x8055000)
        Stack size          = 0x8000 (32768) bytes

ν”„λ‘œκ·Έλž¨ μ†ŒμŠ€

#define _GNU_SOURCE     /* pthread_getattr_np() 선언을 μœ„ν•΄ */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define handle_error_en(en, msg) \
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void
display_stack_related_attributes(pthread_attr_t *attr, char *prefix)
{
    int s;
    size_t stack_size, guard_size;
    void *stack_addr;

    s = pthread_attr_getguardsize(attr, &guard_size);
    if (s != 0)
        handle_error_en(s, "pthread_attr_getguardsize");
    printf(%sGuard size           = %d bytes\n", prefix, guard_size);

    s = pthread_attr_getstack(attr, &stack_addr, &stack_size);
    if (s != 0)
        handle_error_en(s, "pthread_attr_getstack");
    printf("%sStack address       = %p", prefix, stack_addr);
    if (stack_size > 0)
        printf(" (EOS = %p)", (char *) stack_addr + stack_size);
    printf("\n");
    printf("%sStack size          = 0x%x (%d) bytes\n",
            prefix, stack_size, stack_size);
}

static void
display_thread_attributes(pthread_t thread, char *prefix)
{
    int s;
    pthread_attr_t attr;

    s = pthread_getattr_np(thread, &attr);
    if (s != 0)
        handle_error_en(s, "pthread_getattr_np");

    display_stack_related_attributes(&attr, prefix);

    s = pthread_attr_destroy(&attr);
    if (s != 0)
        handle_error_en(s, "pthread_attr_destroy");
}

static void *           /* μƒμ„±ν•œ μŠ€λ ˆλ“œμ˜ μ‹œμž‘ ν•¨μˆ˜ */
thread_start(void *arg)
{
    printf("Attributes of created thread:\n");
    display_thread_attributes(pthread_self(), "\t");

    exit(EXIT_SUCCESS);         /* λͺ¨λ“  μŠ€λ ˆλ“œ μ’…λ£Œ */
}

static void
usage(char *pname, char *msg)
{
    if (msg != NULL)
        fputs(msg, stderr);
    fprintf(stderr, "Usage: %s [-s stack-size [-a]]"
            " [-g guard-size]\n", pname);
    fprintf(stderr, "\t\t-a means program should allocate stack\n");
    exit(EXIT_FAILURE);
}

static pthread_attr_t *   /* λͺ…λ Ήν–‰μ—μ„œ μŠ€λ ˆλ“œ 속성 μ–»κΈ° */
get_thread_attributes_from_cl(int argc, char *argv[],
                              pthread_attr_t *attrp)
{
    int s, opt, allocate_stack;
    long stack_size, guard_size;
    void *stack_addr;
    pthread_attr_t *ret_attr_p = NULL;   /* μŠ€λ ˆλ“œ 속성 객체λ₯Ό μ΄ˆκΈ°ν™”
                                            ν•˜λŠ” 경우 attrp둜 μ„€μ • */
    allocate_stack = 0;
    stack_size = -1;
    guard_size = -1;

    while ((opt = getopt(argc, argv, "ag:s:")) != -1) {
        switch (opt) {
        case 'a':   allocate_stack = 1;                     break;
        case 'g':   guard_size = strtoul(optarg, NULL, 0);  break;
        case 's':   stack_size = strtoul(optarg, NULL, 0);  break;
        default:    usage(argv[0], NULL);
        }
    }

    if (allocate_stack && stack_size == -1)
        usage(argv[0], "Specifying -a without -s makes no sense\n");

    if (argc > optind)
        usage(argv[0], "Extraneous command-line arguments\n");

    if (stack_size >= 0 || guard_size > 0) {
        ret_attrp = attrp;

        s = pthread_attr_init(attrp);
        if (s != 0)
            handle_error_en(s, "pthread_attr_init");
    }

    if (stack_size >= 0) {
        if (!allocate_stack) {
            s = pthread_attr_setstacksize(attrp, stack_size);
            if (s != 0)
                handle_error_en(s, "pthread_attr_setstacksize");
        } else {
            s = posix_memalign(&stack_addr, sysconf(_SC_PAGESIZE),
                               stack_size);
            if (s != 0)
                handle_error_en(s, "posix_memalign");
            printf("Allocated thread stack at %p\n\n", stack_addr);

            s = pthread_attr_setstack(attrp, stack_addr, stack_size);
            if (s != 0)
                handle_error_en(s, "pthread_attr_setstacksize");
        }
    }

    if (guard_size >= 0) {
        s = pthread_attr_setguardsize(attrp, guard_size);
        if (s != 0)
            handle_error_en(s, "pthread_attr_setstacksize");
    }

    return ret_attrp;
}

int
main(int argc, char *argv[])
{
    int s;
    pthread_t thr;
    pthread_attr_t attr;
    pthread_attr_t *attrp = NULL;    /* μŠ€λ ˆλ“œ 속성 객체λ₯Ό μ΄ˆκΈ°ν™”
                                        ν•˜λŠ” 경우 &attr둜 μ„€μ • */

    attrp = get_thread_attributes_from_cl(argc, argv, &attr);

    if (attrp != NULL) {
        printf("Thread attributes object after initializations:\n");
        display_stack_related_attributes(attrp, "\t");
        printf("\n");
    }

    s = pthread_create(&thr, attrp, &thread_start, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_create");

    if (attrp != NULL) {
        s = pthread_attr_destroy(attrp);
        if (s != 0)
            handle_error_en(s, "pthread_attr_destroy");
    }

    pause();    /* λ‹€λ₯Έ μŠ€λ ˆλ“œμ—μ„œ exit() ν˜ΈμΆœν•  λ•Œ μ’…λ£Œ */
}

SEE ALSO

pthread_attr_getaffinity_np(3), pthread_attr_getdetachstate(3), pthread_attr_getguardsize(3), pthread_attr_getinheritsched(3), pthread_attr_getschedparam(3), pthread_attr_getschedpolicy(3), pthread_attr_getscope(3), pthread_attr_getstack(3), pthread_attr_getstackaddr(3), pthread_attr_getstacksize(3), pthread_attr_init(3), pthread_create(3), pthreads(7)


2019-03-06

⚠️ **GitHub.com Fallback** ⚠️