[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Strange behavior with errno...
From: |
Wodarczyk Christopher-CWODARC1 |
Subject: |
Strange behavior with errno... |
Date: |
Tue, 25 Nov 2003 16:11:27 -0600 |
I am running a program which uses popen followed by fgets. popen is used to
redirect output from the 'bin/ps -a' command to a file pointer which is parsed
by fgets. I am seeing some strange behavior with the methods though.... here
is the code as we've had it before....
const int lineSize=80;
char *cmd="/bin/ps -a"; // Run ps for all processes
FILE *ptr;
char buf[lineSize];
char *unixPidStr, *junk, *process_name;
int fileCnt = 0;
char *p = NULL;
char **pp = &p;
std::ostringstream oss;
if((ptr=popen(cmd,"r"))==NULL)
{
return(OM_FAIL);
}
// Read until list of PIDs is empty or until 5 are read, the max
// Make sure to read and discard the first line, which is the header
if(fgets(buf, lineSize, ptr) == NULL )
{
oss << "Any errors? " << strerror(errno) << endl;
logInfo((oss.str()).c_str(), LogOrDisplay_t_LOG_ONLY, FN_ADMIN);
pclose(ptr);
return(OM_FAIL);
}
else
logInfo(buf, LogOrDisplay_t_LOG_ONLY, FN_ADMIN);
-----------------------------------------------------------------------------
Now, if I change the code to this....
const int lineSize=80;
char *cmd="/bin/ps -a"; // Run ps for all processes
FILE *ptr;
char buf[lineSize];
char *unixPidStr, *junk, *process_name;
int fileCnt = 0;
char *p = NULL;
char **pp = &p;
std::ostringstream oss;
errno = 0; // Reset errno to 0
if((ptr=popen(cmd,"r"))==NULL)
{
return(OM_FAIL);
}
// Read until list of PIDs is empty or until 5 are read, the max
// Make sure to read and discard the first line, which is the header
if(fgets(buf, lineSize, ptr) == NULL )
{
oss << "Any errors? " << strerror(errno) << endl;
logInfo((oss.str()).c_str(), LogOrDisplay_t_LOG_ONLY, FN_ADMIN);
pclose(ptr);
return(OM_FAIL);
}
else
logInfo(buf, LogOrDisplay_t_LOG_ONLY, FN_ADMIN);
I end up in the else case and fgets parses my file just fine... is there
some weird thing going on with errno? I looked up the manpages which indicates
errno is thread-safe (my program is multi-threaded) and I don't specifically
set errno in any of my functions that I use anywhere. It just seems necessary
to zero out errno before I do the popen and fgets system calls....
BTW... without resetting errno the fault doesn't occur all the time.
Basically, this code is timed and gets executed about every 10 seconds or so...
out 10 times, the fault occurs about 6 times... so it passes 4 out of 10 times
without resetting errno to 0.
If I do decide to go with a fix with resetting errno to 0, is there any
possible impact it could have on something...?
Thanks for any input!
Christopher Wodarczyk
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Strange behavior with errno...,
Wodarczyk Christopher-CWODARC1 <=