[英]how to receive the data by the user space application from the kernel space?
我正在計算dev.c內核源代碼中的中斷時間,如下所示:
extern double InterruptTime;
InterruptTime = ktime_get_real(); //timestamp
我正在使用procfs從內核空間向用戶空間寫入數據,並在內核空間中使用以下api將數據發送到用戶空間。 PROCFS.c:
struct device {
double array[100];
}chr_arr;
ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
int len;
chr_arr.array =InterruptTime; // Is this possible ??
len = count >= strlen(chr_arr.array) ? strlen(chr_arr.array) : count;
*offset += len;
if (*offset >= strlen(chr_arr.array))
return 0;
if (copy_to_user(buf,chr_arr.array,len))
return -EFAULT;
return len;
}
如上所示,是否可以從PROCFS.c中的dev.c中讀取InterruptTime? 從上述內核代碼發送的數據將如何在用戶端(即InterruptTime)接收?
我還不確定,是否只需要提供一個InterruptTime
值,還是需要緩沖這些值,因為它們是為用戶空間代碼生成的,以便以后檢索。 如果沒有額外的緩沖工作,並且為了簡單起見,我建議遵循以下思路:
ssize_t dev_read(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
if ( count < sizeof(InterruptTime) ) {
// Not enough space provided.
return 0; // Or some error code maybe.
}
if (copy_to_user(buf,&InterruptTime,sizeof(InterruptTime)) {
return -EFAULT;
} else {
return sizeof(InterruptTime); // Number of bytes we copied.
}
}
(請注意,這不是很干凈,應通過緩沖數據以允許讀取任意大小,正確的錯誤處理等來加以改進)。
然后,在用戶空間中,您將執行以下操作
#include <fcntl.h>
...
int fd = open("/proc/...", O_RDONLY);
double timestamp;
loff_t dummyOffset;
if ( read( fd, ×tamp, sizeof(timestamp) ) != sizeof(timestamp) ) {
// Failure.
} else {
// Use value in timestamp...
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.