10

Do I need to include a library? Can anyone please elaborate in it?

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

But I want to printk something with current->pid

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

...and is giving me an error

error: dereferencing pointer to incomplete type

Manishearth
  • 14,882
  • 8
  • 59
  • 76
randomizertech
  • 2,309
  • 15
  • 48
  • 85

3 Answers3

15

You're looking for #include <linux/sched.h>. That's where task_struct is declared.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • +1 I think you actually answered the question, as opposed to me. – Rob I May 31 '12 at 17:32
  • @RobI Thanks. I'm not entirely sure either, but I think the question is about kernel programming, given the [previous question](http://stackoverflow.com/questions/10837446/module-init-not-showing-the-printk-that-i-want-it-to). – cnicutar May 31 '12 at 17:34
  • `#include ` is needed as well. Thanks! – randomizertech May 31 '12 at 18:10
7

Your code should work. You are probably missing some header.

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

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 points to the task running on a CPU at a given moment. Its type is struct task_struct and it 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 browse the code for these files in the Linux Cross Reference:

betabandido
  • 18,946
  • 11
  • 62
  • 76
-2

I think you're looking for the getpid() system call. I don't know what current is though.

Rob I
  • 5,627
  • 2
  • 21
  • 28