Thread: execl()/fork() output

  1. #16
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Gives me: open: No such file or directory
    You'll need additional open flags if you expect the file to be created for you. http://www.opengroup.org/onlinepubs/...ions/open.html

    >> but it doesnt write anything to stdout.txt either
    You haven't even saved the file handle returned by open(). Why would you expect anything to be written to it?

    >> I'm guessing close(1) closes stdout?
    Yes, 1 corresponds to standard out.
    http://www.opengroup.org/onlinepubs/...ons/stdin.html

    >> Why _exit(1) vs exit(1)?
    http://www.opengroup.org/onlinepubs/...ons/_Exit.html

    It seems to me that you've jumped into the deep end without first learning to swim...
    http://www.amazon.com/Pointers-C-Ken.../dp/0673999866

    gg

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    
         char small[32], big[128];
         int i = 16;
    
    if(strstr(argv[1], "t8")) { i = 8; }
    if(strstr(argv[1], "t16")) { i = 16; }
    if(strstr(argv[1], "t32")) { i = 32; }
    if(strstr(argv[1], "t64")) { i = 64; }
    if(strstr(argv[1], "t128")) { i = 128; }
    
         memset(big, 'Z', sizeof(big));
    
         snprintf(small, i, "%s", big);
         printf("%s\n", small);
    
         return 0;
    
    }
    That is the program I am running through the parent. As you can see, if the values are t64 or t128, the program crashes. I simply want to run that program 5 times with the 5 different values and be able to see the segmentation faults when they occur.

    Code:
    bash$ ./bvss t8
    ZZZZZZZ
    bash$ ./bvss t16
    ZZZZZZZZZZZZZZZ
    bash$ ./bvss t32
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    bash$ ./bvss t64
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    Segmentation fault
    bash$ ./bvss t128
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    Segmentation fault
    bash$
    
    bash$ ./test
    ZZZZZZZ
    ZZZZZZZZZZZZZZZ
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    bash$
    Code:
    void run(char *bin, char *data)
    {
    
    if(fork() == 0)
    {
    
    if(execl(bin, bin, data, 0) == -1)
    {
    
         perror("execl");
         _exit(1);
    
    }
    }
    
    else
    {
    
    int pid, status;
    
         pid = wait(&status);
    
    }
    }
    
    .....
    
    for(i = 0; i < 5; i++)
    {
    
         run(PATH, data);
    
    }
    See as I've explained my complete situation, I still can't get my code to work. I ask kindly for a solution.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > snprintf(small, i, "%s", big);
    Well yeah, when you lie about the size of the buffer, then it's gonna hurt you at some point.

    Anyway, it will be the shell which prints segmentation fault, not the program which dies.

    Having done a wait(&status), you need to read this
    http://www.manpagez.com/man/2/waitpid/
    and do things like
    Code:
    if ( WIFSIGNALED(status) ) {
      int sig = WTERMSIG(status);
      printf( "Program died due to signal %d\n", sig );
    }
    etc and so forth.
    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.

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You're lucky that bvss doesn't segfault every time you run it since small is never null-terminated.

    >> Segmentation fault
    That's coming from bash, not bvss. So you can either run bvss via bash or detect the segfault and printf("Segmentation fault") yourself.

    gg

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Thanks both of you, I finally got it to work using the signal catching solution.

    Couple short questions:

    1) snprintf() null terminates for us, so I don't need to do a snprintf(blah, sizeof(blah)-1 just sizeof(blah), correct?

    2) With memset(blah, 'Z', sizeof(blah)), could I and would I need to null terminate blah like blah[SIZE] = '\0' or blah[SIZE-1] = '\0' or ?

    Thanks again for you help

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, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04: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, 05:27 AM

Tags for this Thread