xosski

ElevateX

Jan 9th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <linux/kernel.h>
  2. #include <linux/syscalls.h>
  3. #include <linux/slab.h> // For memory allocation
  4. #include <linux/list.h> // For linked list implementation
  5. #include <linux/mutex.h> // For mutex lock to synchronize elevator state
  6.  
  7. // Elevator state
  8. enum elevator_state { IDLE, MOVING, OFFLINE };
  9.  
  10. // Elevator request type
  11. enum passenger_type { REGULAR = 0, VIP, MAINTENANCE, EMERGENCY };
  12.  
  13. // Request structure
  14. struct elevator_request {
  15. int start_floor;
  16. int destination_floor;
  17. enum passenger_type type;
  18. struct list_head list;
  19. };
  20.  
  21. // Elevator structure
  22. struct elevator {
  23. enum elevator_state state;
  24. int current_floor;
  25. int num_passengers;
  26. struct list_head request_queue;
  27. struct mutex lock; // Mutex for synchronizing elevator operations
  28. };
  29.  
  30. // Initialize elevator structure
  31. static struct elevator *elevator = NULL;
  32.  
  33. // Start elevator system
  34. SYSCALL_DEFINE0(start_elevator) {
  35. if (elevator) {
  36. // Elevator is already active
  37. return -EINVAL;
  38. }
  39.  
  40. elevator = kmalloc(sizeof(struct elevator), GFP_KERNEL);
  41. if (!elevator) {
  42. // Memory allocation failed
  43. return -ENOMEM;
  44. }
  45.  
  46. // Initialize the elevator state
  47. elevator->state = IDLE;
  48. elevator->current_floor = 1;
  49. elevator->num_passengers = 0;
  50. INIT_LIST_HEAD(&elevator->request_queue);
  51. mutex_init(&elevator->lock);
  52.  
  53. printk(KERN_INFO "Elevator started\n");
  54. return 0; // Successfully started
  55. }
  56.  
  57. // Issue a request to the elevator system
  58. SYSCALL_DEFINE3(issue_request, int, start_floor, int, destination_floor, int, type) {
  59. if (!elevator) {
  60. // Elevator is not active
  61. return -ENODEV;
  62. }
  63.  
  64. // Validate request parameters
  65. if (start_floor < 1 || start_floor > 10 || destination_floor < 1 || destination_floor > 10 || type < 0 || type > 3) {
  66. return -EINVAL; // Invalid request parameters
  67. }
  68.  
  69. struct elevator_request *new_request;
  70. new_request = kmalloc(sizeof(struct elevator_request), GFP_KERNEL);
  71. if (!new_request) {
  72. return -ENOMEM;
  73. }
  74.  
  75. // Initialize the new request
  76. new_request->start_floor = start_floor;
  77. new_request->destination_floor = destination_floor;
  78. new_request->type = type;
  79.  
  80. // Add the new request to the queue
  81. mutex_lock(&elevator->lock);
  82. list_add_tail(&new_request->list, &elevator->request_queue);
  83. mutex_unlock(&elevator->lock);
  84.  
  85. printk(KERN_INFO "Request added: Start floor %d, Destination floor %d, Passenger type %d\n", start_floor, destination_floor, type);
  86.  
  87. return 0; // Valid request
  88. }
  89.  
  90. // Process elevator requests
  91. static void process_requests(void) {
  92. struct elevator_request *request, *tmp;
  93.  
  94. mutex_lock(&elevator->lock);
  95.  
  96. // Process the requests in the queue
  97. list_for_each_entry_safe(request, tmp, &elevator->request_queue, list) {
  98. if (elevator->state != MOVING) {
  99. // If the elevator is not moving, start moving to the requested floor
  100. elevator->state = MOVING;
  101. printk(KERN_INFO "Elevator moving from floor %d to floor %d\n", elevator->current_floor, request->start_floor);
  102. elevator->current_floor = request->start_floor;
  103.  
  104. // Move to the destination floor
  105. printk(KERN_INFO "Elevator moving to destination floor %d\n", request->destination_floor);
  106. elevator->current_floor = request->destination_floor;
  107.  
  108. // Simulate passengers arriving
  109. elevator->num_passengers++;
  110.  
  111. // After processing the request, remove it from the queue
  112. list_del(&request->list);
  113. kfree(request); // Free the memory for the processed request
  114. }
  115. }
  116.  
  117. elevator->state = IDLE; // Return to IDLE state after processing all requests
  118. mutex_unlock(&elevator->lock);
  119. }
  120.  
  121. // Stop the elevator system
  122. SYSCALL_DEFINE0(stop_elevator) {
  123. if (!elevator) {
  124. // Elevator is not active
  125. return -ENODEV;
  126. }
  127.  
  128. // Ensure there are no passengers before stopping
  129. if (elevator->num_passengers > 0) {
  130. return -EBUSY; // Passengers are still onboard, cannot stop
  131. }
  132.  
  133. // Deactivate the elevator system and free memory
  134. mutex_lock(&elevator->lock);
  135. kfree(elevator);
  136. elevator = NULL;
  137. mutex_unlock(&elevator->lock);
  138.  
  139. printk(KERN_INFO "Elevator stopped\n");
  140. return 0; // Successfully stopped
  141. }
  142.  
  143. // Helper function to simulate elevator operations periodically (e.g., on a timer or tasklet)
  144. void elevator_tasklet(void) {
  145. if (elevator) {
  146. process_requests();
  147. }
  148. }
Add Comment
Please, Sign In to add comment