Write a program that prints "Hello, World!" from a child process it creates and prints the ids of the child and parent processes in the format "The child and parent process ids are: 1234 and 1235." A failure of fork() should be caught and printed as an error message using err_sys call.

Respuesta :

Answer:

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

#include <unistd.h>

#include <sys/types.h> //NOTE: Added

void err_sys(const char *x)

{

printf("\n");

perror(x);

printf("\n");

exit(1);

}

void main()

{

pid_t pid;

 

if( (pid = fork()) == -1 )

{

err_sys("fork failed");

}

 

else if( pid == 0 )

{

printf("\nHello, World!\n");

printf("The child and parent ids are: %d and %d.\n\n", getpid(), getppid());

}

 

else

{

wait(NULL);

}

}

ACCESS MORE