进程
进程有独立的地址空间
Linux为每个进程创建task_struct
每个进程都参与内核调度,互不影响
线程
进程在切换时系统开销大
很多操作系统引入了轻量级进程LWP
同一进程中的线程共享相同地址空间
Linux不区分进程、线程
线程特点
通常线程指的是共享相同地址
空间的多个任务 使用多线程的好处
大大提高了任务切换的效率
避免了额外的TLB & cache的刷新
线程共享资源
一个进程中的多个线程共享以下资源: 可执行的指令、 静态数据 、进程中打开的文件描述符 、当前工作目录 用户ID、 用户组I。
线程私有资源
每个线程私有的资源包括: 线程ID (TID) 、PC(程序计数器)和相关寄存器 、堆栈 、错误号 (errno) 、优先级 、执行状态和属性。
Linux线程库
pthread线程库中提供了如下基本操作
1、创建线程2、 回收线程 3、结束线程
同步和互斥机制
信号量、互斥锁
线程创建 – pthread_create
#include <pthread.h>
int pthread_create(pthread_t *thread, const
pthread_attr_t *attr, void *(*routine)(void *), void *arg);
成功返回0,失败时返回错误码
thread 线程对象 attr 线程属性,NULL代表默认属性
routine 线程执行的函数
arg 传递给routine的参数 ,参数是void * ,注意传递参数格式,
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *testThread(void *arg){
printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
// return NULL;
printf("input arg=%d\n",*(int*)arg);
pthread_exit(NULL);
printf("after pthread exit\n");
}
int main(){
pthread_t tid;
int ret;
int arg = 5;
ret = pthread_create(&tid,NULL,testThread,(void *)&arg);
printf("This is main thread,tid=%lu\n",tid);
sleep(1);
}
编译错误分析:
1.
createP_t.c:14:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
ret = pthread_create(&tid,NULL,testThread,NULL); ^
In file included from createP_t.c:1:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int * (*)(char *)’
意义:表示pthread_create参数3的定义和实际代码不符合,期望的是void * (*)(void *) ,实际的代码是int * (*)(char *)
解决方法:改为pthread_create(&tid,NULL,(void*)testThread,NULL);
2.
createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status --------这个链接错误,
表示pthread_create这个函数没有实现
解决方法:编译时候加 -lpthread
注意事项:1. 主进程的退出,它创建的线程也会退出。
线程创建需要时间,如果主进程马上退出,那线程不能得到执行
获取线程的id
通过pthread_create函数的第一个参数;通过在线程里面调用pthread_self函数
线程结束 – pthread_exit
#include <pthread.h>
void pthread_exit(void *retval);
结束当前线程
retval可被其他线程通过pthread_join获取
线程私有资源被释放
线程查看tid函数
pthread_t pthread_self(void) 查看自己的TID
#include <pthread.h>
pthread_t pthread_self(void);
线程间参数传递
pthread_create(pthread_t *thread, const
pthread_attr_t *attr, void *(*routine)(void *), void *arg);
最后一个参数
线程间参数传递:(重点难点)
编译错误:
createP_t.c:8:34: warning: dereferencing ‘void *’ pointer
printf("input arg=%d\n",(int)*arg);
^
createP_t.c:8:5: error: invalid use of void expression
printf("input arg=%d\n",(int)*arg);
错误原因是void *类型指针不能直接用*取值(*arg),因为编译不知道数据类型。
解决方法:转换为指定的指针类型后再用*取值 比如:*(int *)arg
- 通过地址传递参数,注意类型的转换
- 值传递,这时候编译器会告警,需要程序员自己保证数据长度正确
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *testThread(void *arg){
printf("This is a thread test,pid=%d,tid=%lu\n",getpid(),pthread_self());
// return NULL;
printf("This is %d thread.\n", (int)arg);
printf("This is %d thread.\n", *(int*)&i);//传地址主线程需要休眠,因为取值时间较长
// pthread_exit(NULL);
while(1){
sleep(1);
}
printf("after pthread exit\n");
}
int main(){
pthread_t tid[5];
int ret;
int arg = 5;
int i;
for(i=0;i<5;i++){
ret = pthread_create(&tid[i],NULL,testThread,(void *)i);
//ret = pthread_create(&tid[5],NULL,testThread,(void *)i);出现数组越界
// sleep(1);
printf("This is main thread,tid=%lu\n",tid[i]);
}
while(1){
sleep(1);
}
}
运行错误:
*** stack smashing detected ***: ./mthread_t terminated
已放弃 (核心已转储)
原因:栈被破坏了(数组越界)