Thursday, 30 August 2012

How to get Motherboard details



We can find out all the details about the motherboard in our system using the command lshw.
#lshw -class bus |grep -A 6 Motherboard
       description: Motherboard
       product: DH55TC
       vendor: Intel Corporation
       physical id: 0
       version: AAE70932-302
       serial: BTTC10900DL8
       slot: To be filled by O.E.M.

Inter Process Communication using shared memory


Processes can communicate between each other in various ways, one of the techniques is by using shared memory, i.e. the processes share a memory region between each other which both can read from. Processes can read and write from this memory what ever that has to be shared.

To create or get access to an already created shared memory we use the function shmget (shared memory get).

int shmget(key_t key, size_t size , int shmflg);

Arguments: key : The key used by the process to identify the shared memory, it is assigned by the process and the other processes which want to communicate with this shared memory need to know this key.

size: The size of the shared memory that is allocated.

flag: If the shared memory is being created then we can use the flag IPC_CREAT as the flag. If we are trying the get access to a shared memory already created then we can pass the mode flag specifying the rights for owner,group and the world in the same format as followed by "chmod".

Return : An identifier to the shared memory which is used along with the "key" to identify the memory segment for communication.

Once the share memory is allocated to start the communication we need forts attach this shard memory with the memory of the process to this we use the function shmat (shared memory attach)


void * shmat(int shmid,const void *shmaddr , int shmflg);

Arguments:

shmid: The identifier returned by shmget

shmaddr: The address where the segment should get attached, if left NULL the system automatically chooses a suitable location for the attach.

shmflg: Used in the specific case when mapping of the segment should replace any existing mapping in the range starting at shmaddr , can be left NULL otherwise.

Return: The address of the shared memory that was attached to the process.

Once the memory segment is attached data can be read from and written to the shared memory using the address returned by shmat and the the functions sscanf and sprintf respectively.

Example:

The following program creates a shared memory in the default mode and then writes a message in the shard memory.

Note the used of IPC_CREAT flag with the shmget call.

write_shm.c
------------------------------------------------------------------------------------------------------------
#include <sys/ipc.h>
#include<sys/shm.h>
#include <stdio.h>
main()
{
key_t key=12345;
int shm_id;
void *shm;
char *message="hello";
shm_id =shmget(key,10*sizeof(char),IPC_CREAT);
shm = shmat(shm_id,NULL,NULL);
sprintf(shm,"%s",message);
}
------------------------------------------------------------------------------------------------------------


The following code tries access the shared memory created by the above code and read the data written in it. Note that the value of key used by both the codes are same, failing which the shmget will be unable to identify the shared memory.


 read_shm.c
------------------------------------------------------------------------------------------------------------
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
main()
{
key_t key=12345;
int shm_id;
void *shm;
char *message;
message=malloc(10*sizeof(char));
shm_id=shmget(key,10*sizeof(char),NULL);
shm=shmat(shm_id,NULL,NULL);
if(shm == NULL)
{
printf("error");
}
sscanf(shm,"%s",message);
printf("\nmessage=%s\n",message);
}
------------------------------------------------------------------------------------------------------------


Compile the above programs
Compile the above programs

$gcc write_shm.c -o write_shm
$gcc read_shm.c -o read_shm

execute them

$./write_shm
$./read_shm
Message = hello

You should see the message written by the first process displayed by the second process thus achieving the required communication between the processes.

Wednesday, 25 July 2012

COLINUX:


Running Linux under Windows Using  Cooperative Linux

coLinux
coLinux is a port of the standard Linux kernel. In other words, coLinux is the Linux kernel that's modified to run cooperatively with another operating system. The host operating system (Windows or Linux) maintains control of the physical resources of the operating system, while the guest operating system (coLinux) is provided with a virtual abstraction of the hardware. The host operating system must provide the means to execute a driver in the privileged ring (ring 0) and export the means to allocate memory (see Figure 3).

Figure 1. coLinux executes as a process of the host operating system
coLinux executes as a process           of the host operating system
The root file system for coLinux is a regular file within the host operating system. To Windows it's just a regular file, but to coLinux it's an ext3 file system that can be read and written to.
Other features needed by the Linux kernel, such as networking or video access, are proxied externally. Networking is made accessible to coLinux through a TUN/TAP driver (which is covered in "Networking," below). In short, this driver provides user-space access to the Ethernet device so that packets can be transmitted and received. Access to the display is also proxied. Recall that X Window System is a protocol by which video output can be sent from one host to another. Therefore, by using an X Window System server on the host operating system, video output can be redirected to the available X server.
Now on to the installation of coLinux and configuration for both networking and video display.
Installing coLinux
Installing coLinux is surprisingly simple. There are a few steps involved, depending upon what you intend to do, but they're straightforward and worked on my Windows XP box without a single problem. This section explores installing coLinux and enabling services such as networking.
The first step is to download a coLinux distribution. Go to http://www.colinux.org and select Downloads from the left sidebar. If your browser doesn't take you to SourceForge, select the link to go there directly. Near the middle of the page is the coLinux-stable package. Download the coLinux executable file (at the time of this writing, the latest is coLinux-0.6.4.exe). The 0.6.4 release of coLinux is the 2.6.11 Linux kernel. When it has finished downloading, double-click the file to install.
After the usual license acceptance, you'll be asked for the components that you'd like to install. Leave these as is (all should be selected), including downloading a root file system image. To make things easier later on, change the destination folder for the coLinux install to c:\colinux\ because colinux is the standard install subdirectory from the perspective of configuration files.

When the installation program asks for a root file system image, select the Debian distribution because it's the smallest and extracts to only 1GB. Select Install to perform the install and root file system download.
When the install completes, you're not quite done yet. The next step is to open a folder to the install subdirectory to decompress the root file system.
The coLinux README file includes lots of additional information about other install options. This is available in the install subdirectory (c:\colinux). There will be an oddly named file that ends in .bz2. Rename this file to root_fs (this is the default root file system file in the configuration).
At this point, installation of coLinux is basically done. You can then start the coLinux daemon to run with Windows XP as follows (invoking through a Command Prompt window):
$ colinux-daemon.exe -c default.colinux.xml
            

After invoking the colinux-daemon, the boot window is displayed (see Figure 4). This provides the same boot information that you find in the traditional Linux boot. Notice that coLinux boots extremely fast.

Figure 2. The coLinux boot window
The coLinux boot window
A console window is also produced (see Figure 3) that attaches to the colinux-daemon. From this window, you can log in to coLinux to interact with the shell. The default username and password is root/root.

Figure 3. The coLinux virtual console (shell)
The coLinux virtual console           (shell)
From Figure 3, you can see that the coLinux console is made up of two sections: the traditional console and the virtual console that provides information about the monitor.
Networking
Networking support for coLinux is done from the Linux perspective when the install is complete. Recall that the TAP driver is loaded during the install. The TAP driver is a user-space tap onto the Ethernet device managed by the host operating system. The TAP driver allows the guest operating system to read or write raw Ethernet frames to a virtual Ethernet device (extended to user space). The virtual Ethernet device in user space then moves Ethernet frames to and from the real Ethernet device in the host operating system. Note that the TAP driver moves Ethernet frames, while the TUN driver is used for Internet Protocol (IP) frames.
For the TAP driver to work, the host operating system must share the available Ethernet device. To enable sharing within Windows XP, open the Network Connections panel from the Control Panel. Select the active local area connection, and then open the properties. Select the Advanced tab, and then select the check box for allowing other network users to connect through this computer's Internet connection (see Figure 6).

Figure 4. Local area connection properties for enabling network device sharing
Local area connection           properties for enabling network device sharing
After the network is shared, you can start coLinux and use the network as you would normally. This is shown in Figure 7 with the ping command.

Figure 5. Using the shared network device is transparent with coLinux through the TAP driver
Using the shared network           device is transparent with coLinux through the TAP driver
With little effort and configuration, coLinux provides networking out of the box.

X Window System
A console window is fine, but a graphical window manager would be ideal. With an open source X server, such as Xming, you can create xterms or use other graphical applications with coLinux. There are numerous documented options, such as Virtual Network Computing (VNC), but I'll show you how using Xming.
The first step is to download Xming from SourceForge. After installing, there's one file, called X0.hosts, that you need to update in the install subdirectory. It contains the remote hosts that are permitted access to the X server. Simply add the IP address of the machine that hosts the coLinux process. Start the Xming X server and coLinux, and then perform the following commands in coLinux:
colinux:~# export DISPLAY=192.168.1.3:0.0
colinux:~# xterm &
            

Note that the IP address specified here is the IP address of the coLinux host. After you execute the xterm command, a new xterm window is presented, as shown in Figure 6.

Figure 6. The xterm created with Xming
The xterm created with Xming
Extending coLinux
Whichever root file system you download, it may not include everything that you'd like. But you can easily extend the root file system. For example, with Debian GNU/Linux, you can use the Advanced Packaging Tool (APT), a package management system, to install new packages or update existing ones. The first step is to update the APT metadata, which maintains management information about the installed packages (including newly available packages and where to get them):
$ apt-get update
            

Now you can update your root file system with other packages that you need. For example, if you want to add the wonderful Ruby language to your root file system, you invoke the following command:
$ apt-get install ruby
            

In this way, you can use an existing root file system and tailor it to your specific needs. You can also rebuild coLinux from the sources if it lacks something that you need.
Advantages of coLinux
coLinux is a great way to use and experiment with Linux. Like Cygwin, it allows you to develop and execute Linux applications on the Windows operating system (through coLinux). You can also maintain the Linux operating system by installing, upgrading, or removing applications with apt-get.
Unlike Cygwin, you can execute Linux applications on coLinux without rebuilding. In this respect, coLinux is a real Linux operating system that runs (or cooperates) with the Windows operating system.
Another interesting advantage of coLinux is its portability. You can have a coLinux distribution with a custom set of applications on a given Windows host (within the root file system). You can move the root file system to another host, and then restart it. This allows for a mobile development platform where the compressed root file system fits on a standard Universal Serial Bus (USB) memory stick.
Finally, coLinux is fast because it's essentially running on the native hardware.
Problems with coLinux
The primary disadvantage of coLinux is that it has the ability to crash the entire machine (all cooperating operating systems) because the guest operating system runs in a privileged mode in the host kernel. It also has some dependencies on external software for normal operation (windows and networking support). Outside of this, it's quite easy to install and configure. In the many hours that I've used it, I've never seen a crash.

Thursday, 17 May 2012

Basic SVN Commands

Here I'm gonna show you some basic SVN commands with some examples.

Checking out
svn co http://[hostname]/trunk

Checking in
svn ci

Viewing SVN information
This will display information, such SVN URL, revision number, etc.
svn info

Viewing SVN status
This will display information, such as files modified, added, deleted, etc.
svn status

Updating the local SVN repository
svn up

Viewing the diff
svn diff

Viewing SVN logs
svn log

Adding a file into SVN
svn add new_file.txt

Moving/Renaming a file from one place to another place
svn move old_file.txt new_file.txt

Deleting a file from SVN
svn del junk_file.txt

Reverting local changes in a file
svn revert whatever_file.txt

Reverting local changes recursively
svn revert -R *

Creating a branch/tag
svn copy http://[hostname]/trunk http://[hostname]/branches/1.0
svn copy http://[hostname]/trunk http://[hostname]/tags/1.0

De-commiting changes in the trunk
This will first do a diff between HEAD and rev 123 in the trunk and then do a dry-run before performing an actual merge.
svn diff -r HEAD:1234 http:/[hostname]/trunk
svn merge -r --dry-run HEAD:1234 http:/[hostname]/trunk
svn merge -r HEAD:1234 http:/[hostname]/trunk
svn ci

Merging changes from the branch to the trunk
Assume that the current directory is where the trunk code was checked out.
svn diff -r 1234:HEAD http://[hostname]branches/1.0
svn merge -r 1234:HEAD --dry-run http://[hostname]/branches/1.0
svn merge -r 1234:HEAD http://[hostname]/branches/1.0
svn ci

Merging changes from the trunk to the branch
Assume that the current directory is where the branch code was checked out.
svn diff -r 1234:HEAD http://[hostname]/trunk
svn merge -r 1234:HEAD --dry-run http://[hostname]/trunk
svn merge -r 1234:HEAD http://[hostname]/trunk
svn ci

Viewing SVN externals
svn propset svn:externals .

Editing SVN externals
svn propedit svn:externals .

Viewing who modified the file
svn blame whosefault.txt

Getting Started with GDB

GDB is a very powerful debugging tool for C/C++ and many other languages. Here I'm gonna show you how to get started with GDB.

testgdb.cpp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
  
string globalmsg = "Hello World";
  
void half(int& a) {
    a /= 2;
}
  
int whatever() {
    int i = 5;
    i *= 3;
    return i;
}
  
int main() {
    int a = 10;
    half(a);
    cout << a << endl;
    cout << whatever() << endl;
    return 0;
}

1. Compile the code
g++ -c -g -Wall -O0 testgdb.cpp
The -O0 flag is useful for debugging information because the compiler may do its own optimization that can eliminate some debugging information, such as -O2 flag for example.
2. Link it
g++ -o testgdb testgdb.o
3. Run it
gdb ./testgdb
After running this statement, there will be gdb prompt.
4. Set the breakpoint in line 18 and 19
break testgdb.cpp:18
break testgdb.cpp:19
5. See the list of breakpoints
info break
6. Remove the second breakpoint
delete 2
7. Run the program
run
8. See the current line
list
9. Step over to the next line
next
10. See the current line again
list
11. Step in to the half() function
step
12. Print the arguments
info args
13. Go to the next line
next
14. Print the value a now
print a
The new value should be 5.
15. Step out from half() function
finish
16. Print all the local variables
info locals
17. Set another breakpoint in line 13
break testgdb.cpp:13
18. Jump to that newly-set breakpoint
continue
19. Step over
next
20. Print the new value of i
print i
The value of i should be 15
21. Step out from the whatever() function
finish
22. Get the length of globalmessage string
print globalmessage.size()
23. Quit gdb
quit

I hope this brief introduction is useful. For more information, type
help <command>

Tuesday, 3 April 2012

HowToUbootScriptcmd

U-boot is a very versatile universal opensource bootloader, a customized version of which is used in VT8500 and WM8505-based devices. It has a built-in mini-shell with some handy commands that can be accessed through a [[serial console]]. Alternatively, required command sequences to e.g. redefine boot parameters and load a custom OS (like the Linux kernel) can be put into a so-called scriptcmd file. This is basically just a plain text script for the U-boot’s shell with just some binary header added to it.
The useful fact is that by default VT8500/WM8505 versions of U-boot will happily execute a scriptcmd file put into a script/ folder on the first partition (FAT) of an SD card, which allows for a simple and non-destructive redefinition of the boot sequence.
Below are the simple steps to take to create your own scriptcmd file (assuming that you have the mkimage program from U-boot tools package installed already).
  • Create a plain text file with the commands you need and call it, for example, script.txt. In the simplest case its contents may be something like this:
setenv bootargs rootwait root=/dev/sda1
fatload mmc 0 0 /script/uImage
bootm 0
The first statement redefines the kernel command line to allow the first partition on a USB drive (/dev/sda1) to be used as a root filesystem. The second one loads /script/uImage from the first partition of the SD card to a memory address 0. The third one executes the Linux kernel uImage that we've just loaded into memory.
  • Generate the final scriptcmd file (the one with a binary header) by issuing a command like this:
mkimage -A arm -O linux -T script -C none -a 0 -e 0 -n “My scriptcmd” -d script.txt scriptcmd
  • Put the scriptcmd file together with your kernel uImage to a script/ folder on the first (FAT) partition on an SD card and turn your device on with this card inserted. If everything is OK, your kernel should boot up.

Pthread Example


Set up Ubuntu to use static ip address

 had bit problem when using tftp server to boot NGW100, every time DHCP tend to find a different ip address for my PC, which is not what I wanted;I don’t want to change NGW100 boot args everytime to use different TFTP server or NFS server.
Here is what I do:
If Your Ubuntu System has set to use DHCP, you will want to change it to a static IP address here is simple tip
open the /etc/network/interfaces file.
sudo vi /etc/network/interfaces
If you are using DHCP for your primary network card which is usually eth0, you will see the following lines
auto eth0
iface eth0 inet dhcp
As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should add and here is the example and you can change these settings according to your network settings.
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
Restart the neworking service using the following command
sudo /etc/init.d/networking restart
My interfaces file like this:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
        address 192.168.1.59
        netmask 255.255.255.0
        broadcast 192.168.1.255
        network 192.168.1.0
        gateway 192.168.1.2

PThread example
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    void *print_message_function( void *ptr );
    main()
    {
         pthread_t thread1, thread2;
         char *message1 = “Thread 1″;
         char *message2 = “Thread 2″;
         int  iret1, iret2;
        /* Create independent threads each of which will execute function */
         iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
         iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
         /* Wait till threads are complete before main continues. Unless we  */
         /* wait we run the risk of executing an exit which will terminate   */
         /* the process and all threads before the threads have completed.   */
         pthread_join( thread1, NULL);
         pthread_join( thread2, NULL);
         printf(“Thread 1 returns: %d “,iret1);
         printf(“Thread 2 returns: %d “,iret2);
         exit(0);
    }
    void *print_message_function( void *ptr )
    {
         char *message;
         message = (char *) ptr;
         printf(“%s “, message);
    }