Page 1 of 1
unknown type name in structs
Posted: Sat Dec 11, 2021 4:15 pm
by 22OsC
Why does this code below is giving me "unknown type name ‘PROCESS’" and "unknown type name ‘THREAD’" ????
Code: Select all
typedef struct _PROCESS
{
uint64_t pid;
char *name;
PROCESS *parent;
THREAD *thread;
int32_t exitcode;
REGISTERS regs;
PageTable *address_space;
PROCESS *next;
struct list_head sibling;
struct list_head children;
} PROCESS;
typedef struct _THREAD
{
uint64_t tid;
uint32_t flags;
enum ThreadState state;
enum ThreadPolicy policy;
enum ThreadPriority priority;
PROCESS *parent;
PageTable* stack;
REGISTERS regs;
struct plist_node sibling;
} THREAD;
I don't understand what's wrong. I am using GCC cross compiler 9.3.0 and the flags are
-std=gnu11 -ffreestanding -fno-stack-protector -fno-pic -fno-pie -mno-80387 -mno-mmx -mno-3dnow -mno-red-zone -mno-sse -mno-sse2 -march=nehalem -mcmodel=kernel
And yes, that's all in the header file.
edit: replacing with struct _PROCESS and struct _THREAD won't work, no compiler errors but I get random garbage
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 4:26 pm
by vhaudiquet
You're trying to use `PROCESS` before you defined it.
You need to use `struct _PROCESS*` instead inside of the structure, because at this time `PROCESS` does not exist yet.
The order you declares things in is important in C. You can't use something before it's definition.
The only exception being structure pointers, because you're telling the compiler "this is a pointer to something", so he knows the size of the object (sizeof(ptr_t)) and can allocate it for you, even if he doesnt know what's inside it.
Please note that this has nothing to do with OS Development...
For more details, please refer to a C programming course.
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 4:32 pm
by vhaudiquet
edit: replacing with struct _PROCESS and struct _THREAD won't work, no compiler errors but I get random garbage
Last edited by 22OsC on Sun Dec 12, 2021 12:29 am, edited 1 time in total.
I see you edited your post after my message ; why do you say it 'won't work' ? if you get no compiler errors it seems to be an improvement to me.
What kind of 'random garbage' are you getting, and where ? random garbage as compiler output ? random garbage at runtime ? what are you talking about ?
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 4:50 pm
by 22OsC
vhaudiquet wrote:edit: replacing with struct _PROCESS and struct _THREAD won't work, no compiler errors but I get random garbage
Last edited by 22OsC on Sun Dec 12, 2021 12:29 am, edited 1 time in total.
I see you edited your post after my message ; why do you say it 'won't work' ? if you get no compiler errors it seems to be an improvement to me.
What kind of 'random garbage' are you getting, and where ? random garbage as compiler output ? random garbage at runtime ? what are you talking about ?
I create a process with name "main" with pid 1, when I try to get there stuff I get for name "3" or something like that and pid just random numbers
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 4:56 pm
by vhaudiquet
Do you have a repository where your code is ? You do realize that the problem you're describing is not only related to those structures, but also to the way you are using them...
By the way, out of curiosity, why do you use different variable types naming conventions : mixing `THREAD` with `PageTable` and `struct list_head` ?
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 5:20 pm
by 22OsC
vhaudiquet wrote:Do you have a repository where your code is ? You do realize that the problem you're describing is not only related to those structures, but also to the way you are using them...
By the way, out of curiosity, why do you use different variable types naming conventions : mixing `THREAD` with `PageTable` and `struct list_head` ?
are other structs, PageTable is from
https://github.com/Absurdponcho/PonchoOS (the os is based from that tutorial) and struct list_head is from linux implementation I managed to get scheduler working and the problem was actually from memory implementation, I used `THREAD *thread = calloc(1, sizeof(THREAD));` but in the calloc I had a typo and now it's fixed. sorry for wasting your time
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 5:24 pm
by Ethin
This is an easy mistake to fix. I'm going to assume that this is in a header file. To resolve this problem, in C, do something like (above your usage of struct process):
You can also (if this structure is hidden) declare the struct in the header file but define it in the C file:
Code: Select all
//In the header file:
typedef struct process process;
In your C source:
Code: Select all
typedef struct _process {
// members
} process;
I'd encourage you however to take a bit of conventions from the Linux kernel, particularly involving the abuse/over-use of typedef to make C look "object-oriented". Avoid use of typedef struct foo foo; if you can, and instead use struct foo to make it clear what foo is.
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 5:42 pm
by deadmutex
You need to use forward declarations if you want to use your struct as a type specifier before it's defined.
Re: unknown type name in structs
Posted: Sat Dec 11, 2021 11:41 pm
by davmac314
vhaudiquet wrote:You're trying to use `PROCESS` before you defined it.
You need to use `struct _PROCESS*` instead inside of the structure, because at this time `PROCESS` does not exist yet.
This would solve the compilation error, but isn't necessary. You can just add appropriate forward declarations:
Code: Select all
typedef struct _PROCESS PROCESS;
typedef struct _THREAD THREAD;
This is, I think, the same fix that Ethin was suggesting above, although he changed the names of the structures and typedefs.