yum install gdb Downloading Packages: gdb-7.2-90.el6.i686.rpm | 2.3 MB 02:25 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : gdb-7.2-90.el6.i686 1/1 Verifying : gdb-7.2-90.el6.i686 1/1 Installed: gdb.i686 0:7.2-90.el6 Complete! root@bogon 20160729]# cc -g 0.c -o 0 [root@bogon 20160729]# gdb 0 #################################################################### fun() { int i=0; for(;i<10;i++) { printf("i = %d \n",i) ; } } int main() { fun(); return 0; } break 3 break fun r n c p i printf i bt <--------------------- 查看函数堆栈 finish <--------------------- 退出函数 q quit ###################################### g++ -o m *.cpp *.h -lpthread 文件见下方 (gdb) set detach-on-fork off (gdb) show detach-on-fork Whether gdb will detach the child of a fork is off. (gdb) catch fork Catchpoint 1 (fork) (gdb) r Starting program: /home/sts/Desktop/20160728/m [Thread debugging using libthread_db enabled] [New Thread 0xb7feeb70 (LWP 11752)] put:2 [New Thread 0xb75edb70 (LWP 11753)] put:3 [New Thread 0xb6becb70 (LWP 11754)] get:2 [New Thread 0xb61ebb70 (LWP 11755)] get:0 put:5 put:7 get:0 get:0 put:9 put:11 get:0 get:2 put:3 put:5 get:0 get:0 put:7 put:9 get:0 get:0 put:11 put:3 get:2 get:0 put:5 put:7 get:0 (gdb) cont Continuing. put:11 put:3 get:2 get:0 put:5 put:7 get:0 get:0 ^C Program received signal SIGINT, Interrupt. 0x00110424 in __kernel_vsyscall () (gdb) info inferiors Num Description Executable * 1 process 11787 /home/sts/Desktop/20160728/m (gdb) inferior 1 [Switching to inferior 1 [process 11787] (/home/sts/Desktop/20160728/m)] [Switching to thread 1 (Thread 0xb7ff06d0 (LWP 11787))] #0 0x00110424 in __kernel_vsyscall () (gdb) #include <stdio.h> #include <pthread.h> #include <iostream> #include <unistd.h> using namespace std; /* 同步关系 多生产和多消费者三种关系 加代码 生产方面 位置共用 消费方面 位置共用 */ int ss[10]; int i=1; int j=1; void*s(void) { while(i++) { ss[i]=i; cout<<"put:"<<ss[i]<<endl; sleep(2); i=i%10+1; } } void*x(void) { while(j++) { cout<<"get:"<<ss[j]<<endl; j=j%10+1; sleep(2); } } int main(void) { pthread_t ids1,ids2,idx1,idx2; pthread_create(&ids1,NULL,(void* (*)(void*))s,NULL); pthread_create(&ids2,NULL,(void* (*)(void*))s,NULL); pthread_create(&idx1,NULL,(void* (*)(void*))x,NULL); pthread_create(&idx2,NULL,(void* (*)(void*))x,NULL); pthread_join(ids1,NULL); pthread_join(ids2,NULL); pthread_join(idx1,NULL); pthread_join(idx2,NULL); return (0); } //g++ -o m *.cpp *.h -lpthread
http://blog.csdn.net/pbymw8iwm/article/details/7876797