Wait system call

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

int main() {
    int i, status;
    pid_t pid;

    pid = fork(); // Create a new process

    if (pid < 0) {
        // Fork failed
        printf("\nProcess creation failure\n");
        exit(-1);
    } 
    else if (pid > 0) {
        // Parent process
        wait(NULL); // Wait for the child process to finish
        printf("\nParent starts\nEven Nos: ");
        for (i = 2; i <= 10; i += 2)
            printf("%3d", i);
        printf("\nParent ends\n");
    } 
    else if (pid == 0) {
        // Child process
        printf("Child starts\nOdd Nos: ");
        for (i = 1; i < 10; i += 2)
            printf("%3d", i);
        printf("\nChild ends\n");
    }

}

Comments

Popular posts from this blog

4