~からインスピレーションを得るこのブログ投稿、私はLinuxデバイスドライバの作業をしています(彼らから学んでいます)。
read
ドライバ関連フィールドは、file_operations
次の機能で初期化されます。
static ssize_t mychardev_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
{
uint8_t *data = "Hello from the kernel world!\n";
size_t datalen = strlen(data);
printk("MYCHARDEV: mychardev_read was called with count equal to %zu\n", count);
if (count > datalen) {
count = datalen;
}
if (copy_to_user(buf, data, count)) {
return -EFAULT;
}
return count;
}
私が理解したように、ユーザースペースがこのドライバによって生成されたデバイスから特定の量のデータを要求すると、転送はdatalen
文字列の長さdata
(この場合は29(末尾の文字列を含む\n
))まで一括して発生します。これは、シェルで次のコマンドを実行することで確認できます。
$ head -c 5 /dev/mychardev-0
$ head -c 29 /dev/mychardev-0
$ head -c 30 /dev/mychardev-0
$ head -c 32 /dev/mychardev-0
結果は出力に次のようになりますdmesg
(およびメソッドに明白な呼び出しがあると仮定printk
)。open
release
[10782.052736] MYCHARDEV: Device open
[10782.052743] MYCHARDEV: mychardev_read was called with count equal to 5
[10782.052751] MYCHARDEV: Device close
[10868.275577] MYCHARDEV: Device open
[10868.275585] MYCHARDEV: mychardev_read was called with count equal to 29
[10868.275598] MYCHARDEV: Device close
[10878.414433] MYCHARDEV: Device open
[10830.796424] MYCHARDEV: mychardev_read was called with count equal to 30
[10830.796438] MYCHARDEV: mychardev_read was called with count equal to 1
[10830.796443] MYCHARDEV: Device close
[10830.796417] MYCHARDEV: Device open
[10878.414441] MYCHARDEV: mychardev_read was called with count equal to 32
[10878.414455] MYCHARDEV: mychardev_read was called with count equal to 3
[10878.414460] MYCHARDEV: Device close
私が理解していないのはなぜ次のコマンドが
$ cat /dev/mychardev-0 | head -c 5
明らかにする
[11186.036107] MYCHARDEV: Device open
[11186.036120] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036131] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036136] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036141] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036145] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036150] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036154] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036159] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036163] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036168] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036172] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036177] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036181] MYCHARDEV: mychardev_read was called with count equal to 8192
[11186.036187] MYCHARDEV: Device close
原則として、バイト単位で順番に要求するのではなく、一度に一括してドライバ(またはシェルランタイムとする必要がありますか?)からcat
データ(この場合は8192)を要求することを理解しています。cat
パフォーマンスを向上させます。
しかし、なぜそれを何度も呼び出すべきかはわかりません。