免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2410 | 回复: 6
打印 上一主题 下一主题

[SCO UNIX] 关于while中循环执行popen的问题 请各位高手帮忙指教我的程序哪里出现了问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-14 19:38 |只看该作者 |倒序浏览
#include "stdio.h"
#include "stdlib.h"
#include "errno.h"
#include "string.h"
#include "netdb.h"
#include "sys/types.h"
#include "netinet/in.h"
#include "sys/socket.h"

#define SVRPORT 5656

main(){
int sock_fd,rcvnum;
char sSBuf[150],sRBuf[150];

FILE *fp;

struct sockaddr_in serv_addr;

while (1){
        memset(&sSBuf,0,sizeof(sSBuf));
        memset(&sRBuf,0,sizeof(sRBuf));


        /* ============================
        =         获得系统状态        =
        ============================ */

        if((fp = popen("/bin/sgetsys","r") == NULL){
                printf("popen error !!!!!\n";
                pclose(fp);
                exit(1);
        }



        if((fgets(sSBuf,150,fp)) == NULL){
                printf("sgetsys fail !!!!!\n";
                exit(1);       
        }


        if ( pclose(fp) == -1){
                printf("pclose error !!!!!\n";
                exit(1);
        }

        /* ============================
        =        socket 传送数据      =
        ============================ */
        if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
                perror("create socket error !!!!!";
                exit(1);       
        }

        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(SVRPORT);
        serv_addr.sin_addr.s_addr = inet_addr("10.1.2.17";
        bzero(&(serv_addr.sin_zero),;

        if( connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1){
                perror("connect error !!!!!";
                exit(1);       
        }


        if( send(sock_fd, sSBuf, strlen(sSBuf), 0) == -1){
                perror("send error !!!!!";
                exit(1);       
        }

        close(sock_fd);
}

}





####################################################################
其中 sgetsys 为
sgetsys
#生成文件格式为:
# cpu1空闲|cpu2空闲|用户数|%busy|%usr|%sys|%wio|%idle

#echo `date '+%H:%M:%S'`
stat=`sar 1 1|tail -1|awk '{print $2"|"$3"|"$4"|"$5}'`
#stat=`sar -d 1 1|tail -1|awk '{print $3}'`"|"$stat
stat=`who | wc -l | sed "s/ //g"`"|"$stat
stat=`ps -ef -o comm -o pcpu|sort -k 2|grep CPU2|awk '{print $3}'`"|"$stat
stat=`ps -ef -o comm -o pcpu|sort -k 2|grep CPU1|awk '{print $3}'`"|"$stat
echo $stat
#echo `date '+%H:%M:%S'`

论坛徽章:
0
2 [报告]
发表于 2007-05-14 22:02 |只看该作者
我的程序是不是产生了僵死进程啊? 如果是应该怎么办?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-05-15 08:32 |只看该作者
原帖由 hnthjh 于 2007-5-14 22:02 发表
我的程序是不是产生了僵死进程啊? 如果是应该怎么办?


你把 popen 的那个提到 while(1) 外面去。


这个最好是到 C 版去问。

论坛徽章:
0
4 [报告]
发表于 2007-05-15 09:42 |只看该作者
谢谢

论坛徽章:
0
5 [报告]
发表于 2007-05-15 10:29 |只看该作者
我的程序长时间运行会报如下错误:
waring:table-grow-proc table page limit of 1304 pages (MAX-PROC) exceed by 1 pages
newproc-pocess table overflow (MAX-PROC = 15514 exceeded)
是因为 产生僵死进程了么

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2007-05-15 10:35 |只看该作者
这个提示已经很清楚了,都告诉你了啊。

论坛徽章:
0
7 [报告]
发表于 2007-05-15 11:07 |只看该作者
发现运行时popen每次执行shell的进程ID号都在上次ID号的基础上不断增加,应该怎样解决?
我想不断循环以取得系统状态

[ 本帖最后由 hnthjh 于 2007-5-15 11:12 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP