프로세스의 세션 식별 번호를 구하거나, 새로운 세션을 생성한다.

#.include <sys/types.h>

#include <unistd.h>

pid_t getsid(pid_t pid);

- pid: 프로세스 식별 번호

- return: 호출 성공 SID, 실패 -1

 

세션(session):

-일반적으로 시스템과 연결된 하나의 제어 단말기를 포함한 단위

-식별 번호(id)가 부여되어 있따.

-세션>그룹>프로세스

-세션의 리더(프로세스): 자신의 PID=PGID=PSID일 경우

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
    pid_t pid;
    int interval;
 
    if(argc!=3){ printf("error"); exit(1);}
 
    pid=atoi(argv[1]);
    interval=atoi(argv[2]);
 
    printf("shell process....\n");
    printf("process id:%d group id:%d, session id:%d\n",pid,getpgid(pid),getsid(pid));
 
    printf("current process.. not demon...\n");
    printf("process id:%d, group id:%d, session id:%d\n",getpid(),getpgrp(),getsid(0));
 
    sleep(interval);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

데몬 프로세스: setsid()로 데몬 프로세스로 만들 수 있따.

#include<sys/types.h>

#include<unistd.h> 

pid_tsetsid(void); -그룹리더가 아니면 새 세션 생성해 세션과 그룹 리더가 된다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
main(){
    pid_t pid;
 
    if((pid=fork())>0){
        sleep(1);
        exit(1);
    }
    else if(pid==0){
        printf("old session id: %d\n",getsid(0));
        printf("new session id :%d\n",setsid());
        sleep(500);
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

시그널의 개념

시그널(signals)

-소프트웨어 인터럽트 (interrupts)

- 프로세스들 사이에서 비동기적 (asynchronous) 사건의 발생을 전달

 

시그널 이름

-서로 다른 사건을 구별하기 위하여 여러 종류의 시그널을 제공

-모든 시그널은 'SIG'로 시작하는 이름을 갖는다.

-예: SIGINT, SIGABRT, SIGALRM 등

 

시그널에 대한 작업

-시그널의 발생, 전달, 처리

 

시그널의 발생(generation)

터미널에서 특수키를 누르는 경우

하드웨어의 오류 , 0으로 나눈 경우, 잘못된 메모리 참조 등

kill 함수의 호출, 특정 프로세스나 프로세스 그룹에게 원하는 시그널을 발생/전달, 대상 프로세스에 대한 권한이있어야함

kill 명령의 실행, 내부적으로 kill 함수를 호출한다.

소프트웨어적 조건, 네트워크에서의 데이터 오류(SIGURG), 파이프 작업에서의 오류(SIGPIPE), 알람 시계의 종료(SIGALRM) 등

 

시그널의 전달 (delivery)

시그널의 전달

-발생된 시그널이 수신되어 정해진 방법대로 처리 되는 것

-지연 (pending) : 발생된 시그널의 전달되지 못한 상태

 

시그널의 블록(block)

-블록이 해제되거나 무시하도록 변경될 때까지 지연된 상태로 남는다.

-시그널 마스크(signal mask) : 블록될 시그널 집합

 

시그널의 처리

시그널을 무시한다. (ignore)

-SIGKILL과 SIGSTOP 시그널을 제외한 모든 시그널을 무시할 수 있다.

-하드웨어 오류에 의해 발생한 시그널에 대해서는 주의해야 한다.

 

시그널을 처리한다. (catch)

-시그널이 발생하면 미리 등록된 함수(handler)가 수행

-SIGKILL과 SIGSTOP 시그널에는 처리할 함수를 등록할 수 없다.

 

기본 처리 방법에 따른다. (default)

-특별한 처리 방법을 선택하지 않은 경우

-대부분 시그널의 기본 처리 방법은 프로세스를 종료시키는 것이다.

 

기본적인 신호 처리

시그널 발생 함수

#include <sys/types.h>

#inlcude <signal.h>

int kill(pid_t pid, int signo); 특정 프로세스나 프로세스 그룹에게 지정한 시그널을 발생

int raise(int signo); 자기 자신에게 지정한 시그널을 발생

권한

-시그널을 발생하는 프로세스의 실제/유효 사용자 ID가 시그널을 수신할 프로세스의 실제/유효 사용자 ID와 동일한 경우에만 시그널을 전달할 수 있다.

-수퍼유저는 임의의 프로세스에게 시그널을 전달할 수 있다.

-SIGCONT 시그널은 같은 세션에 있는 임의의 프로세스에게 전달할 수 있다.

 

sig 인자

-시그널 번호

-null signal (0):실제로 시그널을 보내지 않고 프로세스의 존재여부를 파악하는 데 사용한다. 존재하지 않는 프로세스에 대한 kill 함수 호출은 에러(-1)를 리턴하고 errno에 ESRCH를 저장

 

pid 인자의 지정 방법

-pid>0 프로세스 ID가 pid인 프로세스에게 시그널을 전달

-pid==0 호출한 프로세스와 같은 프로세스 그룹 ID를 가지고 권한이 있는 모든 프로세스에게 시그널을 전달

-pid<0 pid의 절대값에 해당하는 프로세스 그룹 ID를 가지고 권한이 있는 모든 프로세스에게 시그널을 전달

 

신호 대기

#include <unistd.h>

int pause(void);

시그널이 전달되어 처리될 때까지 프로 세스의 실행을 지연

시그널 처리 함수가 리턴하는 경우에만 pause 함수는 리턴한다. 리턴 값은 에러(-1)이고, errno에 EINTR를 저장한다.

 

신호 정보 출력

#include <siginfo.h>

void psignal(int sig, const char* s);

#include <string.h>

void *strsignal(int sig);

신호 정보를 출력

psignal 함수 : s가 가리키는 메시지 뒤에 콜론을 붙이고 sig에 해당하는 문자열을 표준 에러로 출력

strsignal 함수 : sig에 해당하는 신호의 문자열을 출력

 

시그널 처리 방법의 선택

#include <signal.h>

void (*signal(int signo, void (*func)(int)))(int);

지정한 시그널에 대해 세 가지 처리 방법 중 하나를 선택

signo 인자: 시그널 번호

func 인자

-SIG_IGN : 지정한 시그널을 무시한다. ((void(*)()) 1)

-SIG_DFL : 기본 처리 방법에 따라 처리한다. ((void (*)()) 0)

-사용자 정의 함수(signal handler) : 시그널이 발생하면 호출될 함수의 주소, 하나의 정수형 인자를 갖고 리턴 값이 없는 함수에 대한 포인터 (pointer)

리턴 값: 지정한 시그널에 대한 이전까지의 처리 방법

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <signal.h>
#include <stdio.h>
 
static void sig_usr(int); //one handler for both signals
 
int main(void)
{
    if (signal(SIGUSR1,sig_usr)==SIG_ERR){
        perror("can't catch SIGUSR1"); return -1;}
    if (signal(SIGUSR2,sig_usr)==SIG_ERR){
        perror("can't catch SIGUSR2");return -1;}
    for(;;)
        pause();
}
 
static void sig_usr(int signo) // argument is signal number
{
    if(signo==SIGUSR1)
        printf("received SIGUSR1\n");
    else if(signo==SIGUSR2)
        printf("received SIGUSR2\n");
    else
        fprintf(stderr,"received signal %d\n",signo);
    
    return;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

+ Recent posts