데몬으로 파일에 기록하기.

i_wish_awk_sed_perl의 이미지

간단하게 나마 데몬 프로세스를 만들고 파일에 프로세스 번호를 기록하려고 합니다만 파일은 생성이 되지만 파일에 프로세스 번호를 기록하지 못하고 있습니다.

/* inet6_proc_daemon.c */

#include "inet6_proc_daemon.h"

void pid_fileopen(char *name);

int main(int argc, char *argv[])
{
    pid_t pid;

        if(argc < 2)
        {
                fprintf(stderr, "agrc error\n"); exit(0);
        }

    if((pid = fork()) < 0) exit(0);
        if(pid != 0) exit(0);
    
    chdir("/tmp");
//    setsid(); 
//      signal(SIGHUP, SIG_IGN);   

        pid_fileopen(argv[1]);

    while(1)
    {
        sleep(1);
    }

        return(0);
}

/* inet6_proc_daemon.h */

#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <signal.h>

void pid_fileopen(char *name)
{
        FILE *pid_file;
        char pid[10];

        sprintf(pid, "%d", (int)getpid());

        pid_file = fopen(name, "w");

        fputs(pid, pid_file);

        close(pid_file);
}

제가 데몬에 대해서 제대로 이해하고 있지 못한거 같습니다...데몬 관련 코드를 지우고 그냥 하면 파일을 잘 생성하고 생성된 파일에 프로세스 번호까지 잘 기록하는데 말이죠.

어떤 부분이 잘 못되었는지요?

mithrandir의 이미지

        FILE *pid_file; 
.
.

        fputs(pid, pid_file);

        close(pid_file); 

FILE * 형은 fclose 를 써야합니다.

언제나 삽질 - http://tisphie.net/typo/
프로그래밍 언어 개발 - http://langdev.net