Welcome, guest | Sign In | My Account | Store | Cart

sigaction

C, 42 lines
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  /* 设定信号处理函数 */
   act.sa_handler = NULL;
   act.sa_sigaction = child_handler;
   sigemptyset(&act.sa_mask);
   act.sa_flags = SA_SIGINFO;
   //sigaction(SIGCHLD, &act, NULL);



/**
  *@brief 接受退出信号而后创建新的进程
  *
  */
static void child_handler(int sig, siginfo_t *si, void *data)
{
   int i;
   pid_t *ptr;

   //也许是processing进程
   ptr = (pid_t *)pros_mem;
   for( i = 0; i< pros_num; i++){
       ptr += i;

       if(*ptr == si->si_pid){
           printf("processing process created\n");
           creat_processes((char *)ptr, 1, processing);
           return;
       }
   }

   //也许是caching进程
   ptr = (pid_t *)cach_mem;
   for( i = 0; i< cach_num; i++){
       ptr += i;

       if(*ptr == si->si_pid){
           printf("caching process created\n");
           creat_processes((char *)ptr, 1, caching);
           return;
       }
   }
}

2 comments

J Y (author) 14 years, 11 months ago  # | flag

SIGCHLD如果忽略的话, 会不会调用默认的处理函数, 如果是的话, 我们可以首先调用自己的处理函数, 然后通过默认的函数来释放资源

J Y (author) 14 years, 11 months ago  # | flag

避免defunct进程, manual page: In the case of a terminated child, performing a wait allows the system to release the resources associated with the child;

Created by J Y on Mon, 25 May 2009 (MIT)
C recipes (32)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks