getting the return from forked child process to parent in C++


 
Thread Tools Search this Thread
Top Forums Programming getting the return from forked child process to parent in C++
# 1  
Old 12-02-2010
getting the return from forked child process to parent in C++

This needs to work on HPUX and Linux.

I do a fork and create a child process. During execution of the child process, it is possible child become lost or get killed. That is the reason why I create the child process. However if the child process doesnt get killed, I do want to know the return value(return_I_want_to_save). Towards the end, I make sure all children gets killed anyway.

I cant seems to find a way to do this. Any help would be highly appropriated. This is not a home work problem. I am an intern.


Please see attached txt file for my code.
# 2  
Old 12-02-2010
Um. what you do is pretty much regardless of c++/c : pseudo code
Code:
pid_t arr[2];
int status;
arr[0]=fork();
if (arr[0]=0)
{
     this is the first child process code block
     to get a status the code has to exit(n) where n==status    
     otherwise status is any value that happens to to be on the stack.
}
arr[1]=fork();
if(arr[1]==0)
{
    this is the second process code block
    exit() this one too.
}
// wait for the children to become done
waitpid(arr[0], &status, 0);
// process status with macros fromn sys/wait.h
waitpid(arr[1], &status, 0);

# 3  
Old 12-02-2010
Here is the code, I finally managed to copy it. Creating fork and killing children all that works perfect in this. My issue is, bool return value from WaitForScmi2(waitForScmiTimeLimit), gets lost. I need to pass that value to parent process. Can you please tell me what I am doing wrong here?

Code:
bool System::WaitForScmi(U32 waitForScmiTimeLimit)
{

  
  int pid = fork();
  vector <int> childList;

  if (pid == -1)
  {
    termError("Fork failure in start method, errno %d = %s",
               errno, strerror(errno));
  }
  else if( pid == 0)
  {
    bool return_I_want_to_save = WaitForScmi2(waitForScmiTimeLimit);
    exit(0);
  }
  else
  {
    print("Executing recoverWaitForScmi, started child process: %u", pid);
    childList.push_back(pid);
  }

  if(return_I_want_to_save)
  {
    print("We did it!!!");
  }

  if(!KillChild(childList))// here is how i make sure all child process get killed
  {
     print("It seems like recoverWaitForScmi could not excute correct & ran in to a termError ");
     return(0);
  }
  else
  {
     return(1);
  }
}



bool System::KillChild(vector<int>& childList)
{
  vector <int>::iterator it;
  int childStatus;

  for ( it=childList.begin(); it < childList.end(); it++ )
  {
    int pid  = (*it);
    waitpid(pid, &childStatus, 0);//Wait for child
    if(childStatus)
    {
      // Kill all other children. 
      for (U32 i = 0; i < childList.size(); i++)
      {
        kill(childList[i], SIGINT);
      }
      print("Killing process: %u Complete.", pid);
      // termError("Exiting...");
      print("Exiting...");
      return(0);//indicating child process failure. This return has nothing to do with
                //if killing pass or fail. 
    }
    else
    {
      print("Process: %u Complete.", pid);
      return(1);//indicating child process success. 
                //Notice this return has nothing to do with if killing pass or fail
    }
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Why is the return code of child required by parent ?

Hello everyone, I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent... (1 Reply)
Discussion started by: sreyan32
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

5. UNIX for Dummies Questions & Answers

Command to find parent and child process?

Hi, I have a script that calls other scripts/commands which may or may not spawn other process. From my understanding, when I do a ps -ef, the highest numbered process ID is supposed to be the parent ID of all the other related child processes, is this correct? In most or all... (3 Replies)
Discussion started by: newbie_01
3 Replies

6. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. Shell Programming and Scripting

How to return control from the child script to the parent one?

I have two shell scripts : A.sh and B.sh A.sh echo "In A" exec B.sh echo "After B" B.sh echo "In B" The output is : In A In B I want the output : In A In B After B (4 Replies)
Discussion started by: suchismitasuchi
4 Replies

9. Programming

getting the return code of forked child process (ftp)

Hi, From within my C++ program, I fork a child process and execl an ftp session (solaris), like this : std::string szStartCmd = "ftp -i -n -v 192.168.149.31"; int nExecRes = execl("/bin/sh", "sh", "-c", szStartCmd.c_str(), (char *)0); I use 2 pipes to communicate between my... (7 Replies)
Discussion started by: KittyJ
7 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question