Здравствуйте!
Задача:
следить за дочерним процессом и перезапускать его в случае завершения.
Я принял следующий подход к этой задаче:
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#define NUM_THREADS 3
struct Arg {
pid_t pid;
};
void* product(void* arg) {
while (1) {
// do smth complex
sleep(1);
printf("process %d\n", getpid());
}
}
void* control(void* arg) {
int status;
pid_t w;
do {
w = waitpid((((Arg*)arg)->pid), &status, WUNTRACED | WCONTINUED );
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
void startProcess() {
pid_t cpid;//, w;
pthread_t thread;
//int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
printf("WE SHOULD RESTART PROCESS\n");
exit(EXIT_FAILURE);
}
if (cpid == 0) { // Code executed by child
printf("Child PID is %ld\n", (long) getpid());
Arg arg;
arg.pid = cpid;
pthread_create(&thread,0, &product, &arg);
}
else { // Code executed by parent
Arg arg;
arg.pid = cpid;
pthread_create(&thread,0, &control, &arg);
return;
}
}
int main() {
for(int i=0; i<NUM_THREADS; ++i) {
startProcess();
}
return 0;
}
Насколько я понял, данный подход не совсем правильный.
Возможно, вы можете указать на мои ошибки и подсказать правильный путь.
Спасибо
Алексей
03.10.13 15:49: Перенесено модератором из 'C/C++' — Кодт