Saturday, December 18, 2010

fossdotin

I was watching #fossdotin till late into the night yesterday. I usually don't like/do social networking much. But, I could not resist this one. The channel showed a mixture of energy, enthusiasm, nostalgia and anguish. Kudos to all those who have made this happen.

Though I left early yesterday, I don't feel bad about it. There was surely a certain pleasure about being able to create that electrified environment in my mind sitting all alone in my room with my eyes glued to monitor and fossdotin channel on twitter. Such thought streams are usually far from the imperfections of reality and hence stronger and sweeter.

I saw the picture of team FOSS.IN in one of the tweets. My immediate thought was "most of them are kids". But, what they have achieved is tremendous. Probably the reason why they could achieve all these was that they were "kids" - People with Free and Open minds. Hats off to the this team and their leader who is now really proven to be "Atul".

I saw a few tweets about rekindling FOSS.IN again next year. I think that should only be done with the full consent of Atul Chitnis. This person has become the hero of the event and hijacking the event from him (for whatever reason it may be) is simply going to be cruel. After all, you can start your own FOSS event. Isn't that what the "Free as in Freedom" signifies?

Friday, December 17, 2010

QEMU/ARM hands-on (FOSS.IN)

https://github.com/tuxdna/arm-experiments (Saleem Ansari)

This was the most interesting and useful session that I had in entire FOSS.IN. Above site will give details. However, I guess, there are some issues with instructions. For example, the kernel link is broken. But, it will work if you remove the .af from the url. Also, the image did not load the rootfs. I tried ext2fs instead of cpio and gzip and that loaded the image. Also, rcS is not present at the site. I wrote that from memory. Again, I seem to have missed something there. It fails with the message that "could not open initial console". I guess /dev/ttyX stuff might be missing. Will try to fix that later and post back here once I do that. Anyway, this was a enlightening talk and it now gives me enough confidence to try my own rootfs on different architecture. Many thanks to the presenter.

Update:
I fixed this stuff. For those who are interested, I am listing the important changes
0. The correct link for kernel is: http://www.kernel.org/pub/linux/kernel/v2.6/testing/v2.6.24/linux-2.6.24-rc7.tar.gz
0.1 May be this was not needed. But, I did create a symlink from busybox to /init. Also, it only worked when I did a static build of busybox
1. Some device inodes were missing. Created /dev/{null,console,tty,tty0,tty1,tty2,tty3,tty4,tty5}
2. Created /etc/init.d/rcS and made it executable
#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sys /sys
/sbin/mdev -s
3. Most important of all, EABI support needs to be turned on in kernel. Else, nothing will work. Turn it on before compiling the kernel
4. The filesystem cannot be cpio. May be it can still work with a kernel config change. However, I tried with ext2fs and it worked perfectly well
4.1 To create a ext2fs,
dd if=/dev/zero of=root.fs bs=1M count=4
(As a side note, if you refer to the commands given in the above link, you will see bs=1. That is very slow)

mkfs -t ext2 root.fs
(Press 'y' for the question about root.fs not being a real device)

mkdir rootfs
mount -o loop root.fs rootfs
(Mount your new filesystem somewhere so that you can copy the contents to it)

cd $ROOTFS_PATH
(go to where your rootfs is)

tar c * | tar x -C /path/to/rootfs
(copy the contents to new your new filesystem)

umount /path/to/rootfs
(Now you can start using your root.fs just like rootfs.cpio.gz as described in the link at the top of this post and continue from mkimage part)
Thats all! you will have a bootable setup. I am thinking of creating a small script that will automate all these. Will post it here if I ever do that.
Update: I did make a script. Unfortunately, I was in office and our security policy does not allow me to upload that. Its like magic, u run it, have a coffee and when you come back,  you will see qemu running your own small Linux image :)

mediawiki (FOSS.IN miniconf)

Today I attended a miniconf on mediawiki that took place on the sidelines of FOSS.IN. It was a very interesting talk by Erik Moeller. During the talk Erik kept making notes using etherpad (http://eiximenis.wikimedia.org/MediaWikiNotes). I have copied them below. It was really amazing to know about the kind of effort that goes into making these things happen (which sadly the user community, that includes me, takes for granted). It was also nice to know about a bunch of allied technologies and a number of new enhancements that are coming to mediawiki. I was really fascinated by the new enhanced editor and I downloaded and installed the svn trunk when the talk was on to get a feel of the interface. I will be soon upgrading the wiki at office.

-------------------------------------------------------------------------------
Mediawiki Workshop 2010                                     
Erik Moeller           
FOSS.IN 2010             
[Miniconf]

Manual about how to write a MediaWiki extension: http://www.mediawiki.org/wiki/Manual:Extensions
Subversion check-out instructions: http://www.mediawiki.org/wiki/Subversion
Wikimedia configuration files for MediaWiki: http://noc.wikimedia.org/conf/ 
Wikipedia's configurations: http://en.wikipedia.org/wiki/Special:Version  
IRC: irc.freenode.net #mediawiki #wikimedia-tech

http://dbpedia.org DBPedia - extract info from Wikipedia

SPARQL - RDF extraction for DBPedia

http://semantic-mediawiki.org Semantic Media Wiki - semantic storage for MediaWiki
example:bases wiki http://baseswiki.org

http://baseswiki.org  Info about non-judicial grievance mechanisms

http://svn.wikimedia.org/ Wikimedia Subversion  Repository


how to enable the new toolbar of wikipedia in my mediawiki? WikiEditor extension is the answer and it works if we change the user preference.

browser toolbar: Go to https://addons.mozilla.org/firefox/addon/6401 click add to Firefox and then after 5 secs.addon is installed.

http://en.wikipedia.org/wiki/Main_Page/?useskin=monobook   : old theme of wikipedia (new version: Vector)

http://www.wikia.com -wysiwyg ediing   



http://uncyclopedia.wikia.org   : the sarcastic wikipedia




View Your Mind - a better alternative for mind mapping than freemind perhaps






http://en.wikipedia.org/wiki/Wikipedia:BLP  -- Biographies Guidelines



http://grouplens.org/ conducts research on user interaction 

http://en.m.wikipedia.org  - Mobile version



http://wikibhasha.com/ a conversion utility where articles may be easily converted into indic languages

http://www.openstreetmap.org/ an open source map( edit and contribute :) )



http://translatewiki.net - localizatioin translation communities, language communities, and free and open source projects.



http://www.openwebanalytics.com/  alternate to google analytics

Saturday, December 11, 2010

Flash plugin on 64 bit Chome OS running on Fedora 13 64 bit

I installed chrome and youtube.com told me that I don't have flash installed
On going to adobe flash page, they told me that it has to be there as I am using chrome and I just need to enable it
about:plugins on chrome told me that it is not there :(
Now, what do I do? The answer follows

1. Download the 64 bit flash plugin preview from adobe (See my previous post)
2. Copy libflashplayer.so to a preferred location (I copied it to /usr/lib64/mozilla/plugins)
3. Edit ~/.config/google-chrome/Default/Preferences file and add the section shown in reverse video
 
4. Restart chrome and don't mind about it complaining about issues with config getting corrupted. You will see new entry for flash when you do about:plugins

5. Just go to youtube and start watching your favourite videos :)

Thursday, December 9, 2010

64 bit flash preview

Adobe is back with 64 bit flash for Linux. This is a preview and grab it before they close it again. Last time when they had tried this, there were a number of security issues and preview was pulled down soon.

Installation:

  1. Download the preview version from Adobe labs site
  2. Untar the downloaded plugin 
  3. Copy the resulting plugin to your firefox plugin folder
    1. I had to first create this: mkdir /usr/lib64/firefox-3.6/plugins
    2. Copy libflashplayer.so to that folder
  4. Just re-start firefox and you are ready to browse the flash sites :)
Download link
http://labs.adobe.com/downloads/flashplayer10_square.html

(Its not easy to find the link. It took me 10 minutes to find this. Though there is a get beta note on the download page, it does not take you directly here)

Saturday, November 20, 2010

Silence the console

Well, at times, its very much annoying to hear the console beep from a Linux terminal. Sometimes, it is very untimely. For example, you might be stuck at some command and might press a tab. If there is no possible completion, terminal beeps. Its more like your leg is stuck between tracks when the train is approaching and you are desperately trying to escape. Then the guard standing near you blows his whistle and asks you to get of from the track fast, without trying to help you. Anyway, Here is how you disable it

For text terminals

setterm -blength 0

For graphical terminals

xset b off

And u have peace....

Sunday, November 7, 2010

Chrome with built-in support for pdf

http://chrome.blogspot.com/2010/11/pdf-goodness-in-chrome.html
Well, I can see cloud printing really picking up. PDF is the only supported mime type for cloud printing via Google cloud. Google had earlier mentioned that it expects pdf to be De facto standard for printing documents over cloud. With their Chrome OS release expected only a few days from now, I could not see much movement in this front. But, I guess, Google's plans are very clear now. With this beta release of Chrome, others will have no way other than support pdf extensively on web. I like Google's way of doing things :)

Wednesday, October 20, 2010

Has Linux lost the Desktop war?

http://www.techeye.net/software/linuxs-chance-has-gone
http://www.pcworld.com/businesscenter/article/207999/desktop_linux_the_dream_is_dead.html?tk=hp_new

Those are the two interesting links that I came across recently. They make the statement (rather convincingly) that the Linux has lost in the Desktop front. I look at it from a different perspective. I would say that Desktop has lost its place as a PC (personal computer).

Today, we can clearly see that Desktops are rapidly getting replaced with smart phones and MIDs. And, what are the two most interesting players in that domain? Though a subject of argument, Android is surely one of the forerunners for the title. Next, look at the cloud computing. Though in RC stage, Google chrome OS has made generated enough buzz among the tech community. I am sure that with the weight of Google behind these two distributions, Linux will stay close to you though the Desktop might not!

Thursday, October 7, 2010

http://www.documentfoundation.org

http://www.documentfoundation.org

Its only today I saw this. I am currently downloading this. Good move considering the way Oracle is going now. I am just waiting to see bytecodelangfoundation.org :)

Sunday, September 26, 2010

SCU on ARM11MPCore will not give you coherency

Well, we thought otherwise. But, that was not correct. This was a very interesting experience. We started writing the communication module to make two different cores of ARM11MPCore communicate and found that things are not going fine as we were expecting them to. We write from one core and we cannot read it back from other. First suspect was the new virtual addresses that had happened in VxWorks. However, that was not the issue. After reading about it a bit, we were convinced that it must have been a linear mapping in our case. Now, what was the reason? The next suspect was the cache. However, the process was not simple. No one except me in the whole group believed that it can be caused by cache. Also, I myself was doubtful as I felt SCU should take care of it.

To clear our doubt, we talked to a manager in our company who was an expert in JTAG. He appeared completely assured of the fact that we are doing something dumb with JTAG as we cannot see this memory even in JTAG. When I asked about cache coherency issues, he brushed aside my doubts saying that it can never happen.

Well,  after giving the advice, he walked away without turning back. But, I was the tech lead for this project and I had to get this working somehow. And, something kept telling me that this is a cache issue. So, I decided to keep hammering at that point. Now, we were writing from VxWorks and reading from Linux. I decided to look for code which will flush cache. But, being a noob in VxWorks, I could not find this code. We tried some silly tricks in the beginning like increasing the memory size in the hope that cache will get flushed auto-magically. Fortunately or unfortunately, these tricks did not work. However, one guy in our team was able to dig out VxWorks function to flush cache. It was a simple function that took the cache type, address and length as the arguments and it did its job! (Yes that was simple compared to what I found for Linux. We had to get physical address of the memory, get page structure and then flush the page from cache!) Yes, after flushing the cache, we were able to see what was being written by the second core into the memory location from the code that was running in first core. Later, another member in my team found that SCU will not do this in case of ARM11MPCore. It does this in case of some other newer chips (Cortex-A9). We also read that allocating DMA memory might do the flushing directly. We are yet to try this.

Linux IPI handling for ARM11MPCore (ASMP)

We have been struggling for a while with this. Last Friday, I nailed him down.Ok, the background story...

We knew that IPI (Inter Processor Interrupt) can be used to communicate with other processor. Linux has support for this in SMP mode. However, we are doing ASMP. We wanted to use it slightly differently to device a custom communication module with another core running VxWorks. Now, the first hurdle came in the form of not being able to write to some registers. That was a dumb mistake of not ioremapping the register addresses. After that we faced another problem of not being able to register an interrupt. After a bit of debugging, we found that the reason is that there is no chip descriptor registered for interrupts 1-16 (They are the interrupt numbers for IPI). Well, the init code for interrupt was doing it for interrupts >= 29. But why?

It is this question that forced us to spend a week to resolve. There was one guy working on it continuously and I will break my head whenever I get time. Now, we tried the most obvious. We changed 29 to 1 in init code for interrupts. This enabled us to register the interrupt. However, this did not cause our interrupt function to be called when we wrote to the register. Last Friday, I decided to look at realview code. And it paid off. I could see that, they are also registering the interrupt from 29 onwards in the initialization code. Now, what was the problem? To know that, I had to delve much deeper. The answer came from the low level vector interface routines. The do_IRQ was not responsible for handling IPI. Instead, there was a do_IPI function. We immediately reverted the changes done in the irq initialization code. Then, we put some code into do_IPI

Well, thats it! our interrupts started working. We did face some issues while writing to the shared memory. All about that in another post.

Sunday, September 12, 2010

$35 tablet

Well, today I saw this news about $35 tablet

http://androidos.in/2010/09/the-truth-about-35-android-tablet-from-indian-government/

As per that page, governments claim of developing a tablet here in India is false. The blogger claims that the tablet is imported from China. But, the government claims that it is developed by a student in Vellore. Now, have a closer look at the Chinese one


If you see that closely, this device is based on ARM11. It has 256MB RAM. Well, I have been looking for the specs of the Indian tablet. Guess what I found:

http://armdevices.net/2010/07/23/indias-35-tablet-is-based-on-my-video/

The device seen there is very much speculated to be the Indian device that is priced at $35. However, that seem to be something made by a private company and not by universities or government. More details of this can be found here: http://www.allgosystems.com/html/android_stamp_i.mx233.htm

If that were the device which Mr. Sibal was talking about, then it is pretty clear that there is a difference in the processor itself (this one uses ARM9, i.MX233) and they are indeed different apart from the plastic out side. Sadly, I could not find more details on the stuff Mr. Sibal was talking about. The best I could get was this: http://www.engadget.com/2010/08/11/35-tablet-makes-an-appearance-on-indian-tv-video/

Well, now I am confused. What is the spec of the stuff that government is going to bring out? Who is this AllGo guy? Did the student after making it at campus go and join this AllGo? Was this a reference design that Freescale or someone like that floated and everyone copied? Did the student who designed it, copy the stuff from somewhere else?

The government tablet seems to have 3G support and 2G RAM. The AllGo one does not have 3G and the RAM is only 128MB. Well, I really doubt 2G RAM is a mistake. Else that itself will cost > $35. May be its 2GB flash.Well, I think I will wait for a while till more information is available.

Sunday, August 15, 2010

Process holding the block devices

Recently I had to write a program to shred a live disk. This was a security requirement for our devices. However, while testing the program, I found that this always failed. It was possible to overwrite whole of the disk except for a single sector. I knew that some process is using this part of the disk and that is the reason I was not able to write to it. I did an lsof for the device when the write operation was going on. There were certain process having files opened from that device. However, even after killing them, my program was not successful in writing the entire disk. Later, I even figured out that the programs with open files were not causing any real problem! But, then what was blocking the overwrite?

Now, we have decided to reboot the machine and do this operation before boot up. However, my doubt stayed till today. It is only today that I came to know about the real culprit. It is none other than the kernel. When a FS is mounted, the disk is opened by kernel. As long as it is mounted, kernel holds this open fd. As mount would have exited long back, so, one will never see this being caused by mount!

Friday, July 30, 2010

Looking for Linux kernel and C programmers (jobs)

Ok, we have been paining our HR for a while for this. But, day by day I am losing my confidence on getting any good resumes from them. We are looking for people with C and Linux internal skills. We have three openings. The first one is for Linux system programmers. The second one is for a device driver expert (PCIex, PCI, USB...). Last but not least, we are also looking for someone good at Linux (and optionally VxWorks) BSP. Knowledge of uboot will be an added advantage. If there is someone who is good at all these you are very much welcome. Also, it goes without saying that you should be very good at C.

If you have some extra Java or C++ skills, its going to be and added plus point. But, they are strictly optional. Btw, though we are mostly known for our memory chips and mobile phones, this opening is for our printer division. If you are interested, please send in your to suseelan at gmail.com


Google Cloud printing

Today we had a demo of Google cloud print. There were two teams which participated in the cloud demo. The first one made a client for the printer. It was an inbuilt client and we had our first "Web enabled" printer. We could submit a job at Google cloud service and the printer will fetch it and print. The interesting part was that it was done on the legacy printer.

The second one was an adobe AIR application that we developed and we in fact developed two application. We developed a proxy where one can register the legacy printers to cloud and an application which will allow a user to submit a print job to cloud. We demoed both the applications to our management.

There is one more application that we developed. This allows a user to e-mail a document to an address and the document will get printed on a given printer. The beauty of this application is that the printer does not have to be web enabled. The application works from a PC and does the job of submitting the print jobs to printer

Btw, I just noticed this; I was posting from chrome browser and when I tried to enter the tags, it was automatically selecting some tags for me. I tried to tag it with print and when I put a coma there, it changed the tag to suggested "Serial printer" tag (must be from my old tag cloud). Same way, it was replacing the "Cloud" with cloud computing etc. Had to put fight to make it understand that I am a superior intelligent being ;)

Sunday, June 20, 2010

How (not) to get chrome OS running in vmware

This weekend, I was just trying out Google's Chromium OS. The build went fine, all credits to Gentoo. But, the script to make a vmware image out of it failed miserably. After a lot of debugging I realised that a post install script is getting called which is causing all these issues. Now, what does the post install do?

/postinst messes up with your disk! Thank god that I realised this and I did not have something called /dev/sda12. Whatever be the the reason, they should have at least warned the users that image_to_vmare.sh script will try to mess up with the disk. Since I was pretty sure that I am interested in a vmware image, and no disk need to be involved here, I simply commented out that and proceed.

Once the image was formed, it was not booting in VMWare. Then I downloaded a openfiler vmware image and mounted chromeos disk as secondary. After a bit of tweaking with the grub configuration file (/boot/extlinux.conf), I was able to see something on the screen. I did the following things for this.

1. Marked /dev/sdb12 as a bootable partition using cgpt
2. Removed quiet and console options from kernel command line
3. Changed from USB boot to hdd boot
4. Changed the root device to /dev/sda3 and changed the ro option to rw

Now, comes the second pain. The X won't startup. I still haven't got it solved. But, I can currently run chrome. Lets see what I did for that.

1. Create a back-door entry by editing /etc/init/tty2.conf
1.1 Ask agetty to use /bin/bash as login program
2. Create a small shell script in / and pass the path to it as user name
(These were again done by mounting the disk on another working vmware appliance)
3. Create a /etc/init/network.conf for bringing up the wired network (Yep, that also did not work. Added a modprobe, ifconfig and route to do this)

Booted the machine and after ui.conf failed, I switched to tty2. Just gave my script path as login name and I got a shell. Now, I was able to start the X server. Created a new config file using X -configure and copied it to /etc/X11/xorg.conf. After this, I put the following line to ~/.xinitrc

echo /opt/google/chrome/chrome > ~/.xinitrc

Now, I typed in xinit and I have the chrome running!

There were many other painful stuff that I had to go through from yesterday morning till today 11AM to get this stuff working. But, I am still not happy. I want the UI to come up auto magically and let me login to google. I can see the reboot speed is really fast. I say reboot and the its done in 2-3 seconds! But, I really want to see the time taken by the actual UI to come up. If I get this working, I will post it here.

Currently, chrome allows me to browse the web like it does on a normal browser. Only change I could see was the presence of network icons on the top right corner. Also, if I directly type in http://google.com, it crashes. It seems like it is looking for some key manager and does not find it. At times, I can see it cribbing about CrOS library not getting loaded. Anyway, I only spent a day on this. Rest, next week :)

Sunday, June 13, 2010

FreeMind

Well, I just read about this in LFY today and wanted to try it. Seems like its a cool tool. I made a introductory Linux presentation (meant for our Java developers) in this. May be it is not a real strong use case. But, Freemind sure did help me organize things a bit. The software has got some nice folding and re-arranging options which I liked.

You can find FreeMind at http://freemind.sourceforge.net/wiki/index.php/Main_Page

I am also uploading the stuff that I made (exported to jpeg) using this.

Sunday, April 25, 2010

.vimrc

I used to have a .vimrc earlier. However, over time, I lost it and forgot most of my settings. I am building one. Nothing special. But, the stuff that will make me more productive while coding using vim...

Below is my current .vimrc. I will keep adding to it as and when I modify the .vimrc that I use.



se ts=4
se sw=4

au BufNewFile,BufRead *.c se cindent

function Toggle_hls()
:if ( &hlsearch == 1)
: se nohls
:else
: se hls
:endif
endfunction

map <F2> :call Toggle_hls() <CR>

Friday, April 2, 2010

Fedora 13 on acer 5740

Recently I had to install F12 on an acer 5740. However, F12 simply failed to installed on that machine. The laptop had the following configuration.

  • Core i3 processor
  • 3 GB RAM
  • Intel graphic media accelerator
  • Broadcom gigabit ethernet (BCM57780)
  • Atheros AR928X wireless chip
F12 always failed in hardware detection stage itself. I started F12 installation with a no probe. However, I did not have a local installation media and I was doing a network installation. Unfortunately, the network card was not recognized by F12. I eventually gave up on the F12 idea and started downloading F13 alpha.

F13 (64 bit) went it just fine. Installation started in the graphical mode. I made a custom installation with about 1700+ packages selected. It took some time to install. But, I did not calculate the exact time for installation. All I remember is that it was not that particularly fast.

After it came up, I faced two issues. One was about the broadcom card. The stock tg3 driver did not recognize the card. I downloaded and installed the driver from broadcom site and it started working.
http://www.broadcom.com/support/ethernet_nic/netxtreme_desktop.php

The other issue is with cpuspeed daemon. It just crashes my kernel on reboot. Guess, the driver exit is not written properly. But as it crashes only at the reboots (and still reboot works fine), I am really not bothered about it.

Well, the graphic and wireless drivers worked just out of the box. But after an update, it was hanging at the end of boot-up. I changed to runlevel 3. However, X still works if I do a startx

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.

Tuesday, February 2, 2010

The alignment magic

We faced some trouble with assignment of memory to some structures. We did an mmap and allocated memory to different variables and structures. These variables and structures were defined in random order. There were some variables and some structures followed. This caused the problem of memory alignment. So, today, I did a bit of reading on memory alignment and the program that you see below is the result of that reading.

#include <stdio.h>

typedef struct {
    unsigned char uc;
}TST1;

typedef struct {
    unsigned char uc;
    unsigned short us;
}TST2;

typedef struct {
    unsigned char uc;
    unsigned short us;
    unsigned int ui;
}TST3;

typedef struct {
    unsigned char uc;
    unsigned short us;
    unsigned int ui;
} TST4 __attribute__ ((aligned(2)));

#define PRN_ALIGN(x) printf("Alignment of %s is %d \n",#x, __alignof__ (x))

int main()

{

    PRN_ALIGN(TST1);
    PRN_ALIGN(TST2);
    PRN_ALIGN(TST3);
    PRN_ALIGN(TST4);

    return 0;
}

You can clearly see that the alignment varies for different structures. The way to find alignment is to use the __alignof__ (datatype). However, gcc allows you to force the alignment to be something other than the default value. That is achieved by the __attribute__ ((aligned(AlignmentValue))).

We solved our problem by re-arranging the variables. This caused the structure to appear exactly at the alignment boundary. The other solutions would have been to force all structures to have alignment of 1. Yet another solution would have been to do use the __alignof__ to find the acceptable location for placing the structure. Below code is an example using malloc

    unsigned char *memPtr;
    
    memPtr=malloc(100);

    printf("%p\n",memPtr);
    TST2 *pstMyStructure= (TST2*) (((int)memPtr / __alignof__ (TST2) + 1) * __alignof__ (TST2));
    printf("%p\n",pstMyStructure);
    memPtr = (unsigned char*)((int)memPtr / __alignof__ (TST2) + 1) + __alignof__ (TST2) + sizeof(TST2);
    printf("%p\n",memPtr);

(Do keep in mind that strictly speaking, we cannot interchange integers and pointers)

Saturday, January 30, 2010

Making of a cd image

Last week, I wanted to write a few files to a cd and give to my father. Those were mp3 files. I tried mkisofs (genisoimage) but the result was not acceptable. Almost all file names were screwed due to name size limitation of iso9660. Also, I did not want the folder hierarchy as I was not sure if my dad's mp3 player will accept it.

Today I sat down to make this happen. There might have been other ways. But, this looked simpler than googling for hours for the most optimum ways....

Write a small program to do the linking...


#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    int i=0;
    char str[128];


    if(argc>1)
    {
        for(i=1;i<argc;i++)
        {
            memset(str,0,sizeof(str));
            sprintf(str,"ln -s \"%s\" .",argv[i]);
            printf("%s\n",str);
            system(str);

        }
    }

    return 0;
}


Compile it into a program (say lnk)
gcc lnk.c -o lnk

Create a folder to hold the links to your mp3 files & enter that folder
mkdir temp
cd temp

Create the link from source folder to here
find ../mp3-collection/ -name "*.mp3" | xargs -d '\n' ./lnk

Make your iso file...
cd ..
genisoimage -ldots -allow-lowercase -allow-multidot -f -J -joliet-long -r -o temp.iso temp/*

(I spent 5 - 10 minutes reading genisoimage man page. Feel free to do the same. -f: follow sym link, -J: Joilet extn, -r: Rock ridge extn)

you can now mount this and check the file names etc (mount -o loop temp.iso /mnt). After you are happy with your brand new iso image, use wodim to write it to a cd

wodim dev=/dev/cdrw temp.iso

Catch: We are creating sym links from a folder tree to a single folder. This can cause issues with same file name repeating. An enhancement to lnk.c can be to check the return status of 'system' call and then modify the file name string accordingly.

Thursday, January 7, 2010

Google live search results

Click on http://www.google.co.in/search?q=CES+2010 and scroll down to the middle of the page... Google has gone to the next level of searching. Live update of search results more like your twitter. I don't know what they call it or any other details... But, this is cool...