How to determine your machine is “Little Endian” or “Big Endian”.

What is big and Little Endian ?

Little and big endian are two ways of storing multibyte data-types ( int, float, etc). In little endian machines, last byte of binary representation of the multibyte data-type is stored first. On the other hand, in big endian machines, first byte of binary representation of the multibyte data-type is stored first.

Big Endian(Wikipedia)

Little Endian(Wikipedia)

Is there a quick way to determine endianness of your machine?
There are n no. of ways for determining endianness of your machine. Here is one quick way of doing the same.

#include <stdio.h>
int main()
{
   unsigned int i = 1;
   char *c = (char*)&i;
   if (*c)   
       printf("Little endian");
   else
       printf("Big endian");
   getchar();
   return 0;
}

In the above program, a character pointer c is pointing to an integer i. Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer. If machine is little endian then *c will be 1 (because last byte is stored first) and if machine is big endian then *c will be 0.

How to get a “codesigned” gdb on OS X?

Very interesting problem,  I wanted to run gdb on my mac but i was not able to to run it.  Because it was not codeSigned . Here’s the solution.

The Darwin Kernel requires the debugger to have special permissions before it is allowed to control other processes. These permissions are granted by codesigning the GDB executable. Without these permissions, the debugger will report error messages such as:

Starting program: /x/y/foo
Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
 (please check gdb is codesigned - see taskgated(8))

Codesigning requires a certificate. The following procedure explains how to create one:

(Note ) : I tried many times creating certificate for gdb, basic problem was while creating certificate, Please Create certificate for “System” not for “login” that is main problem. 

  • Start the Keychain Access application (in /Applications/Utilities/Keychain Access.app)
  • Select the Keychain Access -> Certificate Assistant -> Create a Certificate… menu
  • Then:
    • Choose a name for the new certificate (this procedure will use “gdb-cert” as an example)
    • Set “Identity Type” to “Self Signed Root”
    • Set “Certificate Type” to “Code Signing”
    • Activate the “Let me override defaults” option
  • Click several times on “Continue” until the “Specify a Location For The Certificate” screen appears, then set “Keychain” to “System”
  • Click on “Continue” until the certificate is created
  • Finally, in the view, double-click on the new certificate, and set “When using this certificate” to “Always Trust”
  • Exit the Keychain Access application and restart the computer (this is unfortunately required)

Once a certificate has been created, the debugger can be codesigned as follow. In a Terminal, run the following command…

codesign -f -s  "gdb-cert"  <gnat_install_prefix>/bin/gdb

… where “gdb-cert” should be replaced by the actual certificate name chosen above, and should be replaced by the location where you installed GNAT.

Restoring lost commits in git

Hey i am writing post after a long time.

I was working in git and committed some changes, i forgot to pushed to branch and i forgot the commit too. I reseted the HEAD :(.

So, you just did a git reset --hard HEAD^ and threw out your last commit. Well, it turns out you really did need those changes. . Don’t fear, git should still have your commit. When you do a reset, the commit you threw out goes to a “dangling” state. It’s still in git’s datastore, waiting for the next garbage collection to clean it up. So unless you’ve ran a git gc since you tossed it, you should be in the clear to restore it.

$ git show-ref -h HEAD
  7c61179cbe51c050c5520b4399f7b14eec943754 HEAD

$ git reset --hard HEAD^
  HEAD is now at 39ba87b Fixing about and submit pages so they don't look stupid

$ git show-ref -h HEAD
  39ba87bf28b5bb223feffafb59638f6f46908cac HEAD

So our HEAD has been backed up by one commit. At this point if we wanted it back we could just git pull, but we’re assuming that only our local repository knows about the commit. We need the SHA1 of the commit so we can bring it back. We can prove that git knows about the commit still with the fsck command:

$ git fsck --lost-found
  [... some blobs omitted ...]
  dangling commit 7c61179cbe51c050c5520b4399f7b14eec943754

You can also see the that git knows about the commit still by using the reflogcommand:

$ git reflog
  39ba87b... HEAD@{0}: HEAD~1: updating HEAD
  7c61179... HEAD@{1}: pull origin master: Fast forward
  [... lots of other refs ...]

So, we now have our SHA1: 7c61179. If we want to get immediately apply it back onto our current branch, doing a git merge will recover the commit:

$ git merge 7c61179
  Updating 39ba87b..7c61179
  Fast forward
    css/screen.css |    4 ++++
    submit.html    |    4 ++--
    2 files changed, 6 insertions(+), 2 deletions(-)

This command will bring your lost changes back and make sure that HEAD is pointing at the commit. From here you can continue to work as normal! You could also checkout the SHA1 into a new branch, but really a merge is the fastest and easiest way to restore that lost commit once you have the hash. If you have other ways let us know in the comments!

Thanx to gitReady for this valuable post.

Recovering from Broken Grub

On Friday, i was trying to down-grade Grub to grub-legacy. So installed grub-legacy, i knew i was playing with bootloader. When i restart my OS, as expected grub was not able to find out the OS. Problem became more worsen when i came to know, i didn’t installed stage1, stage1.5 and stage2 scripts means i didn’t ran commands(grub-mkconfig).

Dos grub didn’t had grub-install, 

Grub Error

So i googled didn’t found any solution. I read from different blog, websites and tried this.

  1. Use any live os and run grub-install
First Mount the partition where OS is installed. You can find the partition by running. 

#$ blkid
/dev/sda1: UUID="ee51f4e9-1ef8-4b65-8ef4-299600e8cbf4" TYPE="ext4" PTTYPE="dos" PARTUUID="c679c6ed-01" 

/dev/sda2: UUID="cb97ec88-4282-459a-852f-f619138d46d9" TYPE="ext4" PARTUUID="c679c6ed-02"

then run 
sudo mount /dev/sda1 /mnt 
(Make sure partition in write mode)
mount -o remount, rw /dev/sda2
(Here sdb3 where OS is installed)

grub-install --target=/mnt --recheck /dev/sda2

Now Scripts are installed reboot the machine.(Most probably you will get a grub black screen)

Now you have to do 3 things

a. Find the partitions. 

ls

it will show you how many partitions are here,  here you may get like

(hd0) (hd0,5) (hd0,1) (hd1) (hd1,1) (hd1,2) (fd0) (hd0,msdos1) (hd0, msdos2)

Then run

ls /(hd0,0)

and observe the output, if you are getting Linux root(where folders like etc, boot are present) then this is your root.

b. Set the root

root (hd0,0)

Here (hd0,0) Explained Here.

  • The brackets are a must; all devices listed in GRUB menu must be enclosed in brackets.
  • hd stands for hard disk; alternatively, fd stands for floppy disk, cd stands for CD-ROM etc.
  • The first number (integer for geeks) refers to the physical hard drive number; in this case, the first drive, as they are counted from zero up. For example, hd2 refers to the third physical hard drive.
  • The second number refers to the partition number of the selected hard drive; again, partitions are counted from zero up. In this case, 1 stands for the second partition.

From here, it is evident that GRUB (menu) does not discriminate between IDE or SCSI drives or primary or logical partitions. The task of deciding which hard drive or partition may boot is left to BIOS and Stage 1. As you see, the notation is very simple.

Primary partitions are marked from 0 to 3 (hd?,0), (hd?,1), (hd?,2), (hd?,3). Logical partitions in the extended partition are counted from 4 up, regardless of the actual number of primary partitions on the hard disk, e.g. (hd1,7).

For me I guessed, i tried like setting up the root, like above mentioned then. used grub’s ls command if ls /boot+tab shows any thing that partition where you have to install actually re-install your Grub. 

c. Load the kernel

kernel /boot/vmlinux-linux  ro root=/dev/sda2

d. Load the Linux img

initrd /boot/vmlinux-linux-lts.img

Then Run

boot

You will be able to boot the desired OS. [1]

Link Aggregation LAG(IEEE 802.3ad)

Yesterday my colleague asked me about LAG, whats the meaning of LAG and what’s the use of it?

What does Link Aggregation (LAG) mean?

Link aggregation (LAG) is used to describe various methods for using multiple parallel network connections to increase throughput beyond the limit that one link (one connection) can achieve. For link aggregation, physical ports must reside on a single switch. Split Multi-Link Trunking (SMLT) and Routed-SMLT (RSMLT) remove this limitation and physical ports are allowed to connect/split between two switches. This term is also known as Multi-Link Trunking (MLT), Link Bundling, Ethernet/Network/NIC Bonding or NIC teaming.

Link Aggregation (LAG) :

Link aggregation is a technique used in a high-speed-backbone network to enable the fast and inexpensive transmission of bulk data. The best feature of link aggregation is its ability to enhance or increase the network capacity while maintaining a fast transmission speed and not changing any hardware devices, thus reducing cost. Cost Effectiveness LAG is a very common technique for establishing a new network infrastructure using extra cabling above the current requirements. Labor cost is much more than the cost of cabling. Thus, when a network extension is required, the extra cables are used without incurring any additional labor. However, this can be done only when extra ports are available. Higher-Link Availability This is the best feature of LAG. A communication system keeps working even when a link fails. In such situations, link capacity is reduced but data flow is not interrupted. Network Backbone Formerly, there were many techniques used for networking, but IEEE standards are always preferred. LAG supports network load balancing. Different load balancing algorithms are set by network engineers or administrators. Furthermore, network speed is increased by small increments, saving both resources and cost. Limitations With all kinds of implementations, each link and piece of hardware is standardized and engineered to not affect the network efficiency or link speed. Additionally, with single-switching all kind of ports (802.3ad, broadcast, etc.) must reside on a single switch or the same logical switch.

How to setup LAG in linux BOX

Thanx to Techopedia

Seconds Since the “Epoch”

I was supposed to write a RT (Real Time) logging which doesn’t call a single Linux CALL.
I had only seconds from 1st Jan 1970 (Called Eposh).

A value that approximates the number of seconds that have elapsed since the Epoch. A Coordinated Universal Time name (specified in terms of seconds (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time represented as seconds since the Epoch, according to the expression below.

If the year is <1970 or the value is negative, the relationship is undefined. If the year is>=1970 and the value is non-negative, the value is related to a Coordinated Universal Time name according to the C-language expression, where tm_sec,  tm_min,  tm_hour,  tm_yday,  and  tm_year are all integer types:

tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400

The relationship between the actual time of day and the current value for seconds since the Epoch is unspecified.

How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined. As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

Note:
The last three terms of the expression add in a day for each year that follows a leap year starting with the first leap year since the Epoch. The first term adds a day every 4 years starting in 1973, the second subtracts a day back out every 100 years starting in 2001, and the third adds a day back in every 400 years starting in 2001. The divisions in the formula are integer divisions; that is, the remainder is discarded leaving only the integer quotient.

You can convert epoch Seconds to current time please look at this LINK.

Xenomai Timer

Xenomai Timer :Xenomai has two time sources: the sytem timer, which counts the number of nanoseconds since 1970, and a hardware dependent high resolution counter which counts the time since an unspecified point in time (usually the system boot time). This hardware dependent high resolution counter is called “tsc” on a PC, and gave its name to Xenomai native API calls.rt_timer_tsc returns the value of this hardware dependent high-resolution counter.
rt_timer_info returns the same thing in the tsc member of the RT_TIMER_INFO structure, and the value of the system timer at exactly the same time as when the high-resolution counter was read.

This allows to have a correspondence between the two time sources.

rt_alarm_inquire is not related to this and returns some information
about a given alarm. Now, if you allow me, a little advice for the implementation of a “timer library”: you could be tempted to create only one periodic alarm object with Xenomai, and to manage a timer list yourself. Don’t do this. Creating an alarm object for each timer library object make Xenomai aware of the existence of all your application timers, this has several

advantages:
– it gives you information about all your timers in /proc/xenomai
– it allows Xenomai to use its anticipation algorithm for all your timers
– if you are concerned about the scalability of Xenomai timers list
management, you can check the options in the “Scalability” menu of
Xenomai configuration menu (“Real-time subsystem” sub-menu of kernel
configuration menu).
more about timers

Xenomai POSIX skin supports two clocks:
CLOCK_REALTIME maps to the nucleus system clock, keeping time as the amount of time since the Epoch, with a resolution of one system clock tick.

CLOCK_MONOTONIC maps to an architecture-dependent high resolution counter, so is suitable for measuring short time intervals. However, when used for sleeping (with clock_nanosleep()), the CLOCK_MONOTONIC clock has a resolution of one system clock tick, like the CLOCK_REALTIME clock.[1]

%d bloggers like this: