Thread: why this is not working? execl()

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    why this is not working? execl()

    I know that exec() functions dont come back unless they fail.
    that is why i created a child process so to parent process waits for the child.
    But in my case the program does not come back. what am I missing?

    Code:
    /*
     *  main.c
     */
    
    #include "execute.h"
    
    main(){
    	char *argument = "barbara.pgm";
    	printf("executing invert:...\n");
    	
    	execute(2,argument);
    	printf("ended.\n");
    }
    and the execute.h

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    static char *program[8] = {
    			"./avgFilter", 
    			"./binarize", 
    			"./invert", 
    			"", "", "", ""};
    
    void execute(short progNumber, char * fileName){
    	int pid;
    	int status;
    	
    	/*
    	 *	Get a child process
    	 */
    	if((pid = fork()) < 0){
    		perror("fork: ");
    		exit(EXIT_FAILURE);
    	}
    	
    	/*
    	 *	The child executes the program
    	 */
    	if(pid == 0){
    		execl(program[progNumber],program[progNumber], fileName, (char *)0);
    		perror("execl");
    		exit(EXIT_FAILURE);
    	}
    	
    	/*
    	 *	The parent process just waits
    	 */
    	if (pid >= 1) {
    		wait(&status);
    	}
    	
    	exit(EXIT_SUCCESS);
    }
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean "program doesn't come back"? Does "ps" show the applciation running (invert in this case)?

    Also, you should not implement functions in header files unless they are really short (inline functions).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are two other flaws in the program:
    main should return int explicitly.
    A function other than main should never call exit unless in very special cases! It's better to return an error and let main display an error and quit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question

    [QUOTE=matsp;711036]What do you mean "program doesn't come back"? Does "ps" show the applciation running (invert in this case)?

    I mean the reason I created the function is because after doing "./invert barbara.pgm" i have to do something else , in this piece of code is just
    Code:
    printf("ended");
    but this part is not been executing (as it seems)

    the output is :

    Code:
    2:~nacho4d$
    ./main
    executing invert:...
    2:~nacho4d$

    why?
    Mac OS 10.6 Snow Leopard : Darwin

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you called exit(), even in the parent.
    So yes, it never makes it to the printf() in main().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But, as Elysia pointed out, you are calling "exit()" from execute(), so it won't get back to main ever...

    Code:
    	exit(EXIT_SUCCESS);
    will always be executed in your execute() function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    Now I see...
    Thanks Salem and Matsp.
    So, what is the difference between exit and return? can you explain my kindly?

    I changed my program as you suggested and it works ok
    Mac OS 10.6 Snow Leopard : Darwin

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Exit terminates your program. Killing it. It won't execute further.
    Return simply ends the function and returns to the previous function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    I C.
    That is why calling exit from functions is dangerous right? in Others words it should only be called from main() right?
    Mac OS 10.6 Snow Leopard : Darwin

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, except in very special situation which you don't really have to worry about very much.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    thank you very much.
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Can we use select() to capture output from execl()?
    By Nessarose in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-05-2005, 12:53 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM