pthread_sigmask test

#include <stdio.h>
#include <pthread.h>
#include <signal.h>
pthread_t tid1,tid2,tid_sig;
pthread_cond_t cond1,cond2;
pthread_mutex_t mutex1,mutex2;
unsigned int flag1,flag2;

unsigned long thread1_sig_counter=0;
unsigned long thread2_sig_counter=0;
void *thread1_func(void *arg)
{
    sigset_t sigset;
    //block SIGRTMIN
    sigemptyset(&sigset);
    sigaddset(&sigset,SIGRTMIN);
    pthread_sigmask(SIG_SETMASK,&sigset,NULL);
    while(1)
    {
        pthread_mutex_lock(&mutex1);
        while(!flag1)
        {
            pthread_cond_wait(&cond1,&mutex1);
        }
        printf("thread1_func got signal! CNT=%ld\n",thread1_sig_counter++);

        //reset.
        flag1=0;
        pthread_mutex_unlock(&mutex1);
    }
}
void *thread2_func(void *arg)
{
    sigset_t sigset;
    //block SIGRTMIN
    sigemptyset(&sigset);
    sigaddset(&sigset,SIGRTMIN);
    pthread_sigmask(SIG_SETMASK,&sigset,NULL);
    while(1)
    {
        pthread_mutex_lock(&mutex2);
        while(!flag2)
        {
            pthread_cond_wait(&cond2,&mutex2);
        }
        printf("thread2_func got signal! CNT=%ld\n",thread2_sig_counter++);

        //reset.here we set the flag ,it will be waken up by other thread.
        flag2=0;
        pthread_mutex_unlock(&mutex2);
    }
}
void *thread_signal(void *arg)
{
    int ret;
    sigset_t sigset;
    sigemptyset(&sigset);
    sigaddset(&sigset,SIGRTMIN);
    while(1)
    {
        printf("thread signal:wait signal...\n");
        if(0==sigwait(&sigset,&ret))
        {
            if(SIGRTMIN==ret)
            {
                printf("trigger thread1!\n");
                //trigger thread1.
                pthread_mutex_lock(&mutex1);
                flag1=1;
                pthread_cond_signal(&cond1);
                pthread_mutex_unlock(&mutex1);

                printf("trigger thread2!\n");
                //trigger thread1.
                pthread_mutex_lock(&mutex2);
                flag2=1;
                pthread_cond_signal(&cond2);
                pthread_mutex_unlock(&mutex2);
            }
        }
    }
}
//main process.
int main(void)
{
    sigset_t sigset;
    pthread_mutex_init(&mutex1,NULL);
    pthread_mutex_init(&mutex2,NULL);
    pthread_cond_init(&cond1,NULL);
    pthread_cond_init(&cond2,NULL);
    flag1=flag2=0;

    //block SIGRTMIN
    sigemptyset(&sigset);
    sigaddset(&sigset,SIGRTMIN);
    pthread_sigmask(SIG_SETMASK,&sigset,NULL);

    if(pthread_create(&tid1,NULL,thread1_func,NULL))
    {
        printf("create thread1 failed!\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2_func,NULL))
    {
        printf("create thread2 failed!\n");
        return -1;
    }
    if(pthread_create(&tid_sig,NULL,thread_signal,NULL))
    {
        printf("create signal thread failed!\n");
        return -1;
    }
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_join(tid_sig,NULL);

    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_mutex_destroy(&mutex1);
    pthread_mutex_destroy(&mutex2);
    printf("done!\n");
    return 0;
}

/************************************************************************************************** \Copyright (c) Novatek Microelectronics Corp., Ltd. All Rights Reserved. \file server.c \brief server of full duplex chat tools \project Training \chip NA \Date 2025.08.15 **************************************************************************************************/ #include "common.h" int g_sockfd = -1; atomic_bool g_bRunning = ATOMIC_VAR_INIT(true); atomic_bool g_bClientShutdown = ATOMIC_VAR_INIT(false); pthread_t g_readThread, g_writeThread; char g_sClientName[MAX_NAME_LEN] = {'\0'}; /*! \fn void cleanup_client() \brief Clean up client resources */ void cleanup_client() { TEST_PRINT(EN_TRACE, "in"); TEST_PRINT(EN_MSG, "Cleaning up client resources"); atomic_store_explicit(&g_bRunning, false, memory_order_release); atomic_store_explicit(&g_bClientShutdown, true, memory_order_release); shutdown(g_sockfd, SHUT_RDWR); pthread_cancel(g_readThread); pthread_cancel(g_writeThread); if (0 <= g_sockfd) { close(g_sockfd); g_sockfd = -1; } TEST_PRINT(EN_TRACE, "out"); return; } /*! \fn void handle_signal(int sig) \brief signal processing function \param sig: Signal value */ void handle_signal(int sig) { TEST_PRINT(EN_TRACE, "in"); TEST_PRINT(EN_MSG, "Received signal %d, exiting...", sig); cleanup_client(); TEST_PRINT(EN_TRACE, "out"); } /*! \fn void* read_thread(void* arg) \brief Receive server messages \param arg: unused \return NULL */ void* read_thread(void* arg) { TEST_PRINT(EN_TRACE, "in"); ChatMessage chatMsg = {0}; int recvLen = 0; (void)arg; while (atomic_load_explicit(&g_bRunning, memory_order_acquire) && !atomic_load_explicit(&g_bClientShutdown, memory_order_acquire)) { pthread_testcancel(); recvLen = recv(g_sockfd, &chatMsg, sizeof(ChatMessage), 0); if (0 < recvLen) { TEST_PRINT(EN_MSG, "\n[%s]:\n%s", chatMsg.sSender,chatMsg.sContent); fflush(stdout); } else if (0 == recvLen) { TEST_PRINT(EN_MSG, "Server disconnected"); atomic_store_explicit(&g_bRunning, false, memory_order_release); } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { TEST_PRINT(EN_ERROR, "Recv error: %s", strerror(errno)); atomic_store_explicit(&g_bRunning, false, memory_order_release); } } } TEST_PRINT(EN_TRACE, "out"); return NULL; } /*! \fn void* write_thread(void* arg) \brief Read user input \param arg: unused \return NULL */ void* write_thread(void* arg) { TEST_PRINT(EN_TRACE, "in"); int iChoice = 0; ChatMessage chatMsg = {0}; (void)arg; while (atomic_load_explicit(&g_bRunning, memory_order_acquire) && !atomic_load_explicit(&g_bClientShutdown, memory_order_acquire)) { pthread_testcancel(); TEST_PRINT(EN_MSG, "\n==== Chat menu ====\n"); TEST_PRINT(EN_MSG, "\n1. Private chat\n2. Group chat\n3. Online users\n4. Exit"); fflush(stdout); scanf("%d", &iChoice); getchar(); switch (iChoice) { case 1: chatMsg.type = MSG_TEXT; strncpy(chatMsg.sSender, g_sClientName, MAX_NAME_LEN); chatMsg.sSender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Please enter the recipient's username: "); fgets(chatMsg.sReceiver, MAX_NAME_LEN, stdin); chatMsg.sReceiver[strcspn(chatMsg.sReceiver, "\n")] = '\0'; TEST_PRINT(EN_MSG, "Input message: "); fgets(chatMsg.sContent, MAX_MSG_LEN, stdin); chatMsg.sContent[strcspn(chatMsg.sContent, "\n")] = '\0'; chatMsg.type = MSG_TEXT; break; case 2: chatMsg.type = MSG_GROUP; strncpy(chatMsg.sSender, g_sClientName, MAX_NAME_LEN); chatMsg.sSender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Enter group messaging: "); fgets(chatMsg.sContent, MAX_MSG_LEN, stdin); chatMsg.sContent[strcspn(chatMsg.sContent, "\n")] = '\0'; strncpy(chatMsg.sReceiver, "ALL", MAX_NAME_LEN); chatMsg.sReceiver[MAX_NAME_LEN - 1] = '\0'; break; case 3: chatMsg.type = MSG_LIST; break; case 4: atomic_store_explicit(&g_bRunning, false, memory_order_release); goto end; default: TEST_PRINT(EN_ERROR, "invalid Choice"); } TEST_PRINT(EN_DEBUG, "send msg"); if (0 > send(g_sockfd, &chatMsg, sizeof(ChatMessage), 0)) { TEST_PRINT(EN_ERROR, "Send failed: %s", strerror(errno)); goto end; } } end: TEST_PRINT(EN_TRACE, "out"); return NULL; } /*! \fn bool client_login() \brief client login function \return login success : true; failed : false */ bool client_login() { int iChoice = 0; ChatMessage chatMsg = {0}; bool bRes = false; while (!atomic_load_explicit(&g_bClientShutdown, memory_order_acquire)) { start: TEST_PRINT(EN_MSG, "\n1. Login in\n2. Register\n3. Exit"); fflush(stdout); scanf("%d", &iChoice); getchar(); switch (iChoice) { case 1: chatMsg.type = MSG_LOGIN; TEST_PRINT(EN_MSG, "Enter user name"); fflush(stdout); fgets(chatMsg.sSender, MAX_NAME_LEN, stdin); chatMsg.sSender[strcspn(chatMsg.sSender, "\n")] = '\0'; TEST_PRINT(EN_DEBUG, "User name %s", chatMsg.sSender); TEST_PRINT(EN_MSG, "Enter user password"); fflush(stdout); fgets(chatMsg.sContent, MAX_PASSWORD_LEN, stdin); send(g_sockfd, &chatMsg, sizeof(ChatMessage), 0); break; case 2: chatMsg.type = MSG_REGIST; TEST_PRINT(EN_MSG, "Enter user name"); fflush(stdout); fgets(chatMsg.sSender, MAX_NAME_LEN, stdin); chatMsg.sSender[strcspn(chatMsg.sSender, "\n")] = '\0'; TEST_PRINT(EN_MSG, "Enter user password"); fflush(stdout); fgets(chatMsg.sContent, MAX_PASSWORD_LEN, stdin); chatMsg.sSender[strcspn(chatMsg.sContent, "\n")] = '\0'; send(g_sockfd, &chatMsg, sizeof(ChatMessage), 0); break; case 3: bRes = false; goto end; default: TEST_PRINT(EN_ERROR, "invalid Choice"); goto start; } int recvLen = recv(g_sockfd, &chatMsg, sizeof(ChatMessage), 0); if (0 < recvLen) { TEST_PRINT(EN_MSG, "\n[%s]:\n%s", chatMsg.sSender,chatMsg.sContent); fflush(stdout); } else if (0 == recvLen) { TEST_PRINT(EN_MSG, "Server disconnected"); bRes = false; goto end; } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { TEST_PRINT(EN_ERROR, "Recv error: %s", strerror(errno)); bRes = false; goto end; } } if (0 == strncmp(chatMsg.sContent, "Login success", MAX_MSG_LEN)) { strncpy(g_sClientName, chatMsg.sReceiver, MAX_NAME_LEN); g_sClientName[MAX_NAME_LEN - 1] = '\0'; bRes = true; break; } } end: TEST_PRINT(EN_TRACE, "out"); return bRes; } int main(int argc, char* argv[]) { TEST_PRINT(EN_TRACE, "in"); struct timeval tv = {0}; struct sockaddr_in servAddr = {0}; signal(SIGINT, handle_signal); signal(SIGTSTP, handle_signal); if (2 != argc) { TEST_PRINT(EN_ERROR, "Usage: %s <server_ip>\n", argv[0]); return EXIT_FAILURE; } g_sockfd = socket(AF_INET, SOCK_STREAM, 0); if (0 > g_sockfd) { TEST_PRINT(EN_ERROR, "Socket creation failed"); return EXIT_FAILURE; } tv.tv_sec = TIMEOUT_SEC; tv.tv_usec = TIMEOUT_USEC; setsockopt(g_sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(g_sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(SERVER_PORT); if (0 >= inet_pton(AF_INET, argv[1], &servAddr.sin_addr)) { TEST_PRINT(EN_ERROR, "Invalid address"); goto end; } if (0 > connect(g_sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr))) { TEST_PRINT(EN_ERROR, "Connection failed: %s", strerror(errno)); goto end; } TEST_PRINT(EN_MSG, "Connected to %s:%d", argv[1], SERVER_PORT); if (!client_login()) { TEST_PRINT(EN_ERROR, "Client login failed"); goto end; } if (0 != pthread_create(&g_readThread, NULL, read_thread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to create read thread"); goto end; } if (0 != pthread_create(&g_writeThread, NULL, write_thread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to create write thread"); goto end; } if (0 != pthread_join(g_readThread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to join read thread"); goto end; } if (0 != pthread_join(g_writeThread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to join write thread"); goto end; } end: cleanup_client(); TEST_PRINT(EN_TRACE, "out"); return EXIT_SUCCESS; } 在main()函数结束后,会cleanup_client();在cleanup_client();中有线程取消操作,这样有问题吗?
最新发布
08-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值