search

Home  >  Q&A  >  body text

linux - APUE里面第10章,关于信号的这段代码不理解。请指教

在10.6节的可冲入函数段落中,有一段示例程序,这段程序从信号处理函数中调用非可重入函数getpwnam
每一秒my_alarm都会被调用一次。

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

<code>#include<stdio.h>

#include<pwd.h>

#include "apue.h"

 

static void my_alarm(int signo)

{

    struct passwd *rootptr;

    printf("IN signal handler\n");

 

    if((rootptr = getpwnam("root")) == NULL)

        err_sys("getpwnam(root) error");

    alarm(1);

}

 

int main(void)

{

    struct passwd *ptr;

    signal(SIGALRM,my_alarm);

 

    alarm(1);

    for(;;)

    {

        if((ptr = getpwnam("sar")) == NULL)

            err_sys("getpwnam error");

        if(strcmp(ptr->pw_name,"sar") != 0)

            printf("return value corrupted,pw_name = %s\n",ptr->pw_name);

    }

}

</code>

但是当我运行出来之后,显示:getpwnam error: Success

我想知道这个Success在哪里出来的呢。代码里面没有看到。而且,不是说每隔一秒就会调用一次吗,但是程序运行一次就结束了。

怪我咯怪我咯2898 days ago800

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:34:47

    1. Your machine did not sar这个用户。所以程序会执行err_sys("getpwnam error");。然后err_sys最后会执行exit(1)exit.

    2. Because Shenma has Success, you can trace the implementation of apue error handling.

    reply
    0
  • Cancelreply