0% found this document useful (0 votes)
7 views2 pages

Search

Uploaded by

366924bit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Search

Uploaded by

366924bit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-3

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

int main() {
pid_t child_pid;

child_pid = fork();

if (child_pid == -1) {
perror("Error in fork");
exit(EXIT_FAILURE);
}

if (child_pid == 0) {

printf("Child process:\n");
printf("PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());

sleep(2);

printf("Child process is done.\n");


exit(EXIT_SUCCESS);
} else {

printf("Parent process:\n");
printf("PID: %d\n", getpid());
printf("Child PID: %d\n", child_pid);

int status;
waitpid(child_pid, &status, 0);

if (WIFEXITED(status)) {
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process did not exit normally.\n");
}

printf("Parent process is done.\n");


}

return 0;
}

Output:

You might also like