Sunday Dec 20, 2009

What happens under the hood when you execute your C Programs?

I was a little curious to find out how my program gets executed. In order to find that out, I decided to write a simple c program which is here:



 #include <stdio.h>
int main()
{
    sleep(30);
    printf("Just testing an application!");
}



I have kept the sleep(30) in the beginning of the program so as to give me time to execute the DTrace script, about which I will be talking a little later.


Now i complied it with Sun Compiler and GCC



kumar@myosbox:~/Desktop/Demos$ suncc -o sunCCOutput simplec.c


kumar@myosbox:~/Desktop/Demos$ gcc -o gccOutput simplec.c



Now I wrote a simple DTrace script using the pid provider in order to figure out how printf is implemented (execution detail - functions which get invoked when printf function is called) in these compilers. The dtrace script is below:



pid$1::$2:entry
{
        self->trace = 1;
}

pid$1::$2:return
/self->trace/
{
        self->trace = 0;
}

pid$1:::entry,
pid$1:::return
/self->trace/
{
}



Now it was the time to execute this script and compare the outputs. I am going to redirect the output of this script into two text files (sunCCOutput.txt and gccOutput.txt).



kumar@myosbox:~/Desktop/Demos$ ./sunCCOutput 
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep sunCCOutput` printf > SunCCOutput.txt
dtrace: script 'pid.d' matched 6712 probes
kumar@myosbox:~/Desktop/Demos$ ./gccOutput
kumar@myosbox:~/Desktop/Demos$ pfexec dtrace -F -s pid.d `pgrep gccOutput` printf > gccOutput.txt
dtrace: script 'pid.d' matched 6714 probes



Now, the content of the file SunCCOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror


Contents of the file gccOutput.txt is as follows:



CPU FUNCTION                                 
1 -> printf
1 -> _setorientation
1 <- _setorientation
1 -> _ndoprnt
1 -> _findbuf
1 -> isatty
1 -> ___errno
1 <- ___errno
1 -> ioctl
1 <- ioctl
1 <- isatty
1 -> _setbufend
1 -> getxfdat
1 <- getxfdat
1 -> isseekable
1 -> ___errno
1 <- ___errno
1 -> fstat64
1 <- fstat64
1 -> ___errno
1 <- ___errno
1 <- isseekable
1 <- _setbufend
1 <- _findbuf
1 -> _realbufend
1 -> getxfdat
1 <- getxfdat
1 <- _realbufend
1 -> memcpy
1 <- memmove
1 <- memcpy
1 -> memchr
1 <- memchr
1 -> ferror
1 <- ferror
1 <- _ndoprnt
1 -> ferror
1 <- ferror

So you can see that there is no difference between gcc and suncc when if comes to implement the printf function. But its is so interesting to see that there are 19 function calls made everytime I use a printf in my c program! WOW! ;) I wonder if I could know it this easily if there was no Dtrace!

Sunday Dec 06, 2009

FOSS.IN 09 - Abhishek was here!

FOSS.IN 2009 was pinnacle of geekfests and it was after a long time that I saw so many open source fanatics under one roof!! Thanks to Bangalore OpenSolaris User Group (specially Sriram Narayanan and Anil Gulecha) OpenSolaris platform was successful beyond expectations in making hackers heads turn!



Sriram explaining stuff about OpenSolaris to interested participants of FOSS.IN


Everyone was so impressed with our demos based on ZFS, DTrace and Crossbow that I literally lost the count of number of times I heard phrases like "That is so cooool!" Even though we managed to keep just a table and two laptops (one was Sriram's personal laptop and the other one was mine :P ) people kept flocking to us while we demonstrated our simple hacks on OpenSolaris 2009.06.


On the last day, thanks to Joe George from Sun IEC, Bangalore, we managed to get some OpenSolaris T-Shirts and it was a riot! We had around 50 T-shirts and we had kept a condition that anyone who wants a T-Shirt would have to compete an assignment based on either ZFS, DTrace, Crossbow or Zones and we gave different sets of assignments to every participants. People were allowed to use man pages and google and it became such an interesting game that we had a long queue of people waiting for their turn to try their hands out at it.Within 5 hours all our T-Shirts were gone!


Me giving our first winner the opensolaris t-shirt


Me giving a OpenSolaris T-Shirt to our first winner


The most interesting bit happened when one of the participants (who said that he is feeling really out of place because he was very new to the world of computers) walked into our booth and asked if OpenSolaris has got something to do with solar energy. After I had a 15 mins chat, he got so interested and excited that he said even he would like to try his hand at OpenSolaris and by the end of the day, he won a T-Shirt by completing his assignment of creating a ZFS snapshot, deleting the Download folder and rolling back!


One guy trying out his assignment to win Opensolaris t-shirt while others listen to Sriram


One of the participants using my laptop to complete his assignments while others listen are taking their assignments from Sriram


Foss.in


Keynote from Philip Tellis of Yahoo! in FOSS.IN 


A few GNU skeptics who walked to us commenting on the CDDL were sent back satisfied by examples like Belenix and Nexenta. Over all, it was a great event and we learned many new lessons too! I hope hackers who drool on cool technologies will continue to fall in love with OpenSolaris!



About

I am Abhishek and I work for Sun/Oracle! In this blog, I share my interest in systems, solaris, linux and other technologies :-)

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today