0
所以我需要迭代fork()幾次,創建子進程。例如,兒童進程應該「做得很少或沒有處理」;fork()ing - Idle Children
while(1)
sleep(1)
父母然後應該收集兒童的PID並殺死他們(嚴酷的,我知道!)。
然而,他們的方式我在一分鐘內執行了幾次「父」塊中的代碼,但我只需要它執行一次。
所以我需要迭代fork()幾次,創建子進程。例如,兒童進程應該「做得很少或沒有處理」;fork()ing - Idle Children
while(1)
sleep(1)
父母然後應該收集兒童的PID並殺死他們(嚴酷的,我知道!)。
然而,他們的方式我在一分鐘內執行了幾次「父」塊中的代碼,但我只需要它執行一次。
這裏是一個例子;您需要將這些pid存儲在一個表格中(這裏是p [])。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#define NSUB 10
int main()
{
int i, n = NSUB, p[NSUB], q;
for (i = 0; i < n; i++) {
printf ("Creating subprocess %d ...\n", i);
p[i] = fork();
if (p[i] < 0) { perror ("fork"); exit (1); }
if (p[i] == 0) { /* subprocess */
printf ("Subprocess %d : PID %d\n", i, (int) getpid());
while (1) pause();
exit (0);
}
}
sleep(2);
for (i = 0; i < n; i++) {
printf ("Killing subprocess %d ...\n", i);
if (kill (p[i], SIGTERM) < 0) perror ("kill");
}
for (i = 0; i < n; i++) {
printf ("waiting for a subprocess ...\n");
q = wait (NULL);
printf ("Subprocess terminated: PID %d\n", q);
}
exit (0);
}
不,它不。顯示代碼。 – 2013-02-28 16:21:38