수색…
통사론
- pid_t getpid (void);
- pid_t getppid (void);
- pid_t fork (void);
- pid_t waitpid (pid_t pid, int * wstatus, int 옵션);
- int execv (const char * path, char * const argv []);
매개 변수
| 함수, 매개 변수, 반환 값 | 기술 | 
|---|---|
| fork() | 함수 이름 | 
| 없음 | 해당 없음 | 
| PID, 0 또는 -1을 반환합니다. | 호출 프로세스는 새로 작성한 프로세스의 PID를 수신하거나 실패하면 -1을 수신합니다. 자식 (새로 생성 된 프로세스)은 0을 받는다. 실패 할 경우 errno를EAGAIN또는ENOMEM | 
| - | - | 
| execv() | 함수 이름 | 
| const char *path | 실행 파일의 이름을 포함하는 문자열 (경로가있을 수 있음) | 
| char *const argv[] | 인수로서의 문자열 포인터 배열 | 
| 실패하면 -1을 반환합니다. | 성공시이 함수는 반환되지 않습니다. | 
| - | - | 
자식 프로세스를 생성하고 종료 할 때까지 기다린다.
 이 프로그램은 fork() 사용하여 다른 프로세스를 실행하고 waitpid() 사용하여 종료를 기다리는 방법을 보여줍니다. 
- fork()는 현재 프로세스의 동일한 복사본을 만듭니다. 원래 프로세스는 상위 프로세스이고 새로 작성된 프로세스는 하위 프로세스입니다. 두 프로세스는- fork()후에 정확히 계속됩니다.
- waitpid()자식 프로세스가 종료되거나 종료 될 때까지 차단하고 종료 코드와 종료 이유를 반환합니다.
#include <unistd.h>     /* for fork(), getpid() */
#include <sys/types.h>  /* for waitpid() */
#include <sys/wait.h>   /* for waitpid() */
#include <stdlib.h>     /* for exit() */
#include <stdio.h>      /* for printf(), perror() */
int
main(int argc, char *argv[])
{
    /* Create child process.
     *
     * On success, fork() returns the process ID of the child (> 0) to the
     * parent and 0 to the child. On error, -1 is returned.
     */
    pid_t child_pid = fork();
    if (child_pid < 0) {
        perror("fork() failed");
        exit(EXIT_FAILURE);
    } else if (child_pid == 0) {
        /* Print message from child process.
         *
         * getpid() returns the PID (process identifier) of current process,
         * which is typically int but doesn't have to be, so we cast it.
         *
         * getppid() returns the PID of the parent process.
         */
        printf("from child: pid=%d, parent_pid=%d\n",
                 (int)getpid(), (int)getppid());
        /* We can do something here, e.g. load another program using exec().
         */
        exit(33);
    } else if (child_pid > 0) {
        /* Print message from parent process.
         */
        printf("from parent: pid=%d child_pid=%d\n",
                  (int)getpid(), (int)child_pid); 
        
        /* Wait until child process exits or terminates.
         *
         * The return value of waitpid() is PID of the child process, while
         * its argument is filled with exit code and termination reason.
         */
        int status;
        pid_t waited_pid = waitpid(child_pid, &status, 0);
        if (waited_pid < 0) {
            perror("waitpid() failed");
            exit(EXIT_FAILURE);
        } else if (waited_pid == child_pid) {
            if (WIFEXITED(status)) {
                /* WIFEXITED(status) returns true if the child has terminated 
                 * normally. In this case WEXITSTATUS(status) returns child's
                 * exit code.
                 */
                printf("from parent: child exited with code %d\n",
                          WEXITSTATUS(status));
            }
        }
    }
    exit(EXIT_SUCCESS);
}
예제 출력 :
from parent: pid=2486 child_pid=2487
from child: pid=2487, parent_pid=2486
from parent: child exited with code 33
원래 M.Geiger가 만든 여기 에서 복사했습니다.
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow