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 copytouser
#include for kmalloc and kfree
#include for getrandombytes
#define PROCNAME "kruskalmaze"
#define WIDTH Maze width must be odd
#define HEIGHT Maze height must be odd
#define BUFSIZE WIDTH HEIGHT HEIGHT Extra space for newlines
static char mazedata;
Disjoint set UnionFind structures for Kruskal's algorithm
struct disjointset
int parent;
int rank;
;
static struct disjointset sets;
Name:
Date:
Description: This function finds the root of the disjoint set containing the given element.
int findint x
if setsxparent x
setsxparent findsetsxparent;
return setsxparent;
Name:
Date:
Description: This function unites two disjoint sets.
void unionsetsint x int y
int rootX findx;
int rootY findy;
if rootX rootY
if setsrootXrank setsrootYrank
setsrootXparent rootY;
else if setsrootXrank setsrootYrank
setsrootYparent rootX;
else
setsrootYparent rootX;
setsrootXrank;
Name:
Date:
Description: This function initializes the disjoint sets for Kruskal's algorithm.
void initializesetsvoid
int i;
sets kmallocsizeofstruct disjointset WIDTH HEIGHT, GFPKERNEL;
for i ; i WIDTH HEIGHT; i
setsiparent i;
setsirank ;
Name:
Date:
Description: This function generates a maze using Kruskal's algorithm.
# is used for walls and is used for paths.
void generatekruskalmazevoid
int i x y;
int totalcells WIDTH HEIGHT ;
int edges edgecount ;
Initialize maze with walls
memsetmazedata, # BUFSIZE;
for y ; y HEIGHT; y
for x ; x WIDTH; x
mazedatay WIDTH x; Cells
Generate all potential edges vertical and horizontal walls
edges kmallocsizeofintWIDTH HEIGHT GFPKERNEL;
for y ; y HEIGHT; y
for x ; x WIDTH; x
if x WIDTH edgesedgecounty WIDTH xy WIDTH x ;
if y HEIGHT edgesedgecounty WIDTH xy WIDTH x;
Randomly shuffle the edges
for i ; i edgecount; i
int j;
getrandombytes&j sizeofj;
j j edgecount;
if j j j;
Swap edge i and edge j
int temp edgesi;
edgesi edgesj;
edgesj temp;
Apply Kruskal's algorithm
for i ; i edgecount; i
int cell edgesi;
int cell edgesi & xFFFF;
if findcell findcell
unionsetscell cell;
int wall cell cell;
mazedatawall; Knock down the wall
Add newline characters for consolefriendly display
for y ; y HEIGHT; y
mazedatay WIDTH WIDTH
; Insert newline after each row
kfreeedges;
Name:
Date:
Description: This function is called when the proc file is read.
It copies the maze data into the user buffer.
ssizet procreadstruct file file char user buf sizet count, lofft pos
ssizet len BUFSIZE;
if pos len copytouserbuf mazedata, len
return ; 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 mazeinitvoid
mazedata kmallocBUFSIZE, GFPKERNEL;
if mazedata
return ENOMEM;
Initialize disjoint sets
initializesets;
Generate maze using Kruskal's algorithm
generatekruskalmaze;
Create proc file
proccreatePROCNAME, NULL, &procfileops;
printkKERNINFO "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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
