~에서 영감을 얻다이 블로그 게시물, 저는 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
string 길이 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
성능을 향상시킵니다.
그런데 왜 그렇게 여러 번 호출해야 하는지 이해가 되지 않습니다.