pthread_kill(3)
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.
Docbook ¿ø¹®

<a href="/modules/moniwiki/wiki.php/manSearch?google=none&name=pthread_kill">pthread_kill</a>(3)

1장. pthread_kill(3)

¾²·¹µå ½Ã±×³Î °ü·Ã ÇÔ¼öµé


1.1. »ç¿ë¹ý

#include <pthread.h>
#include <signal.h>

int pthread_kill(pthread_t thread, int signo);
int  pthread_sigmask(int  how,  const  sigset_t  *newmask,
               sigset_t *oldmask);
int sigwait(const sigset_t *set, int *sig);
		


1.2. ¼³¸í

ÀÌ ÇÔ¼ö´Â ÇöÀç ¾²·¹µå¿¡ ½Ã±×³Înewmask¿Í how ¸¦ ÀÌ¿ëÇØ¼­ ½Ã±×³Î ¸¶½ºÅ©¸¦ ¸¸µç´Ù. how´Â SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASKÁß Çϳª¸¦ ¼±ÅÃÇÒ ¼ö ÀÖ´Ù. SIG_BLOCK´Â ÇöÀç ¼³Á¤µÈ ½Ã±×³Î ¸¶½ºÅ©¿¡ newmask¸¦ Ãß°¡Çϸç SIG_UNBLOCK´Â ÇöÀç ¼³Á¤µÈ ½Ã±×³Î ¸¶½ºÅ©¿¡¼­ newmask¸¦ Á¦°ÅÇϰí SIG_SETMASK´Â newmask·Î ÇöÀç ½Ã±×³Î ¸¶½ºÅ©¸¦ ¼³Á¤ÇÑ´Ù

pthread_kill()Àº ½Ã±×³Î¹øÈ£signo¸¦ °¡Áö´Â ½Ã±×³ÎÀ» ½º·¹µå ½Äº°¹øÈ£thread¸¦ °¡Áö´Â ¾²·¹µå¿¡ Àü´ÞÇÑ´Ù.

sigwait()´Â ½Ã±×³Î ¼Âset¿¡ ÁöÁ¤µÈ ½Ã±×³ÎÀÌ Àü´ÞµÉ ¶§±îÁö ÇØ´ç¿µ¿ª¿¡¼­ ´ë±âÇÑ´Ù. ¸¸¾à ½Ã±×³ÎÀ» ¹Þ°Ô µÇ¸é ¹ÞÀº ½Ã±×³Î ¹øÈ£¸¦ sig¿¡ º¹»çÇÏ°í ¸®ÅÏÇÑ´Ù. set¿¡ ¸í½ÃµÈ ½Ã±×³ÎµéÀº ¹Ýµå½Ã ºÀ¼â·Î ÁöÁ¤µÇ¾î¾ß ÇÑ´Ù. ±×·¡¾ß¸¸ sigwait()¿¡ ÀÇÇØ¼­ °ËÃâµÇ°í ¸®ÅϵȴÙ. ¸¸¾à ÇØ´ç ½Ã±×³Î¿¡ ´ëÇØ¼­ ½Ã±×³Î Çڵ鷯°¡ ÁöÁ¤µÇ¾î ÀÖ´Ù¸é ÇÔ¼ö´Â È£ÃâµÇÁö ¾Ê°Ô µÈ´Ù.


1.3. ¹Ýȯ°ª

¼º°øÇϸé 0À», ½ÇÆÐÇϸé 0ÀÌ ¾Æ´Ñ °ªÀ» ¸®ÅÏÇÑ´Ù.


1.4. ¿¡·¯

ESRCH

th½Äº°ÀÚ¸¦ °¡Áø ¾²·¹µå°¡ Á¸ÀçÇÏÁö ¾Ê´Â´Ù.

EINVAL

th½Äº°ÀÚ¸¦ °¡Áø ¾²·¹µå°¡ ÀÌ¹Ì detach»óÅ¿¡ ÀÖ´Ù.


1.5. ¿¹Á¦

#include <pthread.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

/*
 * ½Ã±×³Î Çڵ鷯
 * Çڵ鷯°¡ È£ÃâµÈ ¾²·¹µåÀÇ ID¿Í ½Ã±×³Î ¹øÈ£¸¦ Ãâ·ÂÇÑ´Ù.
 */
void sig_handler(int signo)
{
    printf("SIGNAL RECV TH ID %d : %d\n", pthread_self(),signo);
}

void *threadfunc2(void *arg);
void *threadfunc(void *arg);
void *s_signal(void *arg);

// ¾²·¹µå ID¸¦ ÀúÀåÇÑ´Ù.  
int sigid[2];

int main()
{
    int n, i, j;
    pthread_t threadid;

    // ¿øÇÏ´Â ¾²·¹µå·Î ½Ã±×³ÎÀÌ Àü´ÞÇÏ´ÂÁö È®ÀÎÇϱâ À§Çؼ­
    // ¾²·¹µå ID¸¦ È®ÀÎÇÑ´Ù.
    if ((n = pthread_create(&threadid, NULL, threadfunc2, NULL)) != 0 )
    {
        perror("Thread create error ");
        exit(0);
    }
    sigid[0] = threadid;
    printf("thread2 id %d\n", threadid);

    if ((n = pthread_create(&threadid, NULL, threadfunc, NULL)) != 0 )
    {
        perror("Thread create error ");
        exit(0);
    }
    sigid[1] = threadid;
    printf("thread id %d\n", threadid);

    if ((n = pthread_create(&threadid, NULL, s_signal, NULL)) != 0 )
    {
        perror("Thread create error ");
        exit(0);
    }

    pthread_join(threadid, NULL);
}

void *threadfunc(void *arg)
{
    int i=0, j;
    struct sigaction act;
    sigset_t newmask;

    // °á°úÀÇ È®ÀÎÀ» À§Çؼ­ ¾²·¹µå ID¸¦ Ãâ·ÂÇÑ´Ù.
    printf("SIGINT Thread Start (%d)\n", pthread_self());

    // SIGINT¿¡ ´ëÇØ¼­ ½Ã±×³Î Çڵ鷯¸¦ µî·ÏÇßÁö¸¸ 
    // SIGINT¸¦ ºí·°Çϵµ·Ï ÇßÀ¸¹Ç·Î ½Ã±×³ÎÀº Àü´ÞµÇÁö ¾Ê´Â´Ù.  
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGINT);
    act.sa_handler = sig_handler;
    sigaction(SIGINT, &act, NULL);
    pthread_sigmask(SIG_BLOCK, &newmask, NULL);	

    while(1)
    {
        printf("%d\n", i);
        i++;
        sleep(1);
    }
    return ;
}

void *threadfunc2(void *arg)
{
    struct sigaction act;
	int lsigno;

    sigset_t newmask;
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGINT);

	// ½Ã±×³Î Çڵ鷯¸¦ µî·Ï ½ÃÄ×Áö¸¸ 
	// sigwiat()°¡ °¡µð¸®°í ÀÖÀ¸¹Ç·Î ½ÇÇàµÇÁö ¾Ê´Â´Ù.  
    act.sa_handler = sig_handler;
    sigaction(SIGINT, &act, NULL);

    while(1)
    {
		sigwait(&newmask, &lsigno);
		printf("i receive signo %d\n", lsigno);
        sleep(1);
    }
}

/*
 * SIGINT¸¦ µÎ°³ÀÇ ¾²·¹µå·Î ¼­·Î´Ù¸¥ ½Ã°£°£°ÝÀ¸·Î 
 * Àü´ÞÇÑ´Ù. 
 */
void *s_signal(void *arg)
{
    int i = 1;
    while(1)
    {
        sleep(1);
        i++;
        if((i % 7) == 0)
        {
            printf("Send SIGINT %d\n", sigid[0]);
            pthread_kill(sigid[0], SIGINT);
        }
        if((i % 11) == 0)
        {
            printf("Send SIGINT %d\n", sigid[1]);
            pthread_kill(sigid[1], SIGINT);
        }
    }
}
		

EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.