简体   繁体   English

在ac程序中,整个过程是否在主线程终止时终止?

[英]In a c program, does the whole process terminates when the main thread terminate?

In linux, the whole process exits when the main thread terminates no matter how it terminates,by the function exit() or returns from main. 在linux中,无论主线程如何终止,整个过程都会通过函数exit()或从main返回而终止。 If the main thread returns from main(),it will return to the "C runtime" known as crt.o or something like that. 如果主线程从main()返回,它将返回到称为Crt.o或类似名称的“ C运行时”。 In the crt.o,whose c code like this: exit(main(argc, argv)); 在crt.o中,其C代码是这样的:exit(main(argc,argv)); exit() will be called by the main thread exit()将由主线程调用
eventually, as a result, all the threads terminate. 最终,所有线程终止。

Does my thought seem right? 我的想法似乎正确吗?

If in the crt.o exit() is replaced by a thread terminating function like void thread_exit(int),which can only terminates a thread with an exit status, the c source code of crt.o seems like thread_exit(main(argc,argv)),do other thread still run when the main thread terminates? 如果在crt.o中将exit()替换为诸如void thread_exit(int)的线程终止函数,该函数只能终止具有退出状态的线程,则crt.o的c源代码看起来像thread_exit(main(argc, argv)),当主线程终止时,其他线程是否仍在运行?

Returning from main is equivalent to calling exit , and terminates the process . main返回相当于调用exit ,并终止进程 To terminate just a single thread, use pthread_exit . 要仅终止单个线程,请使用pthread_exit Note that it's valid for the initial thread to call pthread_exit (and the process does not terminate until all threads have exited or until one of them calls exit ) and threads other than the initial thread implicitly call pthread_exit if you return from their start functions. 请注意,初始线程调用pthread_exit是有效的(并且直到所有线程退出或其中一个调用exit ,该过程才会终止),如果初始线程以外的其他线程从它们的启动函数返回,则隐式调用pthread_exit

on unix, a process terminates after the last thread has been terminated. 在Unix上,进程在最后一个线程终止后终止。 Note that this can be any thread, not just "main" thread. 请注意,这可以是任何线程,而不仅仅是“主”线程。 So, if you replace exit with pthread_exit , but spawned a thread before returning in main, your process will not terminate. 因此,如果将exit替换为pthread_exit ,但在返回main之前生成了一个线程,则您的进程不会终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM