Add a system call for the Linux kernel

Source: Internet
Author: User
Objective: to add a system to the kernel and call longget_shed_times (unsignedlong * num). when the program calls this function, it stores the number of times scheduled by this process into the memory unit pointed to by num, a 32-bit integer. System environment: CentOS5.532bit + 2.6.18sourcecode...

Objective: to add a system to the kernel and call long get_shed_times (unsigned long * num). when the program calls this function, the number of times the process is scheduled is stored in the memory unit pointed to by num, A 32-bit integer.

System environment: CentOS 5.5 32bit + 2.6.18 source code + i386 architecture

First, add the scheduling count variable unsigned long sched_times in task_struct;


  1. include/linux/sched.h  
  2.  
  3. task_struct {  
  4.  
  5.     .........  
  6.  
  7.     unsigned long sched_times;  
  8.  
  9.     ..........  
  10.  
  11. };  

Initialize sched_times to 0 when creating a new process.


  1. Kernel/fork. c do_fork () function
  2.  
  3. long do_fork(unsigned long clone_flags,  
  4.               unsigned long stack_start,  
  5.               struct pt_regs *regs,  
  6.               unsigned long stack_size,  
  7.               int __user *parent_tidptr,  
  8.               int __user *child_tidptr)  
  9. {  
  10.         struct task_struct *p;  
  11.         int trace = 0;  
  12.         struct pid *pid = alloc_pid();  
  13.         long nr;  
  14.         ............  
  15.  
  16.         p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);  
  17.         p->sched_times = 0;  
  18.         ................  
  19.  
  20. }  

 

Sched_times ++

 


  1. Kernel/sched. c scheduale () function
  2.  
  3. asmlinkage void __sched schedule(void)  
  4.  
  5. {   
  6.  
  7.         ........  
  8.  
  9.         idx = sched_find_first_bit(array->bitmap);  
  10.         queue = array->queue + idx;  
  11.         next = list_entry(queue->next, struct task_struct, run_list);  
  12.         next->sched_times ++;  
  13.         if (!rt_task(next) && interactive_sleep(next->sleep_type)) {  
  14.                 unsigned long long delta = now - next->timestamp;  
  15.                 if (unlikely((long long)(now - next->timestamp) < 0))  
  16.                         delta = 0;  
  17.         .........  
  18.  
  19. }  
  20.  

Add system call


  1. Last side of kernel/sys. c
  2.  
  3. asmlinkage long sys_get_sched_times(unsigned long * addr)  
  4.  
  5. {  
  6.             printk(KERN_ALERT "get_sched_times called,sched_times=%d",current->sched_times);
  7.             return copy_to_user(addr,&(current->sched_times),sizeof(long));  
  8.  
  9. }  

Add the system call number include/asm/unistd. h.

Add # define _ NR_get_sched_times 318 at the end, and change nr_syscils to 319.

Add the following content to the arch/i386/kernel/syscall_table.S file:

OK, re-compile the kernel, and restart.

Test:

Generally, system calls are supported by the C library. However, you can also use syscall or the macros provided by Linux.

Macro: _ syscalln () n ranges from 0 to 6, representing the number of parameters to be passed to the system call, such as open

Long open (const char * filename, int flags, int mode );

# Define _ NR_open 5

_ Syscall3 (long, open, const char *, filename, int, flags, int, mode)

The above method was removed after 2.6.20 because of a security vulnerability.

Directly use syscall (system call number, parameter ...)


  1.  
  2. #include 
       
          
       
  3.  
  4. int main()  
  5. {  
  6.         unsigned long num;  
  7.         int i,x,sum=0;  
  8.  
  9.         scanf("%d",&x);  
  10.         for(i=0;i
       
  11.                 sum = sum +i;  
  12.         printf("%d\n",sum);  
  13.  
  14.         if(syscall(318,&num))  
  15.                 printf("Failed\n");  
  16.         else 
  17.                 printf("sched_times =%d\n",num);  
  18.         return 0;  
  19. }  

Running result:

User layer

Kernel layer

 

 

 

 

 

 

 

 

 

This article is from the blog of "Niu Ge's blog ".

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.