linux 訊號量使用例項

2021-05-23 15:40:33 字數 1177 閱讀 9136

#define major_num 254

static ssize_t globalvar_read(struct file *, char *, size_t, loff_t*);

static ssize_t globalvar_write(struct file *, const char *, size_t, loff_t*);

struct file_operations globalvar_fops =

;static int global_var = 0;

static struct semaphore sem;

static wait_queue_head_t outq;

static int flag = 0;

static int __init globalvar_init(void)

else

return ret;

}static void __exit globalvar_exit(void)

else

}//讀裝置

static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off)

if (down_interruptible(&sem)) //獲得訊號量,相當於獲得鎖

flag = 0;

if (copy_to_user(buf, &global_var, sizeof(int)))

up(&sem); //若讀取成功也釋放訊號量

return sizeof(int);

}//寫裝置

static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len,loff_t *off)

if (copy_from_user(&global_var, buf, sizeof(int)))

up(&sem); //寫成功後,也釋放訊號量

flag = 1;

wake_up_interruptible(&outq); //喚醒等待程序,通知已經有資料可以讀取

return sizeof(int);

}module_init(globalvar_init);

module_exit(globalvar_exit);

訊號量使用例項

include include include include include 2.6.28 module license dual bsd gpl int num 2 5 struct semaphore sem first struct semaphore sem second int thre...

Linux 訊號量使用例項詳解

例項篇 1.定義帶有裝置併發控制方案的結構體 諸如訊號量,自旋鎖等,反正前邊那麼多了 struct csyncontrol dev 然後,將訊號量的初始化工作放到模組初始化部分裡 int csycontrol init void 以後在訪問csyncontrol dev中的共享資源時,需要首先獲取這...

訊號量例項

pv原語的含義 p操作和v操作是不可中斷的程式段,稱為原語。pv原語及訊號量的概念都是由荷蘭科學家e.w.dijkstra提出的。訊號量sem是一整數,sem大於等於零時代表可供併發程序使用的資源實體數,但sem小於零時則表示正在等待使用臨界區的程序數。p原語操作的動作是 1 sem減1 2 若se...