我正在尝试测试9 * 9数独问题的有效性,结果应该通过printk(KERN_INFO "blablabla")
打印出来。我已经尝试了两种方法来编译我的c文件,make
和gcc myfile.c myfile -o -lpthread
。但是,它们都没有奏效。
当我打电话给make
时,它给我一个致命的错误:pthread.h:没有这样的文件或目录,但它在路径/usr/include
中是正确的。
然后我从网上找到了一些建议,所以这次我试了gcc
。不幸的是,在调用gcc myfile.c myfile -o -lpthread
之后我收到了一些错误,例如
/usr/include/linux/list.h: In function 'INIT_LIST_HEAD': /usr/include/linux/list.h:27:17: error: dereferencing pointer to incomplete type 'struct list_head'
和
error: unknown type name 'bool'
,
仅举几例。关键是我在之后通过sudo apt-get update -y
升级我的软件后出现了这些错误。
以下是代码:
#include <pthread.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
typedef struct
{
int row;
int col;
int (* board)[9];
} parameters;
void * find_rows(void * params);
void * find_cols(void * params);
void * check_valid(void * params);
/***************
* ENTRY POINT
**************/
int thread_init(void)
{
// ====== Create the board =======
int board[9][9] = {
{6, 2, 4, 5, 3, 9, 1, 8, 7},
{5, 1, 9, 7, 2, 8, 6, 3, 4},
{8, 3, 7, 6, 1, 4, 2, 9, 5},
{1, 4, 3, 8, 6, 5, 7, 2, 9},
{9, 5, 8, 2, 4, 7, 3, 6, 1},
{7, 6, 2, 3, 9, 1, 4, 5, 8},
{3, 7, 1, 9, 5, 6, 8, 4, 2},
{4, 9, 6, 1, 8, 2, 5, 7, 3},
{2, 8, 5, 4, 7, 3, 9, 1, 6}
};
/*int i, j = 0;
size_t count;
int board[9][9];
char *line = (char *) malloc(100);
FILE *file;
file = fopen("test.txt", "r");
while(getline(&line, &count, file) != -1) {
for (count > 0; count--; j++)
sscanf(line, "%d", &board[i][j]);
i++;
}*/
parameters * data = (parameters *) malloc(sizeof(parameters));
data->row = 0;
data->col = 0;
data->board = board;
// ====== Create the parameters for the 3x3 threads ======
// First 3x3
parameters * data1 = (parameters *) malloc(sizeof(parameters));
data1->row = 0;
data1->col = 0;
data1->board = board;
// Second 3x3
parameters * data2 = (parameters *) malloc(sizeof(parameters));
data2->row = 0;
data2->col = 3;
data2->board = board;
// Third 3x3
parameters * data3 = (parameters *) malloc(sizeof(parameters));
data3->row = 0;
data3->col = 6;
data3->board = board;
// Fourth 3x3
parameters * data4 = (parameters *) malloc(sizeof(parameters));
data4->row = 3;
data4->col = 0;
data4->board = board;
// Fifth 3x3
parameters * data5 = (parameters *) malloc(sizeof(parameters));
data5->row = 3;
data5->col = 3;
data5->board = board;
// Sixth 3x3
parameters * data6 = (parameters *) malloc(sizeof(parameters));
data6->row = 3;
data6->col = 6;
data6->board = board;
// Seventh 3x3
parameters * data7 = (parameters *) malloc(sizeof(parameters));
data7->row = 6;
data7->col = 0;
data7->board = board;
// Eighth 3x3
parameters * data8 = (parameters *) malloc(sizeof(parameters));
data8->row = 6;
data8->col = 3;
data8->board = board;
// Ninth 3x3
parameters * data9 = (parameters *) malloc(sizeof(parameters));
data9->row = 6;
data9->col = 6;
data9->board = board;
// ====== Create the threads ======
pthread_t thread_rows, thread_cols, thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8, thread9;
// ====== Create the return values for the threads ======
void * all_rows;
void * all_cols;
void * square1;
void * square2;
void * square3;
void * square4;
void * square5;
void * square6;
void * square7;
void * square8;
void * square9;
// ====== Initialize the threads ======
pthread_create(&thread_rows, NULL, find_rows, (void *) data);
pthread_create(&thread_cols, NULL, find_cols, (void *) data);
pthread_create(&thread1, NULL, check_valid, (void *) data1);
pthread_create(&thread2, NULL, check_valid, (void *) data2);
pthread_create(&thread3, NULL, check_valid, (void *) data3);
pthread_create(&thread4, NULL, check_valid, (void *) data4);
pthread_create(&thread5, NULL, check_valid, (void *) data5);
pthread_create(&thread6, NULL, check_valid, (void *) data6);
pthread_create(&thread7, NULL, check_valid, (void *) data7);
pthread_create(&thread8, NULL, check_valid, (void *) data8);
pthread_create(&thread9, NULL, check_valid, (void *) data9);
// ======= Wait for all threads to finish their tasks =======
pthread_join(thread_rows, &all_rows);
pthread_join(thread_cols, &all_cols);
pthread_join(thread1, &square1);
pthread_join(thread2, &square2);
pthread_join(thread3, &square3);
pthread_join(thread4, &square4);
pthread_join(thread5, &square5);
pthread_join(thread6, &square6);
pthread_join(thread7, &square7);
pthread_join(thread8, &square8);
pthread_join(thread9, &square9);
// ====== Check whether the Sudoku Puzzle was solved ======
if ( (int)(uintptr_t) all_rows == 1 &&
(int)(uintptr_t) all_cols == 1 &&
(int)(uintptr_t) square1 == 1 &&
(int)(uintptr_t) square2 == 1 &&
(int)(uintptr_t) square3 == 1 &&
(int)(uintptr_t) square4 == 1 &&
(int)(uintptr_t) square5 == 1 &&
(int)(uintptr_t) square6 == 1 &&
(int)(uintptr_t) square7 == 1 &&
(int)(uintptr_t) square8 == 1 &&
(int)(uintptr_t) square9 == 1 )
{
printf("The Sudoku Puzzle is solved!\n");
}
else
{
printf("The Sudoku Puzzle is NOT solved.\n");
}
return 0;
}
void * find_rows(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startRow; i < 9; ++i) {
int row[10] = {0};
for (int j = startCol; j < 9; ++j) {
int val = data->board[i][j];
if (row[val] != 0) {
return (void *) 0;
}
else{
row[val] = 1;
}
}
}
return (void *) 1;
}
void * find_cols(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startCol; i < 9; ++i) {
int col[10] = {0};
for (int j = startRow; j < 9; ++j) {
int val = data->board[j][i];
if (col[val] != 0) {
return (void *) 0;
}
else{
col[val] = 1;
}
}
}
return (void *) 1;
}
void * check_valid(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
int saved[10] = {0};
for (int i = startRow; i < startRow + 3; ++i) {
for (int j = startCol; j < startCol + 3; ++j) {
int val = data->board[i][j];
if (saved[val] != 0) {
return (void *) 0;
}
else{
saved[val] = 1;
}
}
}
return (void *) 1;
}
void thread_exit(void)
{
printk(KERN_INFO "Removing Module\n");
}
module_init( thread_init );
module_exit( thread_exit );
我无法弄清楚出了什么问题......并且不知道如何调试。
提前致谢。
答案 0 :(得分:3)
<pthread.h>
您对kernel代码与应用程序(user-space)代码之间的区别非常 困惑 。你应该避免编写内核代码。阅读CPU modes。
(在大胆编写单行内核代码之前,你需要成为关于C和Linux应用程序编程的大师,即使是一个微不足道的kernel module)
内核代码适用于device drivers之类的内容,而不适用于Sudoku-s。当然,你不能在内核代码中使用pthread:内核通过向应用程序提供 clone(2)系统调用来实现线程。 POSIX C库 - 您的libc.so
,通常是GNU glibc - 在[{3}} - 列出的许多pthreads(7)的帮助下实施system calls,包括{ {1}}和syscalls(2)。
阅读futex(7) 以了解更多操作系统的作用。
我无法弄清楚出了什么问题
您的整个方法都是错误的。 抛弃你的代码。
阅读Operating Systems: Three easy pieces(和ALP然后intro(2) ...)了解如何编写Linux应用程序代码。
花几天时间阅读一些intro(3)。
然后,使用Posix线程编写一些应用程序(使用clone
并调用pthread tutorial和pthread_create(3))来解决您的数独。您可能需要pthread_join(3)和mutexes进行同步。
如果您想学习内核编程,请从一些更简单的任务开始。在此之前,熟练使用Linux应用程序编程。
(添加conditional variables非常困难;它们根本不是POSIX线程。请将其留给高级内核开发人员;首先了解用户空间线程编程,这很困难。)
所以放弃你的内核编程思想。专注于学习C编程和POSIX应用程序编程(需要kernel threads年)。
使用many编译您的{user-space)代码以及所有警告和调试信息:main
。改进它以获得没有警告。学习使用GCC,gcc
compiler工具,例如build automation或GNU make
,ninja
,gdb
debugger,valgrind等{ {3}}