Guru

[알림판목록 I] [알림판목록 II] [글목록][이 전][다 음]
[ Guru ] in KIDS
글 쓴 이(By): glacier (소프)
날 짜 (Date): 1999년 7월 12일 월요일 오후 01시 54분 31초
제 목(Title): 급질] popen시에 문제점...


안녕하세요.
제가 클라이언트-서버 프로그램을 작성해야하는데 문제가 생겨서 질문을 드립니다.
서버 프로그램은 대몬처럼 계속 돌고 있으면서 stdout으로 장애발생을 알려줍니다.
이 서버 프로그램은 제가 전혀 손을 댈 수 없는 상황이구요...
제가 작성해야하는 클라이언트 프로그램은 이 장애정보를 가져다가 파싱하는 
것입니다.
그래서 popen으로 서버를 구동한다음 fgets로 읽어들이려고 했습니다.
그런데 여기서 문제가 발생하더군요.
서버프로그램이 종료하기 전까지 fgets가 동작을 하지 않습니다.
그래서 테스트용으로 서버에 해당하는 프로그램을 만들어서 무한루프를 없애고 
유한루프로 돌렸더니 루프가 끝나고 프로그램이 종료함과 동시에 
fgets를 수행하고 나머지것들도 다 수행을 하더군요.
그렇지만 제가 해야하는것이 서버프로그램이 날려주는 장애정보를 실시간으로 
처리를 해야하기 때문에 
문제가 생깁니다.

아래에 소스를 첨부하겠사오니 부디 고수님들께서 좀 가르쳐 주시기 바랍니다.
미리 감사합니다.  ^^
************************* client program *********************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#define BUFF_SIZE 512
#define FIFO1 "/tmp/fifo.1"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

    char buff[BUFF_SIZE];
    char aladate[10], alatime[10], severity[10], type[10];
    char mnemonic[10], action[20], code[10];

    void init(), output();

void main()
{
    FILE *ptr;
    char *cmd="./outputtest";
    
    int i, j;
    int readfd[2];
    
    if((ptr=popen(cmd,"r")) == NULL) {
        printf("Can't open program!!!\n");
        exit(-1);
    }
    
    while(1) {
        init();
        
        sleep(2);
        
        fgets(buff,BUFF_SIZE,readfd);                 
        
        sscanf(buff,"%8s %8s",aladate, alatime);
            
        i=18;   j=0;
        while(buff[i] != ' ') {
            severity[j] = buff[i];
            i++;  j++;
        }
    
void main()
{
    FILE *ptr;
    char *cmd="./outputtest";
    
    int i, j;
    long pos;
    
    if((ptr=popen(cmd,"r")) == NULL) {
        printf("Can't open program!!!\n");
        exit(-1);
    }

    while(1) {
        init();
        
        sleep(2);
        
        pos=ftell(ptr);
        printf("position : %ld\n",pos);
        
        setlinebuf(ptr);
        
        fgets(buff,BUFF_SIZE,ptr);    
        
        sscanf(buff,"%8s %8s",aladate, alatime);
            
        i=18;   j=0;
        while(buff[i] != ' ') {
            severity[j] = buff[i];
            i++;  j++;
        }
    
        i++;   j=0;
        while(buff[i] != ' ') {
            type[j] = buff[i];
            i++;   j++;
        }
    
        i++;   j=0;
        while(buff[i] != ' ') {
            mnemonic[j] = buff[i];
            i++;   j++;
        }
    
        i++;   j=0;
        while(buff[i] != ' ') { 
            action[j] = buff[i];
            i++;   j++;
        }
    
        i++; j=0;
        while(buff[i] != '\0') { 
            code[j] = buff[i];
            i++;   j++;
        }

        if((code[0] >='0') && (code[0] <= '9'))
            output();
    }
    pclose(ptr);
}

void init()
{
    memset(buff,0x00,BUFF_SIZE);
    memset(aladate,0x00,10);
    memset(alatime,0x00,10);
    memset(severity,0x00,10);
    memset(type,0x00,10);
    memset(mnemonic,0x00,10);
    memset(action,0x00,20);
    memset(code,0x00,10);
    
    return ;
}

void output()
{
    FILE *outfp;
    printf("file opening...\n");
    
    if((outfp=fopen("test.out","a+")) == NULL) {
        printf("Can't create file!!!\n");
        exit(-1);
    }
    
    fprintf(outfp,"date : %s, time : %s, severity : %s, type 
: %s, mnemonic : %s, action : %s, code : %s\n",aladate, alati
me, severity, type, mnemonic, action, code);
    fclose(outfp);
    
    return ;
}

************************ server용 test program **********************
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

void main()
{
    time_t r;
    struct tm *mytime;
    int code1, code2;
    
    void outtime(int a);
    
    code1=1060;   code2=3060;

    while(1) {   
        r=time(NULL);
        mytime=localtime(&r);
        
        outtime(mytime->tm_year);
        printf("-");
        outtime(mytime->tm_mon);
        printf("-");
        outtime(mytime->tm_mday);
        putchar(' ');
        outtime(mytime->tm_hour);
        printf(":");
        outtime(mytime->tm_min);
        printf(":");
        outtime(mytime->tm_sec);
        
        printf(" MAJOR SET FAILED NETWORK %d %d\n",code1, cod
e2);
        code1++;  code2++;  
        sleep(2);
    }   
}

void outtime(int a)
{
    if(a<10)
        printf("0%d",a);
    else
        printf("%d",a);
    
    return ;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ CCL/CNU                            |  (042) 823-8060  +
+ http://ccl.chungnam.ac.kr/~jkpark  |  012-447-0812    +
+ jkpark@ccl.chungnam.ac.kr          |                  +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
[알림판목록 I] [알림판목록 II] [글 목록][이 전][다 음]
키 즈 는 열 린 사 람 들 의 모 임 입 니 다.