Tuesday, March 30, 2010

Painful learning

If you are installing XP on Acer aspire 5740, change the drive mode from AHCI to ATA in BIOS.
Else, you will get the STOP 0x0000007B(0xF78DA63C,0x0000034,0X x0000000,0x0000000) error with the notorious blue screen.
Thanks a lot to the guys at this forum: http://www.cybertechhelp.com/forums/showthread.php?t=156593

Ok, second painful learning, do not delete your favourite distro's iso image to create space for movies :(

Monday, March 8, 2010

Kernel Thread

This post is on how to create a simple kernel thread. Note that the idea here is to explain the kthread and hence not much importance is given to other aspects.


kthread.c




#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kthread.h>

struct task_struct *ts;

int my_thread(void *data)
{


    printk("%s\n",data);<
    while(1)
    {
        printk("I am a kernel thread\n");
        msleep(3000);
        if(kthread_should_stop()) return 0;
    }
    return -1;
}

int tinit(void)
{
    ts=kthread_run(my_thread,"message from init","MyKernelThread");
    return 0;
}

void tstop(void)
{

    kthread_stop(ts);
}

module_init(tinit);
module_exit(tstop);



Makefile



obj-m := kthread.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=`pwd` modules


clean:
    @rm -f *.ko *.o *.mod.o *.mod.c



How does it work?

It is very simple. The function kthread_run is not actually a function. It is a macro defined in include/linux/kthread.h. This macro takes care of creating and starting the kernel thread. my_thread is the function that will be run by this thread. All these are very similar to user land threads. However, there is a notable difference here. From the thread function you should keep checking if your thread is being stopped and return. This is done by calling kthread_should_stop(). If this function returns true, that means it is time for us to return. This is a nice feature as this will allow us to do some cleanup before we exit. (This is a must since, if this function does not return, your rmmod will hang). As you can see, the thread is stopped by kthread_stop call. So, what are you waiting for? Have some fun with your brand new kernel thread

Thursday, March 4, 2010

Command o/p redirection in bash (1)

How do you redirect command o/p in bash?

The simple answer is

bash$ command > out.txt

But, this is only a simple answer. There is much more that one might need to do. Consider a program like the one below.

#include <stdio.h>

int main()
{
    while(1)
    {
        sleep(2);
        fprintf(stdout,"Stdout message\n");
        fprintf(stderr,"Stderr message\n");
    }

    return 0;

}


If you compile and runt the above program this is what you will see.

[root@DER01C c]# ./a.out > out.txt
Stderr message
Stderr message
Stderr message


Why is that? It is because the above redirection does not take care of the stderror. It only redirects only stdout.

How to fix this problem? replace '>' with '&>'

[root@DER01C c]# ./a.out &> out.txt


But, then you will have to segregate the error and out put messages from out.txt. How to avoid this problem?

About that in my next post...

syscalls

man 2 syscalls

This man page lists all system calls. Its a nice experience to view this page and then go through each of the system call man page.

Hmm... This reminds me that I am still running 2.6.23. Time to upgrade and have a new kernel and man page.