小编Meh*_*Meh的帖子

内核模块:访问另一个模块头中定义的结构成员

我正在尝试编写一个内核模块,但我仍然坚持访问另一个模块头中定义的结构成员.我会试着解释一下:

让我们说另一个模块是:

<kernel-src>/drivers/xxx/xxx.c|.h
Run Code Online (Sandbox Code Playgroud)

在xxx.c中有一些我在驱动程序中使用的导出函数.但是我想从xxx.h中定义的struct s_xxx访问成员m1:

struct s_xxx {
    ...
    int m1;
};
Run Code Online (Sandbox Code Playgroud)

接下来我在/usr/include/linux/yyy.h中有这个:

struct s_xxx;
struct s_yyy {
    ...
    struct s_xxx *p_xxx;
};
Run Code Online (Sandbox Code Playgroud)

我的司机我:

#include <linux/yyy.h>
Run Code Online (Sandbox Code Playgroud)

我成功地使用了xxx驱动程序中的导出符号.但是当然如果我尝试从s_xxx访问成员,编译器会抱怨:

struct s_yyy *p_yyy;
...
m = p_yyy->p_xxx->m1; /* error */
Run Code Online (Sandbox Code Playgroud)

在/ usr/include/linux /中找不到xxx.h.到目前为止,我找到了2个解决方法:

1)下载内核源代码并在我的模块中包含xxx.h的完整路径

2)将s_xxx定义从xxx.h复制/粘贴到我的模块

这样做的正确方法是什么?

(对不起那个冗长而糟糕的解释:@)

c linux-device-driver linux-kernel

6
推荐指数
1
解决办法
861
查看次数

pthread互斥锁定/解锁时(1)

我正在尝试编写一个非常简单的多线程程序,只是为了获得它的捕获但我无法理解其中一个案例的确切错误.所以:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

char string[100];
pthread_t thr_id_rd;
pthread_t thr_id_wr;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond, cond1;
int read = 0;

void *thread_rd()
{
    pthread_mutex_lock(&lock);
    while (1) {
        pthread_cond_wait(&cond, &lock);
        printf("rd: entered: %s\n", string);
        pthread_cond_signal(&cond1);
    }
    pthread_mutex_unlock(&lock);
}

void *thread_wr()
{
    pthread_mutex_lock(&lock);
    while (1) {
        printf("wr: enter something: ");
        scanf("%s", string);
        pthread_cond_signal(&cond);
        pthread_cond_wait(&cond1, &lock);
    }
    pthread_mutex_unlock(&lock);
}

int main(int argc, char *argv[])
{
    pthread_create(&thr_id_rd, NULL, thread_rd, NULL);
    pthread_create(&thr_id_wr, NULL, thread_wr, NULL);

    pthread_join(thr_id_rd, NULL);
    pthread_join(thr_id_wr, NULL);

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c mutex pthreads

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×2

linux-device-driver ×1

linux-kernel ×1

mutex ×1

pthreads ×1