콘텐츠로 바로가기

now0930 일지

이런저런 생각

  • 홈
  • 비공개
  • 강좌
  • 잔여 작업 조회
  • 위치

daemon 예제

linux system programming, 174p 예제 실행.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>

int main (void)

{
	pid_t pid, sid;
	int i;
	int j=0;
	/* create new process */
	pid = fork ();
	printf("Pid is %d \n",pid);
	if (pid == -1)
		return -1;
	else if (pid != 0)
		exit (EXIT_SUCCESS);


	/* create new session */
	sid =setsid();
	if (sid == -1)
		return -1;

	printf("sid is %d \n",sid);
	/* set the working directory to the root directory */
	if (chdir("/") == -1){
		printf("check");
		return -1;
	}

	/* close all open files--NR_OPEN is overkill, but works */
	printf("FOPEN_MAX is %d\n",FOPEN_MAX);
	for (i = 0; i < FOPEN_MAX; i++){
		//j=close(i);
		close(i);
		//printf("%d\n", j);
		printf("%d is closed\n", i);
	}

	/* redirect fd's 0,1,2 to /dev/null */
	open("/dev/null", O_RDWR); /* stdin */
	dup(0); /* stdout */
	dup(0); /* stderror */
	/* do its daemon thing... */

	while(1){

		sleep(1);
		//printf("this is test\n");
	}

	return 0;

}

54번 행 printf가 나오지 않음은 stdout을 죽여서 그런 듯 하다.

NR_OPEN을 찾을 수 없는데, 옛날 프로그램이라 다른 문구로 정의된 듯 하다. FOPEN_MAX로 일단 했다.

pi@raspberrypi:~/Project/cCode/systemProgram $ gcc daemon.c ;./a.out;ps aux | grep pi;
Pid is 1939 
Pid is 0 
sid is 1939 
FOPEN_MAX is 16
0 is closed
message+   346  0.0  0.0   6736  3524 ?        Ss    6월22   0:04 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
avahi      350  0.0  0.0   5896  2960 ?        Ss    6월22   0:02 avahi-daemon: running [raspberrypi.local]
pi         846  0.0  0.1  14716  7204 ?        Ss    6월22   0:12 /lib/systemd/systemd --user
pi         849  0.0  0.0  16864  1772 ?        S     6월22   0:00 (sd-pam)
pi         891  0.0  0.0   7256  3568 tty1     S+    6월22   0:00 -bash
pi        1939  0.0  0.0   1852    64 ?        Ss   19:01   0:00 ./a.out
pi        1940  0.0  0.0   8552  2532 pts/1    R+   19:01   0:00 ps aux
pi        1941  0.0  0.0   6116   496 pts/1    S+   19:01   0:00 grep --color=auto pi
root     28022  0.0  0.1  12236  6304 ?        Ss   17:16   0:00 sshd: pi [priv]
pi       28033  0.0  0.0  12236  3532 ?        S    17:16   0:01 sshd: pi@pts/0
pi       28036  0.0  0.0   7392  3856 pts/0    Ss   17:16   0:00 -bash
pi       28244  0.1  0.1  11564  6728 pts/0    S+   17:19   0:06 vi daemon.c
root     28246  0.0  0.1  12236  6252 ?        Ss   17:19   0:00 sshd: pi [priv]
pi       28257  0.0  0.0  12236  3552 ?        S    17:19   0:00 sshd: pi@pts/1
pi       28260  0.0  0.0   7548  3924 pts/1    Ss   17:19   0:00 -bash

pid 1939가 ?로 대몬으로 변신했다.

이 글 공유하기:

  • Tweet
발행일 2020-06-27글쓴이 이대원
카테고리 생활코딩 태그 c, linux, system program

댓글 남기기응답 취소

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.

글 내비게이션

이전 글

순창 채계산 출렁다리

다음 글

thread 예제

2025 5월
일 월 화 수 목 금 토
 123
45678910
11121314151617
18192021222324
25262728293031
4월    

최신 글

  • common mode, differential mode 2025-05-11
  • signal conditioner, 신호 처리기 2025-05-10
  • strain gage 2025-05-09
  • 칼만 필터 2025-05-01
  • positioner(I/P) 2025-04-26

카테고리

  • 산업계측제어기술사
  • 삶 자국
    • 책과 영화
    • 투자
  • 생활코딩
    • LEGO
    • ROS
    • tensorflow
  • 전기기사
  • 피아노 악보

메타

  • 로그인
  • 엔트리 피드
  • 댓글 피드
  • WordPress.org

페이지

  • 소개
  • 잔여 작업 조회
    • 작업 추가
    • 작업의 사진 조회
    • 작업 수정 페이지
  • 사진
    • GPS 입력된 사진
    • 사진 조회
  • 위치
    • 하기 휴가 방문지
    • 해외 출장

태그

android bash c docker driver FSM gps java kernel LEGO linux mysql network program opcua open62541 plc programmers python raspberry reinforcementLearning ros state space system program tensorflow transfer function 경제 미국 민수 삼국지 세계사 실기 에너지 역사 유전자 일본 임베디드 리눅스 전기기사 조선 중국 채윤 코딩 테스트 통계 한국사 한국어

팔로우하세요

  • Facebook
now0930 일지
WordPress로 제작.