How the Linux kernel Analytics operating system works

Source: Internet
Author: User
Tags volatile

Fan Original works reproduced please specify the source "Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

According to the experimental requirements, the operation in the experimental building.

Time Slice Rotation method

(Enter VI main.c to view Mykernel source code)

The most core code in MYMAIN.C

(myinterrupt.c code, each time the clock interrupts the call once PRINTK)

2. Operating system Kernel source code "analysis See note"

1.mypcb.h//header files, references in other. c files, primarily data structure definitions

1   /* 2  *  linux/mykernel/mypcb.h 3  * 4  *  Kernel internal PCB types 5  * 6  *  Copyright (C) 2013  Mengning 7  * 8  */ 9 10 #define MAX_TASK_NUM 4 11 #define KERNEL_STACK_SIZE   1024*8 12 13 /* CPU-specific state of this task */ 14 struct Thread { 15 unsigned long        ip; //16 unsigned long        sp; 17 }; 18 19 typedef struct PCB{ //定义进程相关的数据结构20 int pid; //定义进程id21 volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ 22 char stack[KERNEL_STACK_SIZE]; //内核堆栈23 /* CPU-specific state of this task */ 24 struct Thread thread; 25 unsigned long    task_entry; //进程入口26 struct PCB *next; //进程链表27 }tPCB; 28 29 void my_schedule(void); //调度器

2.MYMAIN.C kernel initialization and NO. 0 # process Startup

1/* 2 * LINUX/MYKERNEL/MYMAIN.C 3 * 4 * Kernel Internal My_start_kernel 5 * 6 * Copyright (c) mengning 7 * 8 */9 #include <linux/types.h> #include <linux/string.h> #include <linux/ctype.h> #includ E <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" TPCB Task[max_task_num]; Declares an array of PCBs TPCB * my_current_task = NULL; volatile int my_need_sched = 0; my_process void (void); 0 void __init My_start_kernel (void)---------int I, +/-Initialize process 0*///Initialize process data structure of NO. 0, TAS K[pid].pid = pid; Task[pid].state = 0;/*-1 not operational, 0 can be run, >0 stop */task[pid].task_entry = Task[pid].thread.ip = (unsigned long) My_pro Cess; Entrance TASK[PID].THREAD.SP = (unsigned long) &task[pid].stack[KERNEL_STACK_SIZE-1]; Stack top position, also defined in mypcb.h Task[pid].next = &task[pid]; Next points to yourself,/*fork more process */i=1;i<max_task_num;i++-memcpy (&task[i],&task[0],sizeof (TPCB)); Copy the status of process No. No. 0 task[i].pid = i; Task[i].state =-1; TASK[I].THREAD.SP = (unsigned long) &task[i].stack[KERNEL_STACK_SIZE-1]; Task[i].next = Task[i-1].next; Task[i-1].next = &task[i]; */* START process 0 by task[0] *//NO. 0 # process start with PID = 0; My_current_task = &task[pid]; MOVL ASM Volatile (%1,%%esp\n\t "/* Set TASK[PID].THREAD.SP to ESP *///Determine ESP location" PUSHL%1\n\t "/* push E  bp *///EBP equals esp51 "PUSHL%0\n\t"/* Push TASK[PID].THREAD.IP *///IP pressure stack "ret\n\t"/* Pop task[pid].thread.ip to EIP  */"POPL%%ebp\n\t"//ebp out of the stack, kernel initialization work done 54:55: "C" (Task[pid].thread.ip), "D" (TASK[PID].THREAD.SP)/* input C or D mean%ecx/%edx*/56);  }58 void my_process (void): {1 int i = 0, and 0-i++, and if (i%10000000 = =) "This is process%d-\n", my_current_task->pid); if (my_need_sched = = 1), my_need_sched = 0; My_schedule (); PRINTK (Kern_notice "This is proCess%d +\n ", my_current_task->pid);  73} 74} 75}

3myinterrupt.c scheduling mechanism

1/* 2 * LINUX/MYKERNEL/MYINTERRUPT.C 3 * 4 * Kernel Internal My_timer_handler 5 * 6 * Copyright (c) Mengni NG 7 * 8 */9 #include <linux/types.h> #include <linux/string.h> one #include <linux/ctype.h> #in Clude <linux/tty.h> #include <linux/vmalloc.h> #include "mypcb.h" TPCB task[max_task_num ]; extern TPCB * MY_CURRENT_TASK; extern volatile int my_need_sched; volatile int time_count = 0; */* Called by timer interrupt. * It runs in the name of the current running process, "so it's use kernel stack of current running process * * ID my_timer_handler (void) #if 1 if (time_count%1000 = = 0 && my_need_sched! = 1)//Set the size of the time slice, set the dispatch flag when the time slice runs out PRINTK (kern_notice ">>>my_timer_handler here<<<\n"); my_need_sched = 1;   ++ ;       #endif Notoginseng return; My_schedule (void) in TPCB * Next, TPCB * prev, and if (MY_CURrent_task = = NULL 46 | | My_current_task->next = = NULL) (PRINTK) (kern_notice ">>>my_schedule<<<\n"); Wuyi/* Schedule */my_current_task->next; next = prev = My_current_task; if (next->state = = 0)/*-1 unrunnable, 0 runnable, >0 stopped * * * * * * Switch to Next process */* + ASM Vol Atile ("PUSHL%%ebp\n\t"/* Save EBP */"MOVL%%esp,%0\n\t"/* Save ESP */"MOVL%2,%%esp\n\t"/* res Tore ESP */"MOVL $1f,%1\n\t"/* Save EIP */"PUSHL%3\n\t" "ret\n\t"/* Restore EIP */"1:\t"/ * Next Process Start Here */"POPL%%ebp\n\t": "=m" (PREV-&GT;THREAD.SP), "=m" (PREV-&GT;THREAD.IP): "M" (NEX  T-&GT;THREAD.SP), "M" (NEXT-&GT;THREAD.IP) 68);  My_current_task = Next; PRINTK (kern_notice ">>>switch%d to%d<<<\n", Prev->pid,next->pid. ->state = 0; My_current_task = Next; PRINTK (Kern_notice ">>>switch %d to%d<<<\n ", prev->pid,next->pid);  */* Switch to New process */+ ASM volatile (PUSHL%%ebp\n\t "/* Save EBP */" MOVL%%esp,%0\n\t "/*   Save ESP */Bayi "MOVL%2,%%esp\n\t"/* Restore ESP */"MOVL%2,%%ebp\n\t"/* Restore EBP */83 "Movl $1f,%1\n\t" /* Save EIP */"PUSHL%3\n\t"//Save entry for current process "ret\n\t"/* Restore EIP * *: "=m" (PREV-&GT;THREAD.SP), "=m" (p   REV-&GT;THREAD.IP): "M" (NEXT-&GT;THREAD.SP), "M" (NEXT-&GT;THREAD.IP) 88);   }90 return;  91}


3. Summary

1. The storage program computer, interrupt mechanism and stack make the computer easier and easier.

2. Interrupts and context process switching are prerequisites for the operating system.

How the Linux kernel Analytics operating system works

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.