Author Archive

Linux – top 10 memory consuming processes

*Show top 10 memory consuming processes in descending order –

[daniel@kauai demo]$  ps havx | awk ' { print $8 " " $10}' | sort -nr  |head  -10
2267936 /usr/libexec/qemu-kvm
841588 /usr/libexec/qemu-kvm
400336 /opt/google/chrome/chrome
316424 /opt/google/chrome/chrome
299740 /opt/google/chrome/chrome
45640 /usr/bin/python
43748 /usr/sbin/named-sdb
39516 /usr/bin/Xorg
31724 libvirtd
24080 /usr/libexec/mysqld


*Continuously show top 10 every one second – Use Ctrl+C to stop.

[daniel@kauai demo]$ while (true); do ps havx | awk ' { print $8 " " $10}' | sort -nr  |head  -10; echo "..... " ; sleep 1 ; done
2267936 /usr/libexec/qemu-kvm
841540 /usr/libexec/qemu-kvm
401500 /opt/google/chrome/chrome
316360 /opt/google/chrome/chrome
300060 /opt/google/chrome/chrome
45640 /usr/bin/python
43748 /usr/sbin/named-sdb
39516 /usr/bin/Xorg
31724 libvirtd
24080 /usr/libexec/mysqld

..... 
2267936 /usr/libexec/qemu-kvm
841540 /usr/libexec/qemu-kvm
401500 /opt/google/chrome/chrome
316360 /opt/google/chrome/chrome
300060 /opt/google/chrome/chrome
45640 /usr/bin/python
43748 /usr/sbin/named-sdb
39516 /usr/bin/Xorg
31724 libvirtd
24080 /usr/libexec/mysqld

..... 
2267936 /usr/libexec/qemu-kvm
841540 /usr/libexec/qemu-kvm
401516 /opt/google/chrome/chrome
316360 /opt/google/chrome/chrome
300060 /opt/google/chrome/chrome
45640 /usr/bin/python
43748 /usr/sbin/named-sdb
39516 /usr/bin/Xorg
31724 libvirtd
24080 /usr/libexec/mysqld

..... 
2267936 /usr/libexec/qemu-kvm
841540 /usr/libexec/qemu-kvm
401528 /opt/google/chrome/chrome
316360 /opt/google/chrome/chrome
300060 /opt/google/chrome/chrome
43748 /usr/sbin/named-sdb
39516 /usr/bin/Xorg
31724 libvirtd
24080 /usr/libexec/mysqld
21260 gnome-terminal

..... 
^C
[daniel@kauai demo]$ 

Linux – grep exclude grep

grep exclude grep from output

When you run a Linux command and pipe it through grep, the grep text itself is shown in the output as well. Once common technique is to use “grep -v” to exclude it. But here is a handy tip which excludes the grep text by placing the first character in a parenthesis –

Normal grep output – notice “grep ssh” is shown in the output :

[daniel@kauai tmp]$ ps aux |grep ssh
root      2795  0.0  0.0  66236  1236 ?        Ss   Mar01   0:38 /usr/sbin/sshd
daniel    6317  0.0  0.0 103320   832 pts/1    S+   23:21   0:00 grep ssh
root     25544  0.0  0.0 100016  4232 ?        Ss   22:17   0:00 sshd: daniel [priv]
daniel   25552  0.0  0.0 100016  2008 ?        S    22:17   0:00 sshd: daniel@pts/8

With the parenthesis trick we can exclude “grep ssh” from the output –

[daniel@kauai tmp]$ ps aux |grep [s]sh
root 2795 0.0 0.0 66236 1236 ? Ss Mar01 0:38 /usr/sbin/sshd
root 25544 0.0 0.0 100016 4232 ? Ss 22:17 0:00 sshd: daniel [priv]
daniel 25552 0.0 0.0 100016 2008 ? S 22:17 0:00 sshd: daniel@pts/8

[/bash]

Linux – empty or truncate files with strange names such as white space etc.

In Linux, it is very common to expect the files in the system to follow certain naming conventions – such as no white space, usually only lower cases, alphanumeric with underscore or dashes. But in some cases, you will find files which don’t follow this convention – the files might have been copied from other OSes such as Microsoft Windows or MacOS. Here is a trick to empty these files without deleting them.

Use the “truncate” command to empty files with non-standard names.

Requirement – empty files with white space in file name. Keep the files, just reduce the size to 0.

# find . -type f -exec ls -l {} \;
-rw-rw-r-- 1 nagios nagios 0 Dec 21 11:21 ./app/\var\log\messages
-rw-rw-r-- 1 nagios nagios 1359 Dec 19 06:26 ./puppet/\var\log\syslog
-rw-rw-r-- 1 nagios nagios 8071 Dec 15 02:30 ./ftp/Microsoft-Windows-EventCollector\Operational

# find . -type f -exec truncate -s 0 {} \;

# find . -type f -exec ls -l {} \;
-rw-rw-r-- 1 nagios nagios 0 Dec 21 11:27 ./app/\var\log\messages
-rw-rw-r-- 1 nagios nagios 0 Dec 21 11:27 ./puppet/\var\log\syslog
-rw-rw-r-- 1 nagios nagios 0 Dec 21 11:27 ./ftp/Microsoft-Windows-EventCollector\Operational

With the command

find . -type f -exec truncate -s 0 {} \;

, we were able to list all files in current directory and empty them.

C programming Language – Code snippets

C Programming Language, 2nd Edition

Compiling and running the sample codes using gcc :

gcc sample.c -o sample
./sample

Chapter 3 – Control Flow

1. Trim whitespace

#include<stdio.h>
#include<string.h>


int main()
{
  int n,i=0;
  char s[]="   HELLO THERE    ";
  printf("Before trim:%s\n",s);
  for(n=strlen(s)-1; n>=0; n--)
   if(s[n]!=' ' && s[n]!='\t' && s[n]!='\n') break;
  s[n+1]='\0';
  while(isspace(s[i++]));
  i=0;
  while(s[i]!='\0') s[i++];
  s[i]='\0';
  printf("After trim:%s\n",s);
  return n;
}

2. Binary search

#include<stdio.h>


int binsearch(int x, int v[], int n);

int main(int argc, char *argv[])
{
  int size=argc,found;
  int counter=0,x, v[size-2];
  x=atoi(argv[size-1]);
  if (argc<3) {printf("Usage: progname integer-list integer-to-be-searched\n"); return -1; }
  while(counter <= size-3)
  { v[counter]=atoi(argv[counter+1]);
    counter++; }
(found=binsearch(x,v,size-2))!=-1 ? printf("Found=%d\n",v[found]): printf("Not found\n");
 return 0;
}

int binsearch(int x, int v[], int n)

{
   int low, high, mid;
   low=0;
   high=n-1;
   while(low<=high)
     {
       mid=(low+high)/2;
       if(x < v[mid])  high=mid-1;
       else if (x > v[mid]) low=mid+1;
       else
         return mid;
     }
return -1;
}

3. Integer to string with argument parsing.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define  SIZE  10

void itoa(int n, char s[]);
void reverse(char s[]);
int main(int argc, char *argv[])
 {
   char s[SIZE];
   int n;
   if(argc!=2) { printf("Usage: progrname integer\n"); return -1; }
   n=atoi(argv[1]);
   itoa(n,s);
   printf("String:%s\n",s);
   return 0;

 }

void itoa(int n, char s[])
 {
    int i, sign;
    if((sign=n)<0) n=-n;
    i=0;
    do {
         s[i++]=n%10 + '0';
       } while( (n/=10)>0);

 if (sign<0) s[i++]='-';
 s[i]='\0';
 reverse(s);
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

4. Reverse string and convert integer to string

#include<stdio.h>
#include<string.h>
#define SIZE 1000 

void reverse(char s[]);
void itoa(int n, char s[]);

int main()
{
  char s[SIZE]="HELLO WORLD!!";
  reverse(s);
  printf("%s\n",s);
  itoa(87690,s);
  printf("%s\n",s);
return 0;
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

void itoa(int n, char s[])
{
  int i, sign;
  if((sign=n)<0) n=-n;
  i=0;
      do {
           s[i++] = n%10 + '0';
         } while((n/=10)>0);
  if(sign<0) s[i++] = '-';
  s[i]='\0';
  reverse(s);
}

5. Command line argument parsing


#include<stdio.h>

int main(int argc, char *argv[])
{
  int i,counter;
  printf("Number of arguments:%d\n",argc);
  printf("Arguments character-by-character\n");
   for(counter=0;counter < argc ; counter++)
    { while(*argv[counter])
      printf("%c ",*argv[counter]++);
      printf("\n");
    }

printf("\n");
return 0;
}

6. Reverse a string

#include<stdio.h>
#include<string.h>
#define SIZE 1000 

void reverse(char s[]);

int main()
{
  char s[SIZE]="HELLO WORLD!";
  reverse(s);
  printf("%s\n",s);

return 0;
}

void reverse(char s[])
{
 int c, i, j;

  for(i=0,j=strlen(s)-1; i<j; i++,j--)
   {
        c=s[i];  s[i]=s[j];  s[j]=c;
   }
}

7. Sorting with shell sort method.


#include<stdio.h>
#define  SIZE  10
void shellsort(int v[], int n);

int main()
{
int counter;
int v[SIZE]={8,13,9,45,90,24,56,11,20,10};
shellsort(v,SIZE);
for(counter=0; counter<SIZE; counter++)
  printf("%d  ",v[counter]);
printf("\n");
return 0;
}

void shellsort(int v[], int n)
{
   int gap, i, j, temp;
   for(gap=n/2; gap>0; gap/=2)
     for(i=gap; i<n; i++)
       for(j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap)
        {
           temp=v[j];
           v[j]=v[j+gap];
           v[j+gap]=temp;
        }
}

8. using switch case


#include<stdio.h>

int main()
{
  int c, i, nwhite, nother,alpha, ndigit[10];
  nwhite=nother=alpha=0;
  for(i=0; i<10; i++) ndigit[i]=0;
  while((c=getchar())!=EOF)
  {
   switch(c)
    {
      case '0': case '1': case '2': case '3': case '4': case '5':
      case '6': case '7': case '8': case '9':
        ndigit[c-'0']++; break;
      case ' ': case '\n': case '\t': nwhite++; break;
      default:
          nother++; break;
     }
   }
printf("Digits =");
for(i=0; i<10; i++) printf(" %d", ndigit[i]);
printf(", white space =%d, other = %d\n", nwhite, nother);
return 0;
}

9. Double input – strip out non integers

#include<stdio.h>
#include<ctype.h>

#define  SIZE  10

int atoi(char s[]);

int main(int argc, char *argv[])
{
  int counter=0;
  char s[SIZE];
  if (argc!=2) {
    printf("Usage: progname integer\n");
    return 1;
  }
  while(*argv[1])  s[counter++]=(*argv[1]++);
  printf("Twice Result=%d\n",2*atoi(s));
return 0;
}
int atoi(char s[])
{
  int n, i,sign;

 for(i=0; isspace(s[i]); i++);

sign=(s[i]=='-')? -1: 1;
if(s[i] == '+' || s[i] == '-') i++;
 for(n=0; isdigit(s[i]); i++)
   n=10*n + (s[i]-'0');

return sign*n;
}

Reference –
https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628#reader_0131103628

Marketing tricks and scams to be aware of – for people interested in personal finance and financial independence

You earned it, and yet all kinds of tactics and tricks will be thrown around to part you from your money. Here are some of the tricks you might need to keep an eye on –

1. Only 1 item left or promotion code valid for 24 hours only ( sense of urgency) : It is meant to create a false scarcity or sense of urgency to urge buyers to spend their money in a rash. Just stay calm, do your own research and make a rational decision before you fall to this trap.

buyer-beware-3-deceptive-online-marketing-tricks-you-need-stop-falling-for

2. Up to $1000 bonus, up to 75% off – “Up to” makes any statement always true and thus deceiving. Keep an eye for this word. It is also heavily used by corporate Public Relations(PR) personnel in the media – say a big company claiming they gave a bonus of up to 10,000 to their employees. In reality, only 1 person might have received 10k, while the rest less than 100 dollars. Very tricky word.

3. Prize scams – applies mostly to online scams telling you that you have won something and you need to wire some money to insure delivery of the prize. Just label the email as spam. The FTC has details on this – https://www.consumer.ftc.gov/articles/0199-prize-scams

4. Recurring payments ( death by thousand cuts) – businesses love this because most of the consumers who signed up for recurring payment don’t use the service as much. Evaluate all your recurring payments and cancel the service if you don’t need it.
When you sign up for a service which has renewal options, such as buying a domain name, insurance etc., the system might default to automatic renewal and keep your payment method in their system. Always make sure to check all the options on the site to turn this off. Paypal is notorious for this, if you make payments using Paypal, chances are the automatic payment is enabled. Paypal makes it difficult to find the option to turn recurring payment off – https://www.paypal-community.com/t5/About-Settings/How-to-stop-recurring-payments/td-p/801420

5. For sale banner with price in small fonts – From a distance you will see the a house for sale banner for “200s” in big ont and if you look closely there is a “mid” word right in front of 200s with a very small text. All kinds of pricing tricks here – https://www.nickkolenda.com/psychological-pricing-strategies/

6. Store tricks – dairy products and other essentials on the back wall and Pricier items at eye level.
http://www.businessinsider.com/tricks-stores-use-to-make-you-spend-more-money-2015-10#-3

7. Work from home scams – if it is too good to be true, it is most likely a scam : https://money.usnews.com/money/personal-finance/saving-and-budgeting/articles/2017-08-30/7-ways-to-spot-a-work-from-home-scam

8. Amyway recruiters ? Multilevel Marketing (MLMs) – They mostly target people sitting on their own, use phrases such as “be your own boss”, “generate passive income”, “work from home”, “what would you do if you had a million dollars?”. They will strike a conversation with you as if they want to be your friend, and pretend like they admire something about you or your smart phone or whatever.

https://www.consumer.ftc.gov/blog/2014/05/telltale-signs-pyramid-scheme
http://www.moneyaftergraduation.com/2013/09/16/amway-is-a-pyramid-scheme/
http://lallouslab.net/2015/03/25/7-tips-to-help-you-spot-amway-wwdb-recruiters-in-coffeeshops/

Additional resources on avoiding the financial trap of pyramid schemes – “Multi-Level Marketing: How Selling Your Way Out of Debt Can Sink You Deeper“.

C programming Language – Code snippets

C Programming Language, 2nd Edition

Compiling and running the sample codes using gcc :

gcc sample.c -o sample
./sample

Chapter 2 – Types, Operators and Expressions

1.Convert to lower case.

#include<stdio.h>

int main(int argc, char *argv[])

{

  while(*argv[1])
   {
     if(*argv[1] >='A' && *argv[1] <='Z') { putchar(*argv[1] + 'a' -'A'); *++argv[1]; }
     else
      {putchar(*argv[1]); *++argv[1]; }
   }

printf("\n");
return 0;
}

2. Get bits


#include<stdio.h>

unsigned getbits(unsigned x,int p, int n);

int main()

{
   int x=16;
   printf("%d\n",getbits(x,4,3));

return 0;
}

unsigned getbits(unsigned x, int p, int n)
 {
   return ( x >> (p+1-n)) & ~(~0 << n);
 }

3. Count one bits


#include<stdio.h>

int bitcount(unsigned x);
int main()

{
  unsigned short x=38;

  printf("%d has %d 1 bits\n",x,bitcount(x));
  return 0;
}

int bitcount(unsigned x)
 {
   int b;
   for(b=0; x!=0; x>>=1)
    if (x&1) b++;
  return b;
}

4. Remove character from string



#include<stdio.h>

int main(int argc, char *argv[])

{


if (argc !=3)  { printf("usage: del string char\n"); return -1;}

while(*argv[1])

{
   if(*argv[1] != *argv[2]) { putchar(*argv[1]); *++argv[1]; }
   else
   { *++argv[1]; continue; }
}
printf("\n");
return 0;

}

5. Convert x to binary


#include<stdio.h>
#define  LEN 16
int main()

{
  int x=112,counter=0;
  int binary[LEN]={0};
  while(x)
   {
     binary[counter]=x%2; x/=2; counter++;
   }
while(counter>=0) { printf("%d",binary[counter]); counter--; }
printf("\n");
return 0;
}

6. Convert char to integer

#include<stdio.h>

#define NUM 1

int main(int argc, char *argv[])

{
 int counter=1,n=0;

 if(argc!=2) { printf("usage: atoi arglist\n"); return -1;}

 while(*argv[NUM])
   {
      if(*argv[NUM]>='0' && *argv[NUM]<='9') { n=10*n+(*argv[NUM]-'0'); *++argv[NUM]; }
      else
       { *++argv[NUM]; continue; }
   }
 printf("number=%d\n",n);
 return 0;

 }

Reference –

https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628#reader_0131103628