
fork() 라는 시스템 콜에 의해 생성된다. fork() 시스템 콜 이후의 명령어를 계속해서 실행한다.fork() 명령어의 반환 코드가 0이면 자식 프로세스이고, OS가 부여한 0이 아닌 pid를 반환하면 부모 프로세스이다. #include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
printf("Hello, Process!\n");
return 0;
}
Hello, Process!
Hello, Process!
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
printf("Hello, Process! %d\n", pid);
return 0;
}
Hello, Process! 1004
Hello, Process! 0
부모 프로세스가 자식 프로세스를 생성한 후 할 수 있는 일은
1️⃣ 자기 프로세스 실행을 계속 하거나
2️⃣ wait() system call을 호출하고, 자기 자신을 ready queue에서 빼내어 wait queue에 넣은 후, 자식 프로세스의 실행 종료를 기다린다.
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
int main()
{
pid_t pid;
pid = fork();
if (pid > 0)
{
wait(NULL);
}
printf("Hello, Process! %d\n", pid);
return 0;
}
Hello, Process! 0
Hello, Process! 16330