Friday, August 26, 2011
How to measure timings in a C program
Wednesday, July 6, 2011
How to : implement a Circular Buffer in C
**************
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
$ cat /etc/issue$ cat /proc/version$ cat /proc/cpuinfo
$ free -mto
$ cd –$ pwd
$ !$ history | grep
$ history –d
$!*
$ ^abc^xyz
$ du –sh */
$ ls –gho
$ apropos
$ diff wp-config.php wp-config.php.old
$ grep –Fx –f file-A.html file-B.html
$ diff –urp /old-wp-directory /new-wp-directory
$ find . -size +10M -exec du -h {} \;$ find . –type f –mtime -2
$ find . –type f –mmin -10
$ find . -name "*.php" -exec grep -i -H "matt mullenweg" {} \;$ cp –i abc.txt xyz.txt
$ tar zcfv backup.tar.gz /wp-directory/
$ ps aux | sort -nrk 3 | head
$ cat access.log | awk '{print $1}' | sort | uniq -c | sort –n | tail$ tail –f access.log | grep Googlebot
$ awk '$9 == 404 {print $7}' access.log | uniq -c | sort -rn | head$ cat access.log | awk '{print $7}' |sort |uniq -c |sort -n |tail -n 100$ find . -type f -name "*.php" -exec sed -i 'labrs.org/' {} \;Monday, May 23, 2011
How to SSH without password / passphrase
- ssh-keygen -t rsa
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.- [hadoop@uidlt02 ~]$ ssh-keygen -t rsa
- Generating public/private rsa key pair.
- Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /home/hadoop/.ssh/id_rsa.
- Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
- The key fingerprint is:
- 53:04:95:b3:d7:ba:aa:2e:90:7a:c0:18:8c:b6:b6:fd hadoop@uidlt02.xxx.com
- The key's randomart image is:
- +--[ RSA 2048]----+
- [hadoop@uidlt02 ~]$
- [hadoop@uidlt02 ~]$ ls -l ~/.ssh/
- total 16
- -rw-------. 1 hadoop hadoop 1675 May 23 15:30 id_rsa
- -rw-r--r--. 1 hadoop hadoop 406 May 23 15:30 id_rsa.pub
- -rw-r--r--. 1 hadoop hadoop 5488 May 22 04:58 known_hosts
- [hadoop@uidlt02 ~]$
- ssh hostname -l username
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.- mkdir -p .ssh
- chmod 700 .ssh
- cd .ssh
- touch authorized_keys
- chmod 644 authorized_keys
- vi authorized_keys
- # copy-paste the entire contents of your local machine's ~/.ssh/id_rsa.pub file in authorized_keys
- # logout
- exit
- # type this command from your local machine
- ssh hostname -l username
Wednesday, May 18, 2011
How to : Free Word to PDF Converter
# 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
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
| 0 | 1 | 2 | 3 | 4 | 5 |
| 1 | 0 | 3 | 2 | 5 | 4 |
| 2 | 3 | 0 | 1 | 6 | 7 |
| 3 | 2 | 1 | 0 | 7 | 6 |
| 4 | 5 | 6 | 7 | 0 | 1 |
| 5 | 4 | 7 | 6 | 1 | 0 |
- 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) {
// ...
}
- Can you write the entry_at function to run in constant space and constant time?
| amn = m ^ n. |
|