dup2()

This is a discussion on dup2() within the C Programming forums, part of the General Programming Boards category; Code: if(pid == 0) { printf("I am the grandchild process %d\n", pid); /* closes the output side of the pipe ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    Question dup2() solved..thanks...

    Code:
    if(pid == 0)
    {
                    printf("I am the grandchild process %d\n", pid);
    
    	/* closes the output side of the pipe */
    	close(pfd[1]);
    
    	/* read input from pipe */
    	while((test = read(pfd[0], buffer, sizeof(buffer))) != 0)
    	printf("Testing : %s \n", buffer);
    							
    	/* closes the input side of the pipe */
    	close(pfd[0]);
    
    	/*test = getc(stdout);
    	printf("Test : %c", test);*/
    					
    	_exit(3);
    }
    if(pid > 0)
    {
    	printf("I am the child process %d\n", pid);
    					
    	/* closes input side of the pipe */
    	close(pfd[0]);
    
    	/* duplicate pipe output to 1 dup2(int oldfd, int newfd) */
    	dup2(pfd[1], 1);
    
    	/* closes the output side of the pipe */
    	close(pfd[1]);
    
    	/* run the OnOff java program */
    	execlp("java", "-classpath e:/cpe3007/ass3",  "OnOff", (char *) NULL);
    					
    	/* error message to display if java OnOff fail to execute */
    	perror("Program OnOff failed to execute \n");
    	_exit(4);
    }
    Managed to solve the earlier problem..thanks in advance anyone who look through.

    There are few questions I required some assistance though:

    Objective: Create a C program taking in one parameter in the name of a mpeg file to run. Another program, a java program is to run as a controller of sort...where when off is pressed, a '0' is output to the stdout and terminated by a newline. This will pause the mpeg file and on will continue movie.

    My idea is to create a parent process to run the mpeg file..then create a child process to run the java program and another grandchild to read the input and output to send the signal to the parent process.

    Therefore any comments, suggestions are welcome. I have a few questions though.

    Q1. Is the code above ok? Primary function is to pipe the output of the java proggie in the parent process into the input of child process.

    Q2. Can a grandchild process kill a parent process ??

    Thanks again in advance.
    Last edited by cruxxe; 06-06-2002 at 02:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2009, 07:13 PM
  2. Trying to understand fork, dup2, and execvp
    By Unclejunebug in forum C Programming
    Replies: 0
    Last Post: 04-29-2009, 08:04 PM
  3. dup2 prob
    By WDT in forum C Programming
    Replies: 6
    Last Post: 04-03-2005, 04:42 PM
  4. problem using dup2()
    By talal*c in forum Linux Programming
    Replies: 0
    Last Post: 04-11-2003, 10:44 PM

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