execl()/fork() output

This is a discussion on execl()/fork() output within the C Programming forums, part of the General Programming Boards category; Code: void run(char *bin, char *data) { if(fork()) { execl(bin, bin, data, 0); } else { int pid, status; pid ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Question execl()/fork() output

    Code:
    void run(char *bin, char *data)
    {
    
    if(fork())
    {
    
         execl(bin, bin, data, 0);
    
    }
    
    else
    {
    
    int pid, status;
    
         pid = wait(&status);
    
    }
    }
    
    .....
    
    for(i = 0; i < 5; i++)
    {
    
         printf("test %d\n", i);
         run(PATH, data);
    
    }
    output (with 'data' being different each time):

    Code:
    bash$ ./test
    test 0
    test 1
    test 2
    test 3
    test 4
    value 1 added
    value 14 added
    value 65 added
    bash$
    The purpose of my program is to run a program 5 different times with different arguments using execl() and fork() and be able to see the full output. As you can see in the above output, values for i=3 and i=4 cause the program I am running do a segmentation fault, which is what is supposed to happen, but it shows no output. I would like to see that output just like the previous outputs.

    I think it is a problem with the code not fork()ing correctly, but I am unsure. Been hitting my head against a wall for days trying to figure this one out. A solution would be simply terrific.

    Thanks so much,

    Todd

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    As you can see in the above output, values for i=3 and i=4 cause the program I am running do a segmentation fault, which is what is supposed to happen, but it shows no output. I would like to see that output just like the previous outputs.
    I have to call a real sleuth for this. Are you trying to say that you are surprised that you cannot find any output after the segfault?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Well, yes, since running the program manually using i=3 or i=4 shows the output then the next line "segmentation fault"., which is fine, its supposed to, but I want it to show that when I run it throw the parent process too.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try: SLEUTH

    And if you want a spanking: the real meaning of sleuth
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    I honestly don't understand what your talking about or how 'sleuth' can help me.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    DITTO, or possibly this
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm guessing that if you want to know whether bad things happened to your execl program, you're going to have to check for it yourself (since your program is running the file, error messages aren't going to go to the terminal necessarily) via the return value of execl.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    I hate to ask, but how exactly do I do that?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    The parent has to detect the situation and do the printf() as to what happened to the child. Normally, the shell does this for you.

    >> via the return value of execl.
    You mean wait/waitpid - execl never returns.
    http://www.opengroup.org/onlinepubs/...s/waitpid.html

    gg

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    But I am using wait()... I don't understand, is there something wrong with my code?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Only by omission -- you need to check status to see what happened to the process.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    I know if I printf() the status it is going to just print any integer, I believe, so once again, thanks for your help, but I need a solution

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    >> you need to check status to see what happened to the process.
    http://www.opengroup.org/onlinepubs/...s/waitpid.html

    Let us know if the documentation is unclear.

    gg

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    http://forums.devshed.com/c-programm...ut-587772.html
    Your code still looks like some non-compilable pseudo-code you just typed out, rather than anything which you've run.

    What's PATH?
    Because execl has some rather strict rules, you can't just pass any old path to it.

    Code:
    if ( fork() == 0 ) {
      close(1);
      if ( open("stdout.txt",O_WRONLY) == -1 ) {
        perror("Can't redirect");
      }
      if ( execl( bin, bin, data, 0) == -1 ) {
        perror("Can't exec");
        _exit(1);
      }
    } else {
      // wait
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Code:
    void run(char *bin, char *data)
    {
    
    if(fork() == 0)
    {
    
         close(1);
    
    if(open("stdout.txt", O_WRONLY) == -1)
    {
    
         perror("open");
    
    }
    
    if(execl(bin, bin, data, 0) == -1)
    {
    
         perror("execl");
         _exit(1);
    
    }
    }
    
    else
    {
    
    int pid, status;
    
         pid = wait(&status);
    
    }
    }
    Gives me: open: No such file or directory

    Then when I touch stdout.txt, it doesn't give me that error anymore, but it doesnt write anything to stdout.txt either, and I still see output from the child.

    Also two questions: I'm guessing close(1) closes stdout? and Why _exit(1) vs exit(1)?

    Thanks.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

Tags for this Thread


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