当前位置: 首页 > java >正文

Linux系统编程-DAY07

一、线程的操作
定义函数指针数组
    typedef void *(*TH)(void *)
    TH th[5] = {th1, th2, th3};

线程的控制:互斥与同步
1.互斥 ---> 在多线程中对临界资源的排他性访问(读写操作)。
    mutex锁:1为开,0为关
互斥机制 ---> 互斥锁 ---> 保证临界资源的访问控制。

pthread_mutex_t   mutex;
互斥锁类型        互斥锁变量 内核对象

框架:
 定义互斥锁 ---> 初始化锁 ---> 加锁 ---> 解锁 ---> 销毁
****                      ***                     ***

1)定义:pthread_mutex_t   mutex; 
2)初始化锁:int pthread_mutex_init(
pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
        atrr  初始化的值,一般是NULL表示默认锁
        返回值:成功 0、失败 非零
3)加锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
4)解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
5)销毁:int pthread_mutex_destroy(pthread_mutex_t *mutex);
6)trylock:int pthread_mutex_trylock(pthread_mutex_t *mutex);
    功能:类似加锁函数效果,唯一区别就是不阻塞。

在线程的操作中,返回值为0都表示为成功

2.线程的同步 ---> 同步 ---> 有一定先后顺序的对资源的排他性访问。
    原因:互斥锁可以控制排他访问但没有次序。

linux下的线程同步  --->信号量机制 --->semaphore.h   posix 
    sem_open();
    信号量的分类:
    1)无名信号量 --->线程间通信
    2)有名信号量 --->进程间通信

框架:信号量的定义--->信号量的初始化 --->信号量的PV操作--->信号量的销毁。

    <1>semaphore 
    信号量的定义 :
       sem_t            sem;
       信号量的类型     信号量的变量
    <2>信号量的初始化:int sem_init(sem_t *sem, int pshared, unsigned int value);
        功能:将已经定义好的信号量赋值。
        参数:sem 要初始化的信号量
              pshared = 0 ;表示线程间使用信号量
              !=0 ;表示进程间使用信号量
      <3>信号量的PV 操作
           P --->申请资源--->申请一个二值信号量 
           V --->释放资源--->释放一个二值信号量
        当有多个资源时,可以使用计数信号量
        eg:sem_init(&sem_WIN, 0, 3);

           P操作对应函数 --->sem_wait();
          V操作对应函数 --->sem_post();

        int sem_wait(sem_t *sem);
        功能:判断当前sem信号量是否有资源可用。
          如果sem有资源(==1),则申请该资源,程序继续运行;如果sem没有资源(==0),则线程阻塞等待,一 旦有资源则自动申请资源并继续运行程序。

          注意:sem 申请资源后会自动执行 sem = sem - 1;

        int sem_post(sem_t *sem);
        功能:函数可以将指定的sem信号量资源释放并默认执行,sem = sem+1;
          线程在该函数上不会阻塞。

    <4>信号量的销毁
           int sem_destroy(sem_t *sem);
           功能:使用完毕将指定的信号量销毁
           参数:sem要销毁的信号量

3.产生死锁的原因主要是:逻辑不对
(1) 因为系统资源不足。
(2) 进程运行推进的顺序不合适。
(3) 资源分配不当等。
如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则
就会因争夺有限的资源而陷入死锁。其次,进程运行推进顺序与速度不同,也可能产生死锁。
产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进线程之间形成一种头尾相接的循环等待资源关系。

二、例题与练习
1.线程互斥---锁

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int count = 0;
pthread_mutex_t mutex;
void *th(void *arg)
{int i = 5000;while(i--){pthread_mutex_lock(&mutex);int tmp = count;printf("%d\n", tmp + 1);count = tmp + 1;pthread_mutex_unlock(&mutex);}return NULL;}int	main(int argc, char **argv)
{pthread_t tid1,tid2;pthread_mutex_init(&mutex, NULL);pthread_create(&tid1, NULL, th, NULL);pthread_create(&tid2, NULL, th, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_mutex_destroy(&mutex);//system("pause");return 0;
}

2.银行取钱案例-练习

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>int WIN = 3;
pthread_mutex_t mutex;
void *th(void *arg)
{while(1){pthread_mutex_lock(&mutex);if(WIN > 0){WIN--;printf("get win\n");pthread_mutex_unlock(&mutex);int n = rand() % 5 + 1;sleep(n);pthread_mutex_lock(&mutex);WIN++;printf("relese win\n");pthread_mutex_unlock(&mutex);break;}else{pthread_mutex_unlock(&mutex);}}return NULL;
}
int	main(int argc, char **argv)
{pthread_t tid[10] = {0};int i;pthread_mutex_init(&mutex, NULL);for(i = 0; i < 10; ++i){pthread_create(&tid[i], NULL, th, NULL);}for(i = 0; i < 10; ++i){pthread_join(tid[i], NULL);}pthread_mutex_destroy(&mutex);//system("pause");return 0;
}

3.线程同步--信号量

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>sem_t sem_H, sem_W;
void *th1(void *args)
{int i = 10;while(i--){sem_wait(&sem_H);printf("hello ");fflush(stdout);sem_post(&sem_W);}return NULL;
}void *th2(void *args)
{int i = 10;while(i--){sem_wait(&sem_W);printf("World\n");sem_post(&sem_H);sleep(1);}return NULL;
}int	main(int argc, char **argv)
{pthread_t tid1;pthread_t tid2;sem_init(&sem_H, 0, 1);sem_init(&sem_W, 0, 0);pthread_create(&tid1, NULL, th1, NULL);pthread_create(&tid2, NULL, th2, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);sem_destroy(&sem_H);sem_destroy(&sem_W);//system("pause");return 0;
}

4.线程:用信号量去解决模拟银行

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
sem_t sem_WIN;
void *th(void *arg) 
{sem_wait(&sem_WIN);printf("get win\n");int n = rand() % 5 + 1;sleep(n);printf("relese win\n");sem_post(&sem_WIN);return NULL;
}
int main(int argc, char **argv) 
{sem_init(&sem_WIN, 0, 3);pthread_t tid[10] = {0};int i;for (i = 0; i < 10; ++i) {pthread_create(&tid[i], NULL, th, NULL);}for (i = 0; i < 10; ++i) {pthread_join(tid[i], NULL);}sem_destroy(&sem_WIN);// system("pause");return 0;
}

http://www.xdnf.cn/news/9569.html

相关文章:

  • JAVA中常用算法详解:排序(冒泡、快速排序)与查找(二分查找)
  • 途景VR智拍APP:开启沉浸式VR拍摄体验
  • 快速入门Java+Spring Ai+deepseek 开发
  • git 一台电脑一个git账户,对应多个仓库ssh
  • ParakeetTDT0.6BV2,语音识别ASR,极速转录, 高精度英文转录,标点支持(附整合包)
  • Dify案例实战之智能体应用构建(二)
  • IBM DB2和MYSQL在安全性、稳定性等方面的差异
  • 时间序列预测算法中的预测概率化笔记
  • GPIO驱动实例代码
  • 【客户案例】借助 DHTMLX Gantt 和 Diagram 构建高效项目与流程管理平台
  • 基于SpringBoot开发一个MCP Server
  • vue 中的ref属性
  • chown修改不成功的解决方案
  • ESP8285乐鑫SOCwifi芯片32bit MCU和2.4 GHz Wi-Fi
  • 零衍课堂 | 环境初始化部署流程
  • 从0到1:多医院陪诊小程序开发笔记(上)
  • VMware 安装 Ubuntu 实战教程
  • python学习打卡day38
  • 截图后怎么快速粘贴到notability?
  • day22-定时任务故障案例
  • 秒杀系统—2.第一版初步实现的技术文档
  • 医院闭环系统业务介绍
  • Linux基础 -- 设备树引脚复用之`/omit-if-no-ref/` 用法解析
  • 8.7 基于EAP-AKA的订阅转移
  • Springboot 集成 TDengine3.0版本
  • git stash 的使用
  • qt ubuntu 20.04 交叉编译
  • python实战:在Linux服务器上使用LibreOffice命令行批量接受Word文档的所有修订
  • MCP 与 AI 模型的用户隐私保护——如何让人工智能更懂“界限感”?
  • Python-114:字符串字符类型排序问题