Processus: fork()¶
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
pid_t pid;
pid = fork();
if (pid == -1) {
perror("erreur fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
/* Processus fils */
printf("Hello from child process with pid: %d and parent pid: %d\n",
getpid(),
getppid()
);
} else {
while (pid != wait(0));
/* Processus pere */
printf("Hello from parent process with pid: %d and child pid: %d\n",
getpid(),
pid
);
}
exit(EXIT_SUCCESS);
}
use nix::sys::wait::wait;
use nix::unistd::ForkResult::{Child, Parent};
use nix::unistd::{fork, getpid, getppid};
fn main() {
let pid = fork();
match pid.expect("Fork Failed: Unable to create child process!") {
Child => println!(
"Hello from child process with pid: {} and parent pid:{}",
getpid(),
getppid()
),
Parent { child } => {
wait();
println!(
"Hello from parent process with pid: {} and child pid:{}",
getpid(),
child
);
}
}
}