我正在使用一个简单的管道编程来编写和读取 tty,它是通过插入来自 o'reilly 的 linux 设备驱动程序书第 3 版的程序代码制成的。我通过插入这个insmod
,并获得了名为tinytty0
.
我的问题是我可以使用这个设备通过管道读取和写入数据吗?我试过一次,数据正在写入驱动程序,但读取尚未完成。我不知道是什么原因。代码如下
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<fcntl.h>
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
fd[1]=open("/dev/ttytiny0",O_WRONLY);
if(fd[1]<0)
{
printf("the device is not opened\n");
exit(-1);
}
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
fd[0]=open("/dev/ttytiny0",O_RDONLY);
if(fd[0]<0)
{
printf("the device is not opened\n");
exit(-1);
}
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}