i'm utilizing a simple pipe programing for writing and reading through the tty which produced from placing this program code in the linux device driver book version 3 of o'reilly. i placed this via insmod ,and acquired the unit named tinytty0.

my real question is can one make use of this device to read data via pipe? i attempted once ,the information is writeng into driver but reading through hasn't be achieved. i have no idea exactly what the reason. the code is offered below

#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);
}

You have to be misunderstanding exactly what the tinytty driver in the Linux Device Drivers book does. In the book:

This sample small tty driver doesn't connect with any real hardware, so its write func- tion simply records within the kernel debug log what data was said to be written.

It's not some type of loopback TTY driver, actually, it transmits a continuing character ('t') every two seconds to whatever is reading through in the device (begin to see the function tiny_timer).

Now, onto your piping issues. Things I see out of your code is you have basically produced a pipe. Then, inside your child process, you close up the read finish from the pipe, and discard the write finish by changing it having a file descriptor for your tiny tty device (that is bad practice, as you've essentially leaked a wide open file descriptor). Then, inside your parent process, you close up the write finish from the pipe and discard the read finish (still bad practice, ie. "seeping open file descriptors"). Finally, within the same parent process, you are trying to see from that which you call the pipe, which isn't a real pipe any longer, since you've closed one finish and changed another having a descriptor towards the tiny tty device. Furthermore, the timer within the driver (that chimes every two seconds) most likely has not gone off, meaning there is nothing that you should read. In my opinion this describes your condition.


For anybody interested, it being known to here's available underneath the the Creative Commons Attribution-ShareAlike 2. license from LWN.net, and also the example motorists/code can be obtained from O'Reilly.