1-6linux系統(tǒng)編程——線程間通信——練習(xí):模擬銀行的排隊(duì)系統(tǒng)

線程間通信

信息交換:

使用多個(gè)線程都可見(jiàn)的內(nèi)存區(qū)域

線程互斥鎖:

保障有同一把鎖保護(hù)的共享資源被多個(gè)線程互斥訪問(wèn)
互斥鎖:pthread_mutex_t
互斥鎖初始化:pthread_mutex_init
互斥鎖的獲?。渔i)pthread_mutex_lock
互斥鎖的釋放(解鎖)pthread_mutex_unlock

線程信號(hào)量:

解決多個(gè)線程在使用有限資源時(shí)的同步問(wèn)題
線程信號(hào)量:sem_t
信號(hào)量的初始化:sem_init
信號(hào)量的獲?。簊em_wait
信號(hào)量的釋放:sem_post
信號(hào)量的銷毀:sem_destory

原子性操作:

或全執(zhí)行,或全不執(zhí)行。

邏輯圖

](http://upload-images.jianshu.io/upload_images/3821352-0f5492e734fe2fc9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

舉個(gè)例子:模擬排隊(duì)系統(tǒng),單運(yùn)程多線程

新建一個(gè)自定義頭文件,存放鏈表結(jié)構(gòu)體

#ifndef __SQ_QUEUE_H__
#define __SQ_QUEUE_H__

// 定義鏈?zhǔn)疥?duì)列中元素的實(shí)際類型的別名
// 注意:隊(duì)列中可以存放任意類型的數(shù)據(jù)
typedef int datatype_t ;

// 定義鏈?zhǔn)疥?duì)列中結(jié)點(diǎn)的類型及其別名
typedef struct _queue_node
{   
    datatype_t data;
    struct _queue_node *next;    
}queue_node_t;

// 定義存放鏈?zhǔn)疥?duì)列隊(duì)首和隊(duì)尾指針的結(jié)構(gòu)類型及其別名
typedef struct _link_queue
{
    queue_node_t *front;
    queue_node_t *rear;
}link_queue_t;

// 創(chuàng)建一個(gè)空的鏈?zhǔn)疥?duì)列
link_queue_t *create_empty_link_queue(void);

// 判斷鏈?zhǔn)疥?duì)列是否為空,為空返回1,否則,0
int is_empty_of_link_queue(link_queue_t *queue);

// 求鏈?zhǔn)疥?duì)列中元素的個(gè)數(shù)即隊(duì)列長(zhǎng)度
int length_of_link_queue(link_queue_t *queue);

// 入隊(duì)操作(即在隊(duì)尾插入數(shù)據(jù)結(jié)點(diǎn))
int insert_to_link_queue(link_queue_t *queue, 
            datatype_t x);

// 出隊(duì)操作(即在對(duì)頭刪除數(shù)據(jù)結(jié)點(diǎn))
int delete_from_link_queue(link_queue_t *queue, 
            datatype_t *px);

// 清空鏈?zhǔn)疥?duì)列
void clear_link_queue(link_queue_t *queue);

// 銷毀鏈?zhǔn)疥?duì)列
void destroy_link_queue(link_queue_t *queue);

// 遍歷打印鏈?zhǔn)疥?duì)列中的數(shù)據(jù)
void print_data_info_of_link_queue(link_queue_t *queue);

#endif

對(duì)鏈表的操作:新建鏈表、尾插、頭刪、清空數(shù)據(jù)、判斷鏈表長(zhǎng)度、刪除鏈表

#include "link_queue.h"
#include <stdlib.h>
#include <stdio.h>

// 創(chuàng)建鏈?zhǔn)疥?duì)列
link_queue_t *create_empty_link_queue(void)
{
    // 為包含隊(duì)首和隊(duì)尾指針的結(jié)點(diǎn)動(dòng)態(tài)申請(qǐng)空間
    link_queue_t *queue = (link_queue_t *)malloc(sizeof(link_queue_t));
    // 隊(duì)首指針和隊(duì)尾指針均指向頭結(jié)點(diǎn)
    queue->front = queue->rear = (queue_node_t *)malloc(sizeof(queue_node_t));
    queue->front->next = NULL;
    
    return queue;
}

// 判斷鏈?zhǔn)疥?duì)列是否為空,為空返回1,否則0
int is_empty_of_link_queue(link_queue_t *queue)
{
    return queue->front == queue->rear;
    // return queue->front->next == NULL;
}

// 求鏈?zhǔn)疥?duì)列的數(shù)據(jù)元素個(gè)數(shù)即隊(duì)列長(zhǎng)度
int length_of_link_queue(link_queue_t *queue)
{
    int len = 0;
    queue_node_t *p = queue->front;
    
    while(p->next != NULL)
    {
        len++;
        p = p->next;
    }
    
    return len;
}

// 入隊(duì)操作,成功返回0,失敗-1
int insert_to_link_queue(link_queue_t *queue, 
            datatype_t x)
{
    // 隊(duì)列不存在,插入失敗返回
    if(queue == NULL)
        return -1;
    
    // 1.為插入的數(shù)據(jù)申請(qǐng)結(jié)點(diǎn)空間
    queue_node_t *p = (queue_node_t *)malloc(sizeof(queue_node_t));
    p->data = x;
    
    // 2.插入到隊(duì)尾指針的后一個(gè)位置
    p->next = queue->rear->next;
    queue->rear->next = p;
    
    // 3.更新隊(duì)尾指針
    queue->rear = queue->rear->next;
    
    return 0;
}

// 出隊(duì)操作,成功,返回0;失敗,-1
int delete_from_link_queue(link_queue_t *queue, 
            datatype_t *px)
{
    // 如果隊(duì)列不存在或隊(duì)列為空,出隊(duì)失敗
    if(queue == NULL || is_empty_of_link_queue(queue))
        return -1;
    
    // 1.定位出隊(duì)的數(shù)據(jù)結(jié)點(diǎn)的位置
    queue_node_t *p = queue->front->next;//頭刪
    // 2.將該結(jié)點(diǎn)從鏈?zhǔn)疥?duì)列中移除
    queue->front->next = p->next;
    // 3.如果需要,將待出隊(duì)元素的值傳出
    if(px != NULL)
        *px = p->data;
    // 4.釋放結(jié)點(diǎn)空間
    free(p);
    
    // 注意:如果出隊(duì)之后,隊(duì)列為空,重定位尾指針的位置
    if(queue->front->next == NULL)
        queue->rear = queue->front;
    
    return 0;
}

// 清空鏈?zhǔn)疥?duì)列
void clear_link_queue(link_queue_t *queue)
{
    queue_node_t *p = NULL;

    // 循環(huán)的采用頭刪法刪除鏈?zhǔn)疥?duì)列的數(shù)據(jù)結(jié)點(diǎn)直到隊(duì)列為空
    while(!is_empty_of_link_queue(queue))
    {
        p = queue->front->next;
        queue->front->next = p->next;
        free(p);
    }
}

// 銷毀鏈?zhǔn)疥?duì)列
void destroy_link_queue(link_queue_t *queue)
{
    // 清空鏈?zhǔn)疥?duì)列
    clear_link_queue(queue);
    // 釋放鏈?zhǔn)疥?duì)列頭結(jié)點(diǎn)空間
    free(queue->front);
    // 釋放隊(duì)首和隊(duì)尾指針結(jié)點(diǎn)空間
    free(queue);
}

// 根據(jù)實(shí)際datatype_t類型打印數(shù)據(jù)
static void print_data(datatype_t x)
{
    printf("%d ", x);
}

// 遍歷打印鏈?zhǔn)疥?duì)列的數(shù)據(jù)結(jié)點(diǎn)
void print_data_info_of_link_queue(link_queue_t *queue)
{
    queue_node_t *p = queue->front;
    
    printf("linkqueue : ");
    while(p->next != NULL)
    {
        p = p->next;
        print_data(p->data);    
    }    
    printf("\n");
}

產(chǎn)生鏈表,隨時(shí)間間隔插入和刪除,有互斥鎖

// 練習(xí):模擬銀行的排隊(duì)系統(tǒng)

#include <stdio.h>
#include "link_queue.h"
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>

#define MAX_CLIENT_COUNT 100

// 全局隊(duì)列:用于存放用戶ID的鏈?zhǔn)疥?duì)列
link_queue_t *queue;
// 互斥鎖:用于保障生產(chǎn)者線程和消費(fèi)者線程互斥訪問(wèn)用戶隊(duì)列
pthread_mutex_t mutex;
// 用戶數(shù)信號(hào)量:用于表示當(dāng)前可用的用戶個(gè)數(shù)
sem_t client_sem;
// 空余排隊(duì)數(shù)信號(hào)量:用于表示當(dāng)前隊(duì)列中能夠繼續(xù)插入的用戶個(gè)數(shù)
sem_t free_space_sem;

// 生產(chǎn)者線程:用于模擬用戶的產(chǎn)生
void *producer_thread(void *arg);

// 消費(fèi)者線程:用于模擬銀行柜臺(tái)的動(dòng)作
void *consumer_thread(void *arg);

int main(int argc, char *argv[])
{
    pthread_t producer_id;
    pthread_t consumer_id;

    // 初始化全局用戶隊(duì)列
    queue = create_empty_link_queue();
    // 初始化互斥鎖
    pthread_mutex_init(&mutex, NULL);
    // 初始化相關(guān)信號(hào)量
    sem_init(&client_sem, 0, 0);
    sem_init(&free_space_sem, 0, MAX_CLIENT_COUNT);//限制隊(duì)列長(zhǎng)度
    
    // create threads
    pthread_create(&producer_id, NULL, producer_thread, NULL);
    pthread_create(&consumer_id, NULL, consumer_thread, NULL);
    
    // join threads 
    pthread_join(producer_id, NULL);//用戶
    pthread_join(consumer_id, NULL);//柜臺(tái)

    sem_destroy(&client_sem);
    sem_destroy(&free_space_sem);//銷毀由sem指向的信號(hào)量

    return 0;
}

// 生產(chǎn)者線程:用于模擬用戶的產(chǎn)生
void *producer_thread(void *arg)
{
    int client_no = 0;
    
    while(1)
    {
        // 1.獲取產(chǎn)生用戶的資格(即讓空余位置-1)
        sem_wait(&free_space_sem);
        
        // 2.模擬產(chǎn)生用戶ID
        client_no = rand()%1000+1;//1~1000
        
        // 3.以互斥的方式向用戶隊(duì)列中插入用戶ID信息
            pthread_mutex_lock(&mutex);
        insert_to_link_queue(queue, client_no);
            pthread_mutex_unlock(&mutex);
        
        // 4.模擬打印用戶入隊(duì)信息
        printf("client %d is in...queue length : %d\n", client_no, length_of_link_queue(queue));
        
        // 5.可用用戶數(shù)目+1
        sem_post(&client_sem);
        
        // 6.模擬用戶的到達(dá)時(shí)間間隔
        sleep(1);
    }
}

// 消費(fèi)者線程:用于模擬銀行柜臺(tái)的動(dòng)作
void *consumer_thread (void *arg)
{
    int client_no = 0;
    
    while (1)
    {
        // 1.申請(qǐng)可用用戶資源(即對(duì)可用用戶數(shù)-1)
        sem_wait(&client_sem);
        
        // 2.以互斥的方式從全局用戶隊(duì)列中獲取用戶ID
        pthread_mutex_lock(&mutex);
        delete_from_link_queue(queue, &client_no);
        pthread_mutex_unlock(&mutex);
        
        // 3.模擬銀行柜臺(tái)的操作
        printf("client %d is out...\n", client_no);  
        
        // 3.隊(duì)列空余資源數(shù)+1
        sem_post(&free_space_sem);
        
        // 4.模擬柜臺(tái)操作的時(shí)間間隔
        sleep(rand()%5+1);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自:Youtherhttps://www.cnblogs.com/youtherhome/archive/201...
    njukay閱讀 1,724評(píng)論 0 52
  • 一、線程的創(chuàng)建和調(diào)度 1.線程是程序執(zhí)行的某一條指令流的映像。 為了進(jìn)一步減少處理機(jī)制的空轉(zhuǎn)時(shí)間,支持多處理器及減...
    穹藍(lán)奧義閱讀 1,178評(píng)論 2 5
  • 系統(tǒng)與網(wǎng)絡(luò)編程 小作業(yè) 公交車停發(fā)車程序 線程 并發(fā)執(zhí)行:看起來(lái)像同時(shí)運(yùn)行,實(shí)際上在單核cpu里只有一個(gè)。將其排成...
    I踏雪尋梅閱讀 510評(píng)論 0 3
  • 簡(jiǎn)介 線程創(chuàng)建 線程屬性設(shè)置 線程參數(shù)傳遞 線程優(yōu)先級(jí) 線程的數(shù)據(jù)處理 線程的分離狀態(tài) 互斥鎖 信號(hào)量 一 線程創(chuàng)...
    第八區(qū)閱讀 8,713評(píng)論 1 6
  • 線程基礎(chǔ) 線程是進(jìn)程的一個(gè)執(zhí)行單元,執(zhí)行一段程序片段,線程共享全局變量;線程的查看可以使用命令或者文件來(lái)進(jìn)行查看;...
    秋風(fēng)弄影閱讀 806評(píng)論 0 0

友情鏈接更多精彩內(nèi)容