线程(三种)和进程间的通信(8种)

一.前言        

         作为一名平平无奇的即将毕业找工作的应届毕业生,大学三年浑浑噩噩,找工作一问三不知,唉!那就只能选择一条花钱培训的道路,现在的我已经不是当初的我,何以解忧唯有暴富,何以暴富唯有敲代码。下面是我整理的一些比较重要的通信问题,希望对你们有帮助。顺便涨涨我这可怜无比的点击量!!!

 

        如果想成为一位优秀的嵌入式软件工程师的,进程和线程的通信可是必不可少的内容哦,也是面试中必问的问题。

二.目录

先概括一下线程和进程间的通信吧!

1.线程间通信

        1.1线程的互斥机制

        1.2.线程的同步机制

               1. 2.1无名信号量(无名信号量适合使用在线程个数比较少的线程中实现同步。)

                1.2.2条件变量(条件变量适合用在多线程的同步机制,定义一个条件变量即可,不需要像无名信号量一样随着线程数的增多而增多。)

2..进程间通信

        2.1传统进程间通信

                2.1.1无名管道

                2.1.2有名管道

                2.1.3信号

        2.2SystemV引入的IPC进程间通信

                2.2.1消息队列

                2.2.2共享内存

                2.2.3信号灯集

        2.3通过网络实现进程间通信(socket(套接字))

        2.4Android系统中(Binder机制)

三.线程通信(三种)

        接下来具体了解一下,我作为软件专业的学生,在大三期间有一门课程叫数据结构,里面也有关于互斥锁,消费者生产者的知识,这篇文章具体的了解一下线程之间的通信。

1.1关于互斥锁的实例,题目是这样的:

        一共有10000元钱张三每次取50,李四每次取100,直到取完或者不不够;加了互斥锁后咱就可以保证在同一时间只有一个人取钱,要不然只剩最后一百两个人·都取不够了,只能谁厉害谁先抢到就谁了。

#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#define PRINT_ERR(msg) \
    do {               \
        perror(msg);   \
        return -1;     \
    } while (0)
// 1.定义互斥锁
pthread_mutex_t mutex;

int money = 10000;
void* task1(void* arg)
{
    //通过死循环持续取钱
    while (1) {
         //上锁
        pthread_mutex_lock(&mutex);
        money -= 50;
        if (money >= 0) {
            printf("张三取了50块钱,剩余money=%d\n", money);
        } else {
            money += 50;
            printf("账户上的余额小于50块钱,取钱失败\n");
            pthread_mutex_unlock(&mutex);
            pthread_exit(NULL);
        }
        sleep(1);
        //解锁
        pthread_mutex_unlock(&mutex);
    }
}
void* task2(void* arg)
{
    while (1) {
        //上锁
        pthread_mutex_lock(&mutex);
        money -= 100;
        if (money >= 0) {
            printf("李四取了100块钱,剩余money=%d\n", money);
        } else {
            money += 100;
            printf("账户上的余额小于100块钱,取钱失败\n");
            pthread_mutex_unlock(&mutex);
            pthread_exit(NULL);
        }
        sleep(1);
        //解锁
        pthread_mutex_unlock(&mutex);
    }
}
int main(int argc, const char* argv[])
{
    //定义两个线程
    pthread_t tid1, tid2;
    // 2.初始化互斥锁
    pthread_mutex_init(&mutex, NULL);
    //创建一个线程
    if ((errno = pthread_create(&tid1, NULL, task1, NULL)) != 0)
      PRINT_ERR("pthread_create task1 error");
     //创建一个线程
    if ((errno = pthread_create(&tid2, NULL, task2, NULL)) != 0)
      PRINT_ERR("pthread_create task1 error");
    //阻塞等待线程的资源tid1
    pthread_join(tid1, NULL);
    //阻塞等待线程的资源tid2
    pthread_join(tid2, NULL);

    // 3.销毁锁
    pthread_mutex_destroy(&mutex);
    return 0;
}

 

 1.2.1生产者生产一部手机,消费者购买一部,如果生产者没有生产,消费者则不能购买,无名信号量就是保证只有当生产者生产以后再购买。

#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <semaphore.h>
#define PRINT_ERR(msg) \
    do {               \
        perror(msg);   \
        return -1;     \
    } while (0)
 //1.定义无名信号量   
sem_t sem1,sem2;
//生产者线程
void* task1(void* arg)
{
    while (1) {
        //申请资源(让信号量的值减去1),(p操作)
        // 如果在调用sem_wait的时候获取不到资源,sem_wait会阻塞
        sem_wait(&sem1);
        printf("我生成了一部华为Mate50手机\n");
        //释放资源说说明sem2有资源了(v操作)
        sem_post(&sem2); 
    }
}
//消费者线程
void* task2(void* arg)
{
    while (1) {
        //p操作
        sem_wait(&sem2); 
        printf("我购买了一部华为Mate50手机\n");
        sleep(1);
        //v操作
        sem_post(&sem1);
        
    }
}
int main(int argc, const char* argv[])
{
    pthread_t tid1, tid2;

    sem_init(&sem1,0,1); //初始化信号量,初始值是1表示有资源
    sem_init(&sem2,0,0); //初始化信号量,初始值是0表示没有资源


    if ((errno = pthread_create(&tid1, NULL, task1, NULL)) != 0)
        PRINT_ERR("pthread_create task1 error");
    if ((errno = pthread_create(&tid2, NULL, task2, NULL)) != 0)
        PRINT_ERR("pthread_create task2 error");

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    sem_destroy(&sem1);
    sem_destroy(&sem2);

    return 0;
}

 1.2.2生产者生产手机,多个消费者购买,需要用到条件变量使其休眠,并且使用锁保证上锁期间不会被别的干扰。

#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <semaphore.h>
#define PRINT_ERR(msg) \
    do {               \
        perror(msg);   \
        return -1;     \
    } while (0)
pthread_cond_t cond;
pthread_mutex_t mutex;
//生产者线程
void* task1(void* arg)
{
    while (1) {
        sleep(1);
        printf("我生成了一部华为Mate50手机\n");
        pthread_cond_broadcast(&cond);
    }
}
//消费者线程
void* task2(void* arg)
{
    while (1) {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex); //阻塞等待资源
        printf("tid=%#lx,我购买了一部华为Mate50手机\n",pthread_self());
        pthread_mutex_unlock(&mutex);
    }
}

int main(int argc, const char* argv[])
{
    pthread_t tid1, tid2, tid3;

    pthread_mutex_init(&mutex,NULL);
    pthread_cond_init(&cond, NULL);

    if ((errno = pthread_create(&tid1, NULL, task1, NULL)) != 0)
        PRINT_ERR("pthread_create task1 error");

    if ((errno = pthread_create(&tid2, NULL, task2, NULL)) != 0)
        PRINT_ERR("pthread_create task2 error");
    if ((errno = pthread_create(&tid3, NULL, task2, NULL)) != 0)
        PRINT_ERR("pthread_create task3 error");
 
    printf("tid2=%#lx,tid3=%#lx\n",tid2,tid3);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
   

    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mutex);
    return 0;
}

 线程间的通信三种写完了,进程间的通信下次再写吧!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值