Linux driver current keywords

Source: Internet
Author: User
Although the kernel module does not execute in the same order as an application, most of the actions performed by the kernel represent a specific process. the kernel code can reference the current process by accessing the global item current, which is in the & lt; asm/current. h & gt;, which generates a pointer pointing to the structure task_st...

Although the kernel module does not execute in the same order as an application, most of the actions performed by the kernel represent a specific process. the kernel code can reference the current process. by accessing the global item current, it is defined in, it generates a pointer pointing to the structure task_struct, in Definition. the current pointer points to the currently running process. during the execution of a system call, such as open or read, the current process is the process that initiates the call. the kernel code can use process-specific information by using current, if it needs.
In fact, current is not really a global variable. to support the SMP system, kernel developers must be forced to develop a mechanism to find the current process on the CPU. this mechanism must also be fast, because references to current occur frequently. the result is a dependency system mechanism, which often hides a pointer pointing to task_struct in the kernel stack. the implementation details are hidden from other kernel subsystems. a device driver can only contain And reference the current process. for example, the following statement prints the process ID and command name of the current process, by accessing some fields in the task_struct structure.
Printk (KERN_INFO "The process is \" % s \ "(pid % I) \ n", current-> comm, current-> pid );
Saved in current-> comm
Command name is the basic name of the program file executed by the current process (truncated to 15 characters, if needed ).
 
In linux, every process is defined by the task_struct data structure. task_struct is what we usually call PCB. she is the only and most effective means of process control. when we call fork (), the system will generate a task_struct structure for us. then inherit some data from the parent process and insert the new process into the process tree for process management. therefore, understanding the structure of task_struct is critical to understanding task scheduling (in linux, tasks and processes are the same concept. before analyzing the definition of task_struct. let's first push its structure based on our theory. 1. process status, which records the waiting, running, or deadlock of the process. 2. scheduling information, scheduling by which scheduling function, and scheduling. 3. process communication status. 4, to insert a process tree, you must have a pointer to the parent and child brothers, of course, task_struct type 5. time information, such as calculating the execution time so that the cpu allocates 6 labels, it is decided that the process belongs to 7, and some file information that can be read and written to and opened 8, process context and kernel context 9, processor context 10, memory information because each PCB is like this, only these structures, to meet all the requirements of a process. open/include/linux/sched. h. find the definition of task_struct.
Struct task_struct {/* these are hardcoded-don't touch */Here are some hardware settings that are transparent to the program. the state indicates whether the process can be executed or interrupted. flage indicates the process number. when fork () is called, addr_limit distinguishes kernel processes from common processes in memory.
Volatile long state;/*-1 unrunnable, 0 runnable,> 0 stopped */
Unsigned long flags;/* per process flags, defined below */int sigpending;
Mm_segment_t addr_limit;/* thread address space: 0-0xBFFFFFFF for user-thead 0-0xFFFFFFFF for kernel-thread */
Struct exec_domain * exec_domain;
Long need_resched;
/* Various fields */count is the counter priorrity is the priority
Long counter;
Long priority;
Cycles_t avg_slice;
/* SMP and runqueue state */variable defined for multiple processors.
Int has_cpu;
Int processor;
Int last_processor;
Int lock_depth;/* Lock depth. We can context switch in and out of holding a syscall kernel lock ...*/
In order to sort in the process tree, the defined parent and child, brother pointer
Struct task_struct * next_task, * prev_task;
Struct task_struct * next_run, * prev_run;
/* Task state */defines the task running status and signal
Struct linux_binfmt * binfmt;
Int exit_code, exit_signal;
Int pdeath_signal;/* The signal sent when the parent dies * // * defines The user numbers, user groups, and process groups of processes */
Unsigned long personality;
Int dumpable: 1;
Int did_exec: 1;
Pid_t pid;
Pid_t pgrp;
Pid_t tty_old_pgrp;
Pid_t session;
/* Boolean value for session group leader */
Is it the header file of the process group?
Int leader;
/*
* Pointers to (original) parent process, youngest child, younger sibling,
* Older sibling, respectively. (p-> father can be replaced with * p-> p_pptr-> pid)
*/
Pointers of parent and child processes
Struct task_struct * p_opptr, * p_pptr, * p_cptr, * p_ysptr, * p_osptr;
/* PID hash table linkage. */some hash tables used in scheduling
Struct task_struct * pidhash_next;
Struct task_struct ** pidhash_pprev;
/* Pointer to task [] array linkage .*/
Struct task_struct ** tarray_ptr;
Struct wait_queue * wait_chldexit;/* for wait4 () waiting queue */struct semaphore * vfork_sem;/* for vfork ()*/
Unsigned long policy, rt_priority;
Unsigned long it_real_value, it_prof_value, it_assist_value;
The nature of a process because the real-time process is different from the scheduling algorithm of a common process, it should be differentiated by variables.
The following are some time information about the process.
Unsigned long it_real_incr, it_prof_incr, it_assist_incr;
Struct timer_list real_timer;
Struct tms times;
Unsigned long start_time;
Long per_cpu_utime [NR_CPUS], per_cpu_stime [NR_CPUS]; defines the size of the time slice
/* Mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
Memory information
Unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; int swappable: 1;
/* Process credentials */
Uid_t uid, euid, suid, fsuid;
Gid_t gid, egid, sgid, fsgid;
Int ngroups;
Gid_t groups [NGROUPS];
Kernel_cap_t cap_effective, cap_inheritable, cap_permitted;
Struct user_struct * user;
The following English notes are clear:
/* Limits */
Struct rlimit rlim [RLIM_NLIMITS];
Unsigned short used_math;
Char comm [16];/* file system info */
Int link_count;
Struct tty_struct * tty;/* NULL if no tty */
/* Ipc stuff */
Struct sem_undo * semundo;
Struct sem_queue * semsleeping;
/* Tss for this task */
Struct thread_struct tss;
/* Filesystem information */
Struct fs_struct * fs;
/* Open file information */
Struct files_struct * files;
/* Memory management info */
Struct mm_struct * mm;
/* Signal handlers */
Spinlock_t sigmask_lock;
/* Protects signal and blocked */
Struct signal_struct * sig; sigset_t signal, blocked;
Struct signal_queue * sigqueue, ** sigqueue_tail;
Unsigned long sas_ss_sp;
Size_t sas_ss_size;
};
After this structure is analyzed, there are still many problems to think about, maybe not to read, but the framework should be well done. the following problems need to be solved: 1. the constants used in task_struct are defined, such as the maximum number of processes, the maximum number of CPUs supported, and so on. 2. when fork () is called, the system allocates a piece of memory. will malloc (1, sizeof (struct task_struck) copy some variables or share a part of memory with the parent process. how to implement the malloc function (in the memory management part, but I don't think I can't) 3 ,. how can we implement a thread? 4. the scheduling policy function schedule () has several situations: time slice rotation, preemption, priority preemption, and multi-level feedback. in addition to time slice rotation, process trees must be traversed (not required for real-time process fifo mechanism). how does linux ensure efficiency? If you change the maximum line to a number, will the efficiency be reduced by 5? what are the pipelines used for process communication and signal structures?

Author "programmer"

Related Article

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.