qfedu-linux-advanced-level/day2/homework/h3.c

24 lines
541 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 编写一个程序创建一个子进程并在子进程中打印出自己的进程IDPID在父进程中打印出子进程的PID。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid < 0)
{
printf("fork error\n");
return -1;
}
if (pid == 0)
{
printf("子进程打印子进程ID: %d\n", getpid());
}
else
{
printf("父进程打印子进程ID: %d\n", pid);
}
return 0;
}