做效果图有哪些网站,湖南优度网络科技有限公司,小城天长网站建设,注册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;
}