Mandalika's scratchpad [ Work blog @Oracle | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17  12.17  01.18  02.18  03.18  04.18  05.18  06.18  07.18  08.18  09.18  11.18  12.18  01.19  02.19  05.19  06.19  08.19  10.19  11.19  05.20  10.20  11.20  12.20 

Wednesday, October 27, 2004
C/C++ & Object Oriented Jargon - 1

Template Class Vs Class Template

Template class: A generic definition or a parameterized class not instantiated until the client provides the needed information; jargon for plain templates

Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed; jargon for plain classes

Dangling pointer

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed

Message Vs Method


- Objects communicate by sending messages to each other
- A message is sent to invoke a method

- Provides response to a message
- It is an implementation of an operation

Adaptor class Or Wrapper class

A class that has no functionality of its own. Its member functions hide the use of a third party software component or an object with the non-compatible interface or a non-object-oriented implementation

Class Invariant

A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are
both preconditions and post-conditions for all operations or member functions of the class

Stack Unwinding

It is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught

Pre-condition/Post-condition to a member function

Precondition: A precondition is a condition that must be true on entry to a member function. A class is used correctly if preconditions are never false. An operation is not responsible for doing anything sensible if its precondition fails to hold. For example, the interface invariants of stack class say nothing about pushing yet another element on a stack that is already full. We say that isful() is a precondition of the push operation

Post-condition: A post-condition is a condition that must be true on exit from a member function if the precondition was valid on entry to that function. A class is implemented correctly if post-conditions are never false. For example, after pushing an element on the stack, we know that isempty() must necessarily hold. This is a post-condition of the push operation

Container Class

A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.


Polymorphism refers to the ability to have more than one method with the same signature in an inheritance hierarchy. The correct method is invoked at run-time based on the context (object) on which the method is invoked. Polymorphism allows for a generic use of method names while providing specialized implementations for them

Function Overloading Vs Function Overriding

Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ

Friday, October 22, 2004
Solaris: Resetting Forgotten Root Password

Need physical or remote console access to the system to use these steps:
  1. Press Stop-A on the console or Ctrl-] and send brk from a remote console connection to access the Open Boot Prompt (OBP)
  2. Insert a bootable Solaris CD and boot into single-user mode with boot cdrom -s
  3. Make a mount point within the /tmp file system by typing mkdir /tmp/mnt
  4. Mount the root partition of the boot disk in /tmp/mnt.
    ex. mount /dev/dsk/c0t0d0s0 /tmp/mnt
  5. Edit /etc/shadow with vi /tmp/mnt/etc/shadow
  6. Remove the encrypted part of the root password (the second field; fields are separated by colons), save, and exit
  7. Unmount the file system with umount /tmp/mnt
  8. Reboot the system and assign a new password at a shell prompt with the passwd command
If you are unable to run vi above, you can edit /etc/shadow using the ed editor

# ed /tmp/mnt/etc/shadow
s/:.............:/::/ (Note: there are 13 dots in the second field)

Tuesday, October 19, 2004
C++: Virtual Function

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

The derived class can either fully replace (override) the base class member function, or the derived class can partially replace (augment) the base class member function. The latter is accomplished by having the derived class member function call the base class member function, if desired

Monday, October 18, 2004
UNIX/Linux: File Permissions (chmod)

A file's permissions are also known as its 'mode'; so to change them we need to use the 'chmod' command (change mode). There are two ways of specifying the new permissions using chmod: symbolic and absolute.

Absolute Mode
Absolute mode is the one with the numbers. You can use simple arithmetic to arrive at the permission you are looking for. Consider:


| owner | group | everyone |
| read | write | execute | read | write | execute | read | write | execute |
| 400 | 200 | 100 | 40 | 20 | 10 | 4 | 2 | 1 |
So just add the appropriate mode numbers to arrive at the desired permission. It may be easier to consider each entity as a single digit, in the usual order (owner group other). As always, this theory is best understand with some examples. Let's imagine a hypothetical file named 'myscript'. 'myscript' is a shell script that we are writing that performs a useful function. When we first create it we don't want others to mess around with it, so we set some restrictive permissions while writing it:

[techno@Govinda]$ chmod 600 myscript
[techno@Govinda]$ ls -l myscript
-rw------- 1 techno users 192 Feb 6 14:55 myscript

Now let us imagine that we need some help with our script, so we make it available to our programmer friend, who just happens to belong to a group called 'web'. We need to change the group, and change the group permissions:

[techno@Govinda]$ chgrp web myscript
[techno@Govinda]$ chmod 660 myscript
[techno@Govinda]$ ls -l myscript
-rw-rw---- 1 techno web 192 Feb 6 14:55 myscript

Our script is now almost done, and we want to test it. We need it to be executable:

[techno@Govinda]$ chmod 770 myscript
[techno@Govinda]$ ls -l myscript
-rwxrwx--- 1 techno web 192 Feb 6 14:55 myscript

Our script is now perfect. We are going to make the script available for all users to run, and we want them to be able to see our handywork so we'll let everybody read and execute it. We don't want users changing it however, so they don't get write permission:

[techno@Govinda]$ chmod 775 myscript
[techno@Govinda]$ ls -l myscript
-rwxrwxr-x 1 techno web 192 Feb 6 14:55 myscript


Normally, when a program is run it inherits all the rights/restrictions of the user that executed it. if a user can't read /var/log/messages, then neither can any program/script executed by that user. There is a way around this, we again use the chmod command but add a '4' at the beginning of the permission string, example:

chmod 4755 myscript

this would execute 'myscript' with the permissions of the files owner(such as root, if the file is own by root),and not the normal user executing 'myscript'. As you can imagine, this should be used sparingly if at all, as it defeats the normal permission structure,and can lead to security issues.


The setgid bit works the same way, except instead of applying to the files owner, it is applied to the files group setting. the chmod command is used again prefixing a '2' as the first digit.

chmod 2755 myscript

Relative Mode
As the name implies, relative mode only changes permissions relative to the current permissions. That is, you can add or remove permissions from the existing ones. The format is pretty much the same as absolute mode: 'chmod [new_mode] file'. It is only the mode that is different.

We have three parts, which for lack of better terms, are '[entity][operator][permissions]'. The entities describe who gets the permissions. They are:

* 'u': user, the file's owner
* 'g': group, the file's group
* 'o': other, everybody else
* 'a': all, all three together

The operators decide whether we add, remove, or emulate absolute mode (ie: describe permissions from scratch). They are:

* '+' : add permissions
* '-': remove permissions
* '=': emulate absolute mode

The permissions we have seen already, they are nothing new:

* 'r' : read permission
* 'w': write permission
* 'x': execute permission

There are actually quite a few more options available, but they should not be necessary for casual use. Perhaps some more examples are in order.

chmod a+x filename # adds execute permissions to all
chmod u+x filename # adds execute permissions to the file's owner
chmod ug+w filename # adds write permissions to the file's owner and group
chmod o-rwx filename # removes read, write, and execute permissions from other
chmod a=rx filename # creates a 555 permission from scratch

As you can see pretty much any combination is valid as long as you follow the '[entity][operator][permissions]' formula.


UNIX/Linux directory access permissions say that if a user has write permissions on a directory, they can rename or remove files there,even if the files don't belong to them. When the owner of the directory sets the sticky bit, renames/removals are only allowed by the files owner, the directories owner and the root user.

chmod +t /tmp # to set the sticky bit
chmod -t /tmp # to remove the sticky bit
chmod 1755 /tmp # prefix a '1' to set the sticky bit

Setting the sticky bit on files was once used to force a copy of the file to stay in swap space, in an attempt to speed execution the next time the file was used. This hasn't been used in quite some time, due to advances in memory management. You can still set the sticky bit on a file, but the kernel will just ignore it.


Saturday, October 16, 2004
Achievement Award

Got an Achievement Award/Certificate from Sun Microsystems, in recognition for my effort with Siebel Benchmark!! =:)

Related post:

Friday, October 15, 2004
Solaris/C/C++: Benefit(s) of Linker (symbol) Scoping


By default, the static linker (ld) makes all ELF symbols global in scope. This means it puts the symbols into the dynamic symbol table of the resulting binary such that other binary modules can access those symbols. The dynamic relocations that the dynamic linker performs during run-time are only necessary for the global (also known as external or exported) symbols. The static linker resolves references to local symbols (for example, names of static functions) statically when it links the binary.

Application performs better if the run-time linker ( has less number of relocations (relocations are expensive). It can be achieved by reducing the scope of some of the symbols i.e., by not exporting all the symbols or simply by not making all symbols global in scope. Export only those symbols that are need by external modules with SS9's (Sun Studio 9) compiler flag -xldscope=hidden & __declspec(dllexport | dllimport) specifiers


1) -Kpic Vs -KPIC

We can take advantage of -Kpic (PIC = Position Independent Code) which is the fastest compared to -KPIC. -Kpis can handle only 2048 global symbols, but fast. Since we were reducing the global symbol count, most of the libraries can be compiled with -Kpic

The PIC-compiled code allows the linker to keep a read-only version of the text (code) segment for a given shared library. The dynamic linker can share this text segment among all running processes, referencing it at a given time

2) Less chance for name collisions with 3rd party libraries

Name collisions are hard to detect/bug. 3rd party libraries can create havoc when some of their symbol names coincide with those in the application. For example, if a third-party shared library uses a global symbol with the same name as a global symbol in one of the application's shared libraries, the symbol from the third-party library may interpose on yours and unintentionally change the functionality of your application without any warning

3) Improved performance

It lets the optimiser produce better code. PLT indirections (when a function call or variable access must be looked up via the Global Offset Table) can be completely avoided, thus substantially avoiding pipeline stalls on modern processors and thus much faster code. Furthermore when most of the symbols are bound locally, they can be safely elided (removed) completely through the entire shared object. This gives greater latitude especially to the inliner which no longer needs to keep an entry point around "just in case"

In summary: Application performs better due to the decreased size of the link maps and reduced number of page faults resulting from symbol scope reduction

4) Improved load times of shared libraries during run-time

5) Improved security

strip utility is not enough to hide the names of the application's routines and data items; stripping eliminates the local symbols but not the global symbols

Dynamically linked binaries (both executables and shared libraries) use two symbol tables: the static symbol table and the dynamic symbol table. The dynamic symbol table is used by the runtime linker. It has to be present even in stripped executables, or else the dynamic linker is not able to find the symbols it needs. The strip utility can only remove the static symbol table

By making most of the symbols of the application local in scope, the symbol information for such local symbols in a stripped binary is really gone and are not available at runtime; so no one can extract it

6) Reduced application binary sizes

More detailed explanation & examples are available at:

1) SS9 C++ user's guide
2) Article: "Enhancing Applications by Directing Linker Symbol Processing" by "Greg Nakhimovsky", Sun Microsystems, Inc.
3) Niall Douglas' "GCC Symbol Visibility Patch" release notes:

Wednesday, October 13, 2004
Sun C/C++: #pragma pack

Let's have a look at the following program and the output

bpte4500s001:/sunbuild1/giri/testcases/%cat aligndata.c
#include <stdio.h>

typedef struct user {
char name[10];
int age;
float salary;
} usr;

int main()
printf("\n ** sizeof(char) = %d, sizeof(int) = %d, sizeof(float) = %d\n ** sizeof(struct user) = %d\n", sizeof(char), sizeof(int), sizeof(float), sizeof(struct user));

return (0);

** sizeof(char) = 1, sizeof(int) = 4, sizeof(float) = 4
** sizeof(struct user) = 20
sizeof(struct user) is supposed to return 18, but returned 20. What's wrong with the compiler - is it a bug?

-NO- it is not a bug. Sun Studio compiler(s) generate 32-bit code, and in that mode there is a significant run-time performance penalty for unaligned accesses, like accessing a 32-bit int which isn't aligned on a 32-bit dword boundary. To produce faster code, Sun Studio compiler(s) pads struct members so that each one can be accessed without delays; this sometimes produces struct size which is larger than the sum of the sizes of its members

If this is not the desired behavior and if we don't want to get the structure padded, we can let compiler know the same by using a compiler directive (aka pragma) called "pack". Syntax of pragma pack:
#pragma pack(n)
n must be 0 or a power of 2. A value of other than 0 instructs the compiler to use the smaller of n-byte alignment and the platform's natural alignment for the data type. A value of 1 instructs the compiler not to perform any alignment. When n is 0 or omitted, the member alignment reverts to the natural alignment values.

If the value of n is the same as or greater than the strictest alignment on the platform, the directive has the effect of natural alignment

A pack directive applies to all structure definitions which follow it, until the next pack directive.

Lets include the pragma 'pack' in our source and run the program

bpte4500s001:/sunbuild1/giri/testcases/%cat aligndata.c
#include <stdio.h>

#pragma pack(1)

typedef struct user {
char name[10];
int age;
float salary;
} usr;

int main()
printf("\n ** sizeof(char) = %d, sizeof(int) = %d, sizeof(float) = %d\n ** sizeof(struct user) = %d\n", sizeof(char), sizeof(int), sizeof(float), sizeof(struct user));

return (0);
** sizeof(char) = 1, sizeof(int) = 4, sizeof(float) = 4
** sizeof(struct user) = 18
When using #pragma pack on a SPARC platform to pack denser than the type's default alignment, the -misalign option must be specified for both the compilation and the linking of the application

Sun Studio 9 C++ user's guide

Dheeraj (aka tw|lit), Naresh Shroff

Monday, October 11, 2004
C/C++: Structure Vs Union

A structure is a collection of items of different types; and each data item will have its own memory location. Where as only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location i.e., only one memory location will be shared by the data items of union.

Size of union will be the size of the biggest variable.

Why do we need Union in the first place?

Sometimes we may not need the data of all the (related) data items of a complex data structure and be storing/accessing only one data item at a time. Union helps in such scenarios.

typedef union
int Wind_Chill;
char Heat_Index;
} Condition;

typedef struct
float temp;
Condition feels_like;
} Temperature;

Wind Chill is only calculated when it is cold and heat index is used only when it is hot. There is no need for both of them at the same time. So when we specify the temp, feels_like will have only one value - either wind chill or heat index, but not both.

The following simple program illustrate the above explanation:
% cat structunion.c
#include <stdio.h>
#include <stdlib.h>

typedef union
int Wind_Chill;
char Heat_Index;
} Condition;

typedef struct
float temp;
Condition feels_like;
} Temperature;

void main()
Temperature *tmp;

tmp = (Temperature *)malloc(sizeof(Temperature));

printf("\nAddress of Temperature = %u", tmp);
printf("\nAddress of temp = %u, feels_like = %u", &(*tmp).temp, &(*tmp).feels_like);
printf("\nWind_Chill = %u, Heat_Index= %u\n", &((*tmp).feels_like).Wind_Chill, &((*tmp).feels_like).Heat_Index);

% cc -o structunion structunion.c

% ./structunion
Address of Temperature = 165496
Address of temp = 165496, feels_like = 165500
Wind_Chill = 165500, Heat_Index= 165500

C/C++/Java: ++ unary operator

#include <stdio.h>

int main()
int i = 5, j = 5;
int total = 0;

total = ++i + j++;
printf("\ntotal of i & j = %d\n", total);
What will be the total after the execution of total = ++i + j++ statement? 12?
Let's check the output of this program:
total of i & j = 11
The ++ (increment) operator adds 1 to the value of a scalar operand. ++ can either be placed before or after the operand. If it appears before the operand, the operand is incremented, and then the incremented value is used in the expression. If it appears after the operand, the value of the operand is used in the expression before the operand is incremented

so in our example, total = ++i + j++ is equivalent to the following three expressions:
i = i + 1;
total = i + j;
j = j + 1;
Then simple substitution & math show the final value of total as "11"

If the operand is a pointer, ++ increments the operand by the size of the object to which it points

Thursday, October 07, 2004
Sun achieves winning Siebel benchmark

I'm very glad to be one of the major contributors of this benchmarking effort. As Sun hasn't published any competitive benchmarking numbers running Siebel on Sun platform for long, we started off with a single goal "must win" and put Sun on the top of UNIX vendors list with very well balanced price/performance ratio. P/P is what really the customer cares about i.e., customer needs superior performance with low cost.

We spent nearly two and half months (project duration: 3 months) on fixing various issues ranging from simple configuration issues to memory leaks in Siebel components & core dumps to scalability issues. Finally on 6th of september (labor day) the big day has arrived; able to simulate 10000 concurrent virtual users hitting the same oracle database (running on a "dual core" UltraSPARC IV machine, E2900) at the same time with an average think time of 30 sec and with an average response time of about 0.5 sec. UltraSPARC IV rocked by handling 662 concurrent users at app tier & 4902 users at database tier. Hewlett-Packard's (holding the top spot by then) numbers are 385 & 1786 at app & db tiers respectively. HPs numbers are not even comparable to Sun's numbers and lagging by 72% & 174% at both app & db tiers. Needless to say Sun Microsystems moved comfortably to the top spot by winning this benchmark. 'Way To Go' Sun!!

Obviously my manager 'George Drapeau' was very excited with the success of the project and discussed the effort in his blog at: &

Sun Microsystems released a press note with the result of the benchmark on 10th October:

Siebel systems published the certified results in their web site:

Topology with all tiers:

Screenshots of load generator & the resource monitoring:

Wednesday, October 06, 2004
Database: Oracle Server Architecture (overview)

Oracle server consists of the following core components:

1) database(s) &
2) instance(s)

1) database consists of:
1) datafiles (.dbf files) <- information (data) will be stored in flat files on the disk
2) redo logs (.rdo & .arc files)
Redo Logs hold recordings of every change made to datafiles; useful for fine-grained
recovery of specific changes. Oracle uses several of these files so that when it gets done
writing to the last file in the series, it begins overwriting to the first online redo log file. A
set of redo log files is called the redo log
3) control files (.ctl files)
contain parameters (to configure how the database starts), and password files (to
manage authentication)

Instances provide the capability of accessing stored data. Each instance consists of:

Program Global Area (PGA)
When single-threaded servers are used, the Program Global Area (PGA) contains data
(such as user’s session variables and arrays) for use only by a single process. Its
contents is not visible to other processes
eg., parse information from queries, database cursors

System Global Area (SGA)
The Shared System Global Area (SGA) contains user data and control information for a
single oracle instance. Its contents are visible/accessible to several user applications
and system processes

3) system processes and user applications
4) db buffer cache, shared pool, redo log buffer etc.,

user data, system data, index, configuration & logs

Database structure
logical & physical structures

tables, tablespaces, segments, extents & oracle blocks

raw files on disk

shared pool:
contains data dictionary and SQL statements submitted for processing by users. None of the user data will be stored here

Tuesday, October 05, 2004
Linux: Frozen Xwindows

If Xwindows seem frozen, the following simple key strokes may bring back the Xserver without the need for a reboot

Two ways to kill the Xwindows session (Xserver)

(1) press "ctrl + alt + backspace"

If xdm been running, a new X server will be spawned immediately and prompts with the login screen

(2) press "ctrl + alt + F2"

It brings up a a virtual console; login and run:

# ps -ax | grep startx
It will give you the PID of the Xserver. Just kill it with:

# kill -9 <PID>
To go back to the first console, just press "alt+F1"


This page is powered by Blogger. Isn't yours?