쓰레드 or 시그널

제로의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct mbuf
{
long msgtype;
char mtext[256];
char myname[16];
int seq;
};

int iflag = 0;
int sigid = 0;

void sig_handler(int signo)
{
printf("sig_handler %d : %d\n", pthread_self(),signo);
signal(signo, SIG_IGN);
iflag = 1;
alarm(0);
}

void *threadfunc(void *arg);

/*--------------------------------------------------------------------*/
/* */
/* */
/*--------------------------------------------------------------------*/
int main()
{
int n, i, j;
int msgtype;
key_t key_id;
struct mbuf mybuf;
pthread_t threadid;

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

sigid = threadid;
printf("thread id %d\n", threadid);

key_id = msgget( 77770099, 0666|IPC_CREAT);
while( 1 )
{
printf("msgrcv(%d)\n", i );
signal( SIGALRM, sig_handler );
iflag = 0;
alarm(1);
if( msgrcv( key_id, (void *)&mybuf, sizeof(struct msgbuf), msgtype, 0 ) == -1 ) {
printf("msgrcv error:%d\n", errno );
alarm(0);
if( iflag == 1 ) {
printf("timeout(%d)\n", iflag );
i++;
continue;
}
}
i++;
printf("data ...\n" );
}

exit(0);
}

/*--------------------------------------------------------------------*/
/* */
/* */
/*--------------------------------------------------------------------*/
void *threadfunc(void *arg)
{
int i=0, j;

printf("Thread Start %d\n", pthread_self());

int rc = 0;
struct timeval tout;

tout.tv_sec = 3;
tout.tv_usec = 0;

while(1) {
printf("select(%d)\n", i );
rc = select ( 0x00, 0x00, 0x00, 0x00, &tout);
i++;
}

return ;
}

<< 실행결과 >>
thread id 2
Thread Start 2
msgrcv(0)
select(0)
sig_handler 2 : 14
select(1)
select(2)
select(3)
select(4)
select(5)
select(6)
select(7)
select(8)

1) 처리결과를 보면 sig_handler 2 : 14 스레드식별자가 2이고 msgrcv함수에서 대기하구 있을까요?

댓글 달기

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 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.