我正在使用 linux redhat,在我的代码中,我有这个简单的流程,它应该读取它确实读取的 ps -fu $USER 输出的每一行,但不是所有的行只有一半。
const char* szPs = "ps -fu $USER"; // The PS command.
bool bSubProcessFound = false;
char szBuf[102400]; //big buffer
FILE* pPsOut = popen(szPs, "r");
if (pPsOut == NULL) {
bFailure = true;
return (0);
}
// Go thru all commands.
std::string sPsLine;
bool bFound = false;
int sizeofbug = sizeof(szBuf); //just checking size
while (fgets(szBuf, sizeof(szBuf), pPsOut) != NULL) {
LogWrite("befor process: %s in ps", szBuf);
// Erase new line at the end of ps string.
szBuf[strlen(szBuf) - 1] = '\0';
// Extract command line.
sPsLine = szBuf;
printf("after process: %s in ps", sPsLine.c_str());
}
例如,如果我在 shell 中运行 ps -fu $USER 命令,我会得到:
fooy 28407 28401 0 09:59 ? 00:00:04 java -Dappsubprocessname=catalina_gui -classpath .:/home/fooy/simple_java_server TCPServer
fooy 26256 26242 2 Oct20 pts/13 00:23:57 /usr/bin/java -Xms128m -Xmx512m -Dorg.eclipse.equinox.p2.reconciler.dropins.directory=/usr/share/eclipse/dropins -XX:CompileCommand=exclude,
但是当我运行代码时,我得到:
fooy 28407 28401 0 09:59 ? 00:00:03 java -Dappsubprocessname=catalin
fooy 26256 26242 1 Oct20 pts/13 00:23:20 /usr/bin/java -Xms128m -Xmx512m
为什么它会削减看起来有 81 个字符限制的线条?甚至像这样的简单命令:
char* in = NULL;
size_t len =0;
FILE * psAux = popen("/bin/ps ax", "r");
while(getline(&in,&sizeofbug,psAux)!=-1)
{
fputs(in,stdout);
}
并且输出是每行长度的 MAX 81:
3377 ? Ss 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3378 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3380 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3381 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3382 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3383 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3384 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
3521 ? S 0:15 [flush-8:32]
3616 ? Ss 0:00 sshd: foo123 [priv]
3624 ? S 0:00 sshd: foo123@notty
3625 ? Ss 0:00 tcsh -c /usr/libexec/openssh/sftp-server
3639 ? S 0:00 /usr/libexec/openssh/sftp-server
3640 ? Ss 0:00 tcsh -c /usr/libexec/openssh/sftp-server
3655 ? S 0:00 /usr/libexec/openssh/sftp-server
4150 ? Sl 0:25 SSSBatchCCC.exe -name ValueTxImporter -ORBInitRef Nam
4154 ? S 0:29 [flush-8:48]
4323 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
4515 ? Ss 0:00 sshd: wwwww02 [priv]
4526 ? S 0:00 sshd: wwwww02@notty
4527 ? Ss 0:00 tcsh -c /usr/libexec/openssh/sftp-server
4547 ? S 0:00 /usr/libexec/openssh/sftp-server
4657 ? S 0:01 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
4742 ? Ss 0:00 sshd: foo123 [priv]
4749 ? Ss 0:00 sshd: foo123 [priv]
4754 ? S 0:00 sshd: foo123@pts/7
4756 ? S 0:00 sshd: foo123@notty
4757 ? Ss 0:00 tcsh -c /usr/libexec/openssh/sftp-server
4773 ? S 0:00 /usr/libexec/openssh/sftp-server
4782 pts/7 Ss 0:00 -tcsh
5183 ? Ss 46:18 /usr/sbin/abrtd
5228 ? Ss 2:24 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messa
5337 pts/13 S+ 0:00 /bin/sh /usr/bin/eclipse
5338 pts/13 S+ 0:00 /usr/lib64/eclipse/eclipse
5352 pts/13 Sl+ 6:37 /usr/bin/java -Xms128m -Xmx512m -Dorg.eclipse.equinox
5472 ? S 0:00 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
5519 ? Sl 11:14 libvirtd --daemon
5864 ? S 0:01 httpd -f /home/wwwww02/DevDev/Apache/conf/httpd.conf
6363 ? S< 0:00 /sbin/udevd -d