Question: Task 2 : Passing Environment Variables from Parent Process to Child Process In this task, we study how a child process gets its environment variables

Task 2: Passing Environment Variables from Parent Process to Child 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);
2
Environment Variable and Set-UID Program Lab
CIS 495/595
default: /* parent process */
//printenv();
exit(0);
}
}
Step 2. Nowcommentouttheprintenv()statementinthechildprocesscase(Line),anduncomment
the printenv() statement in the parent process case (Line ). Compile and run the code again, and
describe your observation. Save the output in another file.
Step 3. Comparethedifference of these two files using the diff command. Please draw your conclusion.

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 Databases Questions!