linux-kernel
포크 시스템 호출
수색…
fork () 시스템 호출
fork()
는 시스템 호출입니다. fork는 부모 프로세스 ( fork()
를 실행 한 프로세스)의 복제본 인 실행중인 프로세스에서 하위 프로세스를 만드는 데 사용됩니다. 하위 프로세스는 상위 프로세스에서 파생됩니다. 부모와 자식은 서로 다른 주소 공간을 가지며, 각각은 변수에 대한 변경과는 독립적입니다.
하위 프로세스는 자체 PID (프로세스 ID)를가집니다. 하위 프로세스의 PPID (상위 프로세스 ID)는 상위 프로세스의 PID와 동일합니다.
체재:
헤더 파일 :
#include <unistd.h>
함수 선언 :pid_t fork(void);
fork ()에는 입력 인수가 필요하지 않습니다.
자식 프로세스가 성공적으로 생성되면 자식 프로세스의 PID는 부모 프로세스에 반환되고 0은 자식 프로세스에 반환됩니다. 실패시 프로세스가 생성되지 않은 상태에서 -1
을 반환합니다.
사용 예 :
#include <stdio.h>
#include <unistd.h>
void child_process();
void parent_process();
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
child_process();
else
parent_process();
return 0;
}
/*getpid() will return the Pid of the
current process executing the function */
void child_process()
{
printf("Child process with PID : %d and PPID : %d ", getpid(),getppid());
}
void parent_process()
{
printf("Parent process with PID : %d", getpid());
}
자식과 부모로부터의 printf
명령문의 순서는 시스템에 순전히 의존하는 스케줄링 메커니즘에 의존한다.
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow