fork 테스트

programming/C_C++ 2010. 8. 25. 11:21
반응형



프로그램 샘플:fork_test.c
--------------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int nMainPid;
    int nChildPid;

    nMainPid  = getpid();

    nChildPid = fork();

    if(nChildPid == -1) {
        perror("fork error ");
        exit(0);
    }
    else if(nChildPid == 0) {
        printf("자식프로세서 nMainPid[%6d] nChildPid[%6d] nGetPid[%6d]\n", nMainPid, nChildPid, getpid());
        pause();
    }
    else{
        printf("부모프로세서 nMainPid[%6d] nChildPid[%6d] nGetPid[%6d]\n", nMainPid, nChildPid, getpid());
        pause();
    }
}
--------------------------------------------------------------------------------

프로그램 샘플 컴파일
--------------------------------------------------------------------------------
# gcc fork_test.c -o fork_test
--------------------------------------------------------------------------------

프로그램 샘플 실행
--------------------------------------------------------------------------------
# ./fork_test
부모프로세서 nMainPid[ 27412] nChildPid[ 27413] nGetPid[ 27412]
자식프로세서 nMainPid[ 27412] nChildPid[     0] nGetPid[ 27413]
--------------------------------------------------------------------------------

실행 프로세서 확인
--------------------------------------------------------------------------------
# ps -ef |grep fork_test |grep -v grep
    UID PID    PPID   C  STIME TTY       TIME  CMD
    test 27412 16186   0 11:17:17 pts/3       0:00 ./fork_test
    test 27413 27412   0 11:17:17 pts/3       0:00 ./fork_test
--------------------------------------------------------------------------------


반응형
Posted by 공간사랑
,