Sie sind auf Seite 1von 41

CONTENTS

S.NO DATE NAME OF THE PROGRAM PAGE STAFF


NO SIGN
CONTENTS
S.NO DATE NAME OF THE PROGRAM PAGE STAFF
NO SIGN
Ex.No:1
Date: BASIC UNIX COMMANDS
________________________________________________
Aim:
To execute and verify basic Unix commands.

1. Command: cat >


Syntax: cat >
Description: used to create a file.
2. Command: cat
Syntax: cat
Description: used to display the content of the file.
3. Command: cat
Syntax: cat >>
Description: used to add text to an existing file.
4. Command: ls
Syntax: ls
Description: used to list directory contents.
5. Command: ls -l
Syntax: ls -l
Description: lists your files in ‘long format’, which contains lot of useful
Information, eg the exact size of the file who owns the file, who
has the right to look at it and when it was last modified.
6. Command: ls -a
Syntax: ls -a
Description: list all files including the one whose file names begin in a dot,
Which you do not always want to see.
7. Command: date
Syntax: date
Description: used to display the current date.
8. Command: cal
Syntax: cal
Description: used to display the calendar.
9. Command: who
Syntax: who
Description: tells you who’s logged on and where they are coming from.

10. Command: uname


Syntax: uname
Description: used to display name of the machine. Command: date
11. Command: pwd
Syntax: pwd
Description: tells you where you currently are. Command: uname
12. Command: head
Syntax: head
Description: displays the first ten lines of the file.
13. Command: tail
Syntax: tail
Description: displays the last ten lines of the file.
14. Command: echo
Syntax: echo
Description: displays or echo’s its Command: who
15. Command: grep
Syntax: grep
Description: used to search for information in a file or files.
16. Command: sort
Syntax: sort
Description: used to sort a file Command: echo
17. Command: clear
Syntax: clear
Description: used to clear the screen.
18. Command: telnet
Syntax: telnet
Description: to login to another machine.
19. Command: look
Syntax: look
Description: display lines beginning with the given string.

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

ACCESS CLASS OPERATOR ACCESS TYPE


u (user) + (add access) r (read)
g (group) - (remove access) w (write)
o (other) =(set exact access) x (execute)

10. Difference in access permissions for files and folders.

ACCESS FILE FOLDER


TYPE
Read If the file contents can be If the directory listing can
read. be obtained.
Write If the user or process can If the user or process can
write to the file (change its change directory contents
contents). somehow: create new or
delete existing files in the
directory or rename files.
Execute If the file can be executed. If user or process can
access the directory that is
go to it.

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.

1. STARTING THE VISUAL EDITOR FROM THE SHELL:

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

2. CURSOR MOVING COMMANDS:

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.

3. CURSOR POSITIONING COMMANDS:

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

4. DISPLAY ADJUSTING COMMANDS:


COMMAND DESCRIPTION
CTRL-D Scrolls the cursor down a block of text in a file.
CTRL-U Scrolls the cursor up a block of text in a file.
CTRL-F Displays the next screenful of text in a file.
CTRL-B Displays the previous screenful of text in a file.

5. SETTING DISPLAY OPTIONS:

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.

6. TEXT DELETING COMMANDS:


COMMAND DESCRIPTION
dd Deletes cursor line of text
#dd Deletes # number of lines of text.
dw Deletes one word frm text
#dw Deletes # number of words from text.
x Deletes one character from the text.
#x Deletes # number of words from text.
7. U
NDO COMMANDS:

COMMAND DESCRIPTION
u Undo
:redo Redoes the text change.
U Undo reverse all text changes made to current line.

8. ADDING TEXT TO A FILE:

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.

8. CHANGING TEXT IN A FILE:

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.

9. CHANGING TEXT IN A FILE:

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.

10. YANKING AND PUTTING LINES AND WORDS:

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.

11. TEXT MOVING COMMANDS:

COMMAND DESCRIPTION
j Joins the next line with the current line.

12. READING, WRITING, AND QUITTING THE EDITOR:

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:

1. Read the radius r.


2. Assign pi=3.
3. Calculate area s = pi*r*r
4. Display the area

Program:

echo To find the radius of the circle


echo enter the number
read r
pi=3
s=`expr $pi \* $r \* $r`
echo the area of the circle $s

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:

1. Read two numbers a, b.


2. Calculate c= a+b
3. Display the sum of two numbers c.

Program:

echo to find addition of two numbers


read a
read b
c=`expr $a \+ $b`
echo the addition of two number is $c

Result:
Thus the program to find the sum of two numbers was successfully
executed and verified.

Ex.No:7
Date: BIGGEST NUMBER
________________________________________________
Aim:

To write a shell program to find biggest of two numbers.

Algorithm:

1. Read two numbers a,b.


2. Check whether a is greater than b or not.
3. If a is greater than b print ‘a’ is biggest number
4. Else print ‘b’ is biggest number.

Program:

echo enter two numbers


read a
read b
if [ $a -gt $b ]
then
echo $a is biggest number
else
echo $b is biggest number
fi

Result:
Thus the program to find biggest of two numbers was successfully
executed and verified.

Ex.No:8 CHECKING THE NUMBER IS EVEN OR ODD


Date:
________________________________________________
Aim:
To write a shell program to find given number is even or odd.

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:

echo enter a number


read a
s=`expr $a % 2`
if test $s -eq 0
then
echo given no is even
else
echo given no is odd
fi

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:

1. Read the number.


2. Initialize the value of i and factorial f to 1.
3. While the value of i is less than the number find the value of factorial
by multiplying I and f.
4. Increment the value of i by 1.
5. Display the result.

Program:

echo to find factorial number


read n
i=1
fact=1
if test $n -eq 0
then
echo factorial is 1
else
while test $i -le $n
do
fact=`expr $fact \* $i`
i=`expr $i \+ 1`
done
echo “the factorial of $n is $fact”
fi

Result:
Thus the program to find the factorial of a given number using shell
program was successfully executed and verified.

Ex.No:10 GENERATING FIBONACCI SERIES


Date:
________________________________________________
Aim:
To find the Fibonacci series using shell program.

Algorithm:

1. Get the limit n from the user.


2. Assign a=-1, b=1 and s=0.
3. If sis less than n then s =a+b and assign a=b and b=s.
4. Print the value of s.
5. Repeat step 3.

Program:

echo “ enter the value of n ”


read n
a= -1
b=1
c=0
d=1
while test $a -le $n
do
c= `expr $a \+ $b`
a=$b
b=$c
d= ` expr $d \+ 1`
echo $c
done

Result:
Thus the program to find the Fibonacci series using shell program
was successfully executed and verified.

Ex.No:11 FINDING SUM OF DIGITS OF A NUMBER


Date:
________________________________________________
Aim:
To find the sum of digits of a number using shell program.

Algorithm:

1. Read the number.


2. Check whether the number is greater than 0.
3. Then divide the number by 10 and get the reminder.
4. Add the reminder to the total and rearrange the number.
5. Print the total value.

Program:

echo “ enter the value of n”


read n
s=0
t=1
while test $t -le $n
do
s= `expr $s \+ $t`
t=`expr $t \+ 1`
done

Result:
Thus the program to find the sum of digits of number was
successfully executed and verified.

Ex.No:12 CHECKING PRIME NUMBER


Date:
________________________________________________

Aim:
To check whether the given number is prime number or not using
shell program.

Algorithm:

1. Read the number n.


2. Initialize i=2.
3. Check whether i is less than n.
4. calculate r=n% i
5. Then check r is equal to 0.
6. Print given number is not prime.
7. Else print given number is prime.

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.

Ex.No:13 CHECKING ARMSTRONG NUMBER


Date:
________________________________________________
Aim:
To check whether given number is Armstrong or not using shell
program.

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.

Ex.No:14 GENERATING MULTIPLICATION TABLE


Date:
________________________________________________
Aim:
To display the multiplication table of a given number using shell
program.

Algorithm:

1. Enter the number for multiplication table a.


2. enter the limit n
3. Assign i = 1.
4. Check i is less than n.
5. Calculate b= a*i
6. display a*i=b

Program:

echo enter the table numbers


read a
echo enter the limit
read n
i=1
while test $i -le $n
do
b=`expr $a \* $i `
echo $a “*” $i = $b
i=`expr $i \+ 1`
done

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:

echo enter the number


read n
echo enter the power
read p
i=1
prod=1
while test $i -le $p
do
prod=’expr $prod “*” $n’
i=`expr $i \+ 1`
done
echo $number raised to $p =$prod

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:

1. Get two numbers a, b.


2. Assign temp = a, a=b, and b= temp.
3. Displayed the swapped numbers.

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.

Ex.No:17 REVERSING A NUMBER


Date:
________________________________________________
Aim:
To reverse the given number using shell program.
Algorithm:
1. Get the number n.
2. Assign b=0, c=0.
3. Test n is greater than 0 then
4. Separate the digits of n then rearrange the digits in reverse
order.
5. Display the reversed number.

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.

Ex. No: 18 FINDING AREA AND PERIMETER OF A CIRCLE


Date:
________________________________________________
Aim:
To write a program in C, to find the area and perimeter of the circle
using pointer.

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:

1. Enter the two values.


2. The variables are pointers
3. Add the pointer variable, so the values are added.
4. Display the result.

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.

Ex. No: 20 SWAPING OF TWO NUMBERS


Date:
________________________________________________
Aim:
To write a program to swap the given two values in the function,
the values are passed as call by address.

Algorithm:

1. Get two values.


2. Interchange two values using temporary variables.
3. The values after swapping are displayed.

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.

Ex. No: 21 CREATING THE ADDRESS BOOK


Date:
________________________________________________
Aim:
To write a C program to create and maintain a file for address book.

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.

Ex. No: 22 SORTING OF STRINGS.


Date:
________________________________________________
Aim:
To write a C program to sort the given strings using pointers.

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.

Ex. No: 23 COUNT THE NUMBER OF CHARACTERS AND LINES


Date:
________________________________________________
Aim:
To write a C program that counts the number of characters and
number of lines in a file.

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.

Ex. No: 24 SIMULATION OF LS COMMAND


Date:
________________________________________________
Aim:
To write a C program to simulate the command ls.

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.

Ex. No: 25 PROCESS CREATION USING FORK SYSTEM CALL


Date:
________________________________________________

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.

Ex. No: 26 SIGNAL HANDLING


Date:
________________________________________________

Aim:
To write a C program to test the system calls like signal, alarm, kill,
pause and sleep.

Algorithm:

1. create a child process using fork() system call


2. If the process id is 0, it is a child process.
3. Wait for a period of time and kill that child process using
kill() system call
4. Give signal to a signal handler to handle the signal.

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.

Ex. No: 27 INTERPROCESS COMMUNICATION USING PIPE


Date:
________________________________________________

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.

Das könnte Ihnen auch gefallen