비동기 수신 (ASync read)

Programming/Network 2007/05/08 19:23 장인정신
/* br / nbsp; Copyright (C) Information Equipment co.,LTD br / nbsp; All rights reserved. br / nbsp; Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt; br / nbsp; CVSTAG=$Header$ br / */ br / br / #include lt;sys/param.hgt; br / #include lt;stdio.hgt; br / #include lt;sys/types.hgt; br / #include lt;unistd.hgt; br / #include lt;signal.hgt; br / br / #ifndef __USE_GNU br / #define __USE_GNU __USE_GNU br / #endif br / #include lt;fcntl.hgt; br / br / void DoSignalIO(int s_Signal) br / { fprintf(stdout, %s : s_Signal = %d\n, __FUNCTION__, s_Signal); } br / br / int main(void) br / { br / int s_Handle; br / s_Handle = open(/dev/stdin, O_RDONLY); br / s_Handle = s_Handle == (-1) ? 0 : s_Handle; br / if(s_Handle != (-1)) br / { br / nbsp; struct siginfo s_SignalInfo; br / nbsp; sigset_t s_SignalSet; br / nbsp; struct sigaction s_SignalAction; br / nbsp; int s_Check, s_ReadBytes, s_Index, s_IsContinue, s_Prompt, s_CursorIndex; br / nbsp; unsigned char s_Buffer[ 32 lt;lt; 10 ]; br / nbsp; char s_Cursor[] = {'-', '/', '|', '\\'}; br / br / nbsp; /* Handler 준비 */ br / nbsp; sigemptyset(amp;s_SignalSet); br / nbsp; sigaddset(amp;s_SignalSet, SIGRTMIN); br / nbsp; sigprocmask(SIG_BLOCK, amp;s_SignalSet, (sigset_t *)0); br / br / nbsp; sigemptyset(amp;s_SignalAction.sa_mask); br / nbsp; s_SignalAction.sa_flags = SA_SIGINFO; br / nbsp; s_SignalAction.sa_restorer = NULL; br / nbsp; s_SignalAction.sa_handler = DoSignalIO; br / br / nbsp; /* ASync fd 설정 */ br / nbsp; fcntl(s_Handle, F_SETFL, O_RDONLY | O_ASYNC | O_NONBLOCK); br / nbsp; fcntl(s_Handle, F_SETSIG, SIGRTMIN); br / nbsp; fcntl(s_Handle, F_SETOWN, getpid()); br / br / nbsp; s_IsContinue = 1; br / nbsp; if(sigaction(SIGRTMIN, amp;s_SignalAction, 0) != (-1)) br / nbsp; { /* Handler 등록 */ br / nbsp; nbsp;s_Prompt = 1; br / nbsp; nbsp;s_CursorIndex = 0; br / nbsp; nbsp;do br / nbsp; nbsp;{ br / nbsp; nbsp; if(s_Prompt == 1) br / nbsp; nbsp; { /* 프롬프트 */ br / nbsp; nbsp; nbsp;fprintf(stdout, \rInput RTS message gt;gt;gt; ); fflush(stdout); br / nbsp; nbsp; nbsp;s_Prompt = 0; br / nbsp; nbsp; } br / nbsp; nbsp; else br / nbsp; nbsp; { /* 심오한 커서 */ br / nbsp; nbsp; nbsp;fprintf(stdout, %c\b\b, s_Cursor[((s_CursorIndex++) / 10) % sizeof(s_Cursor)]); fflush(stdout); br / nbsp; nbsp; nbsp;usleep((1000 / HZ) * 1000); /* CPU 부하 균형 - x86계열엣서는 10ms */ br / nbsp; nbsp; } br / nbsp; nbsp; s_Check = sigwaitinfo(amp;s_SignalSet, amp;s_SignalInfo); br / nbsp; nbsp; if(s_Check == SIGRTMIN) br / nbsp; nbsp; { /* 입력한게 있다고 엽구리 찌르네 */ br / nbsp; nbsp; nbsp;if(s_SignalInfo.si_fd == s_Handle amp;amp;( (s_SignalInfo.si_code == POLL_IN) || (s_SignalInfo.si_code == SI_QUEUE) )) br / nbsp; nbsp; nbsp;{ /* 진짜 입력한거 맞어? */ br / nbsp; nbsp; nbsp; s_ReadBytes = read(s_Handle, amp;s_Buffer[0], sizeof(s_Buffer) - 1); br / nbsp; nbsp; nbsp; if(s_ReadBytes gt; 0) br / nbsp; nbsp; nbsp; { /* 뭔가 있어 */ br / nbsp; nbsp; nbsp; nbsp;for(s_Index = 0;s_Index lt; s_ReadBytes;s_Index++)fprintf(stdout, [0x%02x], s_Buffer[s_Index]); br / nbsp; nbsp; nbsp; nbsp;fprintf(stdout, \n); br / nbsp; nbsp; nbsp; nbsp;s_Prompt = 1; br / nbsp; nbsp; nbsp; } br / nbsp; nbsp; nbsp;} br / nbsp; nbsp; } br / nbsp; nbsp; else fprintf(stdout, Check = %d\n, s_Check); br / nbsp; nbsp;}while(s_IsContinue == 1); br / nbsp; } br / nbsp; if(s_Handle gt; 2)close(s_Handle); br / } br / return(1); br / } br / br / /* vim: set expandtab: */ br / /* End of source */
크리에이티브 커먼즈 라이센스
Creative Commons License
2007/05/08 19:23 2007/05/08 19:23
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://blog.minzkn.com/rss/response/91

댓글+트랙백 ATOM :: http://blog.minzkn.com/atom/response/91

트랙백 주소 :: http://blog.minzkn.com/trackback/91

트랙백 RSS :: http://blog.minzkn.com/rss/trackback/91

트랙백 ATOM :: http://blog.minzkn.com/atom/trackback/91

댓글을 달아 주세요

댓글 RSS 주소 : http://blog.minzkn.com/rss/comment/91
댓글 ATOM 주소 : http://blog.minzkn.com/atom/comment/91