如何将数据传递给kthread_run

时间:2019-10-07 14:33:29

标签: c multithreading linux-kernel kernel-module

我试图用多线程制作简单的内核模块。 所以我正在使用linux / kthread.h,内核v。5.2.11

问题:我无法将char数组传递给线程:分段错误。

这就是我在做什么:

import java.util.Random;
import java.util.Scanner;

public class RockPaperScissors {

    public static final int ROCK = 1;
    public static final int PAPER = 2;
    public static final int SCISSORS =3;

    public static void main(String[] args) {
        System.out.println("Let's play Rock, Paper, Scissors! (best out of 5)");

        System.out.println("Enter either 1 for Rock, 2 for Paper or 3 for Scissors.");
        Scanner input = new Scanner(System.in);
        int numberGuessed = input.nextInt();

        Random generator = new Random();
        int computerNumber = generator.nextInt(3) + 1;

        for (int round = 0; round < 4; round++) {
            if (numberGuessed == computerNumber) {
            System.out.print("It's a tie!");
        }

        if (numberGuessed == 1 && computerNumber == 2) {
            System.out.println("You lose! I chose paper and paper smothers rock!");
        }
        else if (numberGuessed == 1 && computerNumber == 3) {
            System.out.println("You win! I chose scissors and rock smashes scissors!");
        }
        else if (numberGuessed == 2 && computerNumber == 1) {
            System.out.println("You win! I chose rock and paper smothers rock!");
        }
        else if (numberGuessed == 2 && computerNumber == 3) {
            System.out.println("You lose! I chose scissors and scissors cut paper!");
        }
        else if (numberGuessed == 3 && computerNumber == 2) {
            System.out.println("You win! I chose paper and scissors cut paper!");
        }
        else if (numberGuessed == 3 && computerNumber == 1) {
            System.out.println("You lose! I chose rock and rock smashes scissors!");
        }
        else if (numberGuessed != 1 || numberGuessed != 2 || numberGuessed != 3) {
            System.out.println("Invalid user input.");
        }
    }

然后:

typedef struct {
    int num; 
    char origin[MAXSTR]; //part of input for current thread
    struct completion wait_for_thread;      //completion struct
} kthread_arg;

然后我出现了错误。而且,如果您从struct中删除数组,那么一切都会正常。 我确定做错了什么?

1 个答案:

答案 0 :(得分:2)

传递给kernel_run的args必须进行kmalloc,你的args在堆栈中。我遇到了同样的问题,您的代码应为:

struct your_struct* test=NULL;
struct task_struct* t=NULL;
test=(struct your_struct*)kmalloc(sizeof(struct your_struct),GFP_KERNEL);
t=kthread_run(your_function,(void*)test,name);