0

我不明白为什么在下面的程序中,当一个 SIGINT 信号到达时,线程直接终止(即使没有被信号处理程序覆盖)。但是,这不会在此main()过程中发生。

信号处理程序:

volatile sig_atomic_t termina = 0;
static void sigHandler()
{
    termina = 1;
}

线程函数:

static void *clientFun(void *fdSkt_comF)
{
    int fdSkt_com = *((int *) fdSkt_comF);
    char buffer[DIM_BUFFER];
    memset(buffer, '\0', DIM_BUFFER);

    //"termina" is set to 1 by the signal handler when a signal arrives
    while (termina == 0)
    {
        int lenghtRead;
        RETURN_SYSCALL(lenghtRead, read(fdSkt_com, buffer, DIM_BUFFER), "Errore: read(fdSkt_com, buffer, DIM_BUFFER)")

        if(lenghtRead == 0)
            break;

        //lenghtRead conta tutti i caretteri letti (compreso il '\0' se è presente)
        for(int i = 0; i < lenghtRead-1; i++)
        {
            buffer[i] = toupper((unsigned char) buffer[i]);
        }

        SYSCALL(writen(fdSkt_com, buffer, lenghtRead), "writen(fdSkt_com, buffer, lengthBuffer)")
    }
    puts("Ciao");
    return NULL;
}

在哪里:

#define RETURN_SYSCALL(r,c,e) if((r=c)==-1) { perror(e);exit(errno); }
#define SYSCALL(c,e) if(c==-1) { perror(e);exit(errno);}

线程创建和分离:

pthread_t clientId;
THREAD_CREATE(&clientId, NULL, &clientFun, (void *) &fdSkt_com, "Thread setId")
SYSCALL_ZERO(pthread_detach(clientId) , "pthread_detach(&clientId)");

如果要检查所有代码:server.c:https ://pastebin.com/6BQA3mx6

客户端.c:https ://pastebin.com/e42hKPxj

utils.h:https ://pastebin.com/hLbySeFF

conn.h:https ://pastebin.com/fL5cMxcg

4

0 回答 0