<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Silent rants of my life.........</title>
	<atom:link href="http://vkundeti.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vkundeti.wordpress.com</link>
	<description>Algorithms, Life, Technology, UNIX, Love, Workout, VLSI Design Automation .......trying to sort these deterministically in constant time $O(1)$.</description>
	<lastBuildDate>Sun, 20 Apr 2008 04:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vkundeti.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Silent rants of my life.........</title>
		<link>http://vkundeti.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vkundeti.wordpress.com/osd.xml" title="Silent rants of my life........." />
	<atom:link rel='hub' href='http://vkundeti.wordpress.com/?pushpress=hub'/>
		<item>
		<title>[TECH] What does computing the edit distance between a string and its reverse tell us?</title>
		<link>http://vkundeti.wordpress.com/2008/04/19/tech-what-does-computing-the-edit-distance-between-a-string-and-its-reverse-tell-us/</link>
		<comments>http://vkundeti.wordpress.com/2008/04/19/tech-what-does-computing-the-edit-distance-between-a-string-and-its-reverse-tell-us/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 22:12:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Algorithms]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/04/19/tech-what-does-computing-the-edit-distance-between-a-string-and-its-reverse-tell-us/</guid>
		<description><![CDATA[I came across a problem recently which asks for the following &#8220;Given a string find out the minimum number of characters to be inserted to make it a palindrome, and also give the string&#8221;. There might be several ways to solve this problem I have a simple algorithm to solve this problem in O(n2). The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=128&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I came across a problem recently which asks for the following &#8220;Given a string find out the minimum number of characters to be inserted to make it a palindrome, and also give the string&#8221;. There might be several ways to solve this problem I have a simple algorithm to solve this problem in <strong>O(n<sup>2</sup>)</strong>. The observation is the following reccurence.</p>
<p>Let LCS<sup>r</sup>[x,y] = longest common sub sequence between <strong>S<sub>1&#8230;x</sub></strong> and <strong>S<sup>r</sup><sub>1&#8230;y</sub></strong> , where <strong>S<sup>r</sup></strong> is the reverse of<br />
the string <strong>S</strong>.</p>
<p>Let <strong>X=x<sub>1</sub>x<sub>2</sub>&#8230;.x<sub>k</sub></strong> be the be the palindrome with minimum # of inserted characters to make it a palindrome, then <strong>X</strong> has to be formed from <strong>S</strong> as follows.</p>
<p>1. Find a index <strong>k</strong> in <strong>S</strong> such that the cost of transforming <strong>S[1..k]</strong> to <strong>S[k+1...n]</strong> is minimum and the palindrome would be<br />
<strong> X = S&#8217;[1...k]S&#8217;<sup>r</sup>[k+1...n] </strong> or <strong> X = S&#8217;[1..k-1]S[k]S&#8217;<sup>r</sup>[k+1..n]</strong></p>
<p>2. The index <strong>k</strong> such that <strong>LCS<sup>r</sup>[k][len-k]<strong> is maximum.</strong></strong></p>
<pre>
<font size="3">
<code>
/*Builds the LCS between the forward and backwards and finds
a index 'p' which will give minimum # of operations to transform
forward string to backward string.*/
void FindLCSReverse(char *str,unsigned int len){
	int i,j;
	int current_min,max_lcs;
	int imax,jmax,palindex,palindex_r;
	unsigned int operations;
	char path=0;
	for(i=0;iLCS[i][j-1])?LCS[i-1][j]:LCS[i][j-1];
			}
		}
	}
	/*Compute the p which gives minimum inserts to transform
	 *forward string to backward
	 */
	max_lcs=0;
	imax=len-1;jmax=0;
	for(i=len-1;i&gt;=1;i--){
		if(LCS[i][len-i-1] &gt; max_lcs){
			max_lcs = LCS[i][len-i-1];
			imax = i;
			jmax = len-i-1;
		}
		if(i&gt;0 &amp;&amp; LCS[i-1][len-i-1] &gt;= max_lcs){
			max_lcs = LCS[i-1][len-i-1];
			imax = i-1;
			jmax = len-i-1;
		}
	}
	/*Now find the actual string*/
	i=imax;
	j=jmax; 

	palindex_r=0;
	while(i!=0 || j!=0){
		if(i&gt;0 &amp;&amp; j&gt;0){
			if(buf[i] == buf[len-j]){
				palindrome_rev[palindex_r++] = buf[i];
				i--; j--;
				continue;
			}
			current_min = (LCS[i-1][j] &gt; LCS[i][j-1])?LCS[i-1][j]:LCS[i][j-1];
			if(current_min == LCS[i-1][j]){
				if(current_min == LCS[i][j-1] &amp;&amp; buf[i] &lt; buf[len-j]){
					palindrome_rev[palindex_r++] = buf[len-j];
					j--;
				}else{
					palindrome_rev[palindex_r++] = buf[i];
					i--;
				}
			}else if(current_min == LCS[i][j-1]){
				if(current_min == LCS[i-1][j] &amp;&amp; buf[len-j] &lt; buf[i]){
					palindrome_rev[palindex_r++] = buf[i];
					i--;
				}else{
					palindrome_rev[palindex_r++] = buf[len-j];
					j--;
				}
			}
		}else if(j==0){
			palindrome_rev[palindex_r++] = buf[i];
			i--;
		}else if(i==0){
			palindrome_rev[palindex_r++] = buf[len-j];
			j--;
		}
		/*path=1 (left) path=2 (down) path=3 (diag)*/
	}

	for(i=0;i&lt;palindex_r;i++){
		palindrome[palindex_r-1-i] = palindrome_rev[i];
	}
	palindrome_rev[palindex_r] = '';
	palindrome[palindex_r] = '';

	/*printf("imax = %u jmax = %u len=%u \n",imax,jmax,len);*/
	if(imax+jmax==len-1){
		printf("%s%s\n",palindrome,palindrome_rev);
	}else{
		printf("%s%c%s\n",palindrome,buf[imax+1],palindrome_rev);
	}
}
</code></font></pre>
<p> Download this <a href="http://trinity.engr.uconn.edu/~vamsik/ShortestPalindrome.c"> here </a> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/128/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/128/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=128&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/04/19/tech-what-does-computing-the-edit-distance-between-a-string-and-its-reverse-tell-us/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Reverse Engineering and Creating Crawler BOTS.</title>
		<link>http://vkundeti.wordpress.com/2008/04/16/tech-reverse-engineering-and-creating-crawler-bots/</link>
		<comments>http://vkundeti.wordpress.com/2008/04/16/tech-reverse-engineering-and-creating-crawler-bots/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 06:02:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/04/16/tech-reverse-engineering-and-creating-crawler-bots/</guid>
		<description><![CDATA[I have my share of pleasure reverse engineering the underlying details of SCOPUS. SCOPUS as you might know is the most popular scholarly database used for citation searching. I was trying to solve this problem &#8220;Given a research paper X produce a set of research papers in the same connected component of the CITATION GRAPH&#8221;, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=127&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
I have my share of pleasure reverse engineering the underlying details of SCOPUS. SCOPUS as you might know is the most popular scholarly database used for citation searching. I was trying to solve this problem &#8220;Given a research paper X produce a set of research papers in the same connected component of the CITATION GRAPH&#8221;, Let me define a CITATION GRAPH (CITE(V,E)) , V = {set of all research papers} and E={(i,j)} set of all directed edges from research paper &#8216;i&#8217; to &#8216;j&#8217; such that paper &#8216;j&#8217; refers paper &#8216;i&#8217; in its references. This edge information comes from SCOPUS however SCOPUS gives only one level (depth 1) in the connected component of all related papers, my goal is to get all the related papers (related in the sense fall in the same connected component of the CITE graph).
</p>
<p>
The reverse engineering the underlying comes handy when we want to automate the process of searching all these from the browser ourself.
</p>
<p> I&#8217;m too tired to explain the details of the program which I had written using perl+LWP to create a CRAWLER BOT which gets all the related papers but if you need similar stuff sure the code can help <a href="http://dna.engr.uconn.edu/~vamsik/forward_scopus.pl"> click here </a>
</p>
<p> Unfortunately I don&#8217;t get enough time to write blogs but in past few weeks I had some very interesting technical stuff I want to write.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/127/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/127/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=127&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/04/16/tech-reverse-engineering-and-creating-crawler-bots/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] A simple algorithm to find the coefficient&#8217;s of characteristic polynomial.</title>
		<link>http://vkundeti.wordpress.com/2008/03/13/tech-a-simple-algorithm-to-find-the-coefficients-of-characteristic-polynomial/</link>
		<comments>http://vkundeti.wordpress.com/2008/03/13/tech-a-simple-algorithm-to-find-the-coefficients-of-characteristic-polynomial/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 05:31:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/03/13/tech-a-simple-algorithm-to-find-the-coefficients-of-characteristic-polynomial/</guid>
		<description><![CDATA[Its often the case that the we need coefficients of the characteristic polynomial (xA = &#955;x) rather than just the Eigen values and Eigen vectors of the matrix A, The following simple algorithm which can determine the coefficients of the polynomial from its roots (Eigen values) would be extremely handy, its a simple dynamic programming [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=126&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
Its often the case that the we need coefficients of the characteristic polynomial (<b>xA = &lambda;x</b>) rather than just the Eigen values and Eigen vectors of the matrix <b>A</b>, The following simple algorithm which can determine the coefficients of the polynomial from its roots (Eigen values) would be extremely handy, its a simple dynamic programming algorithm and also illustrates how quickly we can code<br />
dynamic programming algorithms in Matlab because the matrices resize automatically.
</p>
<pre>
<font size="3">

%
% eigen_poly_coeff: input 'A' should be a square matrix
% The program finds the eigen values of A, and constructs
% the caracteristic polynomial
%
% output is the order of coefficients in the increasing
% degree order.
%
function retval = eigen_poly_coeff (A)
v = eig(A)';
B = size(v);
coeff_matrix = eye(1,(B(1,2)+1));
coeff_matrix(1,1) = v(1,1)*-1;
coeff_matrix(1,2) = 1;

for i=3:1:(B(1,2)+1)
   coeff_matrix(1,i) = 1;
   for j=(i-1):-1:1
    coeff_matrix(1,j) = (coeff_matrix(1,j)*
                v(1,(i-1))*-1);
    if(j-1 &gt;=1)
      coeff_matrix(1,j) =
             coeff_matrix(1,j)+coeff_matrix(1,j-1);
    end
   end
end
 retval = coeff_matrix;
endfunction

</font>
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/126/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/126/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/126/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=126&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/03/13/tech-a-simple-algorithm-to-find-the-coefficients-of-characteristic-polynomial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Algorithmic details of UNIX Sort command.</title>
		<link>http://vkundeti.wordpress.com/2008/03/10/tech-algorithmic-details-of-unix-sort-command/</link>
		<comments>http://vkundeti.wordpress.com/2008/03/10/tech-algorithmic-details-of-unix-sort-command/#comments</comments>
		<pubDate>Mon, 10 Mar 2008 06:58:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/03/10/tech-algorithmic-details-of-unix-sort-command/</guid>
		<description><![CDATA[I happened to look at the algorithmic details of UNIX Sort, a LINUX version of the classic UNIX sort is a part of GNU coreutils-6.9.90. This is classic example of the standard External R-Way merge , to sort a data of size N bytes with a main memory size of M so it creates N/M [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=125&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
I happened to look at the algorithmic details of UNIX Sort, a LINUX version of<br />
the classic UNIX sort is a part of GNU  <a href="http://www.gnu.org/software/coreutils/">coreutils-6.9.90</a>. This is classic<br />
example of the standard External <b>R-Way</b> merge , to sort a data of size <b>N</b> bytes with a main memory size of <b>M</b> so it creates <b>N/M</b> runs and merges <b>R</b> at a time, the number of passes through the data is <b>log(N/M)/log(R)</b><br />
passes.In fact the lower bound(runtime) for external sorting is <b>&Omega;((N/M)log(N/M)/log(R))</b>. All the external memory sorting algorithms provided in the literature are optimal so the fight here is minimizing the constant before the number of passes.
</p>
<p>
UNIX sort treats keys are lines (strings), the algorithm followed by<br />
unix sort is in fact the R-Way merge. Let the input file size be<br />
IN_SIZE. </p>
<p>1. Choosing Run Size:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
The sizes of the initial runs are chosen from the total physical<br />
memory (TOTAL_PHY) and available memory (AVAIL_PHY).<br />
RUN_SIZE = (MAX(TOTAL_PHY/8,AVAIL_PHY))/2<br />
<br />
maximum of 1/8th of TOTAL_PHY and AVAIL_PHY and divided by 2.<br />
See function &#8220;default_sort_size (void)&#8221; in the code.</p>
<p>2. Creating Runs:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Unix sort creates a temporary file for every run. So it creates<br />
IN_SIZE/RUN_SIZE (celing) temporary files. Internally it uses merge<br />
sort to sort internally it uses an optimization mentioned in Knuth<br />
volume 3 (2nd edition), problem 5.2.4-23.</p>
<p>3. Merging:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>The number of runs merged at any time is hard coded in the program<br />
see macro NMERGE , NMERGE is defined to be 16 so it merges<br />
exactly 16 runs at any time.<br />
</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/125/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/125/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/125/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/125/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/125/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=125&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/03/10/tech-algorithmic-details-of-unix-sort-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Sorting partially sorted sequences.</title>
		<link>http://vkundeti.wordpress.com/2008/03/06/tech-sorting-partially-sorted-sequences/</link>
		<comments>http://vkundeti.wordpress.com/2008/03/06/tech-sorting-partially-sorted-sequences/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 00:12:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/03/06/tech-sorting-partially-sorted-sequences/</guid>
		<description><![CDATA[Partially Sorted Sequence: A sequence k1,k2,&#8230;kn of n keys such that any key kj differs from its sorted position by atmost d as an example for d=3 we have 3,5,6,1,2,4 I came across this problem recently, turns out that we can solve this problem in several ways in O(nlog(d)) time. I found an interesting and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=124&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
Partially Sorted Sequence: A sequence <b>k<sub>1</sub>,k<sub>2</sub>,&#8230;k<sub>n</sub></b> of <b>n</b> keys such that any key <b>k<sub>j</sub></b> differs from its sorted position by atmost <b>d</b> as an example for <b>d=3</b> we have <b>3,5,6,1,2,4</b><br />
I came across this problem recently, turns out that we can solve this problem in several ways in <b>O(nlog(d))</b> time. I found an interesting and simple way to solve this problem, a simple observation reveals that the <b>smallest</b> element<br />
in the entire sequence of <b>n</b> keys exists in the first <b>d</b> keys.
</p>
<pre>
<font size="3">
/*Build a heap(min) <b>H</b> with first <b>d</b> elements
 *in <b>O(d)</b> time
 */
 for(i=d;i&lt;n;i++){
    DeleteMin(H); /*output this key*/
    InsertHeap(H,k<sub>i</sub>);
 }
/*Note the Heap will have atmost <b>d</b> keys in it
at any time so the Insert and Delete can be done in
<b>O(log(d))</b> worst case.
*/
</font>
</pre>
<p>
 <b>(n-d)log(d) = O(nlog(d))</b></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/124/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/124/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=124&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/03/06/tech-sorting-partially-sorted-sequences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Converting an Las Vegas algorithm from an Expected Time Bound to High Probability bound.</title>
		<link>http://vkundeti.wordpress.com/2008/03/04/tech-converting-an-las-vegas-algorithm-from-an-expected-time-bound-to-high-probability-bound/</link>
		<comments>http://vkundeti.wordpress.com/2008/03/04/tech-converting-an-las-vegas-algorithm-from-an-expected-time-bound-to-high-probability-bound/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 22:20:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/03/04/tech-converting-an-las-vegas-algorithm-from-an-expected-time-bound-to-high-probability-bound/</guid>
		<description><![CDATA[Some times its not always possible to derive the High Probability bounds (1-n-&#945;) for Las Vegas algorithms, I found an interesting way to convert any Las Vegas algorithm with expected bounds on the resources to the High Probability bounds. Assume that Tn be the expected runtime(or any resource) for a Las Vegas algorithm. The plain [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=123&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
Some times its not always possible to derive the High Probability bounds <b>(1-n<sup>-&alpha;</sup>)</b><br />
for Las Vegas algorithms, I found an interesting way to convert any Las Vegas algorithm<br />
with expected bounds on the resources to the High Probability bounds. Assume that T<sub>n</sub> be the expected runtime(or any resource) for a Las Vegas algorithm. The<br />
plain vanilla Las Vegas algorithm have the following structure.
</p>
<pre>
<font size="3">
while(1){
  /*Select a random sample*/
  if(answer found) quit;
}
</font>
</pre>
<p>
Basically any analysis of the above randomized algorithm should tell how long<br />
should we run the loop. So let <b>T<sub>n</sub></b> gives the expected time on how<br />
long this loop runs. What we can do is as follows</p>
<ul>
<li> Let the algorithm run for exactly <b>2T<sub>n</sub></b> steps, if the algorithm<br />
quits before that we are fine, if the algorithm does not quit after <b>T<sub>n</sub></b><br />
times then stop and restart.
</ul>
</p>
<pre>
<font size="3">
counter = 0;
while(1){
 /*Pick a random sample*/
 if(counter++ == 2T<sub>n</sub>){
    counter=0; /*restarting*/
 }
 if(answer found) quit;
}
</font>
</pre>
<p>
We can now show that the above algorithm will now give high probability bounds. Let<br />
<b>X</b> be the random variable which indicates the runtime of the algorithm. Then using Markov inequality <b>P[X &ge; aT<sub>n</sub>] &le; 1/a</sup></b> </p>
<ul>
<li> <b>P[X &ge; 2T<sub>n</sub>] &le; 1/2 </b>
<li> Lets assume that the algorithm runs for <b>k &ge; 2T<sub>n</sub></b> time steps, then we should have done a reset exactly <b>k/(2T<sub>n</sub>)</b> times.
<li> The probability of doing a reset equals <b>P[X &ge; 2T<sub>n</sub>] = P[reset]</b>.
<li> Lets assume all the events are independent then the probability of resetting <b>k/(2T<sub>n</sub>)</b> <b>P[reset k/(2T<sub>n</sub>) times] = P[algorithm running k time steps]</b>
<li> <b>P[reset k/(2T<sub>n</sub>)] &le; (1/2)*(1/2)*(1/2)&#8230;k/(2T<sub>n</sub>) times</b>
<li> <b>P[reset k/(2T<sub>n</sub>)] &le; (1/2)<sup>k/(2T<sub>n</sub>)</sup></b>
<li> we want the above probability to be very small (<b>n<sup>-&alpha;</sup></b>), to<br />
make that <b>(1/2)<sup>k/(2T<sub>n</sub>)</sup> = n<sup>-&alpha;</sup></b>). So<br />
the value of <b>k</b> for which this happens is <b>2T<sub>n</sub>&alpha;log(n)</b></p>
<li> So with high probability after <b>2T<sub>n</sub>&alpha;log(n)</b> time steps<br />
if we quit the loop then we give the correct answer.</p>
<li> So we just have taken an expected bound <b>T<sub>n</sub></b> and converted into <b>&sim;O(log(n)T<sub>n</sub>)</b> time algorithm with high probability rather than<br />
expected bounds.
</ul></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/123/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/123/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=123&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/03/04/tech-converting-an-las-vegas-algorithm-from-an-expected-time-bound-to-high-probability-bound/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Finding max and min in exactly 3n/2-2 Element Comparisions.</title>
		<link>http://vkundeti.wordpress.com/2008/03/01/tech-finding-max-and-min-in-exactly-3n2-2-element-comparisions/</link>
		<comments>http://vkundeti.wordpress.com/2008/03/01/tech-finding-max-and-min-in-exactly-3n2-2-element-comparisions/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 07:17:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/03/01/tech-finding-max-and-min-in-exactly-3n2-2-element-comparisions/</guid>
		<description><![CDATA[We know that the lower bound on the minimum number of comparisons to find max and min in an array of n. In fact the recurrence T(n) = 2T(n/2)+2 solves exactly to 3n/2-2 the following is an interesting way to find max and min in exactly 3n/2-2 comparisons non-recursively(assume &#8216;n&#8217; is even). Also note by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=121&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
 We know that the lower bound on the minimum number of comparisons to find <b>max</b><br />
and <b>min</b> in an array of <b>n</b>. In fact the recurrence <b>T(n) = 2T(n/2)+2 </b><br />
solves exactly to <b>3n/2-2</b> the following is an interesting way to find <b>max</b><br />
and <b>min</b> in exactly <b>3n/2-2</b> comparisons non-recursively(assume &#8216;n&#8217; is even). Also note by comparison we mean the element comparisons as they are significant.
</p>
<pre>
<font size="3">
current_min = a[0]; current_max=a[1];
/* 1 comparison here */
if(current_min &lt; current_max){
  current_min = a[1]; current_max = a[0];
}
/*(n-1)-2+1 times*/
for(i=2;i&lt;n-1;i+=2){
  /*3 Element Comparisons here */
  if(a[i] &lt; a[i+1]){
      if(a[i] &lt; current_min) current_min = a[i];
      if(a[i+1] &gt; current_max) current_max = a[i+1];
  }else{
      if(a[i+1] &lt; current_min) current_min = a[i+1];
      if(a[i] &gt; current_max) current_max = a[i];
  }
}
/*return current_max current_min*/
</font>
</pre>
<p>
 Analysis: Total Comparisions = 1 + (((n-1)-2+1)/2)*3 = 3*n/2-2. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/121/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/121/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/121/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=121&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/03/01/tech-finding-max-and-min-in-exactly-3n2-2-element-comparisions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] Standard I/O Buffers won&#8217;t get flushed when program crashes&#8230;</title>
		<link>http://vkundeti.wordpress.com/2008/02/12/tech-standard-io-buffers-wont-get-flushed-when-program-crashes/</link>
		<comments>http://vkundeti.wordpress.com/2008/02/12/tech-standard-io-buffers-wont-get-flushed-when-program-crashes/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 23:08:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/02/12/tech-standard-io-buffers-wont-get-flushed-when-program-crashes/</guid>
		<description><![CDATA[Today I had a interesting problem, assume that you have a program printing out some details on to the screen and suddenly the program crashes and you want to capture what ever it has printed what would you do? you would try to redirect the output to a file and look at that file and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=119&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p> Today I had a interesting problem, assume that you have a program printing out some<br />
details on to the screen and suddenly the program crashes and you want to capture what<br />
ever it has printed what would you do? you would try to redirect the output to a file and look at that file and what if the contents of the redirected file is empty? how do you explain this? try the following program and try to redirect what ever it prints using &#8216;&gt;&#8217; or a &#8216;|&#8217; (&#8220;./a.out &gt; out&#8221; or &#8220;./a.out | more&#8221;)
</p>
<pre>
<font size="3">
char *crash_me = "crash this";
int main(){
    int i;  

    for(i=0;i out
#3$./a.out | more
</font>
</pre>
</p>
<p>
We can see that the &#8216;out&#8217; file and &#8216;more&#8217; don&#8217;t show any thing which was in fact printed if we just run the program normally, how can we explain this? what exactly is<br />
happened? actually this is what has happened in the first case the terminal output is<br />
line buffered when ever terminal sees &#8216;\n&#8217; it prints that but in the next two cases<br />
the output is written to a file which is not line buffered (but gets written when<br />
the buffer is full) and since the program is crashed before the buffer gets full nothing is printed in case of #2 and #3. I guess one should definitely have a signal<br />
handler for SIGSEGV and other signals which end the program and do a explicit flush as below.
</p>
<pre>
<font size="3">
/*The buffers should be flushed before
 *program crashes.
 **/
void FlushBuffers(int sig){
    fprintf(stderr,"Segmentation Fault\n");
    fflush(stdout);fflush(stderr);
    exit(1);

}
char *crash_me = "crash this";
int main(){
    int i;
    assert(signal(SIGSEGV,FlushBuffers)!=SIG_ERR);
    for(i=0;i&lt;10;i++){
        printf("Before crash line %d\n",i+1);
        if(i==9){
            crash_me[6] = 'C';
        }
    }
}
</font>
</pre>
<p>
I&#8217;m thinking of making a list of this kind of problems on UNIX</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/119/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/119/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=119&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/02/12/tech-standard-io-buffers-wont-get-flushed-when-program-crashes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] SafeRead and SafeWrite</title>
		<link>http://vkundeti.wordpress.com/2008/02/11/tech-saferead-and-safewrite/</link>
		<comments>http://vkundeti.wordpress.com/2008/02/11/tech-saferead-and-safewrite/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 02:50:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/02/11/tech-saferead-and-safewrite/</guid>
		<description><![CDATA[&#8216;read&#8217; and &#8216;write&#8217; are most frequently used system calls, its really a blunder to have something like the following in the code. /*Read and Write blunders*/ if(read(fd,buffer,len) &#60; 0 ){ perror("Read ERROR:"); } if(write(fd,buffer,len) &#60; 0){ perror("Write ERROR:"); } The above code involving &#8216;read&#8217; and &#8216;write&#8217; syscalls may seem perfectly fine but unfortunately thats not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=118&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
&#8216;read&#8217; and &#8216;write&#8217; are most frequently used system calls, its really a blunder to have something like the following in the code.
</p>
<pre>
<font size="3">
/*Read and Write blunders*/
if(read(fd,buffer,len) &lt; 0 ){
  perror("Read ERROR:");
}

if(write(fd,buffer,len) &lt; 0){
  perror("Write ERROR:");
}
</font>
</pre>
<p>
The above code involving &#8216;read&#8217; and &#8216;write&#8217; syscalls may seem perfectly fine but unfortunately thats not true, NEVER IGNORE THE RETURN VALUE of a syscall, its very<br />
common to functions like &#8216;read&#8217; and &#8216;write&#8217; to return values less than &#8216;len&#8217; in several situations like program interrupted by a signal or timeout or if the &#8216;len&#8217; is<br />
very large say in &#8216;Mb&#8217; make sure you have the following &#8216;SafeRead&#8217; and &#8216;SafeWrite&#8217; in<br />
your coding libraries.
</p>
<pre>
<font size="3">
ssize_t SafeWrite(int fd,void *buf,size_t wlen){
 ssize_t len; char *bbuf = (char *)buf;
 size_t writeln=0;
 while(writeln &lt; wlen){
   len = write(fd,&amp;(bbuf[writeln]),wlen-writeln);
   if(len&lt;=0) return len;
   writeln += len;
 }
 return writeln;
}

ssize_t SafeRead(int fd,void *buf,size_t rlen){
 ssize_t len;char *bbuf = (char *)buf;
 size_t readln=0;
 while(readln &lt; rlen){
   len = read(fd,&amp;(bbuf[readln]),rlen-readln);
   if(len&lt;=0) return len;
   readln += len;
 }
 return readln;
}
</font>
</pre>
<p>
Its quite often that the while loop only executes once, but its always possible that due to some reason the &#8216;read&#8217; and &#8216;write&#8217; syscalls may return less than the number of bytes you read or write.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/118/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/118/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=118&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/02/11/tech-saferead-and-safewrite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
		<item>
		<title>[TECH] A Contradictory gcc message.</title>
		<link>http://vkundeti.wordpress.com/2008/01/31/tech-a-contradictory-gcc-message/</link>
		<comments>http://vkundeti.wordpress.com/2008/01/31/tech-a-contradictory-gcc-message/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 06:06:00 +0000</pubDate>
		<dc:creator>Vamsi Kundeti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vkundeti.wordpress.com/2008/01/31/tech-a-contradictory-gcc-message/</guid>
		<description><![CDATA[I have been experimenting on some of my PDM (parallel disk model) sorting code and wanted to create huge files with billions of keys I wanted to create a file with 1 billion integers (1024*1024*1024*4), the program stopped after some time saying that it &#8220;File size limit exceeded&#8221; , however in my shell (csh) the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=116&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
I have been experimenting on some of my PDM (parallel disk model) sorting code<br />
and wanted to create huge files with billions of keys I wanted to create a file<br />
with 1 billion integers (1024*1024*1024*4), the program stopped after some time saying<br />
that it &#8220;File size limit exceeded&#8221; , however in my shell (csh) the &#8216;limit&#8217; command showed unlimited.
</p>
<pre>
<font size="3">

[vamsik@abadon PDMSorting]$ ./pdm_sort
Setting RAND_SEED to 1201763856
Filesize limit exceeded
[vamsik@abadon PDMSorting]$ ls -al key_file.txt
---------- 1 vamsik fuse 2147483647 Jan 31 02:18 key_file.txt
[vamsik@abadon PDMSorting]$
</font>
</pre>
<p>
I saw that the file was created without any permissions, so I tweaked<br />
my &#8216;umask&#8217; but nothing changed (I was doing a open with (O_WRONLY|O_CREAT)). You might be wondering what exactly I&#8217;m trying to say in this post, in fact the story just started, rather than setting my &#8216;umask&#8217; to &#8216;umask 22&#8242;, I have set it to<br />
&#8216;umask 755&#8242; and as usual I was doing a build with &#8216;make&#8217; this is what happened.
</p>
<pre>
<font size="3">
[vamsik@abadon PDMSorting]$ make
gcc  -O2  -I../myutil/            -c ExternalSort.c
ExternalSort.c:1: fatal error: can't open /tmp/cct9kuSr.s for writing: Permission denied
compilation terminated.
The bug is not reproducible, so it is likely a hardware or OS problem.
make: *** [ExternalSort.o] Error 1
[vamsik@abadon PDMSorting]$
</font>
</pre>
<p>
 Looks strange right? , see the funny thing it says <b>&#8220;The bug is not reproducible, so it is likely a hardware or OS problem.&#8221;</b> , its really a stupid error message<br />
how can it say it cannot be reproduces? , just set &#8216;umask 755&#8242; and do a &#8216;gcc&#8217; on any<br />
file its going to say the same thing, in fact the message is a utter contradiction because we can reproduce this by changing the &#8216;umask&#8217;. I&#8217;m going to report this to the &#8216;gcc&#8217; maintainers or submit a patch to the maintainers.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vkundeti.wordpress.com/116/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vkundeti.wordpress.com/116/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkundeti.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkundeti.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkundeti.wordpress.com/116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkundeti.wordpress.com&amp;blog=3527601&amp;post=116&amp;subd=vkundeti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vkundeti.wordpress.com/2008/01/31/tech-a-contradictory-gcc-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2c73fac6e9630c29708f0ee5b2104eb3?s=96&#38;d=identicon" medium="image">
			<media:title type="html">vkundeti</media:title>
		</media:content>
	</item>
	</channel>
</rss>
