Tuesday, June 30, 2009

Using Select() per Thread

void <ProtocolClass>::run()
{
int fd = mPort->GetFileDescriptor();
fd_set readfs;
int maxfd=fd + 1;
int res;

struct timeval Timeout;
Timeout.tv_usec=0;
Timeout.tv_sec=3;


//BYTE ack_message_frame[ACKNOWLEDGE_FRAME_SIZE];
while(true)
{
usleep(10);
FD_ZERO(&readfs);
FD_SET(fd,&readfs);
res=select(maxfd,&readfs,NULL,NULL,NULL);
if(res<0)
perror("\nselect failed");
else if( res==0)
puts("TIMEOUT");
else if(FD_ISSET(fd,&readfs))
{//IF INPUT RECEIVED
qDebug("************RECEIVED DATA****************");
FlushBuf();
qDebug("\nReading data into a read buffer");
int bytes_read=mPort->ReadPort(mBuf,1000);
mFrameReceived=false;
for(int i=0;i<bytes_read;i++)
{
qDebug("%x",mBuf[i]);
}



//if complete frame has been received, write the acknowledge message frame to the port.
if(bytes_read>0)
{
qDebug("\nAbout to Process Received bytes");
ProcessReceivedBytes(mBuf,bytes_read);
qDebug("\n Processed Received bytes");
if(mFrameReceived)
{
int no_bytes=mPort->WritePort(mAcknowledgeMessage,ACKNOWLEDGE_FRAME_SIZE);
}//if frame received
}//if bytes read > 0
} //if input received
}//end while
}

The above is the code for using select call to monitor serial port in a Qt thread's run function. This code cannot be found anywhere else. It's been tested on RHEL4 Qt 4.2.1

Friday, March 27, 2009

Compiling a multithreaded program

Hi all,

If sample.c is our program consisting of code leveraging POSIX threads for multithreading on a Linux platform then the following command is used to compile the program.

gcc -o sample sample.c -lpthread

Cheers!!!!!!

Finding out dependencies of an executable in Linux

Hi all,

ldd command is used to find out the dynamic dependencies of an executable in a Linux/Unix environment.

ldd

will list out all the dynamic libraries used by the executable.It is a great tool to find out the missing modules needed by the executable.

Cheers!

Debugging C,C++ programs on Linux Platform

HI all,

Generally everyone use gdb for debugging C,C++ programs in Linux. There is another tool called valgrind that is an amalgamation of a host of other tools for debugging.For examples memcheck which is used to debug memory related issues, helgrind which is used to debug multithreading programs. The tool is open source and packages exist for all distributions. Usage

valgrind --tool=

Though I must say that you have to read the documentation about the concerned tool to find out exactly what is the meaning of the messages generated by every tool otheriwse its difficult to comprehend the out genereated by the tool for your executable.

Cheers!!!!!!!!!!!!!

Note about pthread_exit

Hi all,

A little note about usage of pthread_exit() while using multithreading on a Linux platform. pthread_exit() is used to kill a process for which it is called but it doesn't release the resource held by the thread immedeatly, this leads to increase in the memory footprint when a thread is killed. To complete this process use pthread_join() or pthread_detach().

Cheers!!!!!!!!!!

Sunday, November 16, 2008

Finding where GRUB stages are installed

Hi all,

The following command when entered in GRUB prompt will list out the drive in which GRUB stage 1 is intalled:

grub> find /boot/grub/stage1

for me it resulted in:
(hd0,5) ------> internal 80 GB SATA
(hd1,0) --------->External USB Western Digital passport

This was because earlier I had grub on internal hard disk which I erased from MBR. And second was for the USB Western Digital passport drive.

One other command that is helpful is:

grub-install --recheck /dev/sdb
This installs grub on the selected drive, WD in my case.

But for a fool proof method use root and setup commands from inside grub prompt to install grub.

Cheers!!!!!!!!!

Installing Fedora Core 9 on Western Digital Passport

Hi all,

Here was the scenario: I have an 80 GB SATA hard drive. I have Windows XP installed on it. Now my idea was to keep my current hard disk configuration unchanged and install a flavour of Linux on an external drive (which is generally a USB interface). With this intention I bought a 160 GB Western Digital Passport USB drive (an excellent drive I must say). I decided to install latest version of fedora FEDORA CORE 9. When I plug in my external disk the resultant device identities are:

/dev/sda -------> 80 GB SATA
/dev/sdb -------> 160 GB WESTERN DIGITAL PASSPORT


1. Now I started out the installation of fedora, choose "linux expert" by typing in at the boot prompt when you boot from fedora CD. But I just followed the normal install routine
2. Did a fresh install, and selected my Western digital drive for installation. Then I did a custom partition of the drive. For every partition I also gave the drive on which to partition i.e /dev/sdb
3. Now I selected the External drive to install GRUB.
4. The installation went perfect and everything was done. Then I did a reboot and through the fedora CD went into the rescue mode by typing "linux rescue" at boot prompt.
5. Select No for Network service when ask and skip the next dialogue box that comes up, you will now be taken up to the shell promp sh.3.2#
6. In my case my boot partition was /dev/sdb1 and root partition was /dev/sdb2. Enter the following set of commands:
mkdir /mnt/source
mkdir /mnt/source/boot
mount /dev/sdb2 /mnt/source
mount /dev/sdb1 /mnt/source/boot
chroot /mnt/source


Remember to execute boot commands in that order. The chroot is to use paths relative to the root directory.
7. Now we will create the initrd image to include modules for USB so that we can boot from our external drive. For this enter the following command:

mkinitrd --with-usb --preload ehci-hcd --preload usb-storage --preload scsi_mod --preload sd_mod
/boot/initrd_usb.gz 2.6.25-14.fc9.i686


The last part of the command is the kernel version. Substitute for the kernel version that you are installing.
8. This with create an initrd image by the name "initrd_usb.gz"
Now you have to replace the intird- entry from the grub.conf file in /boot/grub directory with the "initrd_usb.gz" that we have just created. Also remember to remove the initrd image present in /boot directory before going for a reboot.
9. Now enter the following set of commands:
exit -------> this will exit the chroot
umount /mnt/source/boot
umount /mnt/source
exit --------> to reboot

10. Enter in the bios on reboot and make you boot drive to point to External Drive, in my case Western Digital passport from the First boot device options. Make Sure first that your BIOS supports booting from external drives. Now my PC was bought in 2005 so you have a benchmark as to your BIOS will boot from the USB drive or not.

------------- THE ABOVE PROCEDURE IS THE STANDARD WHICH ONE HAS TO FOLLOW ALWAYS ----------------

But I followed the above procedure in 2 scenarios.

1. Initially, in my first attempt, I also had fedora installed on the internal SATA disk. And I followed the above procedure to intsall FEDORA 9 on my WD drive but the booting stopped after diplaying GRUB at startup and nothing happend not even the stage 1. Then using an UBUNTU LIVE CD fiesty fawn I did a "grub-install /dev/sdb". And now I was fucked because I had tinkered the GRUB on my internal hard disk. I wasn't able to boot into windows also. This is a common occurrence to almost any individual that tinkers with GRUB. But there is nothing to worry here if you have Windows 98 CD. Just boot from it and at the command prompt use the command:
fdisk /mbr
This will fix remove GRUB from your mbr and you will atleast boot into your windows partition.
But this was only a part of the story. Now if I boot up using USB I would get to GRUB STAGE 1.5 and Error 17 would come up and bump, nothing happend.
2. Then I again installed FEDORA 9.Though this time I didn't have GRUB Installed in my internal drive. I followed the install procedure and installed grub from the UBUNTU LIVE CD FIESTY FAWN, by typing "grub" to get into the grub command line
grub> root (hd1,0)
grub> setup (hd1)
grub> quit

Voila!! I had set up grub on my external drive explicitly.
Did a reboot and bingo I got to the stage where I got the splash screen displaying boot options, but when I selected fedora it again gave me Error 17: unknown file system type 0x7 Press an key to continue.......... and on key press it again return to menu.
I edited the menu option by pressing "e" and changed the root (hd1,0) to root (hd0,0) pressed enter and then "b" to boot with selected option and then HURRRRRRRRRRAHHHHHHHHHHH, my 4 days of hand banging into every blog concerning GRUB finally paid of. From here one can figure out that when you boot from a disk which has GRUB on it, GRUB assumes that the drive is hd0 though when you install it can see multiple drives and gives it some other number i.e hd1 or hd2 etc. Thus when you boot up remember to change it to hd0 as it did at the grub command edit feature. To make this permanent, boot into the drive and changed the grub.conf entry and you're done.

Fellas this has been an exhausting exercise and I had been working for close to 35+ hours on getting my drive to boot up. I am now adept at handling any of your queries in this regard. Feel free to comment and/or query. I will be more than happy to every problem of yours.
And lastly, thanks to all the linux community forums, its members and other netizens whose little pieces of information I have gathered to get things to work. But I must admit that there was no single place where I could get all of it. So clubbed everything together here, for every individual to refer to. I hope this helps, and apologies for any typos. I have rechecked all commands here. They have been practically implemented in process and so they work 100%.

Cheers!!!!!!!!!!!!!