Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does current->pid work for linux?

Tags:

c

linux

process

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

like image 310
randomizertech Avatar asked May 31 '12 17:05

randomizertech


People also ask

How is PID determined in Linux?

A PID is automatically assigned to each process when it is created. A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name.

How can I get current PID?

You can get the process ID of a process by calling getpid . The function getppid returns the process ID of the parent of the current process (this is also known as the parent process ID).

What is PID in Linux?

Overview. As Linux users, we're familiar with process identifiers (PID). PID is the operating system's unique identifier for active programs that are running. A simple command to view the running processes shows that the init process is the owner of PID 1.

How do you find the PID of a kernel module?

pid = task_pid_nr(current); to get the current task's pid.


2 Answers

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

like image 135
cnicutar Avatar answered Nov 18 '22 04:11

cnicutar


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:

  • current.h
  • sched.h
like image 7
betabandido Avatar answered Nov 18 '22 04:11

betabandido