SIGCHLD 를 이용해서 child 종료후 출력 하려 하는데 무한루프에 빠져 버립니다.
글쓴이: oksk1111 / 작성시간: 월, 2009/05/11 - 7:33오후
안녕하세요.
리눅스 프로그래밍을 시작한 초보 프로그래머 입니다.
눈팅으로 정보를 이곳 저곳에서 찾다가
도저히 해답을 못 찾아서 글을 올려 봅니다.
지금 현재 하려는 것은
자식 프로세스를 생성해서 종료가 될 경우 이를 SIGCHLD 로 감지한후
그 종료 메시지를 출력 하려 하는 것입니다.
그런데 문제는 종료된 후 출력하는 것 까진 좋은데
그 후엔 무한 루프에 빠져 버립니다. ( 무한 fork() 라고 하는게 더 좋을것 같군요.)
코드는 다음과 같습니다.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <signal.h>
void SigHandler(int);
int main()
{
pid_t pid;
char linestring[1000];
char command_string[1000];
char* strptr;
int i=0;
char* strarg[100];
struct sigaction act; // 시그널 액션 구조체
act.sa_handler = SigHandler;
sigfillset(&(act.sa_mask));
sigaction(SIGCHLD, &act, NULL);
while(1) {
i=0;
printf("basicshell>");
gets(linestring);
strptr=(char*)strtok(linestring," \n");
if (strptr==NULL) continue;
/* fork a child process */
pid = fork();
if (pid == 0) { /* child process */
strcpy(command_string,strptr);
while (strptr!=NULL) {
strarg[i++]=strdup(strptr);
strptr=(char*)strtok(NULL," \n");
}
strarg[i]=NULL;
execvp(command_string,strarg);
} else { /* parent process */
/* parent will wait for the child to complete */
//waitpid(-1, NULL, WNOHANG);
}
}
}
void SigHandler( int signo )
{
pid_t pid;
int status;
while (wait(0) > 0 );
printf("종료된 pid = %d\n", pid );
}어느 부분이 잘못된 것인지, 고수님들 ㅈㅅ함을 무릅쓰고 부탁 드립니다 ㅜ,.ㅜ
Forums:


linestring 초기화..
먼저 linestring을 초기화 해 주시고..
그리고 child thread를 바로 종료 시켜 주는게 좋을듯 하네요..
: strarg[i]=NULL; execvp(command_string,strarg); return 0; //추가.. :[유유자적 바람따라 흘러가고 싶다]
[유유자적 바람따라 흘러가고 싶다]
댓글 달기