中级会员
积分 374
金钱 374
注册时间 2016-6-8
在线时间 79 小时
1 金钱
本帖最后由 我叫做大熙熙 于 2021-3-20 02:36 编辑
偶然发现的,驱动程序中用printk打印,应用程序中加了printf打印,应用程序在调用驱动程序时,应用程序的打印没有在终端显示。举个例子:
应用程序:
int main(int argc, char *argv[])
{
int fd = open(fileName, O_RDWR);
int ret = read(fd, readBuf, 50);
if(ret < 0)
{
printf("[APP]:file '%s' read failed", fileName);
}
else
{
printf("[APP]:read data:%s\r\n", readBuf);
}
return 0;
}
复制代码 驱动程序
static int chrdevbase_open(struct inode *inode, struct file *filp)
{
printk("[KER]: chrdevbase open!\r\n");
return 0;
}
static ssize_t chrdevbase_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue = 0;
/* 向用户空间发送数据 */
memcpy(readbuf, kerneldata, sizeof(kerneldata));
retvalue = copy_to_user(buf, readbuf, cnt);
if(retvalue == 0){
printk("[KER]: ok\r\n");
}else{
printk("[KER]: failed\r\n");
}
return 0;
} 复制代码 最后应用程序尝试去读的时候,发现打印是
[KER]: chrdevbase open!
[KER]: ok
然后就没了,应用程序的打印没打出来,去掉驱动printk的打印的话,应用程序printf的打印是正常的。网上查询的在printf后面加fflush(stdout)这句是刷新缓存区实测没用。
所以想问一下,内核和应用程序都想正常打印的话要怎么做呢?
我来回答