#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main ()
{
pid_t pid;
printf("fork!/n");
pid=fork();
if (pid < 0)
printf("error in fork!/n");
else if (pid == 0)
printf("i am the child process, my process id is %d/n",getpid());
else
printf("i am the parent process, my process id is %d/n",getpid());
return 0;
}
結果:
fork!
i am the child process, my process id is 4286
i am the parent process, my process id is 4285
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main ()
{
pid_t pid;
printf("fork!");
pid=fork();
if (pid < 0)
printf("error in fork!/n");
else if (pid == 0)
printf("i am the child process, my process id is %d/n",getpid());
else
printf("i am the parent process, my process id is %d/n",getpid());
return 0;
}
結果:
fork!i am the child process, my process id is 4286
fork!i am the parent process, my process id is 4285