/* -----------------------------------------------------------------------------------------------------------
ÆÄÀϸí : fork_test.c
±â ´É : fork() ½Ã½ºÅÛ ÄÝ »ç¿ë ¿¬½À
ÄÄÆÄÀÏ : cc -o fork_test fork_test.c
½ÇÇ࿹ : fork_test
--------------------------------------------------------------------------------------------------------------*/
#include <sys/types.h>
#include <unistd.h>
int global_var = 0; /* Àü¿ª º¯¼ö ¼±¾ð */
int main(void) {
pid_t pid;
int local_var = 0; /* Áö¿ª º¯¼ö ¼±¾ð */
if((pid = fork()) < 0) {
printf("fork error\n");
return -1;
/* ÀÚ½Ä ÇÁ·Î¼¼½º */
} else if (pid == 0) {
global_var++;
local_var++;
printf("CHILD - my pid is %d and parent's pid is %d\n", getpid(), getppid());
} else {
/* ºÎ¸ð ÇÁ·Î¼¼½º */
sleep(2); /* 2ÃÊ ½®´Ù */
global_var += 5;
local_var += 5;
printf("PARENT - my pid is %d, child's pid is %d\n", getpid(), pid);
}
printf("\t global var : %d\n", global_var);
printf("\t local var : %d\n", local_var);
}
|