Tuesday, February 10, 2009

C Programs

Contents
========

================
Average of n numbers
================
/*Sum of n numbers */
/*It take the value for num*/
/*it adds the number upto that number and find out the average*/
#include
int main()
{
float num, sum, i;
num=0;
sum=0;
i=0;
float Average;
Average=0;

printf (" Enter the number =");
scanf("%f",&num);
for(i=1;i<=num;i++) { sum=sum+i; } Average=(sum/num); printf("%f\n",Average); return 0; }
============
Prime number
============
/*Program for the prime number*/
/*Input the number*/
/*If the number is 1, print PRIME*/
/*Check wheather the number is greater than zero*/
/*Devide the number from 2 to number-1*/
/*If once the reminder came to 0, Print the number is not prime and break the prgram*/
/*Otherwise print the NUMBER IS PRIME */

#include
int main()
{
int num,i;
printf("Enter the number=");
scanf("%d",&num);
if(num==1)
printf ("1 is a prime number \n");
else if (num<=0) printf ("The input should be greater than 0 \n"); else { i=2; while(i<=num-1) { if(num%i==0) { printf("%d is not a Prime number\n",num); break; } i=i++; } if(i==num) printf("%d is a prime number\n",num); } } ================
Fibonacci series

================
#include
int fibo(int num) /*Recursion function for fibonacci series*/
{
int static i, x=-1, y=1, temp=0;
if(num==0)
return 0;
else {
temp=x+y;
x=y;
y=temp;
printf("%d\n", temp);
fibo(num-1);
}
return x;
}
int main()
{
int fib, no;
printf("Enter the no of fibonacci series to be printed =");
scanf("%d", &no);
if(no<=0) { printf("No is not valid. Enter the No greater than 0\n"); } else { fib=fibo(no); } return 0; } =================== Multiplication Table
===================
/*Enter the number whose table is going to print*/
/*Enter the depth of the table*/
/*print the table by using the while loop*/
#include
int main()
{
int a,b,c,i;
i=1;
printf ("Enter the number = ");
scanf ("%d",&b);
printf ("Enter the depth of the table = ");
scanf ("%d",&a);
printf ("The mutiplication Table of %d\n",b);
printf ("------------------------------\n");
while (i<=a) { c=i*b; ("%d\t*%d\t=%d\n",i,b,c); i=i++; } } ============================= Printing a matrix by using function
==============================

/*Get the number of rows and column*/
/*create a function matrix */
/*Input the matrix by using the for loop*/
/*print the matrix by using the for loop*/
/*call the matrix function from the main function*/
/*End the program*/

#include
matrix()
{
int A[10][10];
int i=0, j=0, row=0, col=0;
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
/*Inputing the matrix*/
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Printing the matrix*/ printf("The given Matrix is \n"); for(i=1; i<=row; i++) { (j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } } int main() { matrix(); return 0; } ===============
Matrix addition

===============
/*---Program for matrix addition---*/
#include
int main()
{
int A[10][10];
int B[10][10];
int SUM[10][10];
int i=0, j=0, row=0, col=0;
/*input the values for the Rows and column for both Matrices*/
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
B[10][10]=0;
SUM[10][10]=0;

/*Inputing the matrix*/
printf("Enter the values for the matrix A\n");
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Inputing the matrix B*/ printf("Enter the values for the matrix B\n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&B[i][j]); } } /*Summming operation */ for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { SUM[i][j]=A[i][j]+B[i][j]; } } /*Printing the matrix A*/ printf("The Matrix A is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } /*Printing the matrix B*/ printf("The Matrix B is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",B[i][j]); } printf("\n"); } /*Printing the matrix SUM */ printf("The sum of the Matrix is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",SUM[i][j]); } printf("\n"); } return 0; } =====================
Matrix multiplication

=====================
/*Input the Values for the rows and columns for both matrices*/
/*Check wheather the column of first and row of the second is equal*/
/*If NO Print the NOTE and terminate the program*/
/*If YES Input the matrices*/
/*multiply the matrices*/
/*Print the Output matrix*/

#include
#include
int main()
{
int i,j,k;
int a,b,c,d; /*three variables to get the row and colum of the two matrix*/
int sum;
int m[10][10],n[10][10],l[10][10];
printf("Note: For matrix multiplication the number of columns in the first matrix should be equal to the number of rows in the second matrix\n");
printf("The first matrix:\n");
printf("Enter the number of rows for the first matrix : ");
scanf("%d",&a);
printf("Enter the number of columns for the first matrix : ");
scanf("%d",&b);
printf("The Second matrix:\n");
printf("Enter the number of rows of the second matrix : ");
scanf("%d",&d);
printf("Enter the number of columns of the second matrix : ");
scanf("%d",&c);

if(b==d)
{
printf("Enter the values of the first matrix:\n");
for(i=0;i
Transpose of Matrix

===================
/*input the rows and column values*/
/*Input the matrix */
/*Interchange the row and column values*/
/*Perform the transpose operation by using the for loop*/
/*print the the output matrix*/

#include
int main()
{
int A[10][10];
int B[10][10];
int i=0, j=0, row=0, col=0;
printf ("ROW = ");
scanf ("%d",&row);
printf ("COLUMN = ");
scanf ("%d",&col);
A[10][10]=0;
B[10][10]=0;
/*Inputing the matrix*/
for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("Enter the number at %d%d =",i,j); scanf("%d",&A[i][j]); } } /*Printing the matrix*/ printf("The given Matrix is \n"); for(i=1; i<=row; i++) { for(j=1; j<=col; j++) { printf("%d\t",A[i][j]); } printf("\n"); } for(i=1; i<=col; i++) for(j=1; j<=row; j++) B[i][j]=A[j][i]; /*Printing the out put matrix*/ printf ("The Transpose of the matrix is\n"); for (i=1; i<=col; i++) { for (j=1; j<=row; j++) { printf("%d\t",B[i][j]); } printf("\n"); } return 0; } ====================================

Read more...

Friday, February 6, 2009

Linux Process management

Introduction
Now we are going to discuss the basics of process management in Linux. This topic is of particular importance if you are responsible for administering a system which has not yet been proven stable, that is not fully tested in its configuration. You may find that as you run software, raised problems requiring administrator intervention. This is the world of process management

Contents
1. Process Management
2. Multitasking
3. Types of Processes
4. Input, Output, and Error Redirection
5. Managing Running Processes
6. Killing Stalled Processes
7. Understanding the init Processes
8. Parent Processes

1. Process Management
Any application that runs on a Linux system is assigned a process ID or PID. This is a numerical representation of the instance of the application on the system. In most situations this information is only relevant to the system administrator who may have to debug or terminate processes by referencing the PID. Process Management is the series of tasks a System Administrator completes to monitor, manage, and maintain instances of running applications.
2. Multitasking
Process Management beings with an understanding concept of Multitasking. Linux is what is referred to as a preemptive multitasking operating system. Preemptive multitasking systems rely on a scheduler. The function of the scheduler is to control the process that is currently using the CPU. In contrast, symmetric multitasking systems such as Windows 3.1 relied on each running process to voluntary relinquish control of the processor. If an application in this system hung or stalled, the entire computer system stalled. By making use of an additional component to pre-empt each process when its “turn” is up, stalled programs do not affect the overall flow of the operating system.

Each “turn” is called a time slice, and each time slice is only a fraction of a second long. It is this rapid switching from process to process that allows a computer to “appear’ to be doing two things at once, in much the same way a movie “appears” to be a continuous picture.
3. Types of Processes
There are generally two types of processes that run on Linux. Interactive processes are those processes that are invoked by a user and can interact with the user. VI is an example of an interactive process. Interactive processes can be classified into foreground and background processes. The foreground process is the process that you are currently interacting with, and is using the terminal as its stdin (standard input) and stdout (standard output). A background process is not interacting with the user and can be in one of two states – paused or running.

The following exercise will illustrate foreground and background processes.
1. Logon as root.
2. Run [cd \]
3. Run [vi]
4. Press [ctrl + z]. This will pause vi
5. Type [jobs]
6. Notice vi is running in the background
7. Type [fg %1]. This will bring the first background process to the foreground.
8. Close vi.

The second general type of process that runs on Linux is a system process or Daemon (day-mon). Daemon is the term used to refer to process’ that are running on the computer and provide services but do not interact with the console. Most server software is implemented as a daemon. Apache, Samba, and inn are all examples of daemons.

Any process can become a daemon as long as it is run in the background, and does not interact with the user. A simple example of this can be achieved using the [ls –R] command. This will list all subdirectories on the computer, and is similar to the [dir /s] command on Windows. This command can be set to run in the background by typing [ls –R &], and although technically you have control over the shell prompt, you will be able to do little work as the screen displays the output of the process that you have running in the background. You will also notice that the standard pause (ctrl+z) and kill (ctrl+c) commands do little to help you.
4. Input, Output, and Error Redirection
In order to work with processes on Linux, you must understand the role of redirection. A process that is set to run in the background will continue to display information on the console until it is told otherwise. Changing where a program sends its output is known as redirection.

Standard Output, Standard Input, and Standard Error are the three items that can be redirected. These items are represented by the files /dev/stdin, /dev/stdout, and /dev/stderr. All interactive process’ are programmed to write to these three files for errors, output and input. By default all three of these files are symlinked to through the /proc directory to the terminal the user is currently using. In the case of an interactive user, the flow is as follows.

/dev/stdout -> /proc/self/fd/1 -> /dev/tty1

The /proc directory is a virtual directory that contains information used to construct the users interactive environment and is thus different for each user. In this case you can see that it provides a layer of abstraction and acts as a link between the stdout file, and the file representing the users console device.

Redirection involves using special characters to tell the shell to send input, output, and errors to files other than the defaults. The following table lists these symbols.

Symbol, Meaning

> Redirect stdout

>> Redirect and append stdout

2> Redirect stderr

2>> Redirect and append stderr

<>

In order for a background process to truly run 100% in the background, you must suppress display of all errors. The following example uses redirection to produce a file listing of the entire file system as a background process.
5. Managing Running Processes
The [ps] command is the command used to manage running processes and can be used for many things including viewing the status of your computer and getting a quick idea of how well the computer is performing.

Here are some common ps commands.

Command, Usefulness

ps View current interactive processes on this terminal.

ps –a All current processes on this terminal, by all users.

ps –x All processes not assigned to a terminal (daemons).

ps –aux Output all process running and include resource utilization information.

The man page for ps contains extensive documentation on how to modify and interpret the output of ps.

Top is a utility that can be used to display a live dataset of the currently running processes. Activate it by typing [top].
6. Killing Stalled Processes
Processes that have stalled or frozen can sometimes cause problems. One of the jobs of a Linux administrator is to identify and resolve stalled processes. The clues that a process has stalled can range from an unresponsive GUI to a noted decrease in system performance. Either way, once you have identified that a processes has stalled you can terminate that process using the [kill] command. The syntax is fairly simple. You kill process by referencing its process ID or PID. This information can be seen in the output of just about any iteration of the ps command. To kill a process you pass a signal to that process. The default signal tells the process to perform a clean shutdown. Here are a few examples.

To kill a single process:

ps

PID TT STAT TIME COMMAND

9408 p9 S 0:00 ue temp2.xdh

9450 pa S 0:01 -Tcsh (Tcsh)

9501 pa T 0:00 less csh.1

9503 pa R 0:00 ps

kill 9501

This kills process number 9501. Notice that the ps command which is entered to check on the process ID’s has the latest process number.

To kill a process that refuses to die:

kill -9 9352

This makes a certain kill of process number 9352.

To kill a background job:

jobs

[1] + Running xterm -g 70x55

kill %1

[1] Done xterm -g 70x55

This kills job number 1 (one); the only job that is currently running.

To kill more than one process:

kill 8939 9543

This kills processes 8939 and 9543.

It is important to note that the kill command does not perform only negative actions. It can also be used to restart processes, and to keep processes running, even after a logout.
7. Understanding the init Processes
[init] is the most important process in Linux. All processes are derived from the init process and can trace their roots back to init. [init] always has a PID of 1 and is owned by root. The [init] process is used to start other processes and must be running for the system to operate. In a later article we will examine the boot process. Here [init] will be discussed in much more detail.
8. Parent Processes
Every process has a parent process, with the exception of [init], whose parent process ID (PPID) is 0. It is important to understand the effects of killing a process, especially if that process has spawned child processes.

Sidebar…for all you parent process’s out there

When you kill a parent process, without first killing the child procesi, then you have created orphans. Orphans will generally cause performance decreases as they are taking up resources, but not doing anything. Additionally, they usually do not self terminate, as that task can often be left up the parent. To ensure that you do not create orphans, make sure that no child processes exist when you kill the parent. This can be determined by reviewing the output of the –l switch of the ps command. Compare the PID and PPID columns.


Read more...

  © PC Template PC by PC 2008

Back to TOP