相关疑难解决方法(0)

fork()如何工作?

我真的很想分叉,这个代码中的pid是做什么的?有人可以解释一下X行和Y行的内容吗?

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = {0,1,2,3,4};
int main()
{
    int i;
    pid_t pid;
    pid = fork();
    if (pid == 0) {
        for (i = 0; i < SIZE; i++) {
            nums[i] *= -i;
            printf("CHILD: %d ",nums[i]); /* LINE X */
        }
    }
    else if (pid > 0) {
        wait(NULL);
        for (i = 0; i < SIZE; i++)
            printf("PARENT: %d ",nums[i]); /* LINE Y */
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c fork

12
推荐指数
3
解决办法
3万
查看次数

标签 统计

c ×1

fork ×1