Program On Usage Of Fork System Call

advertisement
CS-753 PROGRAMMING LAB
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
1
CS-753 PROGRAMMING LAB
#SHELL SCRIPT TO CHECK A NUMBER IS PRIME (OR) NOT
clear
echo "Enter any number"
read a
i=2
r=1
while [ $i -lt $a ]
do
r=`expr $a % $i`
if [ $r -eq 0 ]
then
echo "Not Prime"
break
fi
i=`expr $i + 1`
done
if [ $i -eq $a ]
then
echo "PRIME"
fi
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
2
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO PRINT THE FIRST N PRIME NUMBERS
clear
echo Program for prime number series
echo enter the limit
read n
i=2
while [ $i -lt $n ]
do
t=0
k=`expr $i / 2`
j=2
while [ $j -le $k ]
do
if [ `expr $i % $j` = 0 ]
then
t=1
break
fi
j=`expr %j +1`
done
if [ $t -eq 0 ]
then
echo $i
fi
i=`expr $i + 1`
done
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
3
CS-753 PROGRAMMING LAB
#PROGRAM TO SEARCH THE GIVEN WORD IN FILE
echo
read
echo
read
echo
grep
echo
"enter the word name"
word1
"enter the file name"
file1
"searching for the file"
"$word1" $file1
"selected records shown above"
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
4
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO PRINT THE FIBONACCI NUMBERS
echo enter
read n
i=1
f=0
s=1
next=`expr
echo $f
echo $s
echo $next
while [ $i
do
f=$s
s=$next
next=`expr
echo $next
i=`expr $i
done
the number
$f + $s`
-le `expr $n - 3` ]
$f + $s`
+ 1`
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
5
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO PRINT THE FIRST N FIBONNACI NUMBERS
echo enter a number
read n
i=1
f=0
s=1
next=`expr $s + $f`
echo $f
echo $s
while [ $next -lt $n ]
do
f=$s
s=$next
next=`expr $f + $s`
echo $next
done
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
6
CS-753 PROGRAMMING LAB
#PROGRAM TO FIND THE GCD NUMBER
echo "enter the first number"
read u
echo "enter the second number"
read v
g=0
if [ $u -lt $v ]
then
u=`expr $u + $v `
v=`expr $u - $v `
u =`expr $u -$v `
fi
while test $v -ne 0
do
g=`expr $u % $v `
u=$v
v=$g
done
echo "the gcd is $u"
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
7
CS-753 PROGRAMMING LAB
# PROGRAM TO FIND THE FACTORIAL NUMBER
echo "ENTER THE NUMBER TO FIND FACTORIAL OF IT"
read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo Factorial value is $fact
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
8
CS-753 PROGRAMMING LAB
#PROGRAM TO PRINT THE SUM OF N NATURAL NUMBERS
clear
echo "Enter the value of n"
read n
sum=0
i=1
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum is : $sum"
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
9
CS-753 PROGRAMMING LAB
# PROGRAM TO CHECK A SPECIFIED PATTERN IN THE FILE
clear
echo PROGRAM TO CHECK A SPECIFIED PATTERN IN A FILE
echo "enter file name"
read file1
echo "enter pattern"
read pattern
echo "the satisfied pattern is"
grep $path $file1
echo "the left pattern is"
grep -v $path $file1
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
10
CS-753 PROGRAMMING LAB
11
# SHELL SCRIPT TO IMPLEMENT A CALUCLATOR
clear
echo 1.Addition 2.Substraction 3.Division
echo 4.Multiplication q.Exit
echo Enter your choice
read ch
case $ch in
1) echo enter 2 values
read a
read b
sum=`expr $a + $b`
echo Addition of 2 numbers is $sum ;;
2) echo enter 2 values
read a
read b
diff=`expr $a - $b`
echo difference between 2 numbers is $diff ;;
3) echo enter 2 values
read a
read b
mul=`expr $a \* $b`
echo multiplication
between 2 numbers is $mul ;;
4) echo enter 2 values
read a
read b
div=`expr $a / $b |bc `
echo difference between 2 numbers is $div ;;
q) exit ;;
*) echo INVALID NUMBER ;;
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
# PROGRAM TO COPY CONTENTS OF ONE FILE TO ANOTHER BY
CHANGING CASE AND COMPRESSING
clear
echo "enter the source file name"
read file1
echo "enter the destination file name"
read dest1
dd conv=ucase if=$file1 of=$dname
compress $file1
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
12
CS-753 PROGRAMMING LAB
13
#SHELL SCRIPT TO COPY THE CONTENTS OF ONE FILE TO
ANOTHER BY CHANGING THE CASE
echo
read
read
if [
then
echo
fi
echo
echo
read
case
"enter the source and destination files"
fname1
fname2
! -f $fname1 ]
"enter source file doesnot exist"
1.UPPER CASE 2.LOWER CASE
enter your choice
ch
$ch in
1) echo contents of file is converted
echo into UPPER CASE
dd if=$fname1 of=$fnam2 conv=ucase
echo Newely created file name is $fname2
echo contents are all in upper case ;;
2) echo contents of file is
echo LOWER CASE
dd if=$fname1 of=$fname2
echo Newely created file
echo contents are all in
*) echo WRONG CHOICE ;;
convertd
conv=lcase
name is $fname2
lower case ;;
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO COPY RENAME REMOVE AND LINK FILES
Clear
echo 1.copy 2.rename 3.remove 4.link q.exit
echo enter your choice
read ch
case $ch in
1) echo enter source and destination files
read file1
read file2
if [ -f $file1 ]
then
cp $file1 $file2
echo copying source file to destination
echo is success
else
echo source file doesn't exist
echo try again
fi ;;
2) echo Renaming a file
echo enter file name to be rename
read file1
if [ !-f $file1 ]
then
echo file doesnot exist
fi
echo new name to be given
read $file2
mv $file1 $file2
echo file is renamed ;;
3) echo Removing a file
echo enter a file to be deleted
read $f1
if [ -f $f1 ]
then
rm $f1
echo file is deleted
else
echo file to be deleted is doesn't exist
fi ;;
4) echo linking
echo enter a filename to be linked
read $f1
if [ -f $f1 ]
then
echo enter a filename to which hava link
read $f2
ln $f1 $f2
else
echo File doesnot exist
fi ;;
q) exit ;;
*) echo "invalid number" ;;
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
14
CS-753 PROGRAMMING LAB
15
#SHELL SCRIPT TO CALUCLATE AREA & PERIMETER OF RECTANGLW
* CIRCLE * SQUARE *TRIANGLE
while true
do
echo 1.Rectangle 2.Circle 3.Square
echo 4.Triangle q.Exit
echo Enter your choice
read ch
case $ch in
1) echo Enter length and breadth
read length
read breadth
area=`expr $length \* $breadth`
echo Area is $area
a=2
per=`expr $a \* \( $length + $breadth \ )`
echo perimeter is $per ;;
2) echo Enter Radius of circle
echo radius
pi=3.14
area=`echo $pi \* $radius \* $radius | bc`
echo area is $area
per=`echo $pi \* 2 \* radius | bc`
echo perimeter is $per ;;
3) echo Enter side
read side
area=`expr $side \* $side`
echo area is $area
per=`expr 4 \* $side`
echo perimeter is $per ;;
4) echo Enter base and heigth
read base height
echo enter 3 sides
read s1 s2 s3
area=`expr \ ($base \* $height\) / 2`
per=`expr \ ($si + $s2 + $s3 \) / 2`
echo area is $area
echo perimeter is $per ;;
Q/q) break;;
*) echo INVALID NUMBER TRY AGAIN ;;
esac
done
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
16
#SHELL SCRIPT THAT COMPARES TWO DIRECTIRIES AND DELETES
COMMON FILES FROM THE SECOND DIRECTORY
echo "enter the two directories names"
read s
read n
if [ ! -d $s -o ! -d $n ]
then
echo entered direcorys are not valued
fi
cd $s
for first in *
do
cd ..
cd $n
for second in *
do
if cmp $s $n
then
rm $n
fi
done
cd ..
shift
cd $s
done
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
# PROGRAM FOR FILE OPERATION USING MENU
echo " MENU "
echo "1.list of files"
echo "2.process of user"
echo "3.todays date"
echo "4.cal"
echo "5.quit to unix\nEnter your choice:"
read choice
case "$choice" in
1) ls ;;
2) ps ;;
3) date ;;
4) cal ;;
5) exit ;;
*) echo "invalid option"
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
17
CS-753 PROGRAMMING LAB
#PROGRAM TO USAGE OF GREP COMMAND
clear
echo PROGRAM TO DEMONSTRATE THE GREP COMMAND
echo "the usage of grep"
echo "enter any data and press ctrl+d"
cat>file1
echo "type a word to search"
read word
echo "the word is in"
grep $word file1
echo "the word is not in"
grep-v $word file1
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
18
CS-753 PROGRAMMING LAB
#SHELL SCRIPT TO IMPLEMENT THE VARIOUS OPTION OF GREP
FACILITIES
echo
echo
echo
echo
echo
echo
echo
read
case
1.Displays count of the number of occurences of pattern
2.Displays list of filenames only which meet condition
3.Displays line numbers along with the lines
4.Displays all but the lines matching the expression
5.Ignores case for matching
6.EXIT
"ENTER YOUR CHOICE"
ch
$ch in
1) echo ENTER THE PATTERN TO COUNT
read fpat
echo ENTER THE FILE NAME IN WHICH
echo TO BE SEARCH
read fname
if [ ! -f $fname]
then
echo File does not exist
fi
grep -c "$pat" $fname ;;
2) echo ENTER THE FILE TO FIND LAST
read sname
echo ENTER THE DIRECTORY NAME
echo IN WHICH TO BE SEARCH
read dname
if [ -f $sname -o -d $dname ]
then
grep -l "$dname" $dname
else
echo "A FILE (or) directory doesnot exit"
fi ;;
3) echo ENTER THE PATTERN
read spat
echo ENTER THE FILE IN WHICH TO BE SEARCH
read fname
if [ -f $fname ]
then
grep -n "$pat" $fname
else
echo "file doesn't exist"
exit
fi ;;
4) echo ENTER THE PATTERN
read spat
echo ENTER THE FILE NAME
read fname
grep -v "$spat" $fname | tee zfile
cat zfile ;;
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
19
CS-753 PROGRAMMING LAB
5) echo ENTER THE PATTERN
read spat
echo ENTER THE FILENAME
read fname
if [ ! -f $fname ]
then
echo file doesnot exist
exit
fi
grep -i "$spat" $fname ;;
*) echo "YOUR CHOICE IS INVALID"
echo "1 2 3 4 5 6 OR VALID" ;;
6) exit;;
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
20
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO DETERMINE WHETHER A GIVEN YEAR IS A
LEAP OR NOT
echo
read
if [
-ne
then
echo
else
echo
fi
"Enter the year"
year
`expr $year % 4` -eq 0 -a `expr $year %100`
0 -o `expr $year % 400` -eq 0 ]
"IT is a leap year"
"it is not a leap year"
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
21
CS-753 PROGRAMMING LAB
22
# PROGRAM THAT TAKES ANY NUMBER OF ARGUMENTS AND COPIES
ALTERNATE ARGUMENT CONTENTS TO DESTINATION
clear
count=1
if [ $# -lt 1 ]
then
echo "required parameters are less than one"
exit
fi
eval dest=\$ $#
while [ $count -lt $# ]
do
eval cat \$ $count >>$dest
count=`expr $count + 2`
done
echo "the alternate argument contents are copied to dest"
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
# SHELL SCRIPT TO COPY THE CONTENTS OF ONE FILE TO
ANOTHER BY CHANGING THE CASE
echo
read
read
if [
then
echo
fi
echo
echo
read
case
"enter the source and destination files"
fname1
fname2
!-f $fname1 ]
Enter source file doesnot exist
1.Upper case 2.Lower case
Enter your choice
ch
$ch in
1) echo contents of file is converted
echo into Upper case
dd if= $fname1 of=$fname2 conv=ucase
echo newely created file name is $fname2
echo contents are all in Upper case ;;
2) echo contents of file is converted into
echo Lower case
dd if= $fname1 of= $fname2 conv=lcase
echo newely created file name is $fname2
echo contents are all in Lower case ;;
*) echo WRONG CHOICE ;;
esac
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
23
CS-753 PROGRAMMING LAB
PROGRAM FOR SEMAPHORES
#Include<stdio.h>
#Include<sys/ipc.h>
#Include<sys/types.h>
#Include<sys/sem.h>
main()
{
int pid,semid,sval;
struct sembuf sop;
semid = semget ((key-t)10,1,pc,CREAT!0644);
if(semid==_1)
{
Perror(“Semaphore Creation fail”);
Exit(1);
}
semct1(Semid,0,SETVAL,1);
pid=fork();
if(pid==0)
{
sop.sem-num=0;
sop.sem-op=0;
sop.sem-flg=0;
semop(Semid,&sop,1);
sval=(semct1(semid,0,GETVAL,0);
Printf(“\n child Process after semop with semval:%d”,sval);
Else
{
sleep(1);
sop.sem-num=0;
sop.sem-op=-1;
sop.sem-flg=0;
Semop(Semid,&sop,1);
Sval=Semct1(Semid,0,GETVAL,0);
Printf(“parent process after semop with Semval:%d”,sval);
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
24
CS-753 PROGRAMMING LAB
For semid ds structure
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/ipc.h>
main()
{
int semid;
struct semid –ds status;
semid = semget((key-t)25,1,ipc-CREAT!0644);
Semet1(Semid,0,IPC-STAT,&Status);
Printf(“no of Semaphores in set are %d\n”,status.sem-nsems);
Printf(“Owners userid is %u\n”,Status.sem-perm.Uid);
Printf(“Owners groupid is %u\n”,Status.sem-perm.Uid);
Printf(“Creator userid is %u\n”,Status.sem-perm.Uid);
Printf(“Creator userid is %u\n”,Status.sem-perm.Uid);
Printf(“access mode is % o\n”,status.sem-perm.mode);
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
25
CS-753 PROGRAMMING LAB
Produer consumer problem
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/msg.h>
#include<stdio.h>
#include<string.h>
struct sembuf sop;
inf full,empty,mutex;
struct msg
{
long mtype;
char s[15];
}
Pmsg,cmsg;
Char buf[15];
Producer()
{
int item = 0;
while(item!=0)
{
produce-item(item);
down(empty);
down(mutex);
enter-item(item);
up(mutex);
up(full);
item=(item+1)%10;
}
}
Consumer()
{
int item=0;
while(item!=0)
{
down(full);
down(mutex);
remove-item(item);
up(mutex);
up(empty);
consume-item(item);
item-(item+1(%10;
}
}
produce-item(int I)
{
int mquid;
sleep(1);
printf(“enter data for item no %d”,I);
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
26
CS-753 PROGRAMMING LAB
scanf(“%s”,Pmsg.s);
pmsg.mtype=5);
}
enter-item9int I)
{
int mqid;
mqid = msgget((key-t)25,ipc-CREAT:0644);
msgsnd(mqid,&pmsg,sizeof(pmsg.s),0);
printf(“\nitem %d written\n”,I);
}
down(int semid)
{
sop.sem-num=0;
sop.sem-op=-1;
sop.sem-flg=0;
semop(semid,&sop,1);
}
up(int semid)
{
sop.sem-num=0;
sop.sem-op=-1;
sop.sem-flg=0;
semop(semid,&sop,1);
}
remove-item(int I)
{
int mqid;
mqid = msgget((key-t)25,IPC-CREAT:0644);
msgrcv(mqid,&cmsg,sizeof(cmsg.s),5,0);
printf(“\n item %d read from queue\n”;I);
}
Consume-item(int I)
{
Printf(“\n item %d value:%s\n”,I,cmsg.s);
}
main()
{
int pid,mqid;
mutex = semget(ckey-t)27,I,IPC-CREAT:0644);
semct1(mutex,0,SETVAL,1);
empty=(semget (ckey-t)28,I,IPC-CREAT:0644);
semct1(empty,0,SETVAL:5);
full = semget(ckey-t)29,I, IPC-CREAT:0644);
semct1(full,0,SETVAL,0);
mqid=msgget(ckey-t)25, IPC-CREAT:0644);
pid=fork();
if(pid==0)
consumer();
else
producer();
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
27
CS-753 PROGRAMMING LAB
N reader 1 writer problem using message queues with write code
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/shm.h>
int mutex,db,shmid,arc,semid,no,id;
struct sembuf sop;
char *ptr,buf[10];
struct msg
{
long mtype;
char s[10];
} pmsg,cmsg;
writer()
{
int item =0;
while(item!=4)
{
get_data();
down(db);
write_data();
printf(“\nwrite complete”);
up(db);
item=item+1;
}
}
up(int semid)
{
sop.sem-num=0;
sop.sem-op=-1;
sop.sem-flg=0;
semop(semid,&sop,1);
}
down(int semid)
{
sop.sem-num=0;
sop.sem-op=-1;
sop.sem-flg=0;
semop(semid,&sop,1);
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
28
CS-753 PROGRAMMING LAB
write_data()
{
int nread;
printf(“\nmessage entered”);
scanf(“%s”,pmsg.s);
pmsg.mtype=20;
nread=msgsnd(id,&pmsg,sif(pmsg),0);
printf(“\n value of nread %d”,nread);
printf(“n data written”);
}
get_data()
{
printf(“\n writer preparing data in get_data()”);
}
main()
{
int pid;
mutex=semget(ckey-t)41,I,IPC-CREAT:0644);
semct1(mutex,0,SETVAL,1);
db=semget (ckey-t)42,I,IPC-CREAT:0644);
semct1(db,0,SETVAL,I);
id=msgget(ckey-t)4, IPC-CREAT:0644);
if(id>0)
printf(“\n created”);
else
Perror(“\n unsuccessful”);
Writer();
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
29
CS-753 PROGRAMMING LAB
N reader 1 writers problem using message queues
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/shm.h>
int mutex,db,shmid,arc=0,semid,no,id;
struct sembuf sop;
char *ptr,buf[10];
struct msg
{
long mtype;
char s[10];
} pmsg,cmsg;
reader(int 0)
{
int I=0;
down(mutex);
arc++;
if(arc==1)
down(db);
up(mutex);
read_data(0);
down(mutex);
arc--;
if(arc==0)
up(db);
up(mutex);
read-comp(0);
}
up(int semid)
{
sop.sem-num=0;
sop.sem-op=1;
sop.sem-flg=0;
semop(semid,&sop,1);
}
down(int semid)
{
sop.sem-op=-1;
sop.sem-num=0;
sop.sem-flg=0;
semop(semid,&sop,1);
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
30
CS-753 PROGRAMMING LAB
read-data(int n)
int I;
printf(“\n reader %d reading data”,n);
I=msgrcv(id,&cmsg,sizeof(cmsg)20,0);
cmsg.mtype=20;
Printf(“\n number of bits %s,%d”,cmsg.s,I);
}
read-comp(no)
{
printf(“\n reader %d completed reading”,n);
}
main()
{
int pid;
mutex=semget(ckey-t)41,I,IPC-CREAT:0666);
semct1(mutex,0,SETVAL,1);
db=semget (ckey-t)42,I,IPC-CREAT:0666);
semct1(db,0,SETVAL,I);
id=msgget(ckey-t)4, IPC-CREAT:0666);
pid=fork();
if(pid==0)
reader(1);
else
{
pid=fork();
if(pid==0)
reader(2);
else
reader(3);
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
31
CS-753 PROGRAMMING LAB
N reader 1 writer’s problem using shared memory
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/shm.h>
int mutex,db,shmid,arc,semid,no;
struct sembuf sop;
char *ptr,buf;
reader(int 0)
{
int i=0;
while(I!=3)
{
down(mutex);
arc++;
if(arc==1)
down(db);
up(mutex);
read_data(no);
down(mutex);
arc--;
if(arc==0)
up(db);
up(mutex);
read-comp(no);
i=i+1;
}
}
writer()
{
int item =0;
get_data();
down(db);
write_data();
printf(“\nwrite complete”);
up(db);
item=item+1;
}
up(int semid)
{
sop.sem-num=0;
sop.sem-op=1;
sop.sem-flg=0;
semop(semid,&sop,1);
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
32
CS-753 PROGRAMMING LAB
}
down(int semid)
{
sop.sem-op=-1;
sop.sem-num=0;
sop.sem-flg=0;
semop(semid,&sop,1);
}
write_data()
{
int nread;]
char mes[20];
printf(“\n enter data”);
scanf(“%s”,buf);
printf(“\n message entered”);
ptr =(char *)shmat(semid,(char *),0,0);
printf(“\n after shmat”);
nread=read(0,ptr,sizeof(ptr1));
printf(“\n value of read %d”,nread);
printf(“\n data written”);
}
read_data(int no)
{
int I;
printf(“\n reader %d reading data”,no);
I = shmdt((char *)ptr);
Printf(“\n %d”,I);
Printf(“\n %d”,I);
}
get_data()
{
printf(“\n writer preparing data in get_data()”)_;
}
read-comp(no)
{
printf(“\n reader %d completes reading”,no);
}
main()
{
int pid;
mutex=semget(ckey-t)41,I,IPC-CREAT:0666);
semct1(mutex,0,SETVAL,1);
db=semget (ckey-t)42,I,IPC-CREAT:0666);
semct1(db,0,SETVAL,I);
shmid=shmget(ckey-t)1,I, IPC-CREAT:0666);
arc=(int)shmat(shmid,0,0);
pid=fork();
if(pid==0)
printf(“\n readers”);
else
writer();
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
33
CS-753 PROGRAMMING LAB
34
}
SNO
INDEX
PAGE
NO
SHELL SCRIPTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Shell Script to check the Given number is a Prime or not
Shell Script to print the first N Prime Numbers
Program to Search the given word in a file
Shell Script to print the 1st N Fibonacci numbers
Shell Script to print Fibonnaci numbers between given range
M and N
Program to find the GCD Number
Program to find the factorial number
Program to print the sum of n natural numbers
Shell script to implement a calculator
Program to copy contents of one file to another by changing
case and compressing
Program to check specified pattern in a file
Shell Script tp copy the contents of one file to another by
changing the case
Shell script to copy, rename, remove and link files(menu
based)
Shell Script to calculate Area& Perimeter of Rectangle,
Circle, square &Triangle
Shell Script that compares 2 DIR’s and deletes common files
from 2nd dir
1
2
3
4
5
Program to Usage of Grep Command
Shell Script to implement the various options of grep
utility
Shell Script to determine whether a given year is a leap
(or) not
Program that takes any number of arguments and copies
alternate argument contents to dest
Schell Script to copy the contentsof one file to another by
changing the case
17
18
6
7
8
10
11
9
12
13
14,15
16
20
21
22
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
Program On Usage Of Fork System Call
#include<stdio.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf(“child process with pid:%d”,getpid());
printf(“and parent process pid is:%d”,getppid());
}
else
{
sleep(1);
printf(“parent process with pid:%d\n”,getpid());
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
35
CS-753 PROGRAMMING LAB
PROGRAMMING WITH FORK
#include<Stdio.h>
#include<unistd.h>
main()
{
int pid;
char str[10];
pid=fork();
if(pid==0)
{
printf(“enter String”);
scanf(“%s”,str);
}
else
{
wait(1);
printf(“Entered String is”:str);
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
36
CS-753 PROGRAMMING LAB
PROGRAM FOR EXECUTION
#include<stdio.h>
main()
{
printf(“previous processed:%d \n”,getpid());
printf(“previous processed:%d \n”,getppid());
execl(“/home/libm/sam”,”samp”(char *)0);
printf(“this will not be printed”);
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
37
CS-753 PROGRAMMING LAB
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
38
CS-753 PROGRAMMING LAB
Sno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
39
IPC PROCEDURES
Program On Usage Of Fork System Call
Program with fork
Program for exec
Program to show the Existence of Orphan Process
Program for Echo Server Using Pipes
Program using Message Queues
Program for Semaphores
For Semid-ds Structure
For Shared Memory
For Producer Consumer Problem
N readers 1 writers problem using message Queues
(writers)
N readers 1 writers problem using message Queues
(readers)
N readers 1 writers problem using shared Memory (writers)
Program for dining philosophers problem
PAGENO
1
2
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
CS-753 PROGRAMMING LAB
PROGRAM TO SHOW THE EXISTANCE OF ORPHAN PROCESS
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf(“\n child process %d”,getpid());
printf(“\n child parent process %d”,getppid());
sleep(15);
printf(“\n child process %d”,getpid());
printf(“\n childs parent process %d”,getppid());
}
else
{
printf(“parent process %d \n”,getpid());
printf(“parents parent process %d\n”,getppid());
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
40
CS-753 PROGRAMMING LAB
PROGRAM FOR ECHO SERVER USING PIPES
#include<stdio.h>
#include<String.h>
main()
{
int pdes1[2],pdes2[2],pid,pipeid1,pipeid2;
char msg[15];
strcpy(msg,” “);
pipeid1=pipe(pdes1);
if(pipeid1!=-1)
pipeid2=pipe(pdes2);
else
printf(“first pipe creation filled”);
pid=fork();
if(pid==0)
{
close(pdes1[0]);
close(pdes2[1]);
strpy(msg,”hello—pipe”);
write(pdes1[1],&msg,sizeof(msg));
write(pdes2[0],&msg,sizeof(msg));
printf(“\n echoing server response:%s \n”,msg);
}
else
{
close(pdes1[1]);
close(pdes2[0]);
read(pdes1[0],&msg,10);
printf(“\n client input:%s \n”,msg);
strcp(msg,”message read”);
write(pdes2[1],&msg,sizeof(msg));
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
41
CS-753 PROGRAMMING LAB
PROGRAM USING MESSAGE QUEUES
#include<sys/types.h>
#include<sys/msg.h>
#include<string.h>
struct msgbuf
{
long mtype;
char mtext[10];
}msg1;
main()
{
strcut msgbuf msg2;
int mgid,mg;
mgid=fork();
mg=msgget((key_t)232,IPC_create0666);
if(mgid!=0)
{
msg1.mtypr=1;
strcpy(msg1.mtext,”abc_I”);
msgsnd(mg,&msg1,sizeof(msg1),IPC_NOWAIT);
msgrcv(mg,&msg1,sizeof(msg1),IPC_NOWAIT);
printf(“\n Server:%s\n”,msg1.mtext);
}
else
{
msgcrv(mg,&msg2,sizeof(msg2),IPC_NOWAIT);
printf(“\n Client:%s\n”,msg2.mtext);
strcpy(msg2.mtext,”xyz_II”);
msgsnd(mg,&msg2,sizeof(msg2),IPC_NOWAIT);
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
42
CS-753 PROGRAMMING LAB
PROGRAM FOR DINING PHILOSOPHER PROBLEM
#include<stdio.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define n 5
#define left(I-1)%n
#define right(I+1)%n
#define think 0
#define hungry 1
#define eating 2
int mutex,s[n],state[n],shmid,i;
struct sembuf sop;
char *ptr;
philosopher(int i)
{
thinking(i);
take_forks(i);
eat(i);
put_forks(i);
}
thinking(int I)
{
printf(“\n philosopher %d is in %d state”,I,state[I]);
sleep(1);
}
take_forks(int I)
{
down(mutex);
state[I]=hungry;
test(i);
up(mutex);
down(s[i]);
}
put_forks(int I)
{
down(mutex);
state[I]=think;
test(left);
test(right);
up(mutex);}
test(int I)
{
int k;
k=I;
if(I==0)
I=s;
If((state[k]==hungry)&&(state[left]!=eating)&&(state[right]!=eating))
{
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
43
CS-753 PROGRAMMING LAB
state[k]=eating;
up(s[k]);}
eat(int i)
{
printf(“philosopher %d is in %d state \n”, i, state[i]);
printf(“philosopher %d has cmpleted”);
}
up(int semid)
{
sop.sem_num=0;
sop.sem_op=1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
down(int semid)
{
sop.sem_num=0;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
main()
{
int pid,I,key,nread;
mutex=segmet((key_t)4,1,IPC_CREAT|0666);
semctl(mutex,0,SETVAL,1);
for(I=0;I<n;I++)
state[I]=think;
for(I=0:I<n;I++)
{
key=(key_t)1;
s[I]=semget(key,I,IPC_CREATE|0666);
semctl(s[I],0,SETVAL,1);
}
shmid=shmget((key_t)10,1,IPC_CREAT|0666);
ptr=(char *)shmat(shmid,0,0);
printf(“\n ENTERING DATA”);
nread=read(0,ptr,sizeof(ptr));
printf(“\n DATA READ %d”,nread);
pid=fork();
if(pid==0)
philosopher(0);
else
{
pid=fork();
if(pid==1)
philosopher(1);
else
philosopher(2);
}
}
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
44
CS-753 PROGRAMMING LAB
Vignan Institute of Technology & Science Deshmukhi – Advanced UNIX Programming -LAB
45
Download