Friday, August 26, 2011

How to measure timings in a C program

Include the header file
Time variables have data type clock_t. 
If you want to measure the timimg of a block, note the time t1 just before the block begins and the time t2 just after the block exits. Use the C library call clock() to know a particular time. 
Divide (t2 - t1) by CLOCKS_PER_SEC to get the number of seconds the block took. 
If the block returns too fast so that you get a zero time, repeat the block some number of times (say, 100 or 1000 or ...) and record the timing for these many executions of the block. 
The prototype example
#include /* Include standard i/o header file */
#include  /* Include the timing header file */

...

clock_t t1, t2;    /* Time variables */
int i,             /* Temporary index */
   repCount;       /* Repetition count */
float timeTaken;   /* Total time taken */

...

repCount = 1000;   /* Set repetition count */

t1 = clock();      /* Record timing before the block begins */

/* Execute the block repCount times */
for (i=0; i

Wednesday, July 6, 2011

How to : implement a Circular Buffer in C

Below is a simple example of how to implement a circular buffer in C. The video / audio buffer size can be changed through the VIDEO_BUFFER_SIZE / AUDIO_BUFFER_SIZE Macros.

**************
CIRCULAR_BUFFER
**************
#include
#include
#include


/* Buffer Size */
#define VIDEO_BUFFER_SIZE 10
#define AUDIO_BUFFER_SIZE 5
#define video_stream_id 1
#define audio_stream_id 2

FILE *fpv=NULL,*fpa=NULL;
#pragma pack(1)

typedef struct
{
unsigned int *video[VIDEO_BUFFER_SIZE]; /**< Element of ciruclar buffer */ unsigned int *audio[AUDIO_BUFFER_SIZE]; /**< Element of ciruclar buffer */ unsigned int vwritePointer; /**< write pointer */ unsigned int vreadPointer; /**< read pointer */ unsigned int *vsize; /** size of circular buffer */ unsigned int awritePointer; /** write pointer */ unsigned int areadPointer; /** read pointer */ unsigned int *asize; /** size of circular buffer */ } CircularBuffer; static int total_vsize = 0; /** Init Ciruclar Buffer **/ CircularBuffer* CircularBufferInit(CircularBuffer** pQue) { unsigned int sz = sizeof(CircularBuffer); *pQue = (CircularBuffer*) malloc(sz); //unsigned int *(*(*pQue)->keys)) = (unsigned int **)calloc(size,sizeof(unsigned int *));
if(*pQue)
{
printf("\n Init CircularBuffer: CB SIZE = %d \n",sizeof(CircularBuffer));
(*pQue)->vsize = (unsigned int *)calloc(VIDEO_BUFFER_SIZE,sizeof(unsigned int));//VIDEO_BUFFER_SIZE;
(*pQue)->vwritePointer = 0;
(*pQue)->vreadPointer = 0;
(*pQue)->asize = (unsigned int *)calloc(AUDIO_BUFFER_SIZE,sizeof(unsigned int));//AUDIO_BUFFER_SIZE;
(*pQue)->awritePointer = 0;
(*pQue)->areadPointer = 0;
}
return *pQue;
}

inline int CircularBufferIsFull(CircularBuffer* que,unsigned int stream_id)
{
int isFULL;
if(video_stream_id == stream_id)
{
isFULL = ((que->vwritePointer + 1) % VIDEO_BUFFER_SIZE == que->vreadPointer);
}
else if(audio_stream_id == stream_id)
{
isFULL = ((que->awritePointer + 1) % AUDIO_BUFFER_SIZE == que->areadPointer);
}
//else{}
return isFULL;
}

inline int CircularBufferIsEmpty(CircularBuffer* que,unsigned int stream_id)
{
int isEMPTY;
if(video_stream_id == stream_id)
{
isEMPTY = ((que->vreadPointer + 1) % VIDEO_BUFFER_SIZE == que->vwritePointer);
}
else if(audio_stream_id == stream_id)
{
isEMPTY = ((que->areadPointer + 1) % AUDIO_BUFFER_SIZE == que->awritePointer);
}
// else{}
return isEMPTY;
}

inline int CircularBufferWrite(CircularBuffer* que, void* k,unsigned int stream_id,unsigned int size)
{
int isFull = 0;
//printf("\n DATA = %d\n",(unsigned int)k);
isFull = CircularBufferIsFull(que,stream_id);
if(video_stream_id == stream_id)
{
que->video[que->vwritePointer] = (unsigned int *)calloc(size,sizeof(char));
que->vsize[que->vwritePointer] = size;
//*que->video[que->vwritePointer] = k;

//printf("\n ##### TOTAL SIZE = %d\n",que->vsize[que->vwritePointer]);
memcpy(que->video[que->vwritePointer],k,size);
que->vwritePointer++;
que->vwritePointer %= VIDEO_BUFFER_SIZE;
}
else if(audio_stream_id == stream_id)
{
que->audio[que->awritePointer] = (unsigned int *)calloc(size,sizeof(char));
que->asize[que->awritePointer] = size;
//*que->audio[que->awritePointer] = k;
memcpy(que->audio[que->awritePointer],k,size);
que->awritePointer++;
que->awritePointer %= AUDIO_BUFFER_SIZE;
}
//else{}
//que->vwritePointer++;
//que->vwritePointer %= que->size;

return isFull;
}

inline int CircularBufferRead(CircularBuffer* que, void* pK,unsigned int stream_id,unsigned int size)
{
int isEmpty = 0;
isEmpty = CircularBufferIsEmpty(que,stream_id);
//printf("\n ## DATA = %d\n",(unsigned int)*que->video[que->readPointer]);
//*pK = (unsigned int)*que->video[que->readPointer];
if(video_stream_id == stream_id)
{
memcpy(pK,que->video[que->vreadPointer],size);
que->vreadPointer++;
que->vreadPointer %= VIDEO_BUFFER_SIZE;
}
else if(audio_stream_id == stream_id)
{
memcpy(pK,que->audio[que->areadPointer],size);
que->areadPointer++;
que->areadPointer %= AUDIO_BUFFER_SIZE;
}
//else{}

return(isEmpty);
}

int main(int argc, char *argv[])
{
CircularBuffer* que;
unsigned int a = 0,b = 0,*p=NULL,j=188,k=0,*pk;
int isEmpty,isFull;
unsigned int *readb,bytes;

p = (unsigned int *)calloc(j,sizeof(char));

// pk = (unsigned int *)calloc(j,sizeof(char));
readb = (unsigned int *)calloc(j,sizeof(char));
fpv = fopen("/home/suroy7/video_es.txt","a+");
fpa = fopen("/home/suroy7/audio_es.txt","a+");
if ( NULL == fpv || NULL == fpa)
{
printf("\n File doesn't exist \n");
}

CircularBufferInit(&que);

do{
a += 1;
isFull = CircularBufferWrite(que,(void *)&a,1,j);
}while(!isFull);


if(isFull)
{
for(k=0;k < VIDEO_BUFFER_SIZE ; k++ ) { //memcpy(pk,que->video[k],j);
//printf("\n $$$ DATA = %#x %02d",*que->video[k],*pk);
fwrite(que->video[k],1,que->vsize[k],fpv);
}
}

//fseek(fpv, 0, SEEK_SET);
rewind(fpv);
printf("\n ## VIDEO FILE DATA = ");
do{

bytes = fread(readb,1,j,fpv);
printf(" %02d ",*readb);
}while( bytes > 0 );

#if 1
isFull = 0;
do{
b += 1;
isFull = CircularBufferWrite(que,(void *)&b,2,j);
}while(!isFull);

if(isFull)
{
for(k=0;k < AUDIO_BUFFER_SIZE ; k++ ) { //memcpy(pk,que->video[k],j);
//printf("\n $$$ DATA = %#x %02d",*que->video[k],*pk);
fwrite(que->audio[k],1,que->asize[k],fpa);
}
}
//fseek(fpv, 0, SEEK_SET);
rewind(fpa);
printf("\n ## AUDIO FILE DATA = ");
do{

bytes = fread(readb,1,j,fpa);
printf(" %02d ",*readb);
}while( bytes > 0 );
printf("\n");
printf("\n CircularBuffer AUDIO DATA = ");
do {
isEmpty = CircularBufferRead(que, p,2,j);
printf(" %02d ", *p);
} while (!isEmpty);
//printf("\n");
#endif


printf("\n CircularBuffer VIDEO DATA = ");
do {
isEmpty = CircularBufferRead(que, p,1,j);
printf(" %02d ", *p);
} while (!isEmpty);
printf("\n\n");

fclose(fpv);
fclose(fpa);
//free(pk);
free(readb) ;
free(p);
free(que);
return 0;
}

Saturday, June 4, 2011

How to : Find simple OR duplicate files in Linux / Unix

Recently I have bought an USB hard-disk which is 1TB . And I have copied some movies around 500GB from one of my friends Hard disk. Then when I am checking the content I found that some of the movies are repeated and that to with different names, in different folders etc. This made finding them difficult. So how to find all those files and deleted them if not required. There is one command in Linux called fdupes(File DUPlicatES) which is not installed by default. Lets see how we can use it.

In Redhat/Fedora/CentOS

#yum install fdupes 

In Debain/Ubuntu

#apt-get install fdupes

Once installed you can use right away to find duplicates..

To find duplicates in a folder

#fdupes /path/to/folder

This will find all the duplicate files in that folder. What about if you want to find in sub folders too?

Use -r option to recursive search..

#fdupes -r /path/to/folder

but this will not show what is the size of that duped files? Then there is any option to find sizes too? Yes you can use -S to find sizes too..

#fdupes -S -r /path/to/folder

What about deleting them with a conformation so that you no need to go into every folder and delete them.

#fdupes -d -r /path/to/folder

Sample output

#fdupes -d -S -r /media/Movies/

Handy Command Line Tricks for Linux


Command Line Tricks for Linux

There are many Linux commands which helps us in day to day Life .... find some useful ones here

1. Linux comes in several flavors. The following commands will help you determine which Linux Distro is installed on your host, what’s the version of your Linux kernel, the CPU model, processor speed, etc.
$ cat /etc/issue$ cat /proc/version$ cat /proc/cpuinfo
2. Find the total amount of RAM available on your Linux box and how much is free.
$ free -mto
3. The command cd.. takes you up one directory level but cd – will move you to the previous working directory. Or use the command pwd to print the full path of the current directory that you can copy-paste later into the shell.
$ cd –$ pwd
4. The command history will show a list of all the recently executed commands and each will have an associated number. Use ! to execute that command again. Or, if the history is too long, use grep to search a particular command.
$ !$ history | grep 
5. You can remove any particular command from the shell history by number.
$ history –d 
6. If you made an error while typing a command name, just enter the correct command name and then use !* to reuse all the previous arguments.
$  !*
7. Re-run a command but after replacing the text abc in the command with xyz.
$ ^abc^xyz
8. This will list the size of all sub-folders of a directory in KB, MB or GB.
$ du –sh */
9. A better version of the ls command that displays file sizes in KB and MB.
$ ls –gho
10. You can use man to learn more about the syntax of a command but what if you don’t remember the name of the command itself? Use apropos then.
$ apropos 
11. Compare the content of two text files to see what has changed.
$ diff wp-config.php wp-config.php.old
12. Find lines that are common in any two text files.
$ grep –Fx –f file-A.html file-B.html
13. Compare the content of two directories recursively.
$ diff –urp /old-wp-directory /new-wp-directory
14. Find all files under the current directory that are larger than 10 MB in size.
$ find . -size +10M -exec du -h {} \;
15. Find all files on the system that have been modified in the last 2 days.
$ find . –type f –mtime -2
16. Find all files on the system that were modified less than 10 minutes ago
$ find . –type f –mmin -10
17. Find all PHP files that contain a particular word or phrase.
$ find . -name "*.php" -exec grep -i -H "matt mullenweg" {} \;
18. When copying or moving files, Linux won’t show a warning if you are overwriting an existing file. Therefore always use the –i switch to prevent overwrites.
$ cp –i abc.txt xyz.txt
19. Backup the content of the current folder into a tarball file using gzip compression.
$ tar zcfv backup.tar.gz /wp-directory/
20. Find processes with the highest CPU usage. Then use kill –9 pid to kill a process.
$ ps aux | sort -nrk 3 | head
21. Execute the following command in your Apache logs directory to determine hits coming from individual IP addresses.
$ cat access.log | awk '{print $1}' | sort | uniq -c | sort –n | tail
22. Monitor hits from Google bots to your website in real-time.
$ tail –f access.log | grep Googlebot
23. To find all files and web pages on your site that return a 404 error , run the following command in the Apache logs directory.
$ awk '$9 == 404 {print $7}' access.log | uniq -c | sort -rn | head
24. Find the 100 most popular pages of your site using Apache server logs again.
$ cat access.log | awk '{print $7}' |sort |uniq -c |sort -n |tail -n 100
25. Quickly find and replace a string in or more files.
$ find . -type f -name "*.php" -exec sed -i 'labrs.org/' {} \;

Monday, May 23, 2011

How to SSH without password / passphrase


Goal: to login from local machine to Remote machine / Server via ssh without typing password
This simple tutorial explains how to SSH to a remote machine without typing your password. You can use this technique if you find yourself logging in to the same machine frequently and find typing your password tedious. It is also useful in scenarios when you have a script which needs to pull some files from a remote machine or perform a task on a remote machine via SSH, and you want to run this script automatically without having a human to type a password. May be deployed on Cloud , setting up a Hadoop Cluster
These instructions work on Linux and Mac. You can achieve the same result on Windows using Putty, but I haven’t documented the putty specific instructions here.
Step 1 of 2 : On local machine: Generate Authentication Keys
  1. ssh-keygen -t rsa

Accept the default choice. Hit enter.
Enter passphrase (empty for no passphrase):Enter same passphrase again:
Hit enter twice. A passphrase encrypts your private key so that no one can see it. However, you should NOT encrypt your private key if you want a password-less login.
  1. [hadoop@uidlt02 ~]$ ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /home/hadoop/.ssh/id_rsa.
  7. Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. 53:04:95:b3:d7:ba:aa:2e:90:7a:c0:18:8c:b6:b6:fd hadoop@uidlt02.xxx.com
  10. The key's randomart image is:
  11. +--[ RSA 2048]----+
  12. [hadoop@uidlt02 ~]$

What just happened?
On your local server you just created 2 files in your ~/.ssh directory.
  1. [hadoop@uidlt02 ~]$ ls -l ~/.ssh/
  2. total 16
  3. -rw-------. 1 hadoop hadoop 1675 May 23 15:30 id_rsa
  4. -rw-r--r--. 1 hadoop hadoop 406 May 23 15:30 id_rsa.pub
  5. -rw-r--r--. 1 hadoop hadoop 5488 May 22 04:58 known_hosts
  6. [hadoop@uidlt02 ~]$

id_rsa contains your private key. id_rsa.pub contains your public key.
Step 2 of 2 : On remote machine: authorize password less login
Login to remote machine
  1. ssh hostname -l username

Enter yes and press enter.
Enter your password, and hit enter.
Create a .ssh directory on the remote machine and create a authorized_keys file in that directory. You need to copy the entire contents of your local machine’s ‘id_rsa.pub’ and paste it in the .authorized_keys file on the remote server.
  1. mkdir -p .ssh
  2. chmod 700 .ssh
  3. cd .ssh
  4. touch authorized_keys
  5. chmod 644 authorized_keys
  6. vi authorized_keys
  7. # copy-paste the entire contents of your local machine's ~/.ssh/id_rsa.pub file in authorized_keys
  8. # logout
  9. exit


Important: Make sure you have the right permissions for .ssh directory and authorized_keysfile, as shown in chmod command above otherwise SSH will not honor your authorized_keys.
You should now be able to login to the remote server without typing your password.
  1. # type this command from your local machine
  2. ssh hostname -l username
SSH should log you in without password! Now, you can also scp or rsync (over ssh) without having to enter your password.

Wednesday, May 18, 2011

How to : Free Word to PDF Converter

All of us require at some point of time to convert a *.doc file to *.pdf . Yeah ! you guessed it right as alll of you already know , just thought of sharing :


# It compresses the size.
# It makes more portable.
# It makes sure your document is untampered , no accidental modification happens.


Here are the sites which allows you to do it for free, but only with some restrictions :


# freepdfconvert
# Adobecreatepdf
# Pdfonline


Enjoy happy converting !!

Sunday, May 8, 2011

How to : get command prompt by registry file addition

Very frequently we need to create command prompt at a certain folder. Most users directly go to command prompt and then type out the entire pathname to get to the required folder. 
Would it not be much easier to get to the command prompt by directly right-clicking on the folder? 
Below is how you can do it. This is valid only for Win XP. Other operating systems have not been checked.

1. Create a text file wherever it is easy for you to access it. 
2. Copy and past the following data into the file command.txt :

REGEDIT4
[HKEY_CLASSES_ROOT\Directory\shell\DosHere]
@="Command &Prompt Here"

[HKEY_CLASSES_ROOT\Directory\shell\DosHere\command]
@="C:\\Windows\\System32\\cmd.exe /k cd \"%1\""

[HKEY_CLASSES_ROOT\Drive\shell\DosHere]
@="Command &Prompt Here"

[HKEY_CLASSES_ROOT\Drive\shell\DosHere\command]
@="C:\\Windows\\System32\\cmd.exe /k cd \"%1\""

3. Save the file
4. Now rename the extension from a command.txt to a command.reg (registry type file). Go to DOS prompt and type C:\ren command.txt command.reg
5. Double clicking the file will pop up a dialog box saying "Are you sure you want to add the data to the registry".
6. Say yes and the data gets added into your windows registry.
7. Now any folder where you want a command prompt, it is as easy as right clicking on the folder and choosing "Command Prompt Here". 

Simple as that....

Matrix Checkerboard


We wish to find, for an arbitrary N, an N x N matrix with the following property: each entry in the matrix is the smallest non-negative number which does not appear either above the entry or to its left.
For example, for N=6, we might have the matrix:
012345
103254
230167
321076
456701
547610

  1. Write a function that computes the entry in a particular row and column. That is, complete the following:

    int entry_at(int row, int column) {

        // ...

    }

  2. Can you write the entry_at function to run in constant space and constant time?
Soln: Assume we count rows and columns starting with 0: 0, 1, 2, and so on. Let amn denote the number that appears in the (m, n)-cell. Then

amn = m ^ n.
It follows that this particular Latin square serves as the "addition" table for the XOR operation over the set of nonnegative integers. In particular, using the binary representation,
a100, 1000= 100 ^ 1000
= (1100100)2 ^ (1111101000)2
= (1110001100)2
= 908.
[Of course, if initially rows and columns were counted starting with 1, then we should consider instead 99 ^ 999 which is 900!]