Sort an integer array, What language do you use?

Array data type, used in a programming language to specify a variable that can be indexed

During my experiment with array / hash variable, i’ve found this interesting website. The wiki explains how to sort the integers in the array by using a variety of languages. even some programming languages that I do not know at all.

I’ll pick a few examples of sorting functions described in the wiki

Sort an integer array using C language:

#include <stdlib.h>  /* qsort() */
#include <stdio.h>   /* printf() */

int intcmp(const void *aa, const void *bb)
{
	const int *a = aa; *b = bb;
	return (*a < *b) ? -1 : (*a > *b);
}

int main()
{
	int nums[5] = {2,4,3,1,2};
	qsort(nums, 5, sizeof(int), intcmp);
	printf("result: %d %d %d %d %d\n",
		nums[0], nums[1], nums[2], nums[3], nums[4]);
	return 0;
}

test the round robin DNS features of the resolver functions

Date: 3 Nov 2005
Author: Daniel Stenberg
License: freely available to do whatever you want with.

roundrobin.c – test the round robin DNS features of the resolver functions

Note: this test script is written to be compiled and run on Linux. It is
not as portable as it could be, but that is just to make it a simpler test
case.

$ gcc roundrobin.c

This source snippet resolves a name with multiple IP addresses and prints them out in the order the addresses were returned by the resolving function. It first uses getaddrinfo() (called GAI) and then gethostbyname() (called GHBN).

On my three test machines they both show the same sympthoms:

The GAI list is a lot less “random” than the GHBN one. The GAI list almost always returns the same first entry on repeated invokes (while the subsequent entries comes somewhat more random). The GHBN list is returned in a much more random fashion.

The test machines are all running Debian Unstable glibc 2.3.5

What this program does:

It runs N resolves of a given host names. It stores the order it gets the returned addresses. When all N resolves are done, it checks how the returned addresses were distributed. The procedure is first done with GAI and then with GHBN. The output is presented in list index order. That means: ‘index 0’ is the first address in the returned list and ‘index 1’ is the second address and so on. We have found out that in the GAI case you very often get 100% of the same address in index 0.

We have three hosts names that resolves to multiple IP addresses:

bad2.haxx.se
bad10.haxx.se
bad11.haxx.se

As you will see, none of them resolves any sensible data for other purposes
than resolve tests or similar.

Counting Directory Size Recursively Using C Part 2

Even there’s an easier way calculating directory size ftw syscall/API

NAME
ftw, nftw - file tree walk

DESCRIPTION
ftw() walks through the directory tree that is located under the directory dirpath, and calls fn() once for each entry in the tree.
By default, directories are handled before the files and subdirectories they contain (pre-order traversal).

CONFORMING TO
POSIX.1-2001, SVr4, SUSv1.

example code size.c

Counting Directory Size Recursively Using C

I’ve just found this great articles at Jim Plank website. I know, it was lots easier using linux du command

Without any further nonsense talk, here we go. First we need download some source code at Jim Plank website

# mkdir prsize
# cd prsize
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Prsize/prsize8.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/dllist.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Dllists/dllist.h
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/jrb.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/include/jrb.h
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/src/jval.c
# wget http://www.cs.utk.edu/~plank/plank/classes/cs360/360/include/jval.h

prsize8.c is the main program, while dllist.c/h jrb.c/h jval.c/h is library that prsize8 statically linked in.

Now we need to create some static library.(Original website doesn’t explain how to create the libs)

# gcc -c dllist.c -o dllist.o
# gcc -c jrb.c -o jrb.o
# gcc -c jval.c -o jval.o

# ar rcs libdllist.a dllist.o
# ar rcs libjrb.a jrb.o
# ar rcs libjval.a jval.o

Zombie Process

A zombie, or defunct, process is a process that has terminated, but its parent process has not taken it out of the process table with the wait() family of system calls. This typically happens when a program forks/execs another program, but then doesn’t reap it properly. The easiest way to write the parent to properly reap its children is to setup a signal handler for SIGCHLD that calls wait().

See the man pages on your local system for signal and wait(2).

Zombie and defunct states of a process are different things. Zombie is created when a child dies and parent didn’t call wait. Defunct is created when parent dies before the child finishes.

Defunct processes can’t be killed since they are already dead. To make them disappear you have to kill their parent process…

It’s not possible to kill defunct processes.

A good way to kill lists of processes is:

$ ps axf | grep name-of-process | grep -v -grep | awk '{print "kill -9 ",$1}' | sh

gethostbyname() vs getaddrinfo()

In IPv4 environment(usually) we used to call function gethostbyname() to do DNS lookups.then the information is load into a struct hostent.

#include <netdb.h>
struct hostent *gethostbyname(const char *name);

struct  hostent {
        char    *h_name;        /* official name of host */
        char    **h_aliases;    /* alias list */
        int     h_addrtype;     /* host address type */
        int     h_length;       /* length of address */
        char    **h_addr_list;  /* list of addresses from name server */
};
#define h_addr  h_addr_list[0]  /* address, for backward compatibility */

So, the easiest way to get information from gethostbyname() call is by extracting hostent structure.

Querying Visitor IP address

There are many ways, scripting/language to obtain remote IP address of the user who is browsing our website.

PERL

#!/usr/bin/perl
use CGI;
print "Content-type: text/plain; charset=iso-8859-1\n\n";
my $q = new CGI;
print "<b>Your Remote IP Address :" . $q->remote_host() . "</b>";

PHP

<?php
echo "<b>Your Remote IP Address :" . $_SERVER['REMOTE_ADDR'] . "</b>";
?>