- 论坛徽章:
- 0
|
需要在wep page上显示内存和CPU的信息。我是使用top命令来获取的,可是top命令并不能进行重定向,那么只好对标准输出进行重定向了:
- if( (pid=vfork()) < 0) // vfork failure
- {
- goto ret;
- }
- else if(pid == 0) // child process
- {
- int fd = -1;
- if( (fd=open(topFile, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
- {
- perror("Create file failure:");
- exit(-1);
- }
- // STDOUT is topFile, then the "top" command output will send to $topFile
- dup2(fd, 1);
- close(fd);
- execl("/bin/top", "top", "-n1", NULL);
- }
复制代码
关于ping的:
- if( (pid=vfork()) < 0) // vfork failure
- {
- goto ret;
- }
- else if(pid == 0) // child process
- {
- int fd = -1;
- if( (fd=open(pingFileName, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
- {
- perror("Create file failure:");
- exit(-1);
- }
- // Let the ping command print information output to $pingFileName
- dup2(fd, 1);
- close(fd);
- execl("/bin/ping", "ping", "192.168.1.1", "-c4", NULL);
- }
复制代码
然后再用C对文件进行解析,获取自己需要的信息。
[ 本帖最后由 dglwx 于 2009-1-13 19:47 编辑 ] |
|