-1

I have this piece of code that I developed just to address a problem that I have in another large program that I am developing.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring> 
#include <cstdlib> 
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#include <string>
#include <iostream>

using namespace std;
void processLine (char []);
void readLine(char []);

const int LIMIT = 512;
int main(int argc, char *argv[])
{
    char oneLine[LINE_MAX];
    readLine(oneLine);
    
        
    return 0;
}
void readLine(char line[])
{
    processLine(line);
    

 //Otherstuff
 ------------

}
void processLine(char line[])
{
    
    pid_t process;
    int child_status;

    string input;
    cout << "Input: ";
    cin >> input;

    
        process = fork();
        if(process == 0)
        { // do nothing
        }
        else 
        {
                        //parent
                    if(input == "quit")
            {
                printf("Quit command found ! \nExiting ");
                    
                for(int i = 0;i < 3;i++)
                {
                    printf(".");
                    fflush(stdout);
                    sleep(1);
                    
                }
            
                printf("\n");
                    exit(0);            
            }
            else
            {
                wait(&child_status);
            }
        }
    
}

My goal is simple, When the user enter quit.

I will just display

Quit command found

Exiting ...

And there is a delay of one second between each of these three dots.

However the output that I get is

Quit command found

Exiting . other stuff ..

However, what seems to happen is that the parent process returns and then executes other stuff from the calling function before it continues to print the other two dots. How would I avoid the parent process from doing that ?

Community
  • 1
  • 1
Pro
  • 173
  • 4
  • 15
  • @Olaf This is mainly c – Pro Feb 07 '16 at 17:04
  • And artificial delays in a program are always cause of irritation an aggravate the user. – too honest for this site Feb 07 '16 at 17:05
  • I'd say (in case the user entered "*quit*") the **child** "*returns and then executes other stuff from the calling function*". – alk Feb 07 '16 at 17:07
  • 1
    It's the child process that prints the `other stuff`: the return of `fork` is `0` for the child, your `// do nothing`, which then exists `processLine`, continuing to do `other stuff`. – Kenney Feb 07 '16 at 17:07
  • "This is mainly c" which makes it badly codes C++, but still not C! Similar syntax does not imply same semantics. Unless it compiles with a C compiler, it is not C. – too honest for this site Feb 07 '16 at 17:08
  • @Kenney How would the child process from doing that when the user enters quit ? – Pro Feb 07 '16 at 17:09
  • You can't, really. It behaves in the way you wrote it: you first enter "quit", *then* you fork the child, *then* you wait for 3 seconds (and let the child run for 3 seconds doing it's stuff), and only *then* you exit. Perhaps what you intend is something like this: first `fork`, then loop over `stdin` (in the parent) until you get a `quit`, and then immediately call exit. – Kenney Feb 07 '16 at 17:13
  • @Kenney I fixed it, I basically check again in the child if the input is quit and I exit in this case !! – Pro Feb 07 '16 at 17:19

1 Answers1

0

Use waitpid() like this:

pid_t childPid;  
childPid = fork();
...
int returnStatus;    
waitpid(childPid, &returnStatus, 0);  // Parent process waits here for child to terminate.

Use it here

if(childPid == 0)  // fork succeeded 
{   
   // Do something   
   exit(0); 
}
else  // Main (parent) process after fork succeeds 
{    
    int returnStatus;    
    waitpid(childPid, &returnStatus, 0);
}
agent420
  • 3,291
  • 20
  • 27