关于僵尸进程和线程的问题
自从我阅读一些有关进程和线程的新主题以来,我脑海中浮现出这些问题。如果有人能帮助我,我会很高兴。
1) 如果一个线程被标记为不可取消,然后该进程在临界区内被终止,会发生什么?
2)我们的程序是否有操作系统已知的主线程?我的意思是操作系统是否给予程序的第一个线程一些有利的权利或其他什么?
3)当我们杀死一个进程并且线程没有加入时,它们会变成僵尸吗?
i had these questions in my mind since i was reading some new topics on processes and threads. I would be glad if somebody could help me out.
1) What happens if a thread is marked uncancelable, and then the process is killed inside of the critical section?
2) Do we have a main thread for the program that is known to the operating system? i mean does the operating system give the first thread of the program some beneficial rights or something?
3) When we kill a process and the threads are not joind, do they become zombies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,不要杀死或取消线程,让它们自杀。如果你从外部杀死一个线程,你永远不知道你会留下什么副作用——变量、同步原语的状态等等。如果您发现一个线程有必要终止另一个线程,那么让有问题的线程检查开关、捕获信号等,并在退出之前清理其状态。
1) 如果不可取消指的是分离的,则与连接的线程相同。如果你盲目地杀掉它,你不知道你会留下什么烂摊子。
2)从应用程序级别的角度来看,最重要的是,如果主线程 exits() 或 returns() ,它将关闭所有其他线程。如果主线程通过 pthread_exit() 自行终止,则剩余线程将继续运行。
3) 与进程非常相似,线程将保留一些资源,直到它被收获(加入)或程序结束,除非它作为分离运行。
RE 注意:线程不共享堆栈,它们各自拥有自己的堆栈。有关线程创建的一些信息,请参阅 clone() 。
First, don't kill or cancel threads, ask them to kill themselves. If you kill a thread from outside you never know what side effects - variables, state of synchronization primitives, etc.- you leave behind. If you find it necessary for one thread to terminate another then have the problematic thread check a switch, catch a signal, whatever, and clean up its state before exiting itself.
1) If by uncancelable you mean detached, the same as a joined thread. You don't know what mess you are leaving behind if you are blindly killing it.
2) From an application level viewpoint the primary thing is that if the main thread exits() or returns() it is going to take down all other threads with it. If the main thread terminates itself with pthread_exit() the remaining threads continue on.
3) Much like a process the thread will retain some resources until it is reaped (joined) or the program ends, unless it was run as detached.
RE Note: The threads don't share a stack they each have their own. See clone() for some info on thread creation.