fork download
  1. #include <stdio.h>
  2. #include<unistd.h>
  3. #include <sys/types.h>
  4. int main() {
  5. pid_t pid;
  6. pid = fork();
  7. if (pid < 0) {
  8. perror("Fork failed...");
  9. return 1;
  10. }
  11. else if (pid == 0) {
  12. printf("PID: %d\n", getpid());
  13. printf("Parent PID: %d\n", getppid());
  14. } else
  15. {
  16. printf("PID: %d\n", getpid());
  17. printf("Child PID: %d\n", pid);
  18. }
  19. return 0;
  20. }
  21.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
PID: 3945060
Child PID: 3945063
PID: 3945063
Parent PID: 1