linux c的sleep 和多线程测试

简介:

 一直担心在线程里加sleep会引起进程所有的线程挂起,测试过了不会,这样可以用sleep来判断程序是否是多线程

 

 
  1. #include <pthread.h> 
  2. #include <stdio.h> 
  3. #include <sys/time.h> 
  4. #include <string.h> 
  5. #include <unistd.h> 
  6. #define MAX 10 
  7.  
  8. pthread_t thread[2]; 
  9. void * thread1() 
  10.     while(1)printf ("thread1 : I'm thread 1\n"); 
  11. void * thread2() 
  12. {  
  13.     printf("thread2 : I'm thread 2\n"); 
  14.     sleep(3); 
  15.     printf("thread2 : I'm thread 2 end\n"); 
  16.     exit(0); 
  17. void thread_create(void
  18. int temp; 
  19. memset(&thread, 0, sizeof(thread)); 
  20. if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) 
  21. printf("create thread 1 fail\n"); 
  22. else 
  23. printf("create thread 1 success\n"); 
  24. if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) 
  25. printf("create thread 2 fail\n"); 
  26. else 
  27. printf("create thread 2 success\n"); 
  28. void thread_wait(void
  29.     if(thread[0] !=0) { //comment4 
  30. pthread_join(thread[0],NULL); 
  31. printf("thread1 end\n"); 
  32. if(thread[1] !=0) { //comment5 
  33. pthread_join(thread[1],NULL); 
  34. printf("thread2 end\n"); 
  35. int main() 
  36. printf("i am main\n"); 
  37. thread_create(); 
  38. printf("mail waitin \n"); 
  39. thread_wait(); 
  40. return 0; 

      本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/603507,如需转载请自行联系原作者



相关文章
|
9月前
|
Linux Shell
linux自动崩溃,模拟测试
该脚本创建一个 systemd 服务和定时器,在系统启动3分钟后触发崩溃。通过向 /proc/sysrq-trigger 写入 &quot;c&quot; 来实现内核崩溃,用于测试系统崩溃后的恢复机制。
206 4
|
6月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
308 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
6月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
546 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
7月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
443 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
7月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
506 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
8月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
542 18
|
7月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
627 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
652 0
|
7月前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
789 0
|
7月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
276 0