Linux_多线程(进程与线程的联系_pthread库_线程创建_线程等待_线程正常终止_线程取消_线程分离_pthread_t与LWP)

1.线程的定义,进程和线程的关系

线程是“一个进程内部的控制序列”一切进程至少都有一个执行线程。

在这里插入图片描述
根据上图可知:

  1. 线程在进程内部运行,透过进程地址空间,可以看到进程的大部分资源
  2. 每个线程是进程内部的一个独立的执行分支.上图的1 2 3 号都是当前进程的执行流
  3. 进程是:task_struct +进程地址空间 + 页表(进程是承担系统分配资源的基本单位)。创建进程不仅要创建task_struct还要开辟进程地址空间,维护页表,申请系统资源等。
  4. 创建线程:创建task_struct,关联当前进程的进程地址空间即可
  5. 对于CPU来讲,不需要识别到底是进程还是线程。CPU通过调度独立task_struct即可。所以CPU调度的task_struct<OS原理上的进程控制块
  6. 进程是资源分配的基本单位,线程是调度的基本单位

在这里插入图片描述

2.Linux下的线程

Linux并没有为线程管理设计新的数据结构,而是复用了进程管理的数据结构,用进程模拟线程操作。

在Linux中,所有的可执行流都可以看作一个“轻量级进程”,只不过进程的地址空间等相关资源是直接关联当前进程的。
所以Linux是没有线程的系统调用,但是提供了例如vfork()函数这样的创建"轻量级进程"函数接口。

Linux原生线程库(pthread库pthread.h)

基于Linux下创建"轻量级"进程的系统调用,在用户层的角度上模拟实现的一套线程调用接口。称为pthread库

在链接库时需要使用编译器-lpthread选项

线程的优点

  1. 线程的创建与切换的成本比进程的创建与切换成本低(线程切换的是上下文信息,进程切换还要切换进程地址空间页表等成本高
  2. 可以将计算或IO任务分送给多个线程执行,提高效率

线程的私有数据

线程虽然共享进程的资源,但是同一个进程内不同的线程还是有自己的数据
eg:
1.每个线程都有自己的线程ID。
2.每个线程都有自己的独立的栈区。
3.每个线程都有自己的上下文信息(一组寄存器)
4.errno(C语言提供的存放错误码的全局变量)
5.调度优先级
6.信号屏蔽字

3.线程控制

①创建线程(pthread_create(pthread.h))

在这里插入图片描述
参数解释
thread:输出型参数,调用结束后会将线程ID输出
attr:创建线程的属性(线程栈大小等),一般默认为NULL
start_routine:函数指针,创建的线程要执行那个函数,这个函数为返回值为void参数为void
arg:给线程要执行的函数传入的参数,即要给start_routine所指向的函数指针传入的参数。没有则设置为NULL.
返回值:成功返回0,失败返回错误码,并且输出型参数thread值没有定义为随机值。

eg:

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  while(1)
  {
    
    
    printf("%s Pid:%d PPid:%d\n",meg,getpid(),getppid());
    sleep(1);
  }
}

int main()
{
    
    
  pthread_t tid;
  pthread_create(&tid,NULL,Func,(void*)"thread1");
  while(1)
  {
    
    
    printf("Main Thread Pid:%d PPid:%d\n",getpid(),getppid());
    sleep(2);
  }
  return 0;
}

在这里插入图片描述

ps -aL显示系统线程ID(LWP)

在这里插入图片描述
注意:LWP就称为轻量级进程ID,操作系统调度的基本单位

②线程显示自己的线程ID(pthread_self(pthread.h)用户级线程ID≠LWP)

在这里插入图片描述
返回线程ID(无符号长整型)

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  while(1)
  {
    
    
    printf("%s Pid:%d PPid:%d Tid:%lu\n",meg,getpid(),getppid(),pthread_self());
    sleep(1);
  }
}

int main()
{
    
    
  pthread_t tid[3];
  int i;
  char*str[3]={
    
    "thread1","thread2","pthread3"};
  for(i=0;i<3;i++)
  {
    
    
    pthread_create(&tid[i],NULL,Func,(void*)str[i]);
    printf("%s tid:%lu\n",str[i],tid[i]);
  }
  while(1)
  {
    
    
    printf("Main Thread Pid:%d PPid:%d Tid:%lu\n",getpid(),getppid(),pthread_self());
    sleep(2);
  }
  return 0;
}

在这里插入图片描述

③线程等待(pthread_join(pthread.h)阻塞形式等待)

当线程运行结束时,主线程没有等待线程,就会类似僵尸进程一样的错误。线程和进程一样也需要等待。
在这里插入图片描述
thread:要等待的线程ID。
retval:获取线程退出时的退出码,输出型参数,传入void*变量的地址。函数调用结束后变量的值就是线程退出码。强制转化为int打印
等待成功返回0,失败返回错误码。

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  int cout=3;
  while(cout>0)
  {
    
    
    printf("%s Pid:%d PPid:%d Tid:%lu\n",meg,getpid(),getppid(),pthread_self());
    sleep(1);
    cout--;
  }
  return (void*)15;
}

int main()
{
    
    
  pthread_t tid[3];
  int i;
  char*str[3]={
    
    "thread1","thread2","thread3"};
  for(i=0;i<3;i++)
  {
    
    
    pthread_create(&tid[i],NULL,Func,(void*)str[i]);
    printf("%s tid:%lu\n",str[i],tid[i]);
  }
  int j;
  for(j=0;j<3;j++)
  {
    
    
    void*ret=NULL;
    pthread_join(tid[j],&ret);
    printf("%s quit! Exit Code:%d Tid:%lu\n",str[j],(int)ret,tid[i]);
  }
  return 0;
}

在这里插入图片描述

④线程正常终止(pthread_exit(pthread.h))

注意:在main函数return代表主线程退出,整个进程退出,其他线程也退出

exit函数也代表进程退出,如果线程以exit退出,进程退出则其他线程也会退出

在这里插入图片描述

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  int cout=3;
  while(cout>0)
  {
    
    
    printf("%s Pid:%d PPid:%d Tid:%lu\n",meg,getpid(),getppid(),pthread_self());
    sleep(1);
    cout--;
  }
  pthread_exit((void*)15);
}

int main()
{
    
    
  pthread_t tid[3];
  int i;
  char*str[3]={
    
    "thread1","thread2","thread3"};
  for(i=0;i<3;i++)
  {
    
    
    pthread_create(&tid[i],NULL,Func,(void*)str[i]);
    printf("%s tid:%lu\n",str[i],tid[i]);
  }
  int j;
  for(j=0;j<3;j++)
  {
    
    
    void*ret=NULL;
    pthread_join(tid[j],&ret);
    printf("%s quit! Exit Code:%d Tid:%lu\n",str[j],(int)ret,tid[i]);
  }
  return 0;
}

在这里插入图片描述

⑤线程取消(取消目标线程 pthread_cancel(一般由主线程取消其他线程))

在这里插入图片描述
成功返回0 失败返回非零值
一个线程被取消,这个线程的退出码为-1。

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  int cout=3;
  while(cout>0)
  {
    
    
    printf("%s Pid:%d PPid:%d Tid:%lu\n",meg,getpid(),getppid(),pthread_self());
    sleep(1);
    cout--;
  }
  pthread_exit((void*)15);
}

int main()
{
    
    
  pthread_t tid[3];
  int i;
  char*str[3]={
    
    "thread1","thread2","thread3"};

  for(i=0;i<3;i++)
  {
    
    
    pthread_create(&tid[i],NULL,Func,(void*)str[i]);
    printf("%s tid:%lu\n",str[i],tid[i]);
  }
  pthread_cancel(tid[0]);
  pthread_cancel(tid[2]);
  int j;
  for(j=0;j<3;j++)
  {
    
    
    void*ret=NULL;
    pthread_join(tid[j],&ret);
    printf("%s quit! Exit Code:%d Tid:%lu\n",str[j],(int)ret,tid[i]);
  }
  return 0;
}

在这里插入图片描述
不推荐用新线程取消主线程,会发生未定义动作

4.线程分离(线程分离后不需要主线程等待pthread_detach(pthread.h))

在这里插入图片描述
不关系线程退出码时可以选择线程分离。线程分离后,线程退出信息由系统回收。主线程等待会报错

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  pthread_detach(pthread_self());
  char*meg=(char*)arg;
  int cout=3;
  while(cout>0)
  {
    
    
    printf("%s Pid:%d PPid:%d Tid:%lu\n",meg,getpid(),getppid(),pthread_self());
    sleep(1);
    cout--;
  }
  pthread_exit((void*)15);
}

int main()
{
    
    
  pthread_t tid[3];
  int i;
  char*str[3]={
    
    "thread1","thread2","thread3"};

  for(i=0;i<3;i++)
  {
    
    
    pthread_create(&tid[i],NULL,Func,(void*)str[i]);
    printf("%s tid:%lu\n",str[i],tid[i]);
  }
  while(1)
  {
    
    
    printf("Hello Linux\n");
    sleep(3);
  }
  return 0;
}

在这里插入图片描述

5.用户级线程ID(pthread_t)与LWP

pthread_t本质是一个地址

Linux没有真正的线程,系统只提供LWP,OS只对LWP这种内核执行流进行管理。上述的供用户使用的接口由pthread库来描述管理

pthread库为动态库,会被映射到进程地址空间堆栈之间的共享区中
Linux动静态库复习链接

在这里插入图片描述
所以线程切换时不会执行内核代码,而是通过pthread_t找到库中线程对应的位置,保存上下文信息进行线程切换。

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

void*Func(void*arg)
{
    
    
  char*meg=(char*)arg;
  int cout=3;
  while(cout>0)
  {
    
    
    printf("%s pthread_t %p\n",meg,pthread_self());
    sleep(1);
    cout--;
  }
  printf("End Pthread\n");
  pthread_exit((void*)15);
}

int main()
{
    
    
  pthread_t tid;
  pthread_create(&tid,NULL,Func,(void*)"Pthread1");
  pthread_join(tid,NULL);
  return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dodamce/article/details/121875340