Multi-Process Testing System

0

Source Code

https://github.com/alexcho617/OShw2#readme

프로그래밍 문제에는 응시자가 제출한 코드와 출제자의 코드가있다.
어떠한 문제이든 출제자의 의도 안에서 테스트 케이스들을 전부 일치시키는것이 응시자의 목적이다.

fork()는 하나의 자식 프로세스 인스탄스를 만들어 프로세스 이미지를 현재 부모 프로세스와 교체하고 동시에 CPU는 왔다갔다 하면서 처리한다.

이미지 출처: https://linuxhint.com/fork_linux_system_call_c/

    pid_t compiler_child;
    compiler_child = fork();
    //gcc compiler
    if(compiler_child == 0) 
    {
        //printf("gcc compiler \n");
        execl("/usr/bin/gcc", "gcc", "compiler.c", "-o", "compiler", (char*) NULL);
    }

위에 예제는 pctest의 compiler.c 를 gcc로 compile하는 과정인데 fork()와 execl()를 연계하여 흔히 사용하는 예시이다. 여기서 주의해야 할 점은 execl()은 해당 프로세스를 terminate 시킨다는 것이다.

        pid_t term_compiler_child;
        int exit_code_compiler_child;
        term_compiler_child = wait(&exit_code_compiler_child); //grand_target process가 끝날 때 까지 기다림
        
        execl("./compiler", "compiler", target_file, solution_file, (char*) NULL);

wait()은 차일드 프로세스가 끝나기 까지 기다린다. 위의 예제에선 compiler.c의 컴파일링이 완전이 끝날때까지 기다렸다가 execl() 로 실행 시킨다.

1개의 댓글

comment-user-thumbnail
2021년 6월 2일

좋은 글 감사합니다.

답글 달기