#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    pid_t pid1, pid2;
    int status1, status2;

    if ((pid1 = fork()) == 0) {
        // First child
        printf("Soy el primer hijo (%d, hijo de %d)\n", getpid(), getppid());
        printf("Ejecutando un ls con execl...\n");
        execl("/bin/ls", "ls", "-l", NULL);
        perror("execl failed");  // Add error handling if execl fails
        exit(1);
    } else {
        // Parent
        if ((pid2 = fork()) == 0) {
            // Second child
            printf("Soy el segundo hijo (%d, hijo de %d)\n", getpid(), getppid());
            printf("Ejecutando un ps con execv...\n");
            char *args[] = {"ps", "-A", NULL};
            execv("/bin/ps", args);
            perror("execv failed");  // Add error handling if execv fails
            exit(1);
        } else {
            // Parent
            // Wait for the first child
            waitpid(pid1, &status1, 0);
            // Wait for the second child
            waitpid(pid2, &status2, 0);
            printf("Soy el padre (%d, hijo de %d)\n", getpid(), getppid());
        }
    }
    return 0;
}

