linnux同步方法多种锁介绍
生活随笔
收集整理的這篇文章主要介紹了
linnux同步方法多种锁介绍
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://blog.csdn.net/zsf8701/article/details/7844316
1.互斥鎖
2.自旋鎖
3.讀寫鎖
4.文件鎖
5.條件變量
6.信號(hào)量
?
https://blog.csdn.net/I_am_JoJo/article/details/7592298
?
?
linux下多種鎖的比較
https://blog.csdn.net/kowzb/article/details/77160249
?
?
https://www.cnblogs.com/cchust/p/4252500.html
?
遞歸與不可遞歸鎖
http://blog.chinaunix.net/uid-26983585-id-3316794.html
?
生產(chǎn)者消費(fèi)者
https://blog.csdn.net/yusiguyuan/article/details/48265205
?
1.生產(chǎn)者消費(fèi)者兩種實(shí)現(xiàn)方式(信號(hào)量,信號(hào)量與互斥鎖,互斥鎖與條件變量)
2.信號(hào)量實(shí)現(xiàn)讀寫鎖
https://www.cnblogs.com/xybaby/p/6559212.html
?
緩沖區(qū)使用
/* 已知循環(huán)緩沖區(qū)是一個(gè)可以無(wú)限循環(huán)讀寫的緩沖區(qū),當(dāng)緩沖區(qū)滿了還繼續(xù)寫的話就會(huì)覆蓋我們還沒(méi)讀取到的數(shù)據(jù)。下面定義了一個(gè)循環(huán)緩沖區(qū)并初始化,請(qǐng)編寫它的write函數(shù),確保不會(huì)破壞將要讀取的數(shù)據(jù),編寫read函數(shù)確保讀取的buf是已經(jīng)寫入的數(shù)據(jù),請(qǐng)?zhí)幚砗米x寫的rd_pos與wr_pos的值。 */#include <stdio.h> #include <pthread.h> #include <string.h>#define BUF_SIZE 64#define WRITE_SIZE 4#define READ_SIZE 64typedef struct _ring_buf {char *buf;unsigned int buf_size;unsigned int rd_pos;unsigned int wr_pos;unsigned int diff_pos;pthread_mutex_t mutex;pthread_cond_t cond; }ring_buf;ring_buf ring_head; void Init(ring_buf *p_ring, char *buf, unsigned int size) {memset(p_ring, 0, sizeof(ring_buf));p_ring->buf = buf;p_ring->buf_size = size;p_ring->rd_pos = 0;p_ring->wr_pos = 0;p_ring->diff_pos = p_ring->wr_pos - p_ring->rd_pos;pthread_mutex_init(&p_ring->mutex, NULL);pthread_cond_init(&p_ring->cond, NULL); }int deal_read_pos(int read_size) {int wr_pos,rd_pos,diff_pos,remain_size;if(read_size > ring_head.buf_size) {printf("read size errro,bigger than max size\n"); return -1;}wr_pos = ring_head.wr_pos;rd_pos = ring_head.rd_pos;diff_pos = ring_head.diff_pos;if(wr_pos == rd_pos) {if(diff_pos == ring_head.buf_size) {return ring_head.buf_size;} else {//diff_pos = 0printf("no read space here %s %d\n", __FUNCTION__, __LINE__);return 0;}} else if(wr_pos > rd_pos) {ring_head.diff_pos = wr_pos - rd_pos; remain_size = ring_head.diff_pos;if(read_size > remain_size) {printf("no read space here %s %d\n", __FUNCTION__, __LINE__);return 0; } else { //read_size <= remain_sizereturn remain_size;}} else if(wr_pos < rd_pos) {ring_head.diff_pos = ring_head.buf_size - rd_pos + wr_pos; remain_size = ring_head.diff_pos;if(read_size > remain_size) {printf("no read space here %s %d\n", __FUNCTION__, __LINE__);return 0; } else { //write_size <= remain_sizereturn remain_size;}}}void *read_fun(void *arg) {printf("%s start here~%d~~\n", __FUNCTION__, __LINE__);while(1) {pthread_mutex_lock(&ring_head.mutex); if(deal_read_pos(READ_SIZE) > 0) {ring_head.rd_pos = (ring_head.rd_pos + READ_SIZE)%ring_head.buf_size;ring_head.diff_pos = ring_head.diff_pos - READ_SIZE;printf("read---->write_pos:%d read_pos:%d diff_pos:%d\n", ring_head.wr_pos, ring_head.rd_pos, ring_head.diff_pos);pthread_cond_signal(&ring_head.cond);} else {printf("~~~~~wait write~~~~~write_pos:%d read_pos:%d diff_pos:%d\n", ring_head.wr_pos, ring_head.rd_pos, ring_head.diff_pos);pthread_cond_wait(&ring_head.cond, &ring_head.mutex); }pthread_mutex_unlock(&ring_head.mutex); usleep(1000*1000);}return; }/* fun:judge we can write write_size or not write_size:need to allocate size to write return:return remain space to write,or return 0 cannot get remain size */int deal_write_pos(int write_size) {int wr_pos,rd_pos,diff_pos,remain_size;if(write_size > ring_head.buf_size) {printf("write size errro,bigger than max size\n"); return -1;}wr_pos = ring_head.wr_pos;rd_pos = ring_head.rd_pos;diff_pos = ring_head.diff_pos;if(wr_pos == rd_pos) {if(diff_pos == ring_head.buf_size) {printf("no write space here %s %d\n", __FUNCTION__, __LINE__); return 0;} else {//diff_pos = 0return ring_head.buf_size;//remain size to write }} else if(wr_pos > rd_pos) {ring_head.diff_pos = wr_pos - rd_pos; remain_size = ring_head.buf_size - ring_head.diff_pos;if(write_size > remain_size) {printf("no write space here %s %d\n", __FUNCTION__, __LINE__); return 0; } else { //write_size <= remain_sizereturn remain_size;}} else if(wr_pos < rd_pos) {ring_head.diff_pos = ring_head.buf_size - rd_pos + wr_pos; remain_size = ring_head.buf_size - ring_head.diff_pos;if(write_size > remain_size) {printf("no write space here %s %d\n", __FUNCTION__, __LINE__); return 0; } else { //write_size <= remain_sizereturn remain_size;}} }void *write_fun(void *arg) {printf("%s start here~%d~~\n", __FUNCTION__, __LINE__);while(1) {pthread_mutex_lock(&ring_head.mutex); if(deal_write_pos(WRITE_SIZE) > 0) {ring_head.wr_pos = (ring_head.wr_pos + WRITE_SIZE)%ring_head.buf_size;ring_head.diff_pos = ring_head.diff_pos + WRITE_SIZE;printf("write---->write_pos:%d read_pos:%d diff_pos:%d\n", ring_head.wr_pos, ring_head.rd_pos, ring_head.diff_pos);pthread_cond_signal(&ring_head.cond);} else {printf("~~~~~wait read~~~~~write_pos:%d read_pos:%d diff_pos:%d\n", ring_head.wr_pos, ring_head.rd_pos, ring_head.diff_pos);pthread_cond_wait(&ring_head.cond, &ring_head.mutex); }pthread_mutex_unlock(&ring_head.mutex); usleep(1000*1000);}return; }int main(void) {char buf[BUF_SIZE] = {0};pthread_t read_t;pthread_t write_t;int ret = -1;Init(&ring_head, buf, BUF_SIZE);ret = pthread_create(&read_t, NULL, read_fun, NULL);if(ret != 0) {printf("pthread create fail here %s %d\n", __FUNCTION__, __LINE__);return -1;}ret = pthread_create(&write_t, NULL, write_fun, NULL);if(ret != 0) {printf("pthread create fail here %s %d\n", __FUNCTION__, __LINE__);return -1;} printf("start here~~~\n");pthread_join(read_t, NULL);pthread_join(write_t, NULL);printf("end here~~\n");return 0; }?
總結(jié)
以上是生活随笔為你收集整理的linnux同步方法多种锁介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2022年中科大细胞生物学实验原理往年题
- 下一篇: 7. 代码模板的使用