FIFO is a first in first out data structure that which can be used to hold any kind of data. Linux kernel provides useful APIs to make creation and use of FIFO data structure easy.
The first step in creation of FIFO is to define the FIFO required with the type of data it will be holding using
Once the FIFO has been defined we can use a number of functions available to add,remove, check the FIFO. To put data into the FIFO
To take the date out of the FIFO
Just to see the above functions in use we will create a proc entry which will output one data from a FIFO that gets created as soon as the module is inserted into the kernel.
To create a module with 6 char entries
An array of values to be added to the FIFO
To add the entries into the FIFO when the module gets inserted into the kernel, the init function needs to call the kfifo_put function
Once the fifo has been created, we can extract one data at a time using the function kfifo_get . To ensure that only one data is read from the FIFO each time we use the cat command on the proc entry extra checks and flags are needed
The function kfifo_is_empty returns true when the fifo is empty else it returns false.
The full code for creation of the proc entry called "fifo" which will output one data value at a time is:
If the file is saved as proc_read_fifo.c, it can be compiled using
To test the code, once the compile and insert the code
Read the proc entry using the cat command
The output shows that every call to read the entry removes one item from the fifo and when all entries are removed "FIFO empty" message gets printed.
The first step in creation of FIFO is to define the FIFO required with the type of data it will be holding using
Once the FIFO has been defined we can use a number of functions available to add,remove, check the FIFO. To put data into the FIFO
To take the date out of the FIFO
Just to see the above functions in use we will create a proc entry which will output one data from a FIFO that gets created as soon as the module is inserted into the kernel.
To create a module with 6 char entries
An array of values to be added to the FIFO
To add the entries into the FIFO when the module gets inserted into the kernel, the init function needs to call the kfifo_put function
Once the fifo has been created, we can extract one data at a time using the function kfifo_get . To ensure that only one data is read from the FIFO each time we use the cat command on the proc entry extra checks and flags are needed
The function kfifo_is_empty returns true when the fifo is empty else it returns false.
The full code for creation of the proc entry called "fifo" which will output one data value at a time is:
If the file is saved as proc_read_fifo.c, it can be compiled using
To test the code, once the compile and insert the code
Read the proc entry using the cat command
The output shows that every call to read the entry removes one item from the fifo and when all entries are removed "FIFO empty" message gets printed.
No comments:
Post a Comment