execl

This is a discussion on execl within the C Programming forums, part of the General Programming Boards category; Her is my main.c Code: //main.c int main( void ) { int newPID; newPID = fork(); if(newPID == 0) { ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    execl

    Her is my main.c

    Code:
    //main.c
    int main( void )
    {
       int newPID;  
       newPID = fork();
    
       if(newPID == 0)
       {
          execl("son1", "son1", 0);
       }
     
      return 0;
    }
    Her is the son1 file
    Code:
    int main(int argc, char *argv[] )
    {
      printf("Im son1 \n");
    
      return 0;
    }
    It compiles\buildes ok using KDevelop 2.1 but it doesnt print the text from son1.

    Any obvious mistakes ? how do troubleshoot this ?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by vVv
    Did you really name the second program ``son1'' and does it reside in the same directory as your PWD when you start that? Additionally, you can see if execl( ) failed by just checking if it returned.

    Code:
       if(newPID == 0)
       {
          execl("son1", "son1", 0);
          perror( "Yes, it failed" );
          return 1;
       }
    What does this give you?
    The above code give me "yes it failed: Exec format error" error.

    The file name is main.c and son1.c and they are in the sam folder

    Have tryed with execl("son1.c", "son1.c",0); but doesnt work

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by vVv
    From the man page:



    Are you sure that son1 is even a binary executable file? Your idea to just exec( ) the .c source file suggests you aren't.
    Place these files into one directory:

    Code:
    new.c:
    
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int main( void ) {
      pid_t child; 
      child = fork( );
      if( child == 0 ) {
        execl( "son1", "son1", NULL );
        perror( "It doesn't work" );
        return 1;
      }
      return 0;
    }
    
    new2.c:
    
    #include <stdio.h>
    
    int main( int argc, char *argv[] ) {
      printf( "I'm son1\n" );
      return 0;
    }
    Cd into that directory, then type ``gcc new.c -o new && gcc new2.c -o son1 && ./new''. What's your result now?
    The output from the above code is "I'm son1"

    In my orginal directory I have at the moment :
    main.c
    main.o
    son1.c
    son1.o

    If I do ./son1 in the directory the files are located I get the correct output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using execl()
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2010, 02:13 AM
  2. execl and command line arguments
    By imtiaz3 in forum C Programming
    Replies: 13
    Last Post: 09-30-2008, 12:18 PM
  3. 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
  4. execl not executing?
    By talz13 in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2004, 08:08 PM
  5. execl failing
    By carrythe0 in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 12:25 PM

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