进程的产生
fork()
调度器的策略决定哪个进程先运行
父进程及子进程的区别:fork返回值不同,pid不同,ppid不同,
fork之前要先 fflush(NULL)
进程的消亡及释放资源
wait()
waitpid()
exit() 刷新缓冲区
_exit() 不刷新缓冲区
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define LEFT 300000
#define RIGHT 300200
#define N 5
int main(int argc, char *argv[])
{
pid_t pid;
int status = -1;
int i = 0, j = 0, k = 0, flag = 0;
for (k = 0; k < N; k++)
{
fflush(NULL);
pid = fork();
if (pid < 0)
{
perror ("fork()");
exit (1);
}
else if (pid == 0)
{
for (i = LEFT + k; i < RIGHT + 1; i += N)
{
flag = 0;
for (j = 2; j < i / 2; j++)
{
if (i % j == 0)
{
flag = 1;
}
}
if (flag == 0)
{
printf ("[%d] process %d is primer\n", i, k);
}
}
exit (0);
}
}
for (k = 0; k < N; k++)
wait(NULL);
exit(0);
}