Monday, May 23, 2011

finding the memory size in Linux

To my knowledge there are three command to find the memory in Linux:

1)free command:

It is used to print the memory size in bytes by default

>> free
total used free shared buffers cached
Mem: 16438664 16294220 144444 0 235596 13380932
-/+ buffers/cache: 2677692 13760972

options:

-b - to print the bytes

-m - to print the information in MegaBytes

-k - to print the information in kilobytes.

>>free -m
total used free shared buffers cached
Mem: 16053 15953 100 0 214 13119
-/+ buffers/cache: 2619 13433
Swap: 9855 1 9854


2)Top command:

It is used to display the ongoing process in the Cpu and the memory used by the same.

>>Top
PID USERNAME THR PRI NICE SIZE RES SHR STATE TIME CPU COMMAND
4536 root 1 0 -20 37M 8696K 2204K sleep 189:47 1.60% mount
23308 karthik 1 15 0 18M 5948K 4392K sleep 0:00 1.60% vim
3754 root 1 16 0 10M 680K 584K sleep 3:03 0.20% vi


3)dmesg | grep ^Memory
This also gives the information about the available memory.

> dmesg | grep ^Memory
Memory for crash kernel (0x0 to 0x0) notwithin permissible range
Memory: 16435064k/17563644k available (2577k kernel code, 339988k reserved, 1305k data, 212k init

awk unix command

awk is a unix command

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'

-F is used as a the Field separator

awk print

Awk is used to get a particular pattern in a ouput

you can use this to print a particular pattern

To print the third column in the output using space as the delimiter
awk -F " " '{print $3}'

If you want to print the particular row and a particular column you can use like this

awk -F " " 'NA==3{print $2}'

Wednesday, May 18, 2011

Comparing NULL in C example


You can use the below code for comparing String with NULL in C

char * ff;
if (  ff != NULL ) );
{
printf("value isnot null");
}
else
{
printf ("value is null");
}

Comments in C and C++


In C

valid comments are when text are in between /*  .......*/
It can also be used across multiple lines.But it will not support nested comments.

ex:
/*  comments  */
or

/*   First line
Second line  */

It can also be used like:
/*  comments
*   comments
*  comments
*/

 new C compilers also supports inline comments    // .  but old compilers may give syntax error.
ex:
int c;  // for intializing

 C++  supports both type of comments inline comments // and /*  ...*/




strcmp and strncmp in C


strcmp function in c

int strcmp(char * str1,char * str2)
strcmp function  returns a number eitheir 0, positive integer or negative integer

Return vaues:

positive integer       - is returned if string str1 is greater than string str2
0                           - if both the strings are equal
negative integer      -  if string str1 is less than the string str2


In this case comparisons happen using unsigned integers.

for example:
char * i = "hello";
char * j="hello1"; 
printf("%d",strcmp(i,j));
output will be a negative integer

 strncmp in C:

The syntax all are same .but in this case there is an extra argument for setting how many characters you want to compare.

int strncmp(char *str1,char * str2, int );

Functionality all are same.But here int parameter is to set no of characters to compare in two strings.


for example:
char * i = "hello";
char * j="hello1"; 
printf("%d",strcmp(i,j,4));
output will be 0 as we compare only 4 characters and it is same in both the strings.








Tuesday, May 17, 2011

hosts.allow and hosts.deny in Linux

hosts.allow and hosts.deny files are mainly used for restricting the ssh to th editing  your server in which you are    
editing the files.

These files are maintained by the TCP wrappers

These are mainly used for securing your server or system from outside acccess.

The way it happens when a outside ip asks for permission to ssh to your system:
1)The Tcp wrapper first checks for entry in /etc/hosts.allow.If a entry is present ,it does not go to the /etc/hosts.deny.
2)If entry is not present ,then it goes for hosts.deny ->if entry is present , the ip is denied
                                                                                  if entry is not present ,the ip is allowed.

If the entry is not present in both hosts.allow and hosts.deny , the entry is allowed.