call exit and _exit in pthread



在pthread里call这两个函数,行为很怪异,下面慢慢道来
今天出了一个core,gdb一看这个core是由exit调用引起的,该exit是在一个pthread中调用的,于是查了一些资料,写了一些测试例子,但还是不能很圆满地解释,因此发帖询问。
问题描述:在线程中,先调用了printf打印一个字符串,由于异常情况,传入的指针在合法的范围内没有0,因此printf可能访问越界了, printf之后是一个exit函数,exit执行时程序core dump了,原因猜测可能是printf越界导致exit销毁资源时访问越界,导致了core。于是修改程序,将exit改成_exit,本意是不让做资 源销毁,直接退出,但奇怪的事情发生了,程序不能退出,猜测原因是因为thread是用process来实现的,_exit导致主线程不知道子线程已经退 出,所以对主线程没有影响,程序不能退出。因此再写了一个简单程序测试exit的行为,代码大概如下:

void * thfn(void* para){
printf("exit ................./n " );
}

int main(){
for(; ; ){
pthread_t tid;
pthread_create(&tid,NULL,thfn,NULL)
}
}

程序会打印好几个exit...........之后才会退出,估计是pthread库实现的时候注册了atexit函数来处理,该函数的处理会有一些延 迟,因此打印了多个eixt..........,而_exit没法处理,因此_exit会导致程序继续运行,是这样的吗?或者有更详细的解释,谢谢。
#8 688.7 + cd /tmp/build/opentelemetry-cpp-contrib-8933841f0a7f8737f61404cf0a64acf6b079c8a5/instrumentation/nginx #8 688.7 + mkdir -p build #8 688.7 + cd build #8 688.7 + cmake -DCMAKE_BUILD_TYPE=Release -G Ninja -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=/tmp/otel -DBUILD_SHARED_LIBS=ON -DNGINX_VERSION=1.25.5 .. #8 688.9 -- The C compiler identification is GNU 8.5.0 #8 689.0 -- The CXX compiler identification is GNU 8.5.0 #8 689.1 -- Detecting C compiler ABI info #8 689.2 -- Detecting C compiler ABI info - done #8 689.3 -- Check for working C compiler: /usr/bin/cc - skipped #8 689.3 -- Detecting C compile features #8 689.3 -- Detecting C compile features - done #8 689.3 -- Detecting CXX compiler ABI info #8 689.5 -- Detecting CXX compiler ABI info - done #8 689.5 -- Check for working CXX compiler: /usr/bin/c++ - skipped #8 689.5 -- Detecting CXX compile features #8 689.5 -- Detecting CXX compile features - done #8 689.5 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD #8 689.7 -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed #8 689.7 -- Looking for pthread_create in pthreads #8 689.7 -- Looking for pthread_create in pthreads - not found #8 689.7 -- Looking for pthread_create in pthread #8 689.9 -- Looking for pthread_create in pthread - found #8 689.9 -- Found Threads: TRUE #8 689.9 CMake Error at /usr/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package): #8 689.9 Found package configuration file: #8 689.9 #8 689.9 /usr/local/lib/cmake/grpc/gRPCConfig.cmake #8 689.9 #8 689.9 but it set gRPC_FOUND to FALSE so package "gRPC" is considered to be NOT #8 689.9 FOUND. Reason given by package: #8 689.9 #8 689.9 The following imported targets are referenced, but are missing: #8 689.9 protobuf::libprotobuf protobuf::libprotoc #8 689.9 #8 689.9 Call Stack (most recent call first): #8 689.9 /usr/lib64/cmake/opentelemetry-cpp/opentelemetry-cpp-config.cmake:105 (find_dependency) #8 689.9 CMakeLists.txt:5 (find_package) #8 689.9 #8 689.9 #8 689.9 -- Configuring incomplete, errors occurred! #8 ERROR: process "/bin/sh -c /build.sh" did not complete successfully: exit code: 1
最新发布
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值