Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using for_each_process() in my program prevents me from compiling, compiler says semicolon expected?

I'm trying to write a very simple piece of code for a class, but I'm just stumped as to why I can't compile it. Sorry if this is a duplicate or silly question, but I couldn't find any others that answered this for me. My full program is pasted below. When I try to compile, I get the following error:

test.c: In function 'main':

test.c:7:27: error: expected ';' before '{' token

Here's the code:

#include<stdio.h>
#include<linux/sched.h>
#include<linux/kernel.h>

int main(){
    struct task_struct *task;
    for_each_process(task){
        printf("I found task: %d\n", task->pid);
    }
    return 0;
}

I feel like I'm missing something painfully obvious, can anyone point out what the problem is here? I've tried initializing the 'task' object as NULL and using a simpler printf statement that just prints 'test', but nothing I've tried has fixed this compilation error.

like image 790
Ben Avatar asked Feb 10 '23 05:02

Ben


2 Answers

The macro has been moved to <linux/sched/signal.h> in this commit c3edc4010e9d102eb7b8f17d15c2ebc425fed63c in 2017. (https://github.com/torvalds/linux/commit/c3edc4010e9d102eb7b8f17d15c2ebc425fed63c).

(For someone who has trouble in compiling for this reason.)

like image 111
Guo Shuai Avatar answered Feb 16 '23 03:02

Guo Shuai


The preprocessor token you are using for_each_process is either not defined, or not defined to do what you think it does.

Every C++ compiler I've used can be told to dump the post-preprocessing output. If you pass the flag that makes this happen when building your source file, you'll see the code that the compiler is seeing and screwing up on.

gcc -E clang -E or the /E flag in visual studio (heh) for example.

As @Ulrich has mentioned above, apparently that macro is only available within the kernel. Attempting to read <linux/sched.h> directly and determine this is challenging, as there are many, many ifdef/endif pairs.

like image 23
Yakk - Adam Nevraumont Avatar answered Feb 16 '23 04:02

Yakk - Adam Nevraumont