쓰레드 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함수에서 대기하구 있을까요?
댓글 달기