ví dụ về kernel

10 1 0
Tài liệu đã được kiểm tra trùng lặp
ví dụ về kernel

Đang tải... (xem toàn văn)

Thông tin tài liệu

Ví dụ về Kernel, tính giai thừa và tính toán trên ma trận – có hướng dẫn sử dụng#include #include #include #include #include int factorialint n; int matmulint p, int q, int s, int *a, in

Trang 1

Ví dụ về Kernel, tính giai thừa và tính toán trên ma trận – có hướng dẫn sử dụng#include <linux/module.h>

#include <linux/moduleparam.h>#include <linux/kernel.h>

#include <linux/init.h>#include <linux/stat.h>

int factorial(int n);

int matmul(int p, int q, int s, int *a, int *b, int *c);int matadd(int p, int q, int *a, int *b);

int primeBetween(int m, int n);

int maxandminmatrix(int p, int q, int *a);

int numdivisibleinmatrix(int p, int q, int *a, int s);int primeofmat(int p, int q, int *a);

int prime(int n);static int choice = 0;static int p, q, s, d;

static int a[2500], b[2500], c[2500];static char message[2500] ;

module_param(choice, int, S_IRUGO);module_param(d, int, S_IRUGO);module_param(p, int, S_IRUGO);module_param(q, int, S_IRUGO);module_param(s, int, S_IRUGO);

module_param_array(a, int, NULL, S_IRUGO);module_param_array(b, int, NULL, S_IRUGO);

Trang 2

module_param_array(c, int, NULL, S_IRUGO);

static int p02_init(void){

switch (choice) {

case 1: {

printk(KERN_ALERT "%d! = %d\n", d, factorial(d)); break;

}

case 2: {

matadd(p, q, a, b); break;

}

case 3: {

matmul(p, q, s, a, b, c); break;

}

case 4: {

primeBetween(p, s);

Trang 3

break; }

case 5: {

maxandminmatrix(p, q, a); break;

}

case 6: {

numdivisibleinmatrix(p, q, a, s); break;

} case 7: {

primeofmat(p, q, a); break;

} default:

break; }

return 0;}

int factorial(int n){

Trang 4

int gt = 2; int i = 3;

for(i=3; i<=n; i++) {

gt *= i; }

return gt;}

int matadd(int p, int q, int *a, int *b) {int i, j;

for (i = 0; i < p; i++) { for (j = 0; j < q; j++) {

*(c + j + i * q) = *(a + j + i * q) + *(b + j + i * q); //c[i][j] = a[i][j] + b[i][j];

// *(a + j + i * q) += *(b + j + i * q); }

}

printk(KERN_ALERT "\n -\nThe sum of 2 matrices is a matrixwith height %d and width %d\n",p,q);

for (i=0;i<p;i++){ for (j=0;j<q;j++){

printk(KERN_ALERT "%-7d", *(c + (q * i) + j)); }

printk(KERN_ALERT "\n");

Trang 5

}

return 0;}

int matmul(int p, int q, int s, int *a, int *b, int *c) {int i, j, l;

for (i=0;i<p;i++){ for (j=0;j<s;j++){ for (l=0;l<q;l++){

//c[i][j] = c[i][j] + a[i][l] * b[l][j];

*(c + (s * i) + j) = *(c + (s * i) + j) + *(a + (q * i) + l) * *(b + (l * s) + j); }

} }

printk(KERN_ALERT "\n -\nThe product of 2 matrices is a matrix with height %d and width %d\n",p,s);

for (i=0;i<p;i++){ for (j=0;j<s;j++){

printk(KERN_ALERT "%-7d", *(c + (s * i) + j)); }

printk(KERN_ALERT "\n"); }

return 0;}

int primeBetween(int num1, int num2) { int i;

Trang 6

for (i = num1 + 1; i < num2; ++i) { if(prime(i)){

printk(KERN_ALERT "%d\n",i); }

}

return 0;}

int maxandminmatrix(int p, int q, int *a) { int maxValue = a[0];

int minValue = a[0]; int i, j;

for (i = 0; i < p; i++) { for (j = 0; j < q; j++) {

if (a[i * q + j] > maxValue) { maxValue = a[i * q + j]; }

if (a[i * q + j] < minValue) { minValue = a[i * q + j]; }

} }

printk(KERN_INFO "Max of a matrix: %d \n", maxValue); printk(KERN_INFO "Min of a matrix: %d \n", minValue); return 0;

}

Trang 7

int numdivisibleinmatrix(int p, int q, int *a, int s) { int i, j, count = 0;

for (i = 0; i < p; i++) { for (j = 0; j < q; j++) { if (a[i * q + j] % s == 0) { count++;

} } }

printk(KERN_INFO "There are %d numbers divisible by %d in this matrix\n", count, s);

return 0; }

int primeofmat(int p, int q, int *a) { int i, j, count = 0;

for (i = 0; i < p; i++) { for (j = 0; j < q; j++) {

count += prime(a[j + q * i]); }

Trang 8

if(n<=1) return 0; int i;

for (i = 2; i <= n/2; i++) { if (n % i == 0) {

return 0; }

}

return 1;}

static void p02_exit(void){

printk(KERN_ALERT "Exit\n");}

MODULE_LICENSE("GPL");MODULE_AUTHOR("abcx");

Trang 9

Hướng dẫn sử dụngoption 1: tinh giai thua

sudo insmod matran.ko choice=1 d=5

option 2: add mattrix

sudo insmod matran.ko choice=2 p=2 q=2 a="1,2,3,4" b="5,6,7,8"

option 3:nhan 2 ma tran

sudo insmod matran.ko choice=3 p=2 q=3 s=2 a="1,2,3,4,5,6" b="7,8,9,10,11,12"

//choice=3: Chọn chức năng nhân hai ma trận.

//p=2, q=3, s=2: Kích thước của các ma trận (2x3 và 3x2).

//a="1,2,3,4,5,6", b="7,8,9,10,11,12": Các phần tử của hai ma trận.option 4: chức năng liệt kê các số nguyên tố trong một khoảng cho trước sudo insmod matran.ko choice=4 p=10 s=20

//choice=4: Chọn chức năng liệt kê các số nguyên tố trong khoảng.//p=10: Số nguyên tố đầu tiên trong khoảng.

//s=20: Số nguyên tố cuối cùng trong khoảng.option 5 max min ma tran

sudo insmod matran.ko choice=5 p=2 q=2 a="1,2,3,4"

choice=5: Chọn chức năng tính giá trị lớn nhất và nhỏ nhất của ma trận.p=2, q=2: Kích thước của ma trận (2x2).

Trang 10

p=2, q=2: Kích thước của ma trận (2x2).a="2,3,4,6": Các phần tử của ma trận.

s=2: Số nguyên mà bạn muốn kiểm tra xem có bao nhiêu số trong ma trận chia hết cho nó.

option 7: đếm số lượng số nguyên tố trong ma trậnsudo insmod matran.ko choice=7 p=2 q=2 a="2,3,4,5"

choice=7: Chọn chức năng đếm số lượng số nguyên tố trong ma trận.p=2, q=2: Kích thước của ma trận (2x2).

a="2,3,4,5": Các phần tử của ma trận.

sudo dmesg :// log kernel

sudo rmmod test.ko // xoa module

lsmod //Liệt kê các module đã được tải:

modinfo // hien thi thong tin kernel gom tac gia, phien ban, module

Ngày đăng: 08/05/2024, 15:56

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan