Linux探秘坊-------15.线程概念与控制

发布于:2025-07-18 ⋅ 阅读:(15) ⋅ 点赞:(0)

1.线程概念

1.什么是线程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.线程 vs 进程

在这里插入图片描述
不同的操作系统有不同的实现方式:

  • linux :直接使用pcb的功能来模拟线程,不创建新的数据结构
  • windows: 使用新的数据结构TCB,来进行实现,一个PCB里有很多个TCB

3.资源划分

详情可见操作系统书籍中的存储器管理虚拟存储器管理章节!!!!

4.线程理解

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.线程和进程的区别

在这里插入图片描述

  • 黄字代表线程特有的私有数据
  • 一组寄存器和上下文数据---------------证明线程是可以被独立调用
  • 栈--------------证明线程是动态的

1.进程的线程共享!!!!!!!!!!

同⼀地址空间,因此 Text Segment 、 Data Segment 都是共享的,如果定义⼀个函数,在各线程中都可以调⽤ ,如果定义⼀个全局变量,在各线程中都可以访问到, 除此之外,各线程还共享以下进程资源和环境:

  • ⽂件描述符表 (fd)
  • 每种信号的处理⽅式(SIG_IGN、SIG_DFL或者⾃定义的信号处理函数)
  • 当前⼯作⽬录
  • ⽤⼾id和组id

1.父子进程只有代码段是共享的,但主线程和子线程连地址空间都是共享的,所以他们可以使用共享的函数和全局变量 (意思就是如果子线程的全局变量被修改了,主线程看到的是同一个全局变量,也会变化) 轻松实现类似进程间通信!!!!!!!
2.而全局变量在父子进程中是写实拷贝,子变父不变 !!!!!!!!!

在这里插入图片描述

3.linux的线程控制

1.线程创建

创建函数:
在这里插入图片描述
运行后使用 ps - aL指令查看线程
在这里插入图片描述

2.pthread库的引入----为什么需要有线程库?

在这里插入图片描述

4.pthread库的使用

  • 与线程有关的函数构成了⼀个完整的系列,绝⼤多数函数的名字都是“pthread_”打头的
    • 要使⽤这些函数库,要通过引⼊头⽂件 <pthread.h>
    链接这些线程函数库时要使⽤编译器命令的“-lpthread”选项

1.线程创建—pthread_create()

在这里插入图片描述

  • thread是新线程的标识符,是输出型参数(让主线程获取,便于调用其他函数)

2.线程等待—pthread_join()

在这里插入图片描述

  • 这里retval拿到的是子线程的退出码,即子线程函数的返回值,但返回值是void *
  • 所以retval的类型应当是void* 的地址类型即void**

在这里插入图片描述

  • 其中,routine是子线程的入口函数,routine函数结束的话子线程也就结束了

3.线程取消或终止—pthread_exit()/pthread_cancel()

1-----------------pthread_exit()
在这里插入图片描述
2-----------------pthread_cancel()

在这里插入图片描述

  • 如果线程被主线程或其他线程取消,那么主线程join函数得到的返回值固定为-1

4.线程分离—int pthread_detach(pthread_t thread);

在这里插入图片描述

5.线程ID及进程地址空间布局

1.--------------------------pthread_self函数获取id
在这里插入图片描述
2.--------------------------pthread库的动态链接
在这里插入图片描述

  • 所有的线程都是在thread库中建立的,线程的管理块都存储在库中具体的pcb由用户使用系统调用在内核中建立

在这里插入图片描述

  • 创建线程的具体图例

在这里插入图片描述

6.线程互斥

来看一个买票的例子:

/ 操作共享变量会有问题的售票系统代码
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int ticket = 100;
void* route(void* arg) {
    char* id = (char*)arg;
    while (1) {
        if (ticket > 0) {
            usleep(1000);
            printf("%s sells ticket:%d\n", id, ticket);
            ticket--;
        } else {
            break;
        }
    }
}
int main(void) {
    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, NULL, route, (void*)"thread 1");
    pthread_create(&t2, NULL, route, (void*)"thread 2");
    pthread_create(&t3, NULL, route, (void*)"thread 3");
    pthread_create(&t4, NULL, route, (void*)"thread 4");
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);
    pthread_join(t4, NULL);
}

在这里插入图片描述

  • 为了避免买票变成负数,所以要使用 来保证线程间的互斥

在这里插入图片描述

1.锁的使用

#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int ticket = 100;
pthread_mutex_t mutex;//设置锁
void* route(void* arg) 
{
    char* id = (char*)arg;
    while (1) 
    {
        pthread_mutex_lock(&mutex);//pthread_mutex_lock--------上锁
        if (ticket > 0) {
            usleep(1000);
            printf("%s sells ticket:%d\n", id, ticket);
            ticket--;
            pthread_mutex_unlock(&mutex);// pthread_mutex_unlock--------解锁
        } else {
            pthread_mutex_unlock(&mutex);// pthread_mutex_unlock--------解锁
            break;
        }
    }
    return nullptr;
}
int main(void) {
    pthread_t t1, t2, t3, t4;
    pthread_mutex_init(&mutex, NULL);
    pthread_create(&t1, NULL, route, (void*)"thread 1");
    pthread_create(&t2, NULL, route, (void*)"thread 2");
    pthread_create(&t3, NULL, route, (void*)"thread 3");
    pthread_create(&t4, NULL, route, (void*)"thread 4");
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);
    pthread_join(t4, NULL);
    pthread_mutex_destroy(&mutex);//pthread_mutex_destroy-------删除锁
}

网站公告

今日签到

点亮在社区的每一天
去签到