- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
pstack命令是一个在Linux系统中用于查看进程堆栈信息的工具.
写了一个服务端死锁程序,如下:
#include <iostream> #include <thread> #include <mutex> #include <chrono> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <cstring> // 定义两个互斥锁 std::mutex mutex1; std::mutex mutex2; // 处理客户端请求的函数 void handle_client(int client_socket, int client_id) { std::cout << "Client " << client_id << ": Connected" << std::endl; // 模拟客户端请求处理 if (client_id == 1) { // 客户端 1:先获取 mutex1,再获取 mutex2 std::cout << "Client " << client_id << ": Trying to lock mutex1..." << std::endl; std::lock_guard<std::mutex> lock1(mutex1); std::this_thread::sleep_for(std::chrono::seconds(5)); // 增加锁的持有时间 std::cout << "Client " << client_id << ": Locked mutex1, now trying to lock mutex2..." << std::endl; // 尝试获取 mutex2 std::lock_guard<std::mutex> lock2(mutex2); // 死锁发生点 std::this_thread::sleep_for(std::chrono::seconds(5)); // 模拟更多工作 std::cout << "Client " << client_id << ": Locked both mutex1 and mutex2" << std::endl; } else if (client_id == 2) { // 客户端 2:先获取 mutex2,再获取 mutex1 std::cout << "Client " << client_id << ": Trying to lock mutex2..." << std::endl; std::lock_guard<std::mutex> lock2(mutex2); std::this_thread::sleep_for(std::chrono::seconds(5)); // 增加锁的持有时间 std::cout << "Client " << client_id << ": Locked mutex2, now trying to lock mutex1..." << std::endl; // 尝试获取 mutex1 std::lock_guard<std::mutex> lock1(mutex1); // 死锁发生点 std::this_thread::sleep_for(std::chrono::seconds(5)); // 模拟更多工作 std::cout << "Client " << client_id << ": Locked both mutex1 and mutex2" << std::endl; } // 关闭客户端连接 close(client_socket); std::cout << "Client " << client_id << ": Disconnected" << std::endl; } // TCP 服务器主函数 void start_server(int port) { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); // 创建 socket 文件描述符 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // 设置 SO_REUSEADDR 选项 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt failed"); exit(EXIT_FAILURE); } // 绑定 socket 到指定端口 address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(port); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // 监听连接 if (listen(server_fd, 3) < 0) { perror("listen failed"); exit(EXIT_FAILURE); } std::cout << "Server started on port " << port << ". Waiting for connections..." << std::endl; int client_id = 1; // 用于区分不同客户端 while (true) { // 接受新的客户端连接 if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept failed"); continue; } // 为每个客户端创建一个新线程 std::thread client_thread(handle_client, new_socket, client_id++); client_thread.detach(); // 分离线程,允许其独立运行 } } int main() { int port = 8080; start_server(port); return 0; }
编译:g++ -std=c++11 -pthread -o tcp_deadlock_server tcp_deadlock_server.cpp -g 。
用telnet(telnet 127.1 8080)连两次就会死锁,服务端输出如下: 。
Server started on port 8080. Waiting for connections... Client 1: Connected Client 1: Trying to lock mutex1... Client 2: Connected Client 2: Trying to lock mutex2... Client 1: Locked mutex1, now trying to lock mutex2... Client 2: Locked mutex2, now trying to lock mutex1... 。
ps查看进程ID,然后pstack + 进程ID : pstack 915 > pstack_out,将输出重定向到文件,好看一些:
Thread 3 (LWP 919): #0 0x0000fffcc23821dc in ?? () from /lib64/libpthread.so.0 #1 0x0000fffcc237b060 in pthread_mutex_lock () from /lib64/libpthread.so.0 #2 0x00000000004012c4 in __gthread_mutex_lock (__mutex=0x420240 <mutex1>) at /usr/include/c++/7.3.0/aarch64-linux-gnu/bits/gthr-default.h:748 #3 0x0000000000401a88 in std::mutex::lock (this=0x420240 <mutex1>) at /usr/include/c++/7.3.0/bits/std_mutex.h:103 #4 0x0000000000401b34 in std::lock_guard<std::mutex>::lock_guard (this=0xfffcc19ce810, __m=...) at /usr/include/c++/7.3.0/bits/std_mutex.h:162 #5 0x00000000004015ac in handle_client (client_socket=5, client_id=2) at tcp_deadlock_server.cpp:38 #6 0x0000000000402308 in std::__invoke_impl<void, void (*)(int, int), int, int> (__f=@0x248c23e0: 0x401310 <handle_client(int, int)>, __args#0=@0x248c23dc: 5, __args#1=@0x248c23d8: 2) at /usr/include/c++/7.3.0/bits/invoke.h:60 #7 0x0000000000401e18 in std::__invoke<void (*)(int, int), int, int> (__fn=@0x248c23e0: 0x401310 <handle_client(int, int)>, __args#0=@0x248c23dc: 5, __args#1=@0x248c23d8: 2) at /usr/include/c++/7.3.0/bits/invoke.h:95 #8 0x00000000004029cc in std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> >::_M_invoke<0ul, 1ul, 2ul> (this=0x248c23d8) at /usr/include/c++/7.3.0/thread:234 #9 0x0000000000402970 in std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> >::operator() (this=0x248c23d8) at /usr/include/c++/7.3.0/thread:243 #10 0x0000000000402950 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> > >::_M_run (this=0x248c23d0) at /usr/include/c++/7.3.0/thread:186 #11 0x0000fffcc257e134 in ?? () from /lib64/libstdc++.so.6 #12 0x0000fffcc23788cc in ?? () from /lib64/libpthread.so.0 #13 0x0000fffcc22ba1ec in ?? () from /lib64/libc.so.6 Thread 2 (LWP 917): #0 0x0000fffcc23821dc in ?? () from /lib64/libpthread.so.0 #1 0x0000fffcc237b060 in pthread_mutex_lock () from /lib64/libpthread.so.0 #2 0x00000000004012c4 in __gthread_mutex_lock (__mutex=0x420270 <mutex2>) at /usr/include/c++/7.3.0/aarch64-linux-gnu/bits/gthr-default.h:748 #3 0x0000000000401a88 in std::mutex::lock (this=0x420270 <mutex2>) at /usr/include/c++/7.3.0/bits/std_mutex.h:103 #4 0x0000000000401b34 in std::lock_guard<std::mutex>::lock_guard (this=0xfffcc21de820, __m=...) at /usr/include/c++/7.3.0/bits/std_mutex.h:162 #5 0x0000000000401450 in handle_client (client_socket=4, client_id=1) at tcp_deadlock_server.cpp:27 #6 0x0000000000402308 in std::__invoke_impl<void, void (*)(int, int), int, int> (__f=@0x248c2290: 0x401310 <handle_client(int, int)>, __args#0=@0x248c228c: 4, __args#1=@0x248c2288: 1) at /usr/include/c++/7.3.0/bits/invoke.h:60 #7 0x0000000000401e18 in std::__invoke<void (*)(int, int), int, int> (__fn=@0x248c2290: 0x401310 <handle_client(int, int)>, __args#0=@0x248c228c: 4, __args#1=@0x248c2288: 1) at /usr/include/c++/7.3.0/bits/invoke.h:95 #8 0x00000000004029cc in std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> >::_M_invoke<0ul, 1ul, 2ul> (this=0x248c2288) at /usr/include/c++/7.3.0/thread:234 #9 0x0000000000402970 in std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> >::operator() (this=0x248c2288) at /usr/include/c++/7.3.0/thread:243 #10 0x0000000000402950 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(int, int), int, int> > >::_M_run (this=0x248c2280) at /usr/include/c++/7.3.0/thread:186 #11 0x0000fffcc257e134 in ?? () from /lib64/libstdc++.so.6 #12 0x0000fffcc23788cc in ?? () from /lib64/libpthread.so.0 #13 0x0000fffcc22ba1ec in ?? () from /lib64/libc.so.6 Thread 1 (LWP 915): #0 0x0000fffcc23827c4 in accept () from /lib64/libpthread.so.0 #1 0x0000000000401868 in start_server (port=8080) at tcp_deadlock_server.cpp:89 #2 0x00000000004018f8 in main () at tcp_deadlock_server.cpp:102
能看到一共三个线程,Thread 3 (LWP 919)卡在pthread_mutex_lock:#5 0x00000000004015ac in handle_client (client_socket=5, client_id=2) at tcp_deadlock_server.cpp:38, 。
Thread 2 (LWP 917)卡在pthread_mutex_lock:#5 0x0000000000401450 in handle_client (client_socket=4, client_id=1) at tcp_deadlock_server.cpp:27 。
就发现了死锁在的位置 。
不用c++11又写了一个程序,如下:
#include <iostream> #include <cstring> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> #include <errno.h> #include <cstdlib> #include <fcntl.h> #include <stdio.h> // 定义两个互斥锁 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; // 处理客户端请求的函数 void* handle_client(void* arg) { int client_socket = *(static_cast<int*>(arg)); free(arg); // 释放传递给线程的动态分配的内存 std::cout << "Client connected with socket: " << client_socket << std::endl; // 模拟客户端请求处理 if (client_socket == 4) { // 客户端 1:先获取 mutex1,再获取 mutex2 std::cout << "Client " << client_socket << ": Trying to lock mutex1..." << std::endl; pthread_mutex_lock(&mutex1); usleep(5000000); // 休眠 5 秒 std::cout << "Client " << client_socket << ": Locked mutex1, now trying to lock mutex2..." << std::endl; // 尝试获取 mutex2 pthread_mutex_lock(&mutex2); // 死锁发生点 usleep(5000000); // 休眠 5 秒 std::cout << "Client " << client_socket << ": Locked both mutex1 and mutex2" << std::endl; // 释放互斥锁 pthread_mutex_unlock(&mutex2); pthread_mutex_unlock(&mutex1); } else if (client_socket == 5) { // 客户端 2:先获取 mutex2,再获取 mutex1 std::cout << "Client " << client_socket << ": Trying to lock mutex2..." << std::endl; pthread_mutex_lock(&mutex2); usleep(5000000); // 休眠 5 秒 std::cout << "Client " << client_socket << ": Locked mutex2, now trying to lock mutex1..." << std::endl; // 尝试获取 mutex1 pthread_mutex_lock(&mutex1); // 死锁发生点 usleep(5000000); // 休眠 5 秒 std::cout << "Client " << client_socket << ": Locked both mutex1 and mutex2" << std::endl; // 释放互斥锁 pthread_mutex_unlock(&mutex1); pthread_mutex_unlock(&mutex2); } // 关闭客户端连接 close(client_socket); std::cout << "Client disconnected with socket: " << client_socket << std::endl; pthread_exit(NULL); } // TCP 服务器主函数 void start_server(int port) { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); // 创建 socket 文件描述符 if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // 设置 SO_REUSEADDR 选项 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { perror("setsockopt failed"); exit(EXIT_FAILURE); } // 绑定 socket 到指定端口 address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(port); if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // 监听连接 if (listen(server_fd, 3) < 0) { perror("listen failed"); exit(EXIT_FAILURE); } std::cout << "Server started on port " << port << ". Waiting for connections..." << std::endl; int client_id = 1; // 用于区分不同客户端 while (true) { // 接受新的客户端连接 if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { perror("accept failed"); continue; } // 为每个客户端创建一个新线程 pthread_t thread; int* client_socket_ptr = new int(new_socket); // 动态分配存储套接字描述符的内存 if (pthread_create(&thread, NULL, handle_client, static_cast<void*>(client_socket_ptr)) != 0) { perror("pthread_create failed"); delete client_socket_ptr; // 如果线程创建失败,释放内存 close(new_socket); continue; } // 分离线程,允许其独立运行 pthread_detach(thread); // 为了测试死锁,只接受前两个客户端连接 if (client_id >= 3) { close(new_socket); // 关闭多余的连接 continue; } client_id++; } } int main() { int port = 8080; start_server(port); return 0; }
编译运行telnet测试跟上面一样 。
ps查看进程ID,然后gdb跟进程:gdb -p 11560 。
查看所有线程:info threads,进入线程:thread 2 ,然后bt查看线程堆栈,切换另一个线程如上,就能看到两个线程都卡在了lock,具体调试步骤如下:
(gdb) info threads 3 Thread 0x7fb6c0115700 (LWP 11562) 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 2 Thread 0x7fb6bf714700 (LWP 11564) 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 * 1 Thread 0x7fb6c0117720 (LWP 11560) 0x0000003b5200e7ed in accept () from /lib64/libpthread.so.0 (gdb) thread 2 [Switching to thread 2 (Thread 0x7fb6bf714700 (LWP 11564))]#0 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 (gdb) bt #0 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 #1 0x0000003b52009328 in _L_lock_854 () from /lib64/libpthread.so.0 #2 0x0000003b520091f7 in pthread_mutex_lock () from /lib64/libpthread.so.0 #3 0x0000000000400f9f in handle_client (arg=0x13a8010) at tcp_deadlock_server.cpp:48 #4 0x0000003b520077f1 in start_thread () from /lib64/libpthread.so.0 #5 0x0000003b51ce570d in clone () from /lib64/libc.so.6 (gdb) thread 3 [Switching to thread 3 (Thread 0x7fb6c0115700 (LWP 11562))]#0 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 (gdb) bt #0 0x0000003b5200dff4 in __lll_lock_wait () from /lib64/libpthread.so.0 #1 0x0000003b52009328 in _L_lock_854 () from /lib64/libpthread.so.0 #2 0x0000003b520091f7 in pthread_mutex_lock () from /lib64/libpthread.so.0 #3 0x0000000000400eb2 in handle_client (arg=0x13a8010) at tcp_deadlock_server.cpp:33 #4 0x0000003b520077f1 in start_thread () from /lib64/libpthread.so.0 #5 0x0000003b51ce570d in clone () from /lib64/libc.so.6
pstack和gdb都使用 ptrace() 系统调用来附着到目标进程。ptrace() 允许 GDB 暂停目标进程的执行,读取和修改其内存及寄存器,并捕获系统调用.
最后此篇关于c++死锁调试,gdbpstack的文章就讲到这里了,如果你想了解更多关于c++死锁调试,gdbpstack的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!