我有一个基本问题,BUFSIZ
值 (8192) 基本上是一个大小为 (4) 的整数,可容纳大小为 0 的字符数组。
怎么可能真的..
输出
size of buf:0
size of INT:4
size of bufsize:4
num_process:24
after num_process:24
max_process has reached for kblockd:0
编码
#include <iostream>
using namespace std;
class ServiceUtilCheck
{
private:
FILE *ptr;
char buf[0];
char cmd[1024];
int m_max_num_process;
public:
ServiceUtilCheck()
{
m_max_num_process=15;
}
bool check_max_process(const char* processname)
{
int num_process = 0;
int ret =0;
sprintf(cmd, "/bin/ps -e | /bin/grep %s | /usr/bin/wc -l", processname);
if ((ptr = popen(cmd,"r")) != NULL )
{
if (fgets(buf,BUFSIZ,ptr) != NULL )
{
//buf[0] = (char) 3424252;
cout<<" size of buf:" << sizeof(buf) <<endl;
cout<<" size of INT:" << sizeof(int) <<endl;
cout<<" size of bufsize:" << sizeof(BUFSIZ) <<endl;
num_process =atoi(buf);
cout<<"num_process:" << num_process <<endl;
}
cout<<" after num_process:" << num_process <<endl;
if (num_process <= m_max_num_process)
ret = true;
pclose(ptr);
}
return ret;
}
};
int main ()
{
ServiceUtilCheck *serv = new ServiceUtilCheck();
bool max_process= serv->check_max_process("kblockd");
cout<<"max_process has reached for kblockd:"<< max_process <<endl;
return 0;
}