Sponsored Content
Top Forums Programming parent not waiting until child complete executing another program through execl() Post 302186657 by vvaidyan on Thursday 17th of April 2008 05:50:45 PM
Old 04-17-2008
parent not waiting until child complete executing another program through execl()

Hi,
I am calling a program that greps and returns 72536 bytes of data on STDOUT, say about 7000 lines of data on STDOUT.

I use pipe from the program am calling the above program. Naturally, I execute the above program (through execl() ) throught the child process and try to read the STDOUT data through parent process by doing dup2(inpipe[1], STDOUT_FILENO) on child and doing read(inpipe[0], line, 10000), line = char[10000];

I am unable to read all 7000 lines of data from STDOUT into the character array. If I run it straight, only around 450 lines of data is obtained. Then, if I put a sleep(5) function / getchar() in the parent process, the number of lines that I read increases to 900 and 1300. waitpid() is not helping me here. This seems to be some memory problem, but there is no problem with the size of the char array.

Following is the program:

The executable program that prodoces 7000 lines (dfs-zgrep.cc) is:


Code:
# include <stdio.h>
# include <iostream>

using namespace std;

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

	string file_list = "";
   	string search_string = "a";

        file_list = "/home/vivekv/input_file-2.txt";	// file contains 7366 lines of character a (one char only for each line)

  	string query_string = "grep -nr " + search_string + " " + file_list + " | awk -F: '{printf \"%s %s\", $1 \" -\", $2 \" \\n\"}' ";

   	system(query_string.c_str());

 	exit(0);
}


The program (check_pipes.cc) from which am calling the above binary is:


Code:
# include <stdio.h>
# include <iostream>
# include <signal.h>
# include <sys/wait.h>

# define MAXLINE        100000


using namespace std;

int
main(int argc, char * argv[])
{
        int inpipe[2];
        pid_t pid;
        pid_t child_pid;

        if (  pipe(inpipe) < 0 )
        {
                cerr << "PIPE ERROR" << endl;
                return -2;
        }
        if ( (pid = fork()) < 0 )
        {
                cerr << "FORK ERROR" << endl;
                return -3;
        }
        else if (pid == 0)   // CHILD PROCESS
        {
                child_pid = getpid();
                cout << endl << endl << "INSIDE CHILD:-----> pid = " << child_pid << endl << endl;

                close(inpipe[0]);

                if (inpipe[1] != STDOUT_FILENO)
                {
                        if (dup2(inpipe[1], STDOUT_FILENO) != STDOUT_FILENO)
                        {
                                cerr << "dup2 error to stdout" << endl;
                        }

                        execl("/home/vivekv/dfs-fm-1.0.0/dfs-zgrep", "dfs-zgrep", NULL );

                        close(inpipe[1]);
                }


                return 0;
        }
        else    // PARENT PROCESS
        {
                // sleep(5);

                cout << endl << endl << "INSIDE PARENT: waiting for " << pid << " to finish " << endl << endl;

                cout << endl << endl << "CHILD: -----> child_pid = " << child_pid << endl << endl;
                cout << endl << endl << "PARENT:-----> pid = " << pid << endl << endl;

                // waitpid(child_pid, 0, 0);

                int rv;
                char line[MAXLINE];
                close(inpipe[1]);


                if ( (rv = read(inpipe[0], line, MAXLINE)) < 0 )
                {
                        cerr << "READ ERROR FROM PIPE" << endl;
                }
                else if (rv == 0)
                {
                        cerr << "Child Closed Pipe" << endl;
                        return 0;
                }
                cout << endl << endl << "INSIDE PARENT PROCESS...." << endl << line << endl;

                close(inpipe[0]);

                return 0;
        }
        return 0;
}


How do I read all the data and pass it to STDOUT (through cout) without losing data?

Thanks,
vivek

Last edited by vvaidyan; 04-17-2008 at 07:07 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies

2. Programming

shared memory between parent and child, when execl()

Hi, Am new to Linux internals. I want to use shared memory segment between the parent and the child processess. if i create a shared memory before fork,the child inherits the attached shared memory segments but once i call exec in the child process, all attached shared memory segments... (1 Reply)
Discussion started by: rvan
1 Replies

3. Shell Programming and Scripting

parent shell is waiting upon the child shell

Hi, I haev to devlop a script which when executed will take in a sudo privelege and run a set of commands then will go back to parent shell and execute the rest of the command But the problem I am facing is that when the script is executed it takes the sudo privelege but it waits for the... (0 Replies)
Discussion started by: ruchirmayank
0 Replies

4. Solaris

Timed out waiting for Autonegotiation to complete

Received the Timed out message consistently when I tried to jumpstart an M5000 with: boot jsnet:speed=1000,duplex=full - install Made the error go away by adding link-clock parameter: boot jsnet:speed=1000,duplex=full,link-clock=master - install "link-clock=master" disables... (1 Reply)
Discussion started by: markoakley
1 Replies

5. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

6. 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

7. Windows & DOS: Issues & Discussions

AutoSys Job not waiting for script to complete

I'm not sure if this is the right place to post this issue...but here goes... I am converting a set of windows jobs from Control-M to AutoSys r11.3. The same command line is being executed in both systems. The Control-M job runs to compltion in about 1.5 hours, waiting for the entire batch... (3 Replies)
Discussion started by: ajomarquez
3 Replies

8. Shell Programming and Scripting

Waiting for a process to complete in shell script

Hi, I need to initiate a process script which will start and do some processing and then shuts down. Then i need to other verifications. But the the process takes around 25 to 3o minutes. One thing i can monitor the nohup.out file for this process where i can wait for shutting down statement to... (3 Replies)
Discussion started by: Prashanth19
3 Replies

9. Homework & Coursework Questions

Help with Execl system call in a C program?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: "Your a7.c program should use printf to print a nice message. (You can decide what to say.) Then the process... (9 Replies)
Discussion started by: miniviking10
9 Replies

10. Shell Programming and Scripting

Subprocess.popen() should write to log without waiting for process to complete

Here is my test code process = sp.Popen( + , bufsize=1, universal_newlines=True, stdout=sp.PIPE, stderr=sp.STDOUT, cwd=src_home) output, _ =... (2 Replies)
Discussion started by: ezee
2 Replies
All times are GMT -4. The time now is 02:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy