私は、o'reilly の Linux デバイス ドライバー ブック バージョン 3 からプログラム コードを挿入して作成した tty の書き込みと読み取りに、単純なパイプ プログラミングを使用しています。これを 経由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);
}