山东省和城乡建设厅网站摄影网站建设任务书
山东省和城乡建设厅网站,摄影网站建设任务书,网站制作企业有哪些,wordpress作者墙主题线程的概念 进程与线程内核实现 通过函数clone实现的
ps -Lf pidLinux内核线程实现原理 同一个进程下的线程#xff0c;共享该进程的内存区#xff0c; 但是只有stack区域不共享。 线程共享资源 a.文件描述符表 b.每种信号的处理方式 c.当前工作目录 d.用户id和组id 线程…线程的概念 进程与线程内核实现 通过函数clone实现的
ps -Lf pidLinux内核线程实现原理 同一个进程下的线程共享该进程的内存区 但是只有stack区域不共享。 线程共享资源 a.文件描述符表 b.每种信号的处理方式 c.当前工作目录 d.用户id和组id 线程非共享资源 a.线程id b.处理器现场和栈指针内核栈 c.独立的栈空间用户空间栈 d.errno变量 e.信号屏蔽字 f.调度优先级 在主线程里面执行return 相当于整个进程退出了 小技巧 set -o vi 相当于把当前shell弄成了 vi 编辑器模式
7.创建一个线程 man pthread_create #include pthread.hint pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread.#include pthread.h
#include unistd.h
#include stdio.hvoid* func(void *arg) {printf(I am a common thread, pid is %d, tid is %ld\n, getpid(), pthread_self());pthread_exit(NULL);
}int main() {pthread_t tid;pthread_create(tid, NULL, func, NULL);printf(I am a man thread, pid is %d,create tid is %ld\n, getpid(), tid);printf(I am a man thread, pid is %d, tid is %ld\n, getpid(), pthread_self());pthread_exit(NULL);return 0;
}经测试主线程使用pthread_exit函数可以等待子线程的退出。线程退出函数 8.线程回收函数
int pthread_join(pthread_t thread, void **retval);
参数介绍thread: 表示要回收的线程创建线程时候传出的第一个参数retval要回收的线程的退出信息线程回收也是阻塞等待回收 代码案例
#include pthread.h
#include unistd.h
#include stdio.hvoid* func(void *arg) {printf(I am a common thread, pid is %d, tid is %ld\n, getpid(), pthread_self());// pthread_exit((void *)100);return (void*)(100);
}int main() {pthread_t tid;pthread_create(tid, NULL, func, NULL);printf(I am a man thread, pid is %d,create tid is %ld\n, getpid(), tid);printf(I am a man thread, pid is %d, tid is %ld\n, getpid(), pthread_self());void * ret;pthread_join((tid), ret);printf(join tid return value is %d\n, (int)ret);return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90481.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!