Answered step by step
Verified Expert Solution
Question
1 Approved Answer
FORK_BASIC.C #include #include #include /* getpid() and fork() are system calls declared in unistd.h. They return */ /* values of type pid_t. This pid_t is
FORK_BASIC.C
#include
#include
#include
/* getpid() and fork() are system calls declared in unistd.h. They return */
/* values of type pid_t. This pid_t is a special type for process ids. */
/* It's equivalent to int. */
int main(void)
{
pid_t pid;
/* fork a child process */
pid = fork();
if (pid
fprintf (stderr, "Fork failed ");
return 1;
}
else if (pid == 0) { /* child process */
printf ("I am the child process and my fork() call returned %d ", pid);
}
else { /* parent process */
printf ("I am the parent process and my fork() call returned %d ", pid);
}
return 1;
}
FORK_WAIT.C
#include
#include
#include
#include
#include
/* pid_t fork(void) is declared in unistd.h */
/* pid_t is a special type for process ids. it's equivalent to int. */
int main(void) {
pid_t child_pid;
int status;
pid_t wait_result;
child_pid = fork();
if (child_pid == 0)
{
/* this code is only executed in the child process */
printf ("I am a child and my pid = %d ", getpid());
printf ("My parent is %d ", getppid());
/* an exit here would stop only the child process */
}
else if (child_pid > 0)
{
/* this code is only executed in the parent process */
printf ("I am the parent and my pid = %d ", getpid());
printf ("My child has pid = %d ", child_pid);
}
else
{
printf ("The fork system call failed to create a new process "
);
exit(1);
}
/* this code is executed by both parent and child processes */
printf ("I am a happy, healthy process and my pid = %d ", getpid());
if (child_pid == 0)
{
/* this code is only executed by the child process */
printf ("I am a child and I am quitting work now! ");
}
else
{
/* this code is only executed by the parent process */
printf ("I am a parent and I am going to wait for my child ");
do
{
/* parent waits for the SIGCHLD signal sent
to the parent of a (child) process when
the child process terminates */
wait_result = wait(&status);
} while (wait_result != child_pid);
printf ("I am a parent and I am quitting. ");
}
return 0;
}
SIMPLE_FORK1.C
#include
#include
int main()
{
printf ("0. I am process %d ", getpid());
(void) fork();
printf ("1. I am process %d ", getpid());
(void) fork();
printf ("2. I am process %d ", getpid());
}
SIMPLE_FORK2.C
#include
#include
int main()
{
printf ("0. I am process %d ", getpid());
(void) fork();
printf ("1. I am process %d ", getpid());
(void) fork();
printf ("2. I am process %d ", getpid());
(void) fork();
printf ("3. I am process %d ", getpid());
}
SIMPLE_FORK3.C
#include
#include
int main()
{
printf ("0. I am process %d ", getpid());
(void) fork();
printf ("1. I am process %d ", getpid());
(void) fork();
printf ("2. I am process %d ", getpid());
(void) fork();
printf ("3. I am process %d ", getpid());
(void) fork();
printf ("4. I am process %d ", getpid());
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started