Question: In this task, we study how a child process gets its environment variables from its parent. In Unix, fork ( ) creates a new process

In this task, we study how a child process gets its environment variables from its parent. In Unix, fork()
creates a new process by duplicating the calling process. The new process, referred to as the child, is an
exact duplicate of the calling process, referred to as the parent; however, several things are not inherited by
the child (please see the manual of fork() by typing the following command: man fork). In this task,
we would like to know whether the parents environment variables are inherited by the child process or not.
Step 1. Please compile and run the following program, and describe your observation. The program can
be found in the Labsetupfolder; it can be compiled using "gcc myprintenv.c", whichwillgenerate
a binary called a.out. Lets run it and save the output into a file using "a.out > file".
Listing 1: myprintenv.c
#include
#include
#include
extern char **environ;
void printenv()
{
int i =0;
while (environ[i]!= NULL){
printf("%s
", environ[i]);
i++;
}
}
void main()
{
pid_t childPid;
switch(childPid = fork()){
case 0: /* child process */
printenv();
exit(0);
default: /* parent process */
//printenv()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!