쓰레드로 알람 여러개 생성..

stypr의 이미지

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


int check1_print()
{
        printf("\r\nPthread num 1\r\n");
        alarm(5);
}

int check2_print()
{
        printf("\r\nPthread num 2\r\n");
        alarm(5);
}

int check3_print()
{
        printf("\r\nPthread num 3\r\n");
        alarm(5);
}

void check1()
{
        signal(SIGALRM, (void *)check1_print);
        alarm(5);
        while(1);
}

void check2()
{
        signal(SIGALRM, (void *)check2_print);
        alarm(5);
        while(1);
}

void check3()
{
        signal(SIGALRM, (void *)check3_print);
        alarm(5);
        while(1);
}
int main()
{
        pthread_t pth1, pth2, pth3;

        pthread_create(&pth1, NULL, (void *)&check1, NULL);
        pthread_create(&pth2, NULL, (void *)&check2, NULL);
        pthread_create(&pth3, NULL, (void *)&check3, NULL);

        pthread_join(pth1, NULL);
        pthread_join(pth2, NULL);
        pthread_join(pth3, NULL);
}

이런식으로 코딩하게 되면 1,2 알람이 전부 무시되고 .. 3번째 쓰레드에서의
알람만 동작하게 되잖아요..

3개의 쓰레드에서 동시에 다 알람 사용을 할 수 있게 할 수는 없는건가요?

윈도우에서 보면 타이머 이름 정해서 각각 지정가능한데.. 리누기는 어떤지..

답변 부탁드립니다. 행복한 하루 되세욥

MackTheKnife의 이미지

signal대신 sigaction을 쓰면 되는걸로 알고있읍니다.

honestee의 이미지

어떤 의도의 프로그램인지 잘 모르겠지만요, signal은 thread 별로 동작하지 않습니다. 한 쓰레드가 signal을 발생시킨다고 해도 그 signal을 반드시 자기 쓰레드가 받는다는 보장이 없습니다. (solaris의 경우, 보통 다른 쓰레드를 생성한 쓰레드(부모 쓰레드?)가 받죠. 리눅스는 아무나 받는거 같습니다. 옛날에 테스트해본거라 다를 수도 잇음) signal이든, sigaction이든, signal은 보통 global하게 동작한다고 생각하고 짜는게 좋습니다. 아니면 각 signal 번호마다 이걸 받는 thread를 mapping해서 나머지들은 block하게 만들어야하죠.(그러나 이것도 제한이 많습니다.)

사견으로는 쓰레드랑 signal이랑은 무척이나 궁합이 안맞는 거 같아서 섞어서 프로그래밍하면 대략 코드가 구질구질해지는 부분이 많아지는 거 같습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.