如何终止正在等待信号量操作的线程

编程入门 行业动态 更新时间:2024-10-24 22:21:06
本文介绍了如何终止正在等待信号量操作的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个程序,该程序使用ipc的共享内存和信号灯.有一个主服务器进程创建共享内存和信号灯.任何数量的客户端进程都可以连接到共享内存,并在允许时对其进行读写.信号量提供了阻止机制来控制读取和写入.一切正常,除非我尝试终止客户端.用于访问共享内存的信号量块在线程中,并且在进程终止时,我无法释放信号量块,因此线程可以正确退出.我将如何处理?这是针对Linux的.

I am writing a program that uses shared memory and semaphores for ipc. There is one main server process that creates the shared memory and semaphores. Any number of client processes can attach to the shared memory and read and write to it when allowed. The semaphores provide the blocking mechanism to control reads and writes. Everything works fine except when a I try to terminate a client. The semaphore block to access the shared memory is in a thread and on process termination I have no way to release the semaphore block so the thread exits correctly. How would I go about this? This is for Linux.

具体来说,有一个shm和两个sems.第一学期阻止写作,第二学期阻止阅读.当客户端有要写入的内容时,它将等待写入sem为0,然后将其设置为1,进行写入,然后将读取sem设置为0,这将释放等待的服务器以读取客户端写入的内容.读取后,服务器会将写入sem设置回0,并且该行中的下一个客户端开始写入.它挂在一个semop调用上,该调用在读取sem为0时释放.此semop调用在线程中,我需要弄清楚如何在退出主线程之前正确退出该线程.

To be specific, there is one shm and two sems. The first sem blocks writing, and the second blocks reading. When a client has something to write, it waits for the write sem to be 0, then sets it to 1, writes, then sets the read sem to 0 which releases the waiting server to read what the client wrote. once read the server sets the write sem back to 0 and the next client in line gets to write. It hangs on a semop call which releases when read sem is 0. This semop call is in a thread and I need to figure out how to exit that thread correctly before letting the main thread terminate.

以下是我想做的但不起作用的示例(睡眠假装是挂断电话):

Here is an example of what i want to do but isn't working (the sleep is pretending to be the hanging semop call):

#include <stdlib.h> #include <errno.h> #include <pthread.h> #include <signal.h> #include <stdio.h> #include <unistd.h> void termination_handler (int signum) { printf( "Got Signal\n" ); } void *threadfunc( void *parm ) { struct sigaction action; action.sa_handler = termination_handler; sigemptyset( &action.sa_mask ); action.sa_flags = 0; sigaction( SIGUSR1, &action, NULL ); printf("Thread executing\n"); sleep( 100 ); // pretending to be the semaphore pthread_exit( NULL ); } int main() { int status; pthread_t threadid; int thread_stat; status = pthread_create( &threadid, NULL, threadfunc, NULL ); if ( status < 0) { perror("pthread_create failed"); exit(1); } sleep( 5 ); status = pthread_kill( threadid, SIGUSR1 ); if ( status < 0 ) perror("pthread_kill failed"); status = pthread_join( threadid, (void *)&thread_stat ); if ( status < 0 ) perror("pthread_join failed"); exit( 0 ); }

推荐答案

他说,这是针对Linux的.

He said, this is for Linux.

如果您可以确切地说出您的做法,这将非常有用.我假设您正在阻止sem_wait或sem_timedwait.如果您的线程在那阻塞,您想中断它,可以使用pthread_kill.

It would be useful if you could say exactly how are you doing it. I assume you are blocking in sem_wait or sem_timedwait. If your thread is blocking there and you want to interrupt it, you can use pthread_kill.

pthread_kill(blocking_thread_id, SIGUSR1);

当然,您需要设置适当的信号处理程序(man sigaction)以捕获SIGUSR1,并且需要检查sem_wait()的返回码以获取EINTR,在这种情况下,您可以知道自己所要做的一切被打断,没有锁.

Of course, you need to setup the proper signal handler (man sigaction) to catch SIGUSR1 and you need to check the return code of sem_wait() for EINTR, in which case you can do whatever you want to do knowing that you were interrupted and did not get the lock.

在使用进程的情况下,您将仅使用kill()而不使用pthread_kill()提供进程ID. (对不起,起初我误读并认为您正在使用线程)

In the case you are using processes you would use simply kill() and not pthread_kill() providing the process id. (sorry, initially I misread and thought you were using threads)

更多推荐

如何终止正在等待信号量操作的线程

本文发布于:2023-10-09 06:23:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号量   线程   操作   正在等待

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!