fork download
  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int main(int argc, char *argv[]) {
  8. pid_t pid1, pid2;
  9. int status1, status2;
  10.  
  11. if ((pid1 = fork()) == 0) {
  12. // First child
  13. printf("Soy el primer hijo (%d, hijo de %d)\n", getpid(), getppid());
  14. printf("Ejecutando un ls con execl...\n");
  15. execl("/bin/ls", "ls", "-l", NULL);
  16. perror("execl failed"); // Add error handling if execl fails
  17. exit(1);
  18. } else {
  19. // Parent
  20. if ((pid2 = fork()) == 0) {
  21. // Second child
  22. printf("Soy el segundo hijo (%d, hijo de %d)\n", getpid(), getppid());
  23. printf("Ejecutando un ps con execv...\n");
  24. char *args[] = {"ps", "-A", NULL};
  25. execv("/bin/ps", args);
  26. perror("execv failed"); // Add error handling if execv fails
  27. exit(1);
  28. } else {
  29. // Parent
  30. // Wait for the first child
  31. waitpid(pid1, &status1, 0);
  32. // Wait for the second child
  33. waitpid(pid2, &status2, 0);
  34. printf("Soy el padre (%d, hijo de %d)\n", getpid(), getppid());
  35. }
  36. }
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.02s 5280KB
stdin
Standard input is empty
stdout
    PID TTY          TIME CMD
      1 ?        02:50:49 systemd
      2 ?        00:00:08 kthreadd
      3 ?        00:00:00 rcu_gp
      4 ?        00:00:00 rcu_par_gp
      5 ?        00:00:00 slub_flushwq
      6 ?        00:00:00 netns
      8 ?        00:00:00 kworker/0:0H-events_highpri
     10 ?        00:00:00 mm_percpu_wq
     11 ?        00:00:00 rcu_tasks_kthread
     12 ?        00:00:00 rcu_tasks_rude_kthread
     13 ?        00:00:00 rcu_tasks_trace_kthread
     14 ?        00:14:04 ksoftirqd/0
     15 ?        02:36:49 rcu_preempt
     16 ?        00:00:18 migration/0
     18 ?        00:00:00 cpuhp/0
     19 ?        00:00:00 cpuhp/1
     20 ?        00:00:23 migration/1
     21 ?        00:08:36 ksoftirqd/1
     23 ?        00:00:00 kworker/1:0H-events_highpri
     24 ?        00:00:00 cpuhp/2
     25 ?        00:00:23 migration/2
     26 ?        00:05:29 ksoftirqd/2
     28 ?        00:00:00 kworker/2:0H-events_highpri
     29 ?        00:00:00 cpuhp/3
     30 ?        00:00:22 migration/3
     31 ?        00:04:13 ksoftirqd/3
     33 ?        00:00:00 kworker/3:0H-events_highpri
     34 ?        00:00:00 cpuhp/4
     35 ?        00:00:17 migration/4
     36 ?        00:02:04 ksoftirqd/4
     38 ?        00:00:00 kworker/4:0H-events_highpri
     39 ?        00:00:00 cpuhp/5
     40 ?        00:00:17 migration/5
     41 ?        00:03:23 ksoftirqd/5
     43 ?        00:00:00 kworker/5:0H-events_highpri
     44 ?        00:00:00 cpuhp/6
     45 ?        00:00:16 migration/6
     46 ?        00:01:51 ksoftirqd/6
     48 ?        00:00:00 kworker/6:0H-events_highpri
     49 ?        00:00:00 cpuhp/7
     50 ?        00:00:16 migration/7
     51 ?        00:02:02 ksoftirqd/7
     53 ?        00:00:00 kworker/7:0H-events_highpri
     62 ?        00:00:00 kdevtmpfs
     63 ?        00:00:00 inet_frag_wq
     64 ?        00:00:00 kauditd
     66 ?        00:00:02 khungtaskd
     67 ?        00:00:00 oom_reaper
     68 ?        00:00:00 writeback
     69 ?        00:05:02 kcompactd0
     70 ?        00:00:00 ksmd
     71 ?        00:00:56 khugepaged
     72 ?        00:00:00 kintegrityd
     73 ?        00:00:00 kblockd
     74 ?        00:00:00 blkcg_punt_bio
     75 ?        00:00:00 tpm_dev_wq
     76 ?        00:00:00 edac-poller
     77 ?        00:00:00 devfreq_wq
     79 ?        00:01:09 kworker/4:1H-kblockd
     80 ?        00:01:52 kswapd0
     88 ?        00:00:00 kthrotld
     93 ?        00:00:00 acpi_thermal_pm
     94 ?        00:00:00 mld
     95 ?        00:02:04 kworker/0:1H-kblockd
     96 ?        00:00:00 ipv6_addrconf
    101 ?        00:00:00 kstrp
    106 ?        00:00:00 zswap-shrink
    107 ?        00:00:00 kworker/u17:0
    151 ?        00:01:58 kworker/2:1H-kblockd
    162 ?        00:02:00 kworker/1:1H-kblockd
    163 ?        00:14:34 kworker/3:1H-kblockd
    164 ?        00:00:39 kworker/7:1H-kblockd
    165 ?        00:00:59 kworker/5:1H-kblockd
    166 ?        00:00:54 kworker/6:1H-kblockd
    191 ?        00:00:00 ata_sff
    192 ?        00:00:00 scsi_eh_0
    193 ?        00:00:00 scsi_tmf_0
    194 ?        00:00:00 scsi_eh_1
    195 ?        00:00:00 scsi_tmf_1
    196 ?        00:00:00 scsi_eh_2
    197 ?        00:00:00 scsi_tmf_2
    198 ?        00:00:00 scsi_eh_3
    199 ?        00:00:00 scsi_tmf_3
    200 ?        00:00:00 scsi_eh_4
    201 ?        00:00:00 scsi_tmf_4
    202 ?        00:00:00 scsi_eh_5
    203 ?        00:00:00 scsi_tmf_5
    218 ?        00:00:00 md
    222 ?        01:06:14 md0_raid1
    228 ?        00:00:00 kdmflush/253:0
    229 ?        00:00:00 kdmflush/253:1
    248 ?        00:00:00 raid5wq
    281 ?        00:06:23 jbd2/dm-0-8
    282 ?        00:00:00 ext4-rsv-conver
    339 ?        00:39:51 systemd-journal
    376 ?        00:00:07 systemd-udevd
    435 ?        00:00:00 ipmi-msghandler
    457 ?        00:00:00 watchdogd
    464 ?        00:00:00 cryptd
    469 ?        00:00:51 mdadm
    470 ?        00:00:00 kipmi0
    633 ?        00:00:00 dhclient
    645 ?        00:00:24 rpcbind
    646 ?        00:00:22 systemd-timesyn
    6total 16
-rwxr-xr-x 1 root root 14312 Sep 25 21:20 prog
48 ?        00:00:00 rpciod
    649 ?        00:00:00 xprtiod
    653 ?        00:00:00 acpid
    655 ?        00:00:11 cron
    656 ?        00:00:00 svscanboot
    657 ?        00:46:39 dbus-daemon
    659 ?        00:03:54 irqbalance
    661 ?        00:08:02 rsyslogd
    662 ?        00:00:05 smartd
    664 ?        00:17:08 systemd-logind
    665 ?        00:00:00 atd
    667 ?        00:02:55 svscan
    668 ?        00:00:00 readproctitle
    669 ?        00:00:00 supervise
    670 ?        00:00:00 supervise
    671 ?        00:00:00 supervise
    672 ?        00:00:00 supervise
    673 ?        00:00:00 supervise
    674 ?        00:00:00 supervise
    675 ?        00:00:00 supervise
    676 ?        00:00:00 supervise
    677 ?        00:00:00 supervise
    678 ?        00:00:00 supervise
    679 ?        00:00:00 supervise
    680 ?        00:00:00 supervise
    681 ?        00:00:00 supervise
    682 ?        00:00:00 supervise
    683 ?        00:00:00 supervise
    684 ?        00:00:00 supervise
    685 ?        00:00:00 supervise
    686 ?        00:00:00 supervise
    687 ?        00:00:00 supervise
    688 ?        00:00:00 supervise
    689 ?        00:00:00 supervise
    690 ?        00:00:00 supervise
    691 ?        00:00:00 supervise
    692 ?        00:00:00 supervise
    693 ?        00:00:00 supervise
    694 ?        00:00:00 supervise
    695 ?        00:00:00 supervise
    696 ?        00:00:00 supervise
    697 ?        00:00:00 supervise
    698 ?        00:00:00 supervise
    699 ?        00:00:00 supervise
    700 ?        00:00:00 supervise
    707 ?        00:00:00 run
    708 ?        00:00:00 sudo
    709 ?        00:00:00 run
    710 ?        00:00:00 run
    711 ?        00:00:00 run
    712 ?        00:00:00 run
    713 ?        00:00:00 run
    714 ?        00:00:00 sudo
    715 ?        00:00:00 run
    716 ?        00:00:00 run
    717 ?        00:00:00 sudo
    718 ?        00:00:00 run
    719 ?        00:00:00 run
    720 ?        00:00:00 run
    721 ?        00:00:00 run
    722 ?        00:00:00 sudo
    723 ?        00:00:00 run
    724 ?        00:00:00 run
    725 ?        00:00:00 sudo
    726 ?        00:00:00 sudo
    795 ?        00:00:00 sudo
    796 ?        00:00:00 run
    797 ?        00:00:00 run
    798 ?        00:00:00 sudo
    857 ?        00:02:52 multilog
    858 ?        00:00:00 multilog
    859 ?        00:02:53 multilog
    862 ?        00:00:00 multilog
    865 ?        00:07:01 multilog
    866 ?        00:00:00 multilog
    869 ?        00:00:00 multilog
    870 ?        00:06:54 multilog
    871 ?        00:06:57 multilog
    872 ?        00:00:00 multilog
    873 ?        00:02:51 multilog
    874 ?        00:00:00 multilog
    877 ?        30-07:21:48 python3
    878 ?        30-04:28:27 python3
    879 ?        49-14:25:11 python3
    880 ?        48-15:09:18 python3
    881 ?        29-20:51:54 python3
    882 ?        48-18:50:51 python3
    883 ?        30-04:58:42 python3
    884 ?        49-01:20:23 python3
    887 ?        00:06:58 multilog
    888 ?        00:00:00 multilog
    889 ?        00:00:00 multilog
    890 ?        00:02:50 multilog
    944 ?        00:00:00 agetty
    975 ?        00:00:00 agetty
   1179 ?        00:07:45 sshd
   1187 ?        00:25:19 exim4
2475968 ?        00:00:00 kworker/7:1-md
2476259 ?        00:00:00 kworker/0:0-cgroup_pidlist_destroy
2479871 ?        00:00:00 kworker/2:4-cgroup_destroy
2480316 ?        00:00:00 kworker/5:3-rcu_gp
2480412 ?        00:00:00 kworker/3:1-cgroup_pidlist_destroy
2480727 ?        00:00:00 kworker/6:2-inet_frag_wq
2481225 ?        00:00:00 kworker/1:1-cgroup_pidlist_destroy
2482105 ?        00:00:00 kworker/1:2-mm_percpu_wq
2482293 ?        00:00:00 kworker/4:2-events
2482876 ?        00:00:00 kworker/u16:2-events_unbound
2483339 ?        00:00:00 kworker/5:0-cgroup_pidlist_destroy
2483358 ?        00:00:00 kworker/6:0-events
2483380 ?        00:00:00 kworker/0:3-events
2483598 ?        00:00:00 kworker/2:0-events
2483638 ?        00:00:00 kworker/1:3-events
2483658 ?        00:00:00 kworker/3:3-cgroup_destroy
2483671 ?        00:00:00 kworker/u16:1-edac-poller
2483672 ?        00:00:00 kworker/5:1-cgroup_pidlist_destroy
2483686 ?        00:00:00 kworker/4:1-inet_frag_wq
2484188 ?        00:00:00 kworker/7:0-events
2484225 ?        00:00:00 kworker/7:2-inet_frag_wq
2484635 ?        00:00:00 kworker/3:0-events
2484681 ?        00:00:00 kworker/u16:0+netns
2484697 ?        00:00:00 kworker/2:2-cgroup_pidlist_destroy
2484757 ?        00:00:00 kworker/1:0-inet_frag_wq
2484776 ?        00:00:00 kworker/0:1-rcu_gp
2484852 ?        00:00:00 kworker/6:1-events
2485290 ?        00:00:00 kworker/6:3+events
2485340 ?        00:00:00 kworker/0:2-cgroup_destroy
2485681 ?        00:00:00 kworker/7:3
2485700 ?        00:00:00 kworker/5:2-cgroup_pidlist_destroy
2485714 ?        00:00:00 kworker/4:0
2485964 ?        00:00:00 kworker/3:2-events
2486048 ?        00:00:00 bash
2486049 ?        00:00:00 secrun
2486050 ?        00:00:00 prog
2486054 ?        00:00:00 ps
Soy el padre (2486050, hijo de 2486049)