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

e take any posi- Process Concept any posi- Chapter 3 concerns what happens when

ID: 3869259 • Letter: E

Question

e take any posi- Process Concept any posi- Chapter 3 concerns what happens when we takea tive integer n and apply the following algorithm: if n is evern 3x n+1, if n is odd The conjecture states that when this algorithm is continually applied all positive integers will eventually reach 1 . For example, if n = 35, the sequence is 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 Write a C program using the fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. For example, if 8 is passed as a parameter on the command line, the child process will output 8,4,2,1. Because the rent and child processes have their own copies of the data, it will necessary for the child to output the sequence. Have the parent invoke the vait() call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a positive integer is passed on the command line. be 3.15 In Exercise 3.14, the child nror

Explanation / Answer

CODE:

#include <iostream>
#include <sys/types.h>                                                                                                         
#include <sys/wait.h>                                                                                                          
#include <unistd.h>                                                                                                             
#include <cstdlib>                                                                                                             
using namespace std;
#define MAX_VAL 1000

unsigned int dp_array[MAX_VAL+1];

void printChain(unsigned int value)
{
   cout<<"chain is ";
   while(value !=1)
   {

       cout<<value<<",";
       if(value%2==0)
           value=value/2;
       else
           value=3*value+1;
   }
   cout<<"1";
        cout<<endl;
}

unsigned int nextValue(unsigned int value)
{
if(value%2==0)
return (value/2);
else
return (3*value+1);
}

unsigned int countNoOfElement(unsigned int value)
{
int count=1;
while(value!=1)
{
   if ((value<=MAX_VAL) && (dp_array[value]!=0))
   {
       count+= (dp_array[value] -1);
       break;
   }
   else
   {
       value=nextValue(value);
       count++;
   }
}
return count;
}

void ChildProcess(int startNumber)
{

   int element_count=0, max=0;
   int i;
  
   dp_array[0]=0;
   dp_array[1]=1;

   for(i=2;i<=MAX_VAL;i++)
   {
       dp_array[i]=0;
   }


   element_count=countNoOfElement(MAX_VAL);
   dp_array[MAX_VAL]=element_count;

   printChain(startNumber);
  
}

int main(int argc , char *argv[])
{

  
   pid_t pid;                                                                                                             
    int status;                                                                                                             
   int number;                                                                                                              
    if( argc == 2 )
   {                                                                                                       
       cout<<"The argument supplied is "<<argv[1]<<endl;        
        number = atoi(argv[1]);                                                  
   }                                                                                                                            
   else if( argc > 2 ) {                                                                                                        
      cout<<"Too many arguments supplied. ";                                                                                   
      return 0;                                                                                                                 
   }                                                                                                                            
   else
   {                                                                                                                       
      cout<<"One argument expected. ";                                                                                         
      return 0;                                                                                                                 
   }                                                                                                                            
                                                                                                                                
                                                                                                                             
    pid = fork();                                                                                                               
    if (pid == 0)                                                                                                               
          ChildProcess(number);                                                                                                      
    else                                                                                                                        
   {                                                                                                                       
           waitpid(pid, &status, 0);                                                                                       
                                                                                                                          
   }             

return 0;
}

OUTPUT:

bash-4.3$ ./a.out 35                                                                                                              

The argument supplied is 35                                                                                                       

chain is  35,106,53,160,80,40,20,10,5,16,8,4,2,1

bash-4.3$ ./a.out 8                                                                                                               

The argument supplied is 8                                                                                                        

chain is  8,4,2,1

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote