Wednesday, January 25, 2006

Fore-warned is fore-armed

Many of the networking or related applications have to marshal/unmarshal packets to/from structures. And its not uncommon to have more than one field within a byte of a packet. So, what's the best way to construct a packet before sending it over the wire? Say, a SCSI CDB (command descriptor block - similar to an SNMP packet) has to be sent to a device target. Lets simplify the whole thing and say that just 1 byte consisting of 8 1-bit flags is to be sent over the wire. What's the best way to represent such a byte? The first solution that comes to anyone's mind would be bitfields. So, let's go ahead with that.

typedef struct CDB_Byte {
  unsigned char a:1;
  unsigned char b:1;
  unsigned char c:1;
  unsigned char d:1;
  unsigned char e:1;
  unsigned char f:1;
  unsigned char g:1;
  unsigned char h:1;
} CDB_Byte;

Now I want to set only the LSB(least significant bit) and the one next to it. Okay, that's simple.

CDB_Byte byte;
byte.h = 1;
byte.g = 1;

But, I have a question. Is this the correct way to do it? The answer is NO. Let me illustrate why this is'nt the correct way with a simple example.

unsigned char c = 0x01; // LSB set
CDB_Byte byte;
std::memcpy(&byte, &c, 1); //copy the bits
// to change the value of 'c' to 0x03
byte.g = 1; // set the bit next to the LSB
std::memcpy(&c, &byte, 1); // copy the bits back
std::cout << (unsigned)c;

Will this print '3'? The answer is 'depends'. The language standard does not say anything about the allocation of bitfields. The compilers are free to implement them as they deem fit. On linux, the gcc compiler allocates the bitfields starting from the LSB. This can be verified - the above piece of code prints '65'.

hgfedcba

However on HP-UX, the aCC compiler allocates bitfields starting from the MSB. The above piece of code prints '3' (as one might have expected in the first place).

abcdefgh

Moral of the story is this: Never use bitfields to construct structures containing sub-byte fields. Always use a void * chunk of memory, extract a byte, and use masks and bitwise operators to set/extract sub-byte fields. At least, if you expect your application to be portable, don't use bitfields! I would say that bitfields should never be used anyway, 'cos there's no guarantee that the same application will run on the same platform if the compiler vendor rolls out a newer version.

Fore-warned is fore-armed!

Monday, January 23, 2006

System Calls

What is a System call? Why do we need system call? These are the few question raise in mind.
Firstly, System Call is a interface between the application and the underlying hardware [managed by kernel, Operating System]. Secondly, ensures security and stability by not allowing direct access to the hardware. The hardware access[system resources] is maintained by the kernel.

Flow of Execution
I will be explaining about how the system call flows.
Consider a printf() call in simple C program, the printf() is an API which inturn call the write system call. System Call are those which are executed in Kernel Mode[or System Mode] in Process context. What I meant was, the system call is only visible to kernel, and it is executed on behalf of the process which calls it, because as I told System Call uses system resources and hardware access, which is managed by Kernel[security issue].

printf() ---> printf() in the C library --->write() in C library --->write() system call in kernel.

The printf() in library and write() in the library indicates the conversion of the printf() in application to make an appropriate call to write() in Kernel. Its usually called "stub". Remember the RPC [Remote Procedure Call] stubs, which is used to convert function call and its arguments to some protocol. The library routine is responsible for invoking the system call.

There is a trap instruction [int 80h - in x86 arch] that is executed which transfers control from user mode to kernel mode to excute the sytem call. The system call is identified in the kernel by the syscall number that is passed by library routine through a register[EAX - in x86 arch]. Further on I will explaining w.r.t x86 arch and w.r.t Linux OS. Once the trap instruction is executed, it generates an interrupt which is handled by syscall_handler(). The interrupts are dealt in kernel mode itself. This handler checks for the sytem call number in the syscall_table, which contain a detailed list of all the system calls present the kernel. There is a numerous other checks that needs to be handled[security issues, takes lot of time to explain].

If the system call is present in the list w.r.t to the syscall number passed in EAX, the system call is invoked with the arguments passed to it. Now, one more question arises, how is the arguments to system call is passed? The answer is through other registers EBX,ECX,EDX, ESI,EDI contain the first 5 arguments passed. If the arguments is more than 5, then a pointer to memory location where it is present in the user mode is passed through register.

In Linux, the return value of each system call is long int so has to be compatible with the 64-bit arch. The return value is stored in EAX register and switched to user mode.

Adding a System Call in Linux. Reusability[of already existing system call] is highly recommended hence think twice before adding a system call.

For any more details or doubts, scratch a comment to this article.

Saturday, January 21, 2006

Floating Point Anamoly

Source:YPK "Test Your C Skills" Ch-4 "Floating Point Issues" 1st question

main()
{
float a=0.7;
if(a <0.7) a="="b" href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">IEEE
and
Why this anamoly

One good practice is

use something like
#include <math.h>

if(fabs(a - b) <= epsilon * fabs(a))

for some suitably-chosen degree of closeness epsilon (as long as a is nonzero!).


Is a[i] = i++; valid?

This is a very basic question, but many intersting concepts emerge out of it.

Answer is "Behaviour is Undefined".A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior), including doing what you expect.

Briefly: implementation-defined means that an implementation must choose some behavior and document it. Unspecified means that an implementation should choose some behavior, but need not document it. Undefined means that absolutely anything might happen. In no case does the Standard impose requirements; in the first two cases it occasionally suggests (and may require a choice from among) a small set of likely behaviors.

The concept of sequence point can be used to base a discussion on
A sequence point is a point in time (at the end of the evaluation of a full expression, or at the ||, &&, ?:, or comma operators, or just before a function call) at which the dust has settled and all side effects are guaranteed to be complete.The ANSI/ISO C Standard states that
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."

The second sentence can be difficult to understand. It says that if an object is written to within a full expression, any and all accesses to it within the same expression must be for the purposes of computing the value to be written. This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification(as above).

Friday, January 13, 2006

C++: Strange name lookup

This is something I never knew and read about it recently in "Exceptional C++". Let me start with a code example...

namespace A {
  class S { };
  void foo(S& parm) { }
}

namespace B {
  void foo(A::S& parm) { }
  void bar(A::S& parm) {
    foo(parm); // Which "foo" does this call?
  }
}

Try this out and to your surprise you'll find that this gives a "call to foo is ambiguous" error. But why?? The only foo visible at the place of call is in namespace B. Why should this result in an error? Okay, now try this out - comment out the "foo" definition in namespace B.

namespace A {
  class S { };
  void foo(S& parm) { }
}

namespace B {
  // void foo(A::S& parm) { }
  void bar(A::S& parm) {
    foo(parm); // Which "foo" does this call?
  }
}

This compiles without errors! Surprised again? When you run the program, it'd have called A::foo. What happened here? Does this not appear to be a namespace violation. Apparently not! This strange behavior is explained by "Koenig lookup" also known as "Argument dependent lookup".

If you supply a function argument of class type (here parm, of type A::S), then to find the function name the compiler considers matching names in the namespace (here A) containing the argument's type.

But still, is'nt this a namespace violation?? NO. Just consider this piece of code...

std::string str("Hello");
std::cout << str;

This works fine right? How? This works because of Koenig lookup. The function in question here is "operator <<" which is found in "namespace std". But we have'nt used any "using" declaration to bring that function into the current scope. Since std::string is going as an argument into that function, the compiler will automatically look into the std namespace and will find that function. Without this kind of lookup, the same function call would have to be written as...

std::operator <<( std::cout, str) ; // ugly! ain't it??

This is the great value that Koenig lookup gets us and we tend to use it every now and then without realising it. Interesting - is'nt it??

Monday, January 09, 2006

cURL -- A tool limited by imagination

A stock ticker is a piece of code which keeps you updated with the latest(as and when) stock quotations of various companies at the stock exchange. Technically to build this one needs the following tools --

  1. XML
  2. RSS
  3. cURL
  4. sed,awk,grep and simple shell scripting knowledge.

1. XML
Need to know just this concepts elements.

2. RSS (Really Simple Syndicate) -- it is really simple.
As I understand this, there is a link like "http://www.xanadb.com/ticker/YHOO" which provides RSS feed for stocks(YHOO- for yahoo, MSFT-microsoft).
Now observe the xml format carefully. All rss feeds essentially have these 3 elements
title,descrption,link.
An RSS reader is one which makes sense of this rss feed.

So to build the stock ticker here is the design
a) Get the contents of "http://www.xanadb.com/ticker/YHOO" in a variable.
b) Just find the “title” tag. Trash the rest.
c) Find the number and thats it.

3. cURL

Now to achieve step (a) mentioned above we have to use a tool which gets us the contents of the page in a variable or a file.

cURL is shipped freely with most of the Linux installations.

Catch hold of one and execute this

curl "http://www.xanadb.com/ticker/YHOO"

Now go through the manual in case you are using proxies, SSL protected site, etc etc etc.

Now the power is all yours. Just think all the stuff possible with this tool and have i told you this. Its a MIT product.

4. sed,grep and other scripting techniques

Here after its child's play and the scripting skills.

curl "http://www.xanadb.com/ticker/YHOO" | grep title | sed s/title//g

I havent tried the above on a machine , but hope you got an idea as to how the attempt is being made to get the stock quote.

Conclusion

No matter how hard web designers may try to force you to surf their sites interactively, there will always be free tools to help automate the process, thus enriching our overall Net experience.
A deliberate attempt has been made only to answer the "How" part of the tools and not the "What" part of them, as they are available in the links suggested.

Next in Line

PODCAST -- what the hell is this

cant use html tags

After writing the article , I just realized that some html tags (like title, link etc in the exact syntax) cant be used in the article. So I could not use the tags correctly.