Apologies for the confusion. Here are 22 MCQs about Linux kernel and kernel module development with options containing self-contained code:
- Which language(s) can be used for Linux kernel module development?
a) C
b) Rust
c) Both C and Rust
d) Assembly
Answer: c) Both C and Rust
#include <linux/module.h>
int init_module(void)
{
printk(KERN_INFO "Hello from a C kernel module!\n");
return 0;
}
#[no_mangle]
pub extern "C" fn init_module() {
println!("Hello from a Rust kernel module!");
}
- Which command loads a C kernel module into the running kernel?
a) insmod
b) modprobe
c) rmmod
d) lsmod
Answer: a) insmod
insmod mymodule.ko
- How do you define a Rust function as the entry point for a kernel module?
a) #[main]
b) #[start]
c) #[entry]
d) #[no_mangle]
Answer: c) #[entry]
#[no_mangle]
pub extern "C" fn entry_point() {
println!("Hello from Rust entry point!");
}
- Which function is used to allocate memory in a C kernel module?
a) malloc
b) kmalloc
c) alloc
d) memalloc
Answer: b) kmalloc
#include <linux/slab.h>
void my_function() {
char *buffer = kmalloc(128, GFP_KERNEL);
// Use the allocated buffer
kfree(buffer);
}
- Which function is used to allocate memory in a Rust kernel module?
a) malloc
b) alloc
c) Box::new
d) kmalloc
Answer: b) alloc
use alloc::alloc::{alloc, dealloc, Layout};
fn my_function() {
let layout = Layout::new::<u8>();
let buffer = unsafe { alloc(layout) };
// Use the allocated buffer
unsafe { dealloc(buffer, layout) };
}
- How do you remove a C kernel module from the running kernel?
a) insmod
b) modprobe
c) rmmod
d) depmod
Answer: c) rmmod
rmmod mymodule
- In a C kernel module, how do you pass a parameter at load time?
a) By modifying the module source code
b) By passing command-line arguments to insmod
c) By using the sysctl interface
d) By modifying the kernel configuration
Answer: b) By passing command-line arguments to insmod
#include <linux/module_param.h>
static int my_param = 0;
module_param(my_param, int, 0644);
MODULE_PARM_DESC(my_param, "My module parameter");
void my_function() {
printk(KERN_INFO "Value of my_param: %d\n", my_param);
}
- In a Rust kernel module, how do you pass a parameter at load time?
a) By modifying the module source code
b) By passing command-line arguments to insmod
c) By using the sysctl interface
d) By modifying the kernel configuration
Answer: a) By modifying the module source code
use linux_module::module_param;
use linux_module::param::ParamResult;
module_param! {
static MY_PARAM: i32 = 42;
const MY_PARAM_DESC: &str = "My module parameter";
}
fn my_function() {
println!("Value of MY_PARAM: {}", MY_PARAM);
}
- How do you register a character device in a C kernel module?
a) register_chrdev
b) add_device
c) alloc_chrdev_region
d) mkdev
Answer: c) alloc_chrdev_region
#include <linux/fs.h>
int init_module() {
dev_t devno = MKDEV(255, 0);
int ret = alloc_chrdev_region(&devno, 0, 1, "mydev");
if (ret < 0)
return ret;
// Device registration successful
return 0;
}
- How do you register a character device in a Rust kernel module?
a) register_chrdev
b) add_device
c) alloc_chrdev_region
d) mkdev
Answer: a) register_chrdev
use linux_module::fs::{register_chrdev, Result};
fn init_module() -> Result<()> {
let devno = linux_module::mkdev(255, 0);
register_chrdev(devno, "mydev")?;
// Device registration successful
Ok(())
}
- How do you interact with a C kernel module from user space using system calls?
a) Custom IOCTL calls
b) Reading/Writing /dev files
c) Netlink sockets
d) Signals
Answer: b) Reading/Writing /dev files
// User-space C code
#include <fcntl.h>
#include <unistd.h>
int fd = open("/dev/mydev", O_RDWR);
char buffer[1024];
read(fd, buffer, 1024);
write(fd, "Hello", 5);
close(fd);
- How do you interact with a Rust kernel module from user space using system calls?
a) Custom IOCTL calls
b) Reading/Writing /dev files
c) Netlink sockets
d) Signals
Answer: b) Reading/Writing /dev files
// User-space Rust code
use std::fs::OpenOptions;
use std::io::{Read, Write};
let mut file = OpenOptions::new().read(true).write(true).open("/dev/mydev")?;
let mut buffer = [0u8; 1024];
file.read(&mut buffer)?;
file.write(b"Hello")?;
- How do you invoke a C function from a kernel module written in Rust?
a) FFI (Foreign Function Interface)
b) Inline assembly
c) Dynamic loading of shared libraries
d) C function cannot be directly invoked
Answer: a) FFI (Foreign Function Interface)
// Rust kernel module
extern "C" {
fn my_c_function();
}
fn my_rust_function() {
unsafe {
my_c_function();
}
}
- How do you invoke a Rust function from a kernel module written in C?
a) FFI (Foreign Function Interface)
b) Inline assembly
c) Dynamic loading of shared libraries
d) Rust function cannot be directly invoked
Answer: a) FFI (Foreign Function Interface)
// C kernel module
extern void my_rust_function();
void my_c_function() {
my_rust_function();
}
- How do you share data between C and Rust modules?
a) Function parameters
b) Global variables
c) Exchanging data through files
d) Data sharing not possible between C and Rust modules
Answer: a) Function parameters
// Rust kernel module
#[no_mangle]
pub extern "C" fn my_rust_function(data: *mut u8, length: usize) {
// Use the shared data
}
// C kernel module
extern void my_rust_function(uint8_t* data, size_t length);
void my_c_function() {
uint8_t data[64];
size_t length = 64;
my_rust_function(data, length);
}
- Which Rust crate provides seamless interaction with C code?
a) libc
b) rust-c-interop
c) c-ffi
d) interop-rs
Answer: a) libc
// Rust kernel module
extern crate libc;
fn my_rust_function() {
unsafe {
let result = libc::some_c_function();
// Use the result
}
}
- In a C kernel module, how do you access Linux kernel functions?
a) Direct function call
b) Using systemd APIs
c) Through system calls
d) Wrapping kernel functions in user-defined functions
Answer: a) Direct function call
// C kernel module
#include <linux/version.h>
void my_c_function() {
printk(KERN_INFO "Linux version: %s\n", LINUX_VERSION_CODE);
}
- In a Rust kernel module, how do you access Linux kernel functions?
a) Direct function call
b) Using systemd APIs
c) Through system calls
d) Wrapping kernel functions in user-defined functions
Answer: a) Direct function call
// Rust kernel module
use linux_module::kernel_version;
fn my_rust_function() {
println!("Linux version: {}", kernel_version());
}
- How do you pass command-line arguments to a C kernel module?
a) Modify the module source code
b) Using a custom argument parsing library
c) Pass arguments directly to insmod
d) C kernel module does not support command-line arguments
Answer: c) Pass arguments directly to insmod
insmod mymodule.ko myparam=42
- How do you pass command-line arguments to a Rust kernel module?
a) Modify the module source code
b) Using a custom argument parsing library
c) Pass arguments directly to insmod
d) Rust kernel module does not support command-line arguments
Answer: a) Modify the module source code
// Rust kernel module
use linux_module::module_param;
module_param! {
static MY_PARAM: i32 = 0;
const MY_PARAM_DESC: &str = "My module parameter";
}
fn my_rust_function() {
println!("Value of MY_PARAM: {}", MY_PARAM);
}
- How do you manage memory in a C kernel module?
a) Manually allocate and deallocate memory using kmalloc and kfree
b) Use garbage collection provided by the kernel
c) Rely on the automatic memory management of the kernel
d) C kernel modules do not handle memory management
Answer: a) Manually allocate and deallocate memory using kmalloc and kfree
// C kernel module
#include <linux/slab.h>
void my_c_function() {
int* data = kmalloc(sizeof(int) * 10, GFP_KERNEL);
// Use the allocated memory
kfree(data);
}
- How do you manage memory in a Rust kernel module?
a) Manually allocate and deallocate memory using alloc and dealloc
b) Use garbage collection provided by Rust's standard library
c) Rely on the automatic memory management of the kernel
d) Rust kernel modules do not handle memory management
Answer: a) Manually allocate and deallocate memory using alloc and dealloc
// Rust kernel module
use alloc::alloc::{alloc, dealloc, Layout};
fn my_rust_function() {
let layout = Layout::new::<i32>();
let data = unsafe { alloc(layout) as *mut i32 };
if !data.is_null() {
// Use the allocated memory
unsafe {
dealloc(data as *mut u8, layout);
}
}
}
Note: These code snippets provide simplified examples, and in real-world scenarios, additional code and error handling may be required.