Reputation: 161
I am doing some projects on the xv6 operating system. In one of the header files struct proc
is defined.
// Per-process state
struct proc {
/****I added this****/
struct spinlock lock;
/**********************/
uint sz; // Size of process memory (bytes)
pde_t* pgdir; // Page table
char *kstack; // Bottom of kernel stack for this process
enum procstate state; // Process state
int pid; // Process ID
struct proc *parent; // Parent process
struct trapframe *tf; // Trap frame for current syscall
struct context *context; // swtch() here to run process
void *chan; // If non-zero, sleeping on chan
int killed; // If non-zero, have been killed
struct file *ofile[NOFILE]; // Open files
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
};
I added an element of type struct spinlock
(defined in spinlock.h) to struct proc
.
// Mutual exclusion lock.
struct spinlock {
uint locked; // Is the lock held?
// For debugging:
char *name; // Name of lock.
struct cpu *cpu; // The cpu holding the lock.
};
That caused some compilation problems . First, I got this error.
In file included from exec.c:8: proc.h:46:19: error: field ‘lock’ has incomplete type
Which should be easy to fix: I just added #include "spinlock.h"
to proc.h. This time it threw this error.
In file included from proc.h:1, from console.c:15: spinlock.h:2:8: error: redefinition of ‘struct spinlock’
It made me notice something strange about this code and why I was having this redefinition errors: neither spinlock.h nor all of the other header files are #include
guarded.
So, instead of adding #include "spinlock.h"
to proc.h, I had to go to exec.c and do #include "spinlock.h"
before #include "proc.h"
. After that, more errors came. exec.c was not the only file that included proc.h but not spinlock.h before. I ended up doing #include "spinlock.h"
before #include "proc.h"
in more than 10 files.
I don't understand why they didn't #include guard any header file when this is considered so basic to prevent errors. With that, I would have added #include "spinlock.h"
to proc.h and compile successfully instead of going to every file that included proc.h and including spinlock.h before.
I'm assuming this was not an error because the code was written by professionals and there must be a good reason for that.
Upvotes: 2
Views: 182