You are on page 1of 2

#include <stdio.

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

int cpid;

void P2(int arg); /* child process prototype */

void main(void)
{
pid_t pid, p2_pid;

int arg;
printf("Enter number: ");
scanf("%d", &arg);

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

if (pid == 0)
{
P2(arg);
exit(0);
}
else
{
int wstatus;
wait(&wstatus);

//Print Child Process PID


printf("P1 pid = %d\n", getpid());
printf("P2 pid = %d\n", cpid);

//Print exit status of Child Process


if (WIFEXITED(wstatus)) {
int statusCode = WEXITSTATUS(wstatus);
if (statusCode == 0) {
printf("Congratulation! Your child process has been done!\n");
}
else {
printf("Failure with status code: %d\n", statusCode);
}
}

exit(0);
}
}

void P2(int arg)


{
printf("Children PID: %d\n", getpid());

cpid = getpid();

for (int i = 1; i <= 10; ++i) {


printf("%d * %d = %d \n", arg, i, arg * i);
}
}

You might also like