Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include<linux/module.h>
  2. #include<linux/random.h>
  3. #include<linux/kfifo.h>
  4.  
  5. #define NUMBER_OF_ELEMENTS 2
  6.  
  7. static char* foo = "Pierwszy ciag znakowy";
  8. module_param(foo,charp,0644);
  9. MODULE_PARM_DESC(foo,"ciag pierwszy");
  10. static char* foo2 = "drugi ciag znakowy";
  11. module_param(foo2,charp,0644);
  12. MODULE_PARM_DESC(foo2,"ciag drugi");
  13.  
  14. static struct kfifo fifo_queue;
  15.  
  16. static int __init fifomod_init(void)
  17. {
  18. int returned_value;
  19. unsigned int returned_size;
  20. char buff[20];
  21. int fifo_size = sizeof(foo)+sizeof(foo2);
  22.  
  23. returned_value = kfifo_alloc(&fifo_queue,fifo_size,GFP_KERNEL);
  24. if(returned_value) {
  25. pr_alert("Error allocating kfifo!\n");
  26. return -ENOMEM;
  27. }
  28. while(!kfifo_is_full(&fifo_queue)) {
  29. returned_size = kfifo_in(&fifo_queue,foo,sizeof(foo));
  30. if(returned_size!=sizeof(foo))
  31. pr_alert("Enqueue error 1\n");
  32.  
  33. returned_size = kfifo_in(&fifo_queue,foo2,sizeof(foo2));
  34. if(returned_size!=sizeof(foo2))
  35. pr_alert("Enqueue error 2\n");
  36. }
  37. returned_size = kfifo_out_peek(&fifo_queue,buff,sizeof(buff));
  38. if(returned_size!=sizeof(buff))
  39. pr_alert("Error peeking element form the queue!\n");
  40. pr_notice("Value in the queue: %u\n",buff);
  41.  
  42. return 0;
  43. }
  44.  
  45. static void __exit fifomod_exit(void)
  46. {
  47. int returned_size;
  48. char buff[20];
  49.  
  50. while(!kfifo_is_empty(&fifo_queue)) {
  51. returned_size = kfifo_out(&fifo_queue,buff,sizeof(buff));
  52. if(returned_size!=sizeof(buff))
  53. pr_alert("Dequeue error!\n");
  54. pr_notice("Value from the queue: %u\n",buff);
  55. }
  56.  
  57. kfifo_free(&fifo_queue);
  58. }
  59.  
  60. module_init(fifomod_init);
  61. module_exit(fifomod_exit);
  62.  
  63. MODULE_AUTHOR("Arkadiusz Chrobot <a.chrobot@tu.kielce.pl>");
  64. MODULE_LICENSE("GPL");
  65. MODULE_DESCRIPTION("An exemplary module demonstrating the usage of a kernel FIFO queue.");
  66. MODULE_VERSION("1.0");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement