fork + thread
글쓴이: sunbee / 작성시간: 목, 2003/05/15 - 9:50오전
#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h> void* do_loop(void *data) { while(1) { ; } } int main() { pthread_t p_thread; int i = 0, j = 0, k; pid_t pid1, pid2; signal(SIGCHLD, SIG_IGN); for(i = 0; i < 2; i++) // item { pid1 = fork(); //sleep(2); printf("[%d]th fork1 create !! \n", i); if(pid1 == 0) { for(j = 0; j < 3; j++) // room { pid2 = fork(); //sleep(2); printf("[%d]th fork2 create !! \n", j); if(pid2 == 0) { for(k = 0; k < 10; k++) { if(pthread_create(&p_thread, NULL, do_loop, NULL) != 0) { printf("Thread creation failed [%d] \n", k); exit(1); } printf("thread count : [%d]\n", k); } } else if (pid2 == -1) { perror("room fork error : "); } } } else if (pid1 == -1) { perror("item fork error : "); } } printf("programing is end\n"); return 0; }
위에서 sleep 하고 실행한 후 프로그램 종료 후
ps -ef |grep test 해 보면
Quote:
test 21 1 1 00:43:07 pts/0 0:02 ./test_thread2
test 19 14 1 00:43:07 pts/0 0:02 ./test_thread2
test 23 16 1 00:43:07 pts/0 0:02 ./test_thread2
test 29996 29993 2 00:42:59 pts/0 0:06 ./test_thread2
test 29994 1 3 00:42:59 pts/0 0:06 ./test_thread2
test 17 1 1 00:43:07 pts/0 0:02 ./test_thread2
test 13 1 1 00:43:05 pts/0 0:02 ./test_thread2
test 20 1 1 00:43:07 pts/0 0:02 ./test_thread2
test 18 13 1 00:43:07 pts/0 0:02 ./test_thread2
test 15 1 1 00:43:05 pts/0 0:02 ./test_thread2
test 29420 29418 0 07:45:10 pts/3 0:00 -ksh
test 28966 28964 0 06:48:39 pts/0 0:01 -ksh
test 29999 29995 2 00:43:01 pts/0 0:05 ./test_thread2
test 29997 1 2 00:43:01 pts/0 0:05 ./test_thread2
test 29998 29994 2 00:43:01 pts/0 0:04 ./test_thread2
test 28992 28990 0 06:52:23 pts/2 0:01 -ksh
test 29995 1 2 00:42:59 pts/0 0:06 ./test_thread2
test 22 15 1 00:43:07 pts/0 0:01 ./test_thread2
test 24 1 1 00:43:07 pts/0 0:02 ./test_thread2
test 5 29996 2 00:43:01 pts/0 0:04 ./test_thread2
test 16 1 1 00:43:05 pts/0 0:02 ./test_thread2
test 4 1 2 00:43:01 pts/0 0:04 ./test_thread2
test 40 28992 0 00:43:18 pts/2 0:00 grep test
test 14 1 1 00:43:05 pts/0 0:03 ./test_thread2
test 29993 1 3 00:42:57 pts/0 0:09 ./test_thread2
이렇게 프로세스들이 떠 있습니다.
sleep 구문을 빼면 정상 종료 하고요.. 왜 이런겁니까?
그리고.. 위의 프로그램에 대해서 고수님의 의견을 듣고 싶습니다.
저렇게 짜면 안된다.. 왜냐하면 ... 등등..
부탁드립니다^^
Forums:
댓글 달기