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;
-
include/linux/sched.h
-
-
task_struct {
-
-
.........
-
-
unsigned long sched_times;
-
-
..........
-
-
};
Initialize sched_times to 0 when creating a new process.
-
Kernel/fork. c do_fork () function
-
-
long do_fork(unsigned long clone_flags,
-
unsigned long stack_start,
-
struct pt_regs *regs,
-
unsigned long stack_size,
-
int __user *parent_tidptr,
-
int __user *child_tidptr)
-
{
-
struct task_struct *p;
-
int trace = 0;
-
struct pid *pid = alloc_pid();
-
long nr;
-
............
-
-
p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);
-
p->sched_times = 0;
-
................
-
-
}
Sched_times ++
-
Kernel/sched. c scheduale () function
-
-
asmlinkage void __sched schedule(void)
-
-
{
-
-
........
-
-
idx = sched_find_first_bit(array->bitmap);
-
queue = array->queue + idx;
-
next = list_entry(queue->next, struct task_struct, run_list);
-
next->sched_times ++;
-
if (!rt_task(next) && interactive_sleep(next->sleep_type)) {
-
unsigned long long delta = now - next->timestamp;
-
if (unlikely((long long)(now - next->timestamp) < 0))
-
delta = 0;
-
.........
-
-
}
-
Add system call
-
Last side of kernel/sys. c
-
-
asmlinkage long sys_get_sched_times(unsigned long * addr)
-
-
{
-
printk(KERN_ALERT "get_sched_times called,sched_times=%d",current->sched_times);
-
return copy_to_user(addr,&(current->sched_times),sizeof(long));
-
-
}
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 ...)
-
-
#include
-
-
int main()
-
{
-
unsigned long num;
-
int i,x,sum=0;
-
-
scanf("%d",&x);
-
for(i=0;i
-
sum = sum +i;
-
printf("%d\n",sum);
-
-
if(syscall(318,&num))
-
printf("Failed\n");
-
else
-
printf("sched_times =%d\n",num);
-
return 0;
-
}
Running result:
User layer
Kernel layer
This article is from the blog of "Niu Ge's blog ".