介绍
这篇文章主要讲解了linux模拟多线程崩溃和多进程崩溃的代码解析,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
结论是:
多线程下如果其中一个线程崩溃了会导致其他线程(整个进程)都崩溃;
多进程下如果其中一个进程崩溃了对其余进程没有影响;
多线程
# include & lt; stdio.h> # include & lt; string.h> # include & lt; stdlib.h> # include & lt; pthread.h> # include & lt; assert.h> void * fun1 (void *参数) { printf (“fun1输入\ n"); 而(1) { printf (“% s \ n" __FUNCTION__); usleep (1000 * 1000); } printf (“fun1退出\ n"); 返回((void *) 1); } void * fun2 (void *参数) { printf (“fun1输入\ n"); usleep (1000 * 3000); char * ptr=(char *) malloc (sizeof (char)); printf (“ptr1: 0 x % \ n" (ptr); ptr=零; printf (“ptr2: 0 x % \ n" (ptr); 免费(ptr); memcpy(简称:“123“,3); printf (“ptr3: 0 x % \ n" (ptr); printf (“fun2退出\ n"); 返回((void *) 2); } int主要(空白) { pthread_t tid1 tid2; int犯错; 呃=pthread_create(和tid1, NULL, fun1, NULL); 断言(0==犯错); 呃=pthread_create(和tid2, NULL, fun2, NULL); 断言(0==犯错); printf(“主要加入…\ n");//获取字符(); pthread_join (tid1, NULL); pthread_join (tid2, NULL); 返回0; }
多进程
# include & lt; stdio.h> # include & lt; string.h> # include & lt; stdlib.h> # include & lt; pthread.h> # include & lt; assert.h> 无效的乐趣(void *参数) { printf (“fun1输入\ n"); usleep (1000 * 3000); char * ptr=(char *) malloc (sizeof (char)); printf (“ptr1: 0 x % \ n" (ptr); ptr=零; printf (“ptr2: 0 x % \ n" (ptr); 免费(ptr); memcpy(简称:“123“,3); printf (“ptr3: 0 x % \ n" (ptr); printf (“fun2退出\ n"); 返回; } int主要(int命令行参数个数,char * argv []) { 维护(2==命令行参数个数); pid_t pid; int我; (我=0;i看完上述内容,是不是对linux模拟多线程崩溃和多进程崩溃的代码解析有进一步的了解,如果还想学习更多内容,欢迎关注行业资讯频道。
linux模拟多线程崩溃和多进程崩溃的代码解析