Showing posts with label Unix Linux Based Programs. Show all posts
Showing posts with label Unix Linux Based Programs. Show all posts

Unix shell script to get cpu usage

Unix shell script to get CPU usage



#!/bin/bash

id=0; tot=0; p_id=0; p_tot=0;

for ((i=0; $i < 100; ++i )) ; do

p_id=$id; p_tot=$tot;
x=`cat /proc/stat | grep -w cpu`;
id=`echo $x | cut -d " " -f 5`;
tot=0;
for f in `echo $x`; do
tot=$(($tot + $f));
done;

echo "idle CPU usage is % = "$((($id - $p_id)*100/($tot - $p_tot)));

sleep 3;
done



Related Links :

Unix shell script to kill a process by name

Unix shell script to kill a process by name




if [ "$1" == "" ];then
echo "Usage : ./pkill.sh "
else
for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
do
kill -9 $i
done
fi


Related Links :

What are the Advantages of using UNIX with C ?

Well Friends as we know that C can do Much More Different with diferent OS but as best UNIX is cool whys its best partner of C ???? Lets Know....

Portability -- UNIX, or a variety of UNIX, is available on many machines. Programs written in standard UNIX and C should run on any of them with little difficulty.
Multiuser / Multitasking -- many programs can share a machines processing power.
File handling -- hierarchical file system with many file handling routines.
Shell Programming -- UNIX provides a powerful command interpreter that
understands over 200 commands and can also run UNIX and user-defined programs.

Pipe -- where the output of one program can be made the input of another. This can done from command line or within a C program.
UNIX utilities -- there over 200 utilities that let you accomplish many routines without writing new programs. e.g. make, grep, diff, awk, more ....
System calls -- UNIX has about 60 system calls that are at the heart of the operating system or the kernel of UNIX. The calls are actually written in C. All of them can be accessed from C programs. Basic I/0, system clock access are examples. The function open() is an example of a system call.
Library functions -- additions to the operating system.

Related Links :

what is Debugging with GNU Debugger (GDB)

The debugger is the program that you use to figure out why your program isn’t behaving the way you think it should.You’ll be doing this a lot. The GNU Debugger (GDB) is the debugger used by most Linux programmers.You can use GDB to step through your code, set breakpoints, and examine the value of local variables.

Related Links :

C header files in unix & its Working

You can learn a lot about the system functions that are available and how to use them by looking at the system header files.These reside in /usr/include and /usr/include/sys. If you are getting compile errors from using a system call, for instance, take a look in the corresponding header file to verify that the function’s signature is the same as what’s listed in the man page.
On Linux systems, a lot of the nitty-gritty details of how the system calls work are reflected in header files in the directories /usr/include/bits, /usr/include/asm, and /usr/include/linux. For instance, the numerical values of signals are defined in /usr/include/bits/signum.h.
These header files make good reading for inquiring minds. Don’t include them directly in your programs, though; always use the header files in /usr/include or as mentioned in the man page for the function you’re using.

Related Links :

Prpgram to count vowels in given string by using vfork method (use of Vfork Function in c)


#include "stdio.h"
#include "dirent.h"
int main()
{
int j,n,a,i,e,o,u;
char str[50];
a=e=i=o=u=0;
pid_t pid;
if((pid=vfork())<0)
{
perror("FORK ERROR");
exit(1);
}
if(pid==0)
{
printf("Enter the String to count vowels:");
gets(str);
_exit(1);
}
else
{
n=strlen(str);
for(j=0;j
{
if(str[j]=='a' || str[j]=='A')
a++;
else if(str[j]=='e' || str[j]=='E')
e++;
else if(str[j]=='i' || str[j]=='I')
i++;
else if(str[j]=='o' || str[j]=='O')
o++;
else if(str[j]=='u' || str[j]=='U')
u++;
}
printf("Vowels Counting");
printf("Number of A : %d",a);
printf("Number of E : %d",e);
printf("Number of I : %d",i);
printf("Number of O : %d",o);
printf("Number of U : %d",u);
printf("Total vowels : %d",a+e+i+o+u);
exit(1);
}
}

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!