linux小实验(1)---线程
2013-09-13 11:25
357 查看
(1)、线程会随创建它的进程死掉而死掉
(2)、线程不会随创建它的线程死掉而死掉
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void *thread(void *); int main() { pthread_t t_a; pthread_t t_b; pthread_create(&t_a,NULL,thread,(void *)1); pthread_create(&t_b,NULL,thread,(void *)2); printf("main process is teminated!\n"); exit(0); } void *thread(void *junk) { int i=1; for(i=1;i<=4;i++) { printf("thread %d ...\n",(int)junk); sleep(1); } pthread_exit(0); }
(2)、线程不会随创建它的线程死掉而死掉
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> void *thread_parent(void *); void *thread_child(void *); int main() { pthread_t t; void* ret; pthread_create(&t,NULL,thread_parent,(void *)0); pthread_join(t,&ret); printf("parent thread process is terminated.\n"); sleep(10); printf("main process is teminated!\n"); exit(0); } void *thread_parent(void *para) { pthread_t t1,t2; pthread_create(&t1,NULL,thread_child,(void *)1); pthread_create(&t2,NULL,thread_child,(void *)2); pthread_exit(0); } void *thread_child(void *para) { int i=1; for(i=1;i<=4;i++) { printf("child thread %d...\n",(int)para); sleep(1); } printf("child thread %d t is terminated!\n",(int)para); pthread_exit(0); }
相关文章推荐
- linux小实验(4)---线程取消
- 2.3 linux实现服务端与多个客户端间通讯-线程Socket-pthread的方法(实验一)
- 【Linux之路】进程与线程(实验)
- Linux进程线程实验
- linux中线程的基本相关知识
- Linux存储实验五:Samba服务共享文件权限配置
- linux线程退出时执行的程序(线程清理处理程序)简单例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算
- Linux--线程【菜鸟学习日记】
- ARM linux线程相关API
- Linux进程与线程的区别
- Linux 线程实现机制分析
- Linux 结束线程
- linux线程6(CSDN论坛上很火的一个讨论贴)
- Linux实验2
- 进程及线程管理实验2
- Linux 与Windows Ipsec连接实验
- Linux实验报告-源代码编译安装Apache(Tarball文件安装)
- linux线程的实现