Beruflich Dokumente
Kultur Dokumente
Result:
Thus the basic Unix commands are successfully executed and
verified.
Ex.No:2
Date: WORKING WITH FILES
________________________________________________
Aim:
To execute and verify commands used to work with files.
1. Command: cat
Syntax: cat >
Description: used to create a file.
2. Command: cp Command: cat
Syntax: cat >
Description: used to create a file.
3. Command: mv
Syntax: mv file1 file2
Description: move files or rename files.
4. Command: rm
Syntax: rm file1 file2
Description: remove files.
5. Command: lpr
Syntax: lpr file1
Description: used to print the file.
6. Command: ls
Syntax: ls
Description: list the directory contents.
OPTIONS DESCRIPTIONS
ls -a Do not ignore entries starting with (.)
ls –A Do not list implied (.) and (..)
ls -B Do not list implied entries ending
with ~.
ls -C List entries by columns
ls –l Use a long listing format
ls –s Sort by file size.
7. Command: wc
Syntax: wc filename
Description: tells you how many lines, words and characters there are in a
file.
8. Command: diff
Syntax: diff filename1 filename2
Description: used to compare files and shows where they differ.
9. Command: chmod
Syntax: chmod
Result:
Thus the commands used to work with files are successfully
executed and verified.
Ex.No:3
Date: WORKING WITH DIRECTORIES
________________________________________________
Aim:
To execute and verify commands used to work with directories.
1. Make a directory:
Command: mkdir
Syntax: mkdir directory name
Description: to create a new directory.
2. List the contents of the directory.
Command: ls
Syntax: ls
Description: used to list directory contents.
3. Change the name of the directory.
Command: mv
Syntax: mv directory1 directory2
Description: change the name of the directory1 to directory2.
4. Delete the directory.
Command: rmdir
Syntax: rmdir directory name
Description: deletes the directory.
5. Navigating directory:
Command: pwd
Syntax: pwd
Description: displays the current directory.
6. Directory abbreviations:
~ Home directories
.. Parent directory [the directory just above your directory]
. Current directory.
7. Move from one directory to other directory.
Command: cd
Syntax: cd directory name
Description: moves from current directory to the specified directory.
Result:
Thus the commands used to work with directories are successfully
executed and verified.
Ex.No:4
Date: VI EDITOR
________________________________________________
Aim:
To execute and verify the commands used to work in visual editor.
COMMAND DESCRIPTION
vi filename Used to open a file which exists in the current
directory, or to create if it does not already exist.
vi file1 file2 file3 Vi interprets files to open
vi +# filename Instructs vi to open the file and go to specified line
View filename Instructs vi to open the file for editing
COMMAND DESCRIPTION
hjkl Move cursor one space or line left, down, up,
right.
0(zero) Moves cursor to the beginning of whatever line it
is on.
^(caret) Like zero, it moves the cursor to the beginning of
the current line.
$ Moves cursor to the end of the file.
#G Moves the cursor to the line specified by the
number in front of the G.
W Moves cursor to the last line of the file.
E Moves cursor forward to the next end of a word.
b Moves cursor backward to the previous beginning
of a word.
- Positions cursor at the beginning of prior line.
+ Positions cursor at the beginning of next line.
L Positions cursor at lowest line displayed on the
screen.
M Positions cursor at midpoint on the screen
H Positions cursor at the highest line on the screen.
COMMAND DESCRIPTION
fb Moves cursor forward on the line to next b
Fb Moves cursor backward on the line to previous
/word Moves cursor forward through text to next instance of
word
?word Moves cursor backward and through text prior instance
of word
COMMAND DESCRIPTION
:set number Includes line numbers as part of the screen display.
:set nonumber Removes line numbers on screen.
:set all Shows all the set options available.
COMMAND DESCRIPTION
u Undo
:redo Redoes the text change.
U Undo reverse all text changes made to current line.
COMMAND DESCRIPTION
a Inserts text starting text at the end of the line
A Starts adding text at the end of the line
i Starts adding text to the left of the cursor
I Inserts text at the beginning of the line
o Opens a line below the cursor.
O Opens the line above the cursor.
Esc Used to enter Append/Insert Mode, to leave append
mode and return to vi command mode.
COMMAND DESCRIPTION
cw Changes only the word under the cursor
s Substitutes for a single character.
S Substitudes for an entire line.
cc Subsitutes for an entire line.
r Replaces the one character under the cursor with
the next character typed.
C Changes the rest of the line.
COMMAND DESCRIPTION
cw Changes only the word under the cursor
s Substitutes for a single character.
S Substitudes for an entire line.
cc Subsitutes for an entire line.
r Replaces the one character under the cursor with
the next character typed.
C Changes the rest of the line.
COMMAND DESCRIPTION
yy Copies current line into memory buffer.
dd Deletes the current line and places it in the same
memory buffer used by yy
yw Copies current word into memory buffer.
dw Deletes the current word and places it in the same
memory buffer used by yw
p Puts whatever lines are in memory into the file
starting below the cursor line
P Puts the copied or deleted lines just above the cursor
line.
COMMAND DESCRIPTION
j Joins the next line with the current line.
COMMAND DESCRIPTION
:wq Writes changes made to a file during that editing
session into the disk, quits work on the file and
returns to the shell.
:q Quits work on a file if no changes or additions have
been made.
:w filename Writes the buffer version of the file to a new line.
Result:
Thus the commands used to work in visual editor are successfully
executed and verified.
Ex.No:5
Date: AREA OF CIRCLE
________________________________________________
Aim:
To find the area of the circle using shell program.
Algorithm:
Program:
Result:
Thus the program to find the area of circle was successfully executed
and verified.
Ex.No:6
Date: SUM OF TWO NUMBERS
________________________________________________
Aim:
To write a shell program to find sum of two numbers.
Algorithm:
Program:
Result:
Thus the program to find the sum of two numbers was successfully
executed and verified.
Ex.No:7
Date: BIGGEST NUMBER
________________________________________________
Aim:
Algorithm:
Program:
Result:
Thus the program to find biggest of two numbers was successfully
executed and verified.
Algorithm:
1. Read a number a.
2. Calculate s= % 2.
3. Check whether a is equal to 0
4. If s=0 print given no is even
5. Else print given no is odd.
Program:
Result:
Thus the program to find given no is even or odd was successfully
executed and verified.
Ex.No:9
Date: CALCULATING FACTORIAL OF A GIVEN NUMBER
________________________________________________
Aim:
To find the factorial of a given number using shell program.
Algorithm:
Program:
Result:
Thus the program to find the factorial of a given number using shell
program was successfully executed and verified.
Algorithm:
Program:
Result:
Thus the program to find the Fibonacci series using shell program
was successfully executed and verified.
Algorithm:
Program:
Result:
Thus the program to find the sum of digits of number was
successfully executed and verified.
Aim:
To check whether the given number is prime number or not using
shell program.
Algorithm:
Program:
echo enter a number
read n
i=2
while [ $i -lt $n ]
do
r=`expr $n % $i `
if [ $r -eq 0 ]
then
echo the given number is not prime
exit
else
i=`expr $i /+ 1`
fi
done
echo the given number is prime
Result:
Thus the program to check whether given number is prime was
successfully executed and verified.
Algorithm:
1. Read the number n.
2. assign p=$n and c=0
3. Display p.
4. Check n is not equal to 0.
5. Then separate the digits of the numbers by dividing the number by
10.
6. the remainder value is cubed and it is added to the sum value and
display the value c
7. Calculate n= n/10.
8. if p is equal to c
9. Then given number is Armstrong number else it’s not Armstrong
number.
Program:
echo enter the number
read n
p=$n
c=0
echo $p
while [ $n -ne 0 ]
do
b=`expr $n % 10`
c=`expr $b \* $b \$b \+ $c`
n=`expr $n / 10`
done
if [ $p -eq $c ]
then
echo the number is armstrong
else
echo the number is not armstrong
fi
Result:
Thus the program to check whether given number is Armstrong
was successfully executed and verified.
Algorithm:
Program:
Result:
Thus the program to generate multiplication table using shell
program was successfully executed and verified.
Ex.No:15
Date: RAISING NUMBER TO A POWER
________________________________________________
Aim:
To find the value of one number raised to the power using shell
program.
Algorithm:
1. Get the number and power values from the user.
2. Assign i=1and prod=1.
3. if i<power then calculate prod=prod*number
4. increment i by i
5. Print the result.
Program:
Result:
Thus the program to find the value of one number raised to the
power was successfully executed and verified.
Ex.No:16
Date: INTERCHANGING TWO NUMBERS
________________________________________________
Aim:
To write a program to interchanging two numbers using shell
program.
Algorithm:
Program:
echo enter the number
read a
read b
temp=$a
a=$b
b=$temp
echo the number after swaping is $a,$b
Result:
Thus the program to interchange two numbers was successfully
executed and verified.
Program:
echo enter the number to reverse
read n
b=0
c=0
while test $n –gt 0
do
b=`expr $n % 10`
c=$(echo “($c * 10) + $b” / bc
n=`expr $n / 10`
done
echo $c is a reverse number
Result:
Thus the program to reverse the given number was
successfully executed and verified.
Algorithm:
1. Get the radius of the circle.
2. Pass the value of r as argument.
3. Call the function in which area and perimeter is being
calculated.
4. The area, perimeter is returned to the main program.
5. Display the results.
Program:
#include <stdio.h>
main()
{
float r, area, p;
printf(“Enter the radius of the circle”);
scanf(“%f”,&p);
printf(“Area = %f”,area);
printf(“Perimeter = %f”,p);
}
float comp(float *r1,float &p1)
{
float a;
a = (float) 3.1415 *r, *r1;
*p = (float) 3.1415 *2 *r;
return a;
}
Result:
Thus the program in C, to find the area and perimeter of the circle
was successfully executed and verified.
Ex. No: 19
Date: ADDITION OF TWO NUMBERS USING POINTER.
________________________________________________
Aim:
To write a program to add numbers given by the user through the
use of pointers without using variable directly.
Algorithm:
Program:
#include <stdio.h>
main( )
{
int a,b,c;
int *pa,*pb,*pc;
pa=&a;
pb=&b;
pc=&c;
printf(“\n Enter the first number”);
scanf(“%d”,pa);
printf(“\n Enter the second number”);
scanf(“%d”,pb);
*pc=*pa+*pb;
Printf(“\n The sum is %d”,*pc);
}
Result:
Thus the program to add numbers given by the user through the
use of pointers without using variable directly was successfully executed
and verified.
Algorithm:
Program:
#include<stdio.h>
void swap (int*,int*);
main()
{
int x=5,y=10;
clrscr();
printf(“The values before swapping are %d %d”,x,y);
swap(&x,&y);
printf(“The values after swapping are %d %d”,x,y);
getch();
}
void swap (int *a,int*b)
{
Int t;
t = *a;
*a=*b;
*b=t;
}
Result:
Thus the program to swap given two numbers was successfully
executed and verified.
Algorithm:
1. Declare the file pointer
2. Open the file in write mode
3. Enter how many addresses to be added ‘n’.
4. Get the name and address of ‘n’ persons.
5. Open the file in read mode.
6. Display the name and address, until end of file.
7. Close the file.
Program:
#include <stdio.h>
main()
{
int n,i;
char pname[20];
char address[60];
FILE*ka;
clrscr();
ka=fopen(“address.txt”,”w”);
Printf(“\n how many address to be added”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%s %s”,pname,address);
fprintf(ka,”%s%s\n”,pname,address);
}
fclose(ka);
ka=fopen(“address.txt”,”r”);
printf(“Address Book \n”);
while((fscanf(ka,”%s%s”,pname,address) != EOF))
{
printf(“\n%s\t%s\n”,pname,address);
fclose(ka);
}
Result:
Thus the program to create and maintain a file for address book
was successfully executed and verified.
Algorithm:
1. Get the strings using gets ().
2. Compare the strings using the function strcmpi().
3. If the value is greater than 0, interchange the position of the
strings
4. Display the result.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define COLS 50
main()
{
charw[10] [COLS];
char *a[10].*t;
int i;
for(i=0;i<10;i++)
{
gets(w[i]);
a[i]=w[i];
}
printf(“Before sorting the strings are \n”);
for(i=0;i<10;i++)
puts(w[i]);
for(i=0;i<n-1;I++)
for(j=i+1;j<n;j++)
if(strcmpi(a[i],a[j]>0)
{
t= a[i];
a[i]=a[j];
a[j]=t;
}
}
Result:
Thus the program to sort the given strings using pointers was
successfully executed and verified.
Algorithm:
1. Open the file in the read mode.
2. Get each character from the file the end of file.
3. Increment the counter variable for the character and number
of lines is determined.
4. While the end of the file is encountered, the loop gets
terminated.
5. The number of character and lines are displayed.
Program:
#include<stdio.h>
void main()
{
FILE *fopen(),*fp;
Int ch, nc, nlines;
Char fname[30];
nlines=0;
nc=0;
printf(“enter file name”);
fflish(stdin(;
scanf(“%s”,fname));
fp=fopen(fname,”r”);
if(fp==NULL)
{
printf(“cannot open the file %s for reading”,fname);
exit(0)
}
ch=getc(fp);
while(ch!=EOF)
{
if(ch==’\n’)
nlines++
nc++;
ch=getc(fp);
}
fclose(fp);
if(nc!=0);
{
printf(There are %d characters in %S \n”,nlines);
}
else
printf(“file %S is empty”,filename);
}
Result:
Thus the program to count the number of characters and lines was
successfully executed and verified.
Algorithm:
1. Create the pointer.
2. Create an object for accessing the directory entries.
3. Open the directory, specified in the command line.
4. Read the directory entries till the null pointer
5. Display the directory name and directory entries.
6. Close the directory.
Program:
#include<stdio.h>
#include<dirent.h>
main(int argc, char **argv)
{
DIR *dp;
struct dirent *link;
dp=opendir(argv[1]);
printf(“\n contents of the directory %s are \n ,argv[1]);
while ((link=readdir(dp))!=0)
printf(“%s,link->d_nmade);
closedir(dp);
printf(“\n);
}
Result:
Thus the program to simulate ls command was successfully
executed and verified.
Aim:
To write a C program, to create a new process using fork system
calls.
Algorithm:
1. Fork a new child.
2. Check the value returned by the new process.
3. If the value returned by the fork is negative (-1), then display
that there is an error in process creation.
4. If the value returned by fork is equal to 0, then print that it is a
parent process and get the process is using getid() system call
and display it.
5. If the value returned by fork is equal to 0, then print that it is a
child process and get the process id using getid() system call
and display it.
Program:
#include<stdio.h>
#include<unistd.h>
main()
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf(“error in process creation\n”)
exit(1);
}
if(pid !=0)
{
Pid1=getpid(0;
printf(“\n the parent process id is %d \n”,pid1);
}
else
{
Pid2=getpid();
Printf(“\n the child process id is %d \n”,pid2);
}
}
Result:
Thus the program to process creation using fork system call was
successfully executed and verified.
Aim:
To write a C program to test the system calls like signal, alarm, kill,
pause and sleep.
Algorithm:
Program:
#include<signal.>
#include<unistd.h>
void alarm_handler(int sig)
{
printf(“Alarm has gonr off”);
printf(“hello”);
}
int main()
int pid;
printf(“Alaram Application starts”);
if((pid=fork())==0)
{
sleep(0);
kill(getpid(),SIGNALRM);
exit(0);
}
printf(“Waiting for alaram to go off”);
signal(Signalrm,ALARM_HANDLER);
alarm(5);
pause(0;
printf(“done!”);
exit(0);
}
Result:
Thus the program signal handling was successfully executed and
verified.
Aim:
To write a C program for interprocess communication using pipe.
Algorithm:
1. Create a pipe using pipe () system call and check for creation
error.
2. Fork a new child using fork () system call.
3. Write the data into the pipe by closing the read end of the pipe.
4. For the other process, read the data from the pipe by closing the
write end of the file.
Program:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main (void)
int fd[2],nbytes;
pid_t childpid;
char string[]=”hello world!\n”;
char readbuffer[80];
if(pipe(fd)==-1)
{
printf(“error in pipe creation”);
exit(1);
}
if((childpid=fork()0==-1)
{
perror(“fork”);
exit(1);
}
if(childpid==0)
{
prinff(“child process\n”);
close(fd[0]);
printf(“writing message into the pipe”);
write (fd[1],string,strlen(string));
}
else
{
printf(“parent process”);
close(fd[1]);
nbytes=read(fd(0),readbuffer, sixeof(readbuffer));
printf(“received strings”)
printf(“%s”,readbuffer);
}
return (0);
}
Result:
Thus the program for inter process communication using pipe was
successfully executed and verified.