Thread: popen debug

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    11

    popen debug

    we call popen function repeat, sometime it work well,sometime it is wrong. when it work well , we get the command line execute filename;
    when it is wrong , we get the result "s_path_name[i]=(%o)" : (0) (0) (0) (0 ) (0). we get the result (114) (116) ...(12) (0) when it work well.

    if you check the fp , you will find that "fp[0]=%ld" : (537303080) when it work well, (537303072) when it is wrong.

    the c code below:
    sprintf( s_command, "/usr/bin/ps -p %ld |/usr/bin/awk '$1 == %ld {print$4}'", pid, pid );

    if( ( fp = popen( s_command, "r" ) ) == NULL )
    {
    return( FATAL );
    }

    fscanf( fp, "%s", s_path_name );
    for ( i_count=0; i_count<=10; i_count++ )
    fprintf( stderr, "(%ld)", fp[i_count] );
    for ( i_count=0; i_count<=10; i_count++ )
    fprintf( stderr, "(%o)", s_path_name[i_count] );
    if ( pclose( fp ) == -1 )
    fprintf( stderr, "\n the pclose error");
    Last edited by clark_x; 08-31-2001 at 01:19 AM.

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    popen? Never heard of that. Change it to fopen
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    our platform is unix+c. popen is a system call function. it do fork,exec etc.

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    The syntax is exactly the same as fopen. For example:

    Code:
    char string[10][30];
    fptr = fopen(filename,"r"); if(fptr == NULL) exit(1);
    while (fscanf(fptr,"%s",string[i]) != NULL)
    {
       ++i
       if(i > 10) break;
    }
    fclose(fptr);
    for(i=0;i<10;i++) printf("%s\n",string[i]);
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    our platform is AIX4.3. i think this problem is a system environment fault or memory fault.

    but i don't know how to correct it. in fact i make a small c program to test popen, in 100000 call, no fault disappear.
    Last edited by clark_x; 08-28-2001 at 01:16 AM.

  6. #6
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    sprintf( s_command, "/usr/bin/ps -p %ld |/usr/bin/awk '$1 == %ld {print$4}'", pid, pid );
    I don't understand this too well because I use a Windows 2000 Professional. You might want to try posting this on the Linux programming board.
    I compile code with:
    Visual Studio.NET beta2

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    char s_command[ 150 ];
    char s_path_name[ 100 ];

    procinfo.h: unsigned long pi_pid;

    in my debug codes, include below sentense:
    fprintf( stderr, "%ld,%s,%s", pid,s_command,ps_server_type );

    the debug result is ok even the fault occur.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    now i change my code to :

    fscanf( fp, "%s", s_path_name );
    if ( s_path_name[0] == '\0' )
    {
    fprintf( stderr, "\n the s_path_name is NULL ");
    fscanf( fp, "%s", s_path_name );
    }

    pclose( fp );

    so it work well.

    god tell me ! why ? why ? why ?

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    -1

  10. #10
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    If the scan was successful it would return the number of sucessful scans, for example:

    int status = fscanf(fptr, "%s%s%d", sting1,string2,&number);

    This would return '3' if the scan was sucessful. But all this information should be in the prototype.
    I compile code with:
    Visual Studio.NET beta2

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    "These subroutines return the number of successfully matched and assigned input items. This number can be 0 if an early conflict existed between an input character and the control string. If the input ends before the first conflict or conversion, only EOF is returned. If a read error occurs, the error indicator for the stream is set, EOF is returned, and the errno global variable is set to indicate the error."

    IN FACT, it ret -1 when the process is not existent or "sometime".
    in my test the errno is always 4.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    i find that : the fflush() or some other function set errno=29 sometime. why?

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    fscanf failed: Interrupted system call

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    now i see the first call return -1 because some signal occar.
    but how can i catch this signal and ignore this signal.
    i find a SA_RESTART flag. but i don't know how to use system call "signal() and sigaction()"

  15. #15
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Maybe you should post your whole code. I think that Salem is in a better position to answer the question because he uses a *NIX operating system, but just post the whole code or you might never get a full answer.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. popen and fgets, underflow!
    By henrikstolpe in forum Linux Programming
    Replies: 0
    Last Post: 02-06-2009, 03:39 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  4. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM