22 lines
466 B
C
22 lines
466 B
C
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void prepare() { printf("preparing for fork...\n"); }
|
|
|
|
void parent() { printf("in parent after fork...\n"); }
|
|
|
|
void child() { printf("in child after fork...\n"); }
|
|
|
|
int main() {
|
|
pthread_atfork(prepare, parent, child);
|
|
pid_t pid = fork();
|
|
if (pid == 0) {
|
|
// in child process
|
|
printf("I am the child!\n");
|
|
} else {
|
|
// in parent process
|
|
printf("I am the parent!\n");
|
|
}
|
|
return 0;
|
|
}
|