Like Tree1Likes
  • 1 Post By anduril462

Continuously analyze of standard output from other program

This is a discussion on Continuously analyze of standard output from other program within the C Programming forums, part of the General Programming Boards category; I have to create a program that do this start another existing program capture the standard output of the previos ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Continuously analyze of standard output from other program

    I have to create a program that do this

    start another existing program
    capture the standard output of the previos program and depending on what it print, it do something else.

    I will try to explain the situation with an example:

    Let's think that I have a program (example.c) that print a sequence of number (from 1 to 1000).
    I have to create a program that start the "example.c" program and read the output continuously, so, when it print the number 348, it stop the example program!

    I could separate the two processes (caller and example) with a fork, but I don't know how to analyze continuously the output!

    Is there someone that can help me?

    Thank you very much!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    If you're using Linux, look into the popen/pclose functions (check "man popen" for details).
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Or create a pipe(), then fork() a child process and connect stdout of parent to stdin of child.
    Follow that by replacing the parent process with "example.c" using the exec() system call.
    Add logic in the child process to make the parent stop when the counter reaches 348.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    I'm near the right solution, but the program i've created doesn't work properly!
    I would pause the parent process when it print the number 345, but it doesn't do it: if i print all the output, after the line "Process stopped" it print all the other numbers!

    I think that the problem is that it doesn't anayze the output as a runtime, but that it analyze it only after the end of the execution.
    Is there someone who can help me?

    This is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    
    int main() {
    	
    	int pdes[2];
    
    	pipe(pdes);
    
    	int r = fork();
    	
    	if (r == -1)
    	{
    		/* errore */
    		printf("Errore nella fork!\n");
    		
    	}
    	else if(r == 0)
    	{
    		/* son */
    		close(pdes[1]);
    		char buf[20];
    		size_t nbytes;
    		ssize_t bytes_read;
    		nbytes = sizeof(buf);
    		bytes_read = read(pdes[0], buf, nbytes);
    		char string_to_recognize[]="345";
    		int j=0;
    		int l=0;
    		int ok=0;
    		int indice=1;
    		int found=0;
    		while(bytes_read = read(pdes[0], buf, nbytes))
    		{
    			int i;
    			for(i = 0; i < 20;i++)
    			{
    				if ((buf[i] == string_to_recognize[0]) && (found!=1))
    				{
    					j=1;
    					ok=1;
    				}
    				if ((ok!=1) && (found!=1))
    				{
    					if (buf[i] == string_to_recognize[j])
    					{
    						j++;
    						ok=1;
    					}
    					else
    					{
    						j=0;
    					}
    				}
    				if (j==(sizeof(string_to_recognize)-1))
    				{
    					
    					/* after the identification of the string, count 10 letters and then bring the process in pause*/
    					if (l==10)
    					{
    						int id_proc=getppid();  /* get parent id */
    						//execlp("kill", "KILL", "-SIGSTOP "+id_proc, NULL);
    						signal(SIGTSTP, SIG_DFL);   /* reset disposition to default */
    
    						kill(id_proc, SIGTSTP);     /*process pause */
    					}
    					l++;
    					//j=0;
    					found=1;
    				}
    				//printf("%c",buf[i]);
    				ok=0;
    			}
    		}
    		if (found ==1)
    		{
    			l++;
    			/* after the pause, count 10000-10 letters and then i restart the process*/
    					
    			//if (l==10000)
    			//{
    			//	int id_proc=getppid();  /* get the parent id*/
    			//	kill(id_proc, SIGCONT);     /*restart the process */
    			//		
    			//}
    		}
    	}
    	else
    	{
    		/* parent code */
    		close(pdes[0]);
    		dup2(pdes[1], STDOUT_FILENO);
    		execlp("/home/lifedj/progetto/comando.sh", "./comando.sh", "", NULL);
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where does the standard output go the following program
    By Overworked_PhD in forum Linux Programming
    Replies: 1
    Last Post: 02-23-2009, 08:48 PM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  3. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 06:59 AM
  4. continuously updating timer on console display
    By revelation437 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2003, 12:28 PM
  5. filestreams (fstream) and standard input/output (stdlib)
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21