Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
2.4 kB
1
Indexable
Never
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
    pid_t pid, pid1, pid2, pid3, pid4, pid5;

    // Create process p1
    pid = fork();

    if (pid < 0) {
        printf("Failed to create process.\n");
        return 1;
    } else if (pid == 0) {
        // Child process (p1)

        // Create process p2
        pid1 = fork();

        if (pid1 < 0) {
            printf("Failed to create process p2.\n");
            return 1;
        } else if (pid1 == 0) {
            // Child process (p2)

            // Create process p3
            pid2 = fork();

            if (pid2 < 0) {
                printf("Failed to create process p3.\n");
                return 1;
            } else if (pid2 == 0) {
                // Child process (p3)
                printf("p3\n");
            } else {
                // Parent process (p2)
                printf("p2\n");
            }
        } else {
            // Parent process (p1)

            // Create process p4
            pid3 = fork();

            if (pid3 < 0) {
                printf("Failed to create process p4.\n");
                return 1;
            } else if (pid3 == 0) {
                // Child process (p4)
                printf("p4\n");

                // Create process p6
                pid4 = fork();

                if (pid4 < 0) {
                    printf("Failed to create process p6.\n");
                    return 1;
                } else if (pid4 == 0) {
                    // Child process (p6)
                    printf("p6\n");
                } else {
                    // Parent process (p4)

                    // Create process p5
                    pid5 = fork();

                    if (pid5 < 0) {
                        printf("Failed to create process p5.\n");
                        return 1;
                    } else if (pid5 == 0) {
                        // Child process (p5)
                        printf("p5\n");
                    } else {
                        // Parent process (p4)
                        // Wait for p6 and p5 to finish
                    }
                }
            } else {
                // Parent process (p1)
                // Wait for p2 and p4 to finish
                printf("p1\n");
            }
        }
    } else {
        // Parent process (main)
        // Wait for all child processes to finish
    }

    return 0;
}
Leave a Comment