Question: My assignment is to develop a Linux module that uses a proc file read request to generate a maze and send it back to the

My assignment is to develop a Linux module that uses
a proc file read request to generate a maze and send it back to the
user.
I am supposed to call my .c file with a python file, which is not a problem, but I am having issues with the .c file
Below is my code:
#include
#include
#include
#include // for copy_to_user
#include // for kmalloc and kfree
#include // for get_random_bytes
#define PROC_NAME "kruskal_maze"
#define WIDTH 63// Maze width (must be odd)
#define HEIGHT 23// Maze height (must be odd)
#define BUF_SIZE (WIDTH * HEIGHT + HEIGHT)// Extra space for newlines
static char *maze_data;
// Disjoint set (Union-Find) structures for Kruskal's algorithm
struct disjoint_set {
int parent;
int rank;
};
static struct disjoint_set *sets;
/* Name:
* Date:
* Description: This function finds the root of the disjoint set containing the given element.
*/
int find(int x){
if (sets[x].parent != x){
sets[x].parent = find(sets[x].parent);
}
return sets[x].parent;
}
/* Name:
* Date:
* Description: This function unites two disjoint sets.
*/
void union_sets(int x, int y){
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY){
if (sets[rootX].rank < sets[rootY].rank){
sets[rootX].parent = rootY;
} else if (sets[rootX].rank > sets[rootY].rank){
sets[rootY].parent = rootX;
} else {
sets[rootY].parent = rootX;
sets[rootX].rank++;
}
}
}
/* Name:
* Date:
* Description: This function initializes the disjoint sets for Kruskal's algorithm.
*/
void initialize_sets(void){
int i;
sets = kmalloc(sizeof(struct disjoint_set)* WIDTH * HEIGHT, GFP_KERNEL);
for (i =0; i < WIDTH * HEIGHT; i++){
sets[i].parent = i;
sets[i].rank =0;
}
}
/* Name:
* Date:
* Description: This function generates a maze using Kruskal's algorithm.
*'#' is used for walls and '' is used for paths.
*/
void generate_kruskal_maze(void){
int i, x, y;
int total_cells =((WIDTH -1)/2)*((HEIGHT -1)/2);
int *edges, edge_count =0;
// Initialize maze with walls
memset(maze_data, '#', BUF_SIZE);
for (y =1; y < HEIGHT; y +=2){
for (x =1; x < WIDTH; x +=2){
maze_data[y * WIDTH + x]=''; // Cells
}
}
// Generate all potential edges (vertical and horizontal walls)
edges = kmalloc(sizeof(int)*(WIDTH * HEIGHT *2), GFP_KERNEL);
for (y =1; y < HEIGHT; y +=2){
for (x =1; x < WIDTH; x +=2){
if (x +2< WIDTH) edges[edge_count++]=(y * WIDTH + x)<<16|(y * WIDTH + x +2);
if (y +2< HEIGHT) edges[edge_count++]=(y * WIDTH + x)<<16|((y +2)* WIDTH + x);
}
}
// Randomly shuffle the edges
for (i =0; i < edge_count; i++){
int j;
get_random_bytes(&j, sizeof(j));
j = j % edge_count;
if (j <0) j =-j;
// Swap edge i and edge j
int temp = edges[i];
edges[i]= edges[j];
edges[j]= temp;
}
// Apply Kruskal's algorithm
for (i =0; i < edge_count; i++){
int cell1= edges[i]>>16;
int cell2= edges[i] & 0xFFFF;
if (find(cell1)!= find(cell2)){
union_sets(cell1, cell2);
int wall =(cell1+ cell2)/2;
maze_data[wall]=''; // Knock down the wall
}
}
// Add newline characters for console-friendly display
for (y =0; y < HEIGHT; y++){
maze_data[y *(WIDTH +1)+ WIDTH]='
'; // Insert newline after each row
}
kfree(edges);
}
/* Name:
* Date:
* Description: This function is called when the proc file is read.
* It copies the maze data into the user buffer.
*/
ssize_t proc_read(struct file *file, char __user *buf, size_t count, loff_t *pos){
ssize_t len = BUF_SIZE;
if (*pos >= len || copy_to_user(buf, maze_data, len)){
return 0; // End of file
}
*pos += len;
return len;
}
/* Name:
* Date:
* Description: This function is called when the module is loaded.
* It creates the proc file and generates the maze.
*/
static int __init maze_init(void){
maze_data = kmalloc(BUF_SIZE, GFP_KERNEL);
if (!maze_data){
return -ENOMEM;
}
// Initialize disjoint sets
initialize_sets();
// Generate maze using Kruskal's algorithm
generate_kruskal_maze();
// Create proc file
proc_create(PROC_NAME, 0, NULL, &proc_file_ops);
printk(KERN_INFO "Kruskal Maze module loaded, /
What do I need to do to fix the code to work properly?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!