Linux kernel "current" macro

Source: Internet
Author: User

-------------------------------Source---------------------------------

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/tty.h>

/* Function declaration */
void Tty_write_message1 (struct tty_struct *, char *);

/* Initialize function */
static int my_init (void)
{
Char *msg = "Hello tty!\n";
PRINTK ("Hello, from the kernel...\n");

if (current! = NULL)
PRINTK ("pid (command) =%d (%s) \ n", Current->pid,
CURRENT->COMM);

}

/* Cleanup function */
static void My_cleanup (void)
{
PRINTK ("Goodbye, from the kernel ... \ n");
}


Module_init (My_init);
Module_exit (My_cleanup);

This routine is borrowed from <printk.c>
void Tty_write_message1 (struct tty_struct *tty, char *msg)
{
if (TTY && tty->driver && tty->driver->ops && tty->driver->ops->write)
Tty->driver->ops->write (TTY, MSG, strlen (msg));

Write TTY


return;
}

------------------------------Output---------------------

$ sudo insmod Currentptr.ko
Hello tty!

[1029.174761] Hello, from the kernel ...
[1029.174777] pid (command) = 2726 (Insmod)
[1029.174780] Parent PID = 2725 (sudo)
[1029.174782] Parent Parent PID = 7556 (bash)
[1029.174784] Parent parent Parent PID = 7529 (gnome-terminal)

----------------------------------------------------------------------------------------

In the Linux kernel world, with a current macro, you can explore the process-related code

such as: Print process list

struct Task_struct *task

For_each_process (Task)
{
PRINTK (kern_warning "%8d%8ld%8d%s\n", Task->pid,
Task->state, TASK->ON_CPU, Task->comm);
}

---------------------------------------

[2597.598496] 1 1 0 init
[2597.598499] 2 1 0 kthreadd
[2597.598501] 3 1 0 ksoftirqd/0
[2597.598503] 6 1 0 migration/0
[2597.598506] 7 1 0 watchdog/0
[2597.598508] 8 1 0 migration/1

Task_struct is a process structure the size of this structure is approximately 4k in size it can be seen that the structure of a process is associated with a lot of information, and this structure has a lot of pointers,

In the/include/asm-generic/current.h definition does not know whether accurate

struct task_struct;0009 0010 declare_per_cpu (structstaticstruct task_struct * Get_current (void) 0013 {0014     return#define current get_current ()

If you assign a value to current,
current = NULL;
Lvalue required as left operand of assignment
Can be current is not a variable

How do you ensure that current does not point to macros on other cores in multicore systems?
According to DECLARE_PER_CPU, This_cpu_read_stable can have some information.




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.