Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program runs on a c++ compiler within a Linux software. Please complete thi

ID: 3743319 • Letter: T

Question

This program runs on a c++ compiler within a Linux software. Please complete this assignment

Modify this code to see what happens with processes. Recompile and run it several times. Try adding a function to the code and call the function from both the parent and child process.

Add a pointer to the code and see if anything unusual happens (It may, intermittently!)

#include <iostream>

#include <string>

#include <sys/types.h>

#include <unistd.h>

using namespace std;

int iglobal =1;

main ()

{

     int iExample =10;

    string sLocation;

    pid_t pID = fork(); //Create a child process

   

    if(pID==0)

    {

        //Child process code
        sLocation =”In Child Process”;

        iExample++; //Think about the scope of these variables

        iglobal++;

    }//if(pID==0)

         else if (pID <0) //Fork failed!

    {

        cout<<”Failed to spawn process”<<endl;

        exit(1);

    }//else if (pID<0)

    else

      {

        sLocation=”In parent process”;

      }

    cout<<”iglobal “ <<iglobal<<endl;

    cout<<”iExample ”<<iExample<<endl;

    cout<<”Location ” <<sLocation<<endl’;

}//end main()

When you modified the code and recompiled it, what did you find?

When you tried adding a function to the code and called the function from both the parent and child process, what happened?

Did anything unusual happen when you added a pointer to the code?

Explanation / Answer

---------------------------------------------

//output of the program for the given code snippet is

iglobal 1
iExample 10
Location In parent process   
iglobal 2
iExample 11
Location In Child Process  

--------------------------------------------

//Changed code to write function and call from both child and parent

#include <iostream>

#include <string>

#include <sys/types.h>

#include <unistd.h>

using namespace std;

void func(int *iglobal,int *iExample);
int iglobal =1;


main ()

{

int iExample =10;

string sLocation;

pid_t pID = fork(); //Create a child process

if(pID==0)

{

//Child process code
sLocation ="In Child Process";
func(&iglobal,&iExample);

}//if(pID==0)

else if (pID <0) //Fork failed!

{

cout<<"Failed to spawn process"<<endl;

exit(1);

}//else if (pID<0)

else

{

sLocation="In parent process";
func(&iglobal,&iExample);

}

cout<<"iglobal " <<iglobal<<endl;

cout<<"iExample "<<iExample<<endl;

cout<<"Location "<<sLocation<<endl;

}//end main()

void func(int *iglobal,int *iExample)
{
  
*iglobal++;

*iExample++;

}

----------------------------------------

//output after changing to call function from both parent and child

iglobal 1
iExample 10
Location In parent process   
iglobal 1
iExample 10
Location In Child Process

--------------------------------------------------

When we call function with pointers , iglobal and iExample value does not change.

Child gets the copy of the variables. pointer points to different address which contains the values 1 for iglobal and 1 for iExample . Since they point to different physical address both in child and parent , post increamenting them in function of child or parent does not get reflect for child or parent.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote