| Example: process list ("$ps -ef") |
UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:36 ? 00:00:00 /sbin/init root 2 0 0 09:36 ? 00:00:00 [kthreadd] root 3 2 0 09:36 ? 00:00:00 [ksoftirqd/0] root 4 2 0 09:36 ? 00:00:00 [kworker/0] root 5 2 0 09:36 ? 00:00:00 [migration/0] ... root 10 2 0 09:36 ? 00:00:00 [my_hotplugd/0] root 12 2 0 09:36 ? 00:00:00 [my_hotplugd/1] ... root 579 2 0 09:36 ? 00:00:00 [hid_compat] root 613 1 0 09:36 ? 00:00:00 [My_name: my_ker_thd_1] root 614 2 0 09:36 ? 00:00:00 [My_name: my_ker_thd_2] ... |
For creating the first 2 threads try the following procedure:
static void my_kernel_thread_create_1(void);
static void my_kernel_thread_create_2(void);
...
static void my_kernel_thread_create_1(void){
int mypid;
printk(KERN_NOTICE "My_name: Calling kernel_thread(my_ker_thd_1)\n");
mypid = kernel_thread(m_k_t_do_something_1, NULL, CLONE_KERNEL);
printk(KERN_NOTICE "My_name: my_ker_thd_1 = %d\n", mypid);
}
static void my_kernel_thread_create_2(void){
int mypid;
printk(KERN_NOTICE "My_name: Calling kernel_thread(my_ker_thd_2)\n");
mypid = kernel_thread(m_k_t_do_something_2, NULL, CLONE_KERNEL);
printk(KERN_NOTICE "My_name: my_ker_thd_2 = %d\n", mypid);
}
...
static void m_k_t_do_something_1(void){
struct task_struct *curtask = current;
strcpy(curtask->comm, "My name: m_k_t_do_something_1");
set_task_state(curtask, TASK_RUNNING);
printk(KERN_NOTICE "My name: m_k_t_do_something_1 is about to be scheduled.\n");
schedule();
printk(KERN_NOTICE "My_name: m_k_t_do_something_1 is now scheduled.\n");
}
static void m_k_t_do_something_2(void){
struct task_struct *curtask = current;
strcpy(curtask->comm, "My_name: m_k_t_do_something_2");
set_task_state(curtask, TASK_RUNNING);
printk(KERN_NOTICE "My_name: m_k_t_do_something_2 is about to be scheduled.\n");
schedule();
printk(KERN_NOTICE "My_name: m_k_t_do_something_2 is now scheduled.\n");
}
...
...
print some info on all the processes on runqueue;
...
printk(KERN_NOTICE "My_name: m_k_t_do_something threads are about to be created.\n");
my_kernel_thread_create_1();
my_kernel_thread_create_2();
...
printk(KERN_NOTICE "My_name: m_k_t_do_something threads are created.\n");
...
print some info on all the processes on runqueue;
...
run_init_process("/sbin/init");
...
For creating the second type of threads, follow the procedure for creating ksoftirqd and kworker discussed in class:
static __init int spawn_my_hotplug_threads(void){
BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
return 0;
}
static struct smp_hotplug_thread my_hotplug_threads = {
.store = &my_hotplugd,
.thread_fn = run_my_hotplugd,
.thread_comm = "my_hotplugd/%u",
};
static void run_my_hotplugd(unsigned int cpu) {
...
print something to indicate that my_hotplugd/{0,1,2,3} are up and running
...
}
For printing some info on processes, try the following:
...
struct task_struct *tmp_tsk;
...
tmp_tsk = current;
for_each_process(tmp_tsk) {
...
print some info on all the processes on runqueue;
...
}
Now, remove all the threads you created and print all the processes, something similiar to "ps -ef".
Submit a patch file, only the portion of the code you actually modified, not all the files. Include screen shots/dmesg that show that it worked.