parent startet zuerst Kind, dann sich selbst...



  • Hi,

    int main()
    {
        char buff[ MAX_BUFFER ];
        pid_t wait_pid;
        cout << "Ok, jetzt gehts los." << endl;
    
        while (1)
        {
        	if( App1_pid == 0 ) {
        		cout << "App1 startet:  " << endl; 
        		run_executable( App1_Pipe, App1path, App1_pid, foo1 );
        	}        
            int no_bytes = 0;
            sleep(10);
            read_pipebuffer( buff, no_bytes, PIPE_READ_END(App1_Pipe.pipe_stdout.pipe_fd) );
            if( no_bytes > 0 ){
                char *pWord = NULL;
                pWord = strstr( buff, "---loading complete");
                if(pWord){                
                    if( App2_pid == 0 ){
                    	cout << "App2 startet:  " << endl; 
                    	run_executable( App2_Pipe, App2path, App2_pid, foo2 );
                    }
                }
                pWord = NULL;
                pWord = strstr( buff, "kill App1");
                if( pWord ){
                    kill( App1_pid, SIGKILL );
                    printf("App1 terminated!");
    
                }
                pWord = NULL;
                printf("%s\n", buff);
            }
        }
        return 0;
    }
    

    hier run_executable( App1_Pipe, App1path, App1_pid, foo1 ); startet er das Kind. (foo2 ist nur als dummy drin, weil ich dachte das ich vielleicht den Parentcode aufspliten muss...)

    dann startet er run_executable( App2_Pipe, App2path, App2_pid, foo2 ); allerdings ist er das selbst...

    Gruß
    Franky



  • hmm versteh ich das richtig?
    mit run_executable startesd a neuen Prozess? .. Wie schaut die run_executable - Funktion aus? ist ja keine standardfunktion von linux (meines wissens).

    oder generell: WAS WILLST DU ÜBERHAUPT MACHEN? ^^

    Wenn du 'n Vater- / Kind-Prozess erstellen und über PIPE kommunizieren möchtest, solltest du dir fork anschauen (http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/create.html).

    es stellen mir noch andere fragen:
    - sind App1_pid und App2_pid wirklich Global?
    - Was willst du genau machen?

    Falls irgendetwas, was ich verstanden hab, falsch ist, bitte sagen.
    Da wichtige Information nicht angegeben wurden, muss halt vieles interpretiert werden.

    gruß lm



  • Hi,

    die Funktionen, die fehlen...

    int run_executable( pipe_type &child_pipe,
    					const char* progname,
    					int &child_pid )
    {
    	if( pipe( child_pipe.pipe_stdin.pipe_fd ) == -1) {
    		perror( "pipe" );
    	}
    	//init stdin stream
    	PIPE_READ_END( child_pipe.pipe_stdin.fnctl_flags ) =
    			fcntl( PIPE_READ_END( child_pipe.pipe_stdin.pipe_fd ),
    				   F_GETFL );
    	fcntl( PIPE_READ_END( child_pipe.pipe_stdin.fnctl_flags ),
    		   F_SETFL,
    		   PIPE_READ_END( child_pipe.pipe_stdin.fnctl_flags ) | O_NONBLOCK );
    
    	if( pipe( child_pipe.pipe_stdout.pipe_fd ) == -1) {
    		perror( "pipe" );
    	}
    
    	//init stdout stream
    	PIPE_READ_END( child_pipe.pipe_stdout.fnctl_flags ) =
    			fcntl( PIPE_READ_END( child_pipe.pipe_stdout.pipe_fd ),
    				   F_GETFL );
    	fcntl( PIPE_READ_END( child_pipe.pipe_stdout.fnctl_flags ),
    			   F_SETFL,
    			   PIPE_READ_END( child_pipe.pipe_stdout.fnctl_flags ) | O_NONBLOCK );
    
    	if( pipe( child_pipe.pipe_stderr.pipe_fd ) == -1) {
    		perror( "pipe" );
    	}
    
    	//init stderr stream
    	PIPE_READ_END( child_pipe.pipe_stderr.fnctl_flags ) =
    			fcntl( PIPE_READ_END( child_pipe.pipe_stderr.pipe_fd ),
    				   F_GETFL );
    	fcntl( PIPE_READ_END( child_pipe.pipe_stderr.fnctl_flags ),
    		   F_SETFL,
    		   PIPE_READ_END( child_pipe.pipe_stderr.fnctl_flags ) | O_NONBLOCK );
    
    	int pid = vfork();
    	if( pid == -1 ) {
    		perror("fork");
    		exit( EXIT_FAILURE );
    	}
    	else if(pid == 0){
    		child_pid = getpid();
    		close( PIPE_WRITE_END( child_pipe.pipe_stdin.pipe_fd ) );
    		close( PIPE_READ_END( child_pipe.pipe_stdout.pipe_fd ) );
    		close( PIPE_READ_END( child_pipe.pipe_stderr.pipe_fd ) );
    
    		dup2( PIPE_READ_END( child_pipe.pipe_stdin.pipe_fd ), STDIN_FILENO );
    		dup2( PIPE_WRITE_END( child_pipe.pipe_stdout.pipe_fd ), STDOUT_FILENO );
    		dup2( PIPE_WRITE_END( child_pipe.pipe_stderr.pipe_fd ), STDERR_FILENO );
    
    		execl( progname, progname, (char*) 0 );
    	}
    	else if( pid > 0 ){
    		close( PIPE_READ_END( child_pipe.pipe_stdin.pipe_fd ) );
    		close( PIPE_WRITE_END( child_pipe.pipe_stdout.pipe_fd ) );
    		close( PIPE_WRITE_END( child_pipe.pipe_stderr.pipe_fd ) );
    	}
    	return 0;
    }
    
    int read_pipebuffer( char* buff, int &no_bytes, int pipe_fd )
    {
    	char tmpBuff[ MAX_BUFFER ] = {0};
    	int i;
    	size_t bytesread = 0;
    	fd_set fds;
    	struct timeval tv;
    
    	tv.tv_sec = tv.tv_usec = 0;
    	FD_ZERO( &fds );
    	FD_SET( pipe_fd, &fds);
    	int rv = select( pipe_fd + 1, &fds, NULL, NULL, &tv );
    	if( rv == -1 )
    		perror("select");
    	else if ( rv > 0 ){
    		if( FD_ISSET( pipe_fd, &fds ) )
    		{
    			bytesread = read( pipe_fd,
    							  tmpBuff,
    							  sizeof( tmpBuff ) );
    			no_bytes = bytesread;
    			if( bytesread == 0 )
    			{
    				return -1;
    			}
    		}
    	}
    	if(bytesread > 0 ){
    		if( strlen( tmpBuff ) > 0 ){
    			sprintf( buff, "%s",  tmpBuff );
    			tmpBuff[ MAX_BUFFER ] = 0;
    		}
    	}
    	FD_CLR( pipe_fd, &fds );
    	return 0;
    }
    

    z.B. Parent erzeugt 1 bis 5 Kinder

    Parent
    | | | | |
    Child1 Child2 Child3 Child4 Child5

    derzeit habe ich das Problen das Parent und App1 "sleeping" sind...Also habe ich in der Pipe "Bad file descriptor"...

    Gruß
    Franky


Anmelden zum Antworten