gpt4 book ai didi

c、linux - 在内核中使用 pthread.h

转载 作者:行者123 更新时间:2023-11-30 20:48:06 25 4
gpt4 key购买 nike

我正在尝试测试 9*9 数独问题的有效性,结果应通过 printk(KERN_INFO "blablabla") 打印。我尝试了两种方法来编译我的c文件,makegcc myfile.c myfile -o -lpthread 。然而,它们都不起作用。

当我打电话make时它给我带来了一个 fatal error : 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 );

我只是不知道出了什么问题......也不知道如何调试。

提前致谢。

最佳答案

不要使用<pthread.h>在内核代码中

您对 kernel 之间的差异感到非常困惑代码和应用程​​序 ( user-space ) 代码。您应该避免编写内核代码。了解 CPU modes .

(在敢于编写一行内核代码,甚至是微不足道的 kernel module 之前,您需要成为 C 语言和 Linux 应用程序编程的大师)

内核代码适用于类似 device drivers 的内容,不适用于数独。当然,您不能在内核代码中使用 pthread :内核通过提供 clone(2)实现线程。应用程序的系统调用。 POSIX C 库 - 您的 libc.so ,通常是 GNU glibc - 正在实现pthreads(7)在许多人的帮助下system calls -列于syscalls(2) -,包括clonefutex(7) .

阅读 Operating Systems: Three easy pieces 更多地了解操作系统的作用。

I just can't figure out what went wrong

你的整个方法都是错误的扔掉你的代码。

<小时/>

学习多线程 POSIX 应用程序编程

阅读ALP (和 intro(2) 然后 intro(3) ...)学习如何编写 Linux 应用程序代码

花几天时间阅读一些内容 pthread tutorial .

然后,使用 Posix 线程编写一些应用程序(因此具有 main 并调用 pthread_create(3)pthread_join(3) )来解决您的数独问题。您可能需要mutexesconditional variables用于同步。

如果您想学习内核编程,请从一些更简单的任务开始。在此之前,请非常熟练地掌握 Linux 应用程序编程。

(添加 kernel threads 确实很困难;它们根本不是 POSIX 线程。将其留给高级内核开发人员;先学习用户空间线程编程,这已经够困难的了。)

所以放弃你的内核编程想法吧。重点学习 C 编程和 POSIX 应用程序编程(需要 many 年)。

编译您的 ( user-space ) 代码,其中包含所有警告和调试信息: gcc -Wall -Wextra -gGCC 。改进它以获得没有警告。学习使用 gcc compiler , build automation类似 GNU make 的工具或 ninja gdb debugger , valgrind , address sanitizers及其他instrumentation options .

关于c、linux - 在内核中使用 pthread.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com