1

下午好,这是我的“C”线程函数和信号处理程序结合在一起来处理 LINUX SIGIO 信号。我遇到的问题是我第一次运行我的测试程序时,这个函数可以正确处理和识别 SIGIO 信号。但是,在随后的时间(2,3,4...),此函数无法正确识别 SIGIO。我的假设是这种行为与 /proc//fdinfo/1" 文件的生命周期有关。请告诉我 /proc//fdinfo/1" 文件的生命周期的细节以及我是否假设是正确的。非常感谢。

void *reader(int signal)
{
    pid_t pid1,pid2;
    int status;
    int fd[2];
    char buf[256];
    char buffer[256];
    char filename[256];
    int myfd;
    int length;
    struct rusage usage;

    printf("SIGNAL = %d\n",signal);

    length = 256;
    if (signal == 0){
        return NULL;
    }

    pipe(fd);
    if ((pid1 = fork())) {
                sprintf(filename,"/proc/%d/fdinfo/1",pid1);
//              printf("UNLINK = %d\n",unlink(filename));
                myfd = open(filename,O_RDONLY);
                if (myfd == -1){
                        printf("myfd = %d filename = %s\n",myfd,filename);
                        printf("errno = %d\n",errno);
                }
                int myretval = mmap(NULL,
                                    length,
                                    PROT_READ|PROT_WRITE,
                                    MAP_ANONYMOUS|MAP_SHARED,
                                    myfd,
                                    0);
                printf("myretval = %d\n",myretval);
                fcntl(0,F_SETFL,F_SETOWN);
                fcntl(1,F_SETFL,F_SETOWN);
                       fcntl(2,F_SETFL,F_SETOWN);
                fcntl(3,F_SETFL,F_SETOWN);
                if (signal == SIGIO){
                        printf("SIGNAL SIGIO RECEIVED");
                }
                close(fd[WRITE]);
                FILE *read = fdopen(fd[READ],"r");
                while (!feof(read)) {
                        fgets(buf,256,read);
                //      printf("child says: %s",buf);   
                }
                pid2 = wait3(&status,0, &usage);
   }
   else {
                close(fd[READ]);
                close(fileno(stdout));
                dup(fd[WRITE]);
                //  child only unblock mask

                fcntl(0, F_SETFL, O_ASYNC);
                fcntl(1, F_SETFL, O_ASYNC);
                fcntl(2, F_SETFL, O_ASYNC);
                fcntl(3, F_SETFL, O_ASYNC);
                printf("WE ARE HERE\n");
                execl("/h/fchang03/five","five",NULL);

  }

}
4

0 回答 0