How does current-> pid work for linux?

Do I need to include a library? Can anyone comment on this?

I know what is used to get the process id of the current task called from

But I want to print something with current-> pid

printk("My current process id/pid is %d\n", current->pid);

... and gives me an error

error: dereferencing pointer to incomplete type

+8
c linux process
source share
3 answers

You are looking for #include <linux/sched.h> . This is where task_struct declared.

+12
source share

Your code should work. You are probably missing the title.

current is the per-cpu variable defined in linux/arch/x86/include/asm/current.h (all code refers to the x86 case):

 DECLARE_PER_CPU(struct task_struct *, current_task); static __always_inline struct task_struct *get_current(void) { return percpu_read_stable(current_task); } #define current get_current() 

current indicates the task currently running on the processor. Its type is struct task_struct and is defined in linux/include/linux/sched.h :

 struct task_struct { ... pid_t pid; // process identifier pid_t tgid; // process thread group id ... }; 

You can view the code for these files in the Linux Cross Reference :

+7
source share

I think you are looking for the getpid () system call. I do not know what current .

-3
source share

All Articles