lab1-2
a)Display all the words which are entered as Command line arguments
$vi prog2a.sh
echo $*
b)Change permissions of files in PWD as rwx for users
$chmod u-rwx *
$vi prog2b.sh
chmod u+rwx *
c)To print list of all sub directories in the current directory
$vi prog2c.sh
ls-l |grep ^d |cut -c 57-
d)Program to check whether the given year is leap year or not
if [ $# -eq 0 ]
then
year=`date |cut -c 25-`
else
year=$1
fi
if [ `cal 2 $year |wc -w` -eq 38 ]
then
echo Leap Year
else
echo Not a Leap Year
fi
e)Program which takes two file names as arguments.If their contents are same then delete second file
if [ $# -ne 2 ]
then
echo Inproper Arguments
exit
fi
cmp -s $1 $2
code=$?
if [ $code -eq 0 ]
then
echo Files are identical
echo $2 File is deleting
rm $2
elif [ $code -eq 1 ]
then
echo Files are not identical
else
echo Read permission is denied for files
fi
lab 1-3
Write Shell scripts for the following
a)To print the given number in the reverse order
echo Enter a number
read n
rev=0
while [ $n -ne 0 ]
do
r=`expr $n % 10`
s=`expr $rev \* 10`
rev=`expr $s + $r`
n=`expr $n / 10`
done
echo Reverse of a number is $rev
b)To print first 25 Fibonacci numbers
count=2
a=0
b=1
echo The first 25 Fibonacci numbers are
echo $a
echo $b
while [ $count -ne 25 ]
do
c=`expr $a + $b`
a=$b
b=$c
echo $c
count=`expr $count + 1`
done
c)To print the prime numbers between specified range
echo Enter lower boundary
read lb
echo Enter upper boundary
read ub
echo The prime numbers between $lb and $ub
if [ $lb -lt 2 ]
then
lb=2
fi
count=0
while [ $lb -le $ub ]
do
flag=1
j=$lb
i=2
while [ $i -lt $j ]
do
if [ `expr $j % $i` -eq 0 ]
then
flag=0
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 1 ]
then
count=`expr $count + 1`
echo $j
fi
lb=`expr $lb + 1`
done
echo There are $count prime numbers
d)To print first 50 prime numbers
echo The first 50 prime numbers are
count=0
n=2
while [ $count -ne 50 ]
do
flag=1
i=2
j=$n
while [ $i -lt $j ]
do
if [ `expr $j % $i` -eq 0 ]
then
flag=0
break
fi
i=`expr $i + 1`
done
if [ $flag -eq 1 ]
then
count=`expr $count + 1`
echo $j
fi
n=`expr $n + 1`
done
Write Shell programs for the following
lab 1-4
Write Shell scripts for the following
a)To delete all lines containing the word UNIX in the files supplied as arguments
if [ $# -eq 0 ]
then
echo Supply Arguments
exit
fi
i=$#
count=1
while [ $count -le $i ]
do
grep -v -i "unix" $1 >temp
mv temp $1
count=`expr $count + 1`
shift
done
b)Write a Menu driven program for the following options
ch=0
while [ $ch -ne 4 ]
do
echo MENU
echo 1.Contents of /etc/paswd
echo 2.List of users who have currently logged in
echo 3.Present Working Directory
echo 4.Exit
echo Choose an option
read ch
case $ch in
"1")
cat /etc/passwd
;;
"2")
who
;;
"3")
pwd
;;
"4")
exit
;;
"*")
echo Enter correct option
;;
esac
done
c)For sorting,searching and insertion,deletion of elements in the list
if [ $# -eq 0 ]
then
echo Supply Arguments
exit
fi
ch=0
while [ $ch -ne 5 ]
do
echo MENU
echo 1.Sort 2.Search 3.Insert 4.Delete 5.Exit
echo Choose an option
read ch
case $ch in
"1")
sort $1 >temp
mv temp $1
cat $1
;;
"2")
echo Enter city name
read city
count=`grep $city $1 |wc -l`
if [ $count -ne 0 ]
then
echo $city is found in the file $1
else
echo $city is not found in the file $1
fi
;;
"3")
echo Enter city name
read city
echo Enter position
read pos
count=`grep $city $1 |wc -l`
if [ $count -ne 0 ]
then
echo $city is already inserted in the file $1
else
sed -e "${pos}i $city" $1 >temp
mv temp $1
fi
cat $1
;;
"4")
echo Enter city name
read city
count=`grep $city $1 |wc -l`
if [ $count -eq 0 ]
then
echo $city not found in the file $1
else
sed -e "/\<$city\>/d" $1 >temp
mv temp $1
fi
cat $1
;;
"5")
exit
;;
"*")
echo Invalid Option
;;
esac
done
lab cycle 2
1)Write a progrma to copy one file contents into another file using unbuffered I/O
#include
main(int argc,char *argv[])
{
int fd1,fd2,n;
char buf;
fd1=open(argv[1],O_RDONLY);
if(fd1==-1)
{
perror(argv[1]);
exit(0);
}
fd2=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600);
if(fd2==-1)
{
perror(argv[2]);
exit(0);
}
while((n=read(fd1,&buf,1))>0)
write(fd2,&buf,1);
close(fd1);
close(fd2);
}
2)Program to create two processes to run a loop in which one process adds all the even numbers and the other adds all the odd numbers
main()
{
int i,n,sum=0,pid;
printf("Enter a number :");
scanf("%d",&n);
pid=fork();
if(pid>0)
{
for(i=0;i<=n;i+=2)
sum+=i;
printf("Sum of even numbers is %d\n",sum);
}
else if(pid==0)
{
for(i=1;i<=n;i+=2)
sum+=i;
printf("Sum of odd numbers is %d\n",sum);
}
else
perror("FORK");
}
3)Program to create two process 'i' and sends data to process 'j', prints the same after receiving it.(Hint::use vfork())
#include
#include
main()
{
int pid;
char data[50];
pid=vfork();
if(pid==0)
{
printf("Enter the Data\n");
gets(data);
//scanf("%[^\n]",data);
exit(0);
}
else
{
printf("\nThe Data is\n");
puts(data);
}
}
4)Program to demonstrate Orphan process.
main()
{
int pid;
pid=fork();
if(pid>0)
printf("I am parent with PID=%d ,PPID=%d\n",getpid(),getppid());
else if(pid==0)
{
printf("I am child with PID=%d ,PPID=%d\n",getpid(),getppid());
sleep(20);
printf("I am child with PID=%d ,PPID=%d\n",getpid(),getppid());
}
else
perror("FORK");
printf("PID=%d terminates\n",getpid());
}
5)Program which demonstrates how to avoid Zombie using wait().
main()
{
int pid,childpid,status;
pid=fork();
if(pid>0)
{
printf("I am parent with PID=%d\t,PPID=%d\n",getpid(),getppid());
childpid=wait(&status);
printf("A child with PID=%d terminates with exit code %d\n",childpid,status>>8);
}
else if(pid==0)
{
printf("I am child with PID=%d\t,PPID=%d\n",getpid(),getppid());
exit(20);
}
printf("PID=%d terminates\n",getpid());
}
6)Program which demonstrates deadlock between two processes.
#include
main()
{
int p1fd[2],p2fd[2],i;
char buf[50];
pipe(p1fd);
pipe(p2fd);
if(fork()==0)
{
close(p1fd[0]);
close(p2fd[1]);
read(p1fd[0],"hello",6);
write(p2fd[1],"hello",6);
write(1,buf,6);
printf("\n");
}
else
{
close(p1fd[1]);
close(p2fd[0]);
read(p1fd[0],buf,6);
for(i=0;i<6;i++)
buf[i]=toupper(buf[i]);
write(p2fd[1],buf,6);
}
}
7)Program on Inter process communication using pipes and shared memory
//Unnamed Pipes
#include
main(int argc,char *argv[])
{
int fd[2];
pipe(fd);
if(fork()!=0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp(argv[1],argv[1],NULL);
perror("Unnamed Pipe Parent");
}
else
{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("Unnamed Pipe Child");
}
}
//Named Pipes::Reader
#include
#include
main()
{
int fd;
char str[100];
unlink("mypipe");
mknod("mypipe",S_IFIFO,0);
chmod("mypipe",0600);
fd=open("mypipe",O_RDONLY);
while(readline(fd,str))
printf("%s\n",str);
close(fd);
}
int readline(int fd,char *str)
{
int n;
do
{
n=read(fd,str,1);
}while(n>0 && *str++!=NULL);
return (n>0);
}
//Named Pipes::Writer
#include
main()
{
int fd,msglen,i;
char msg[100];
sprintf(msg,"Hello PID is %d\n",getpid());
msglen=strlen(msg)+1;
do
{
fd=open("mypipe",O_WRONLY);
if(fd==-1)
sleep(1);
}while(fd==-1);
for(i=0;i<3;i++)
{
write(fd,msg,msglen);
sleep(3);
}
close(fd);
}
//Shared Memory
#include
#include
#include
main()
{
int pid;
int *shared;
int shmid;
shmid=shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT|0600);
if(fork()==0)
{
shared=shmat(shmid,(void *)0,0);
printf("Child Pointer is %p\n",shared);
*shared=1;
printf("Child values is %d\n",*shared);
sleep(2);
printf("Child Value is %d\n",*shared);
}
else
{
shared=shmat(shmid,(void *)0,0);
printf("Parent pointer is %p\n",shared);
printf("Parent value is %d\n",*shared);
sleep(1);
*shared=42;
printf("Parent value is %d\n",*shared);
sleep(5);
shmctl(shmid,IPC_RMID,0);
}
}
8)Create a semaphore operation on a shared file for write but not read
#include
#include
#define SEMKEY 6891
#define SEQ_NO_FILE "sample"
#define MAXBUF 32
#define PEMS 0666
static struct sembuf op_lock[2]={{0,0,0},{0,1,0}};
static struct sembuf op_unlock[1]={0,-1,IPC_NOWAIT};
int semid=-1;
my_lock()
{
if(semid<0)
if((semid=semget(SEMKEY,1,IPC_CREAT|PEMS))<0)
{ perror("semget"); exit(0); }
if((semop(semid,&op_lock[0],2))<0)
perror("semop");
}
my_unlock()
{
if((semop(semid,&op_unlock[0],1))<0)
perror("semop");
}
main()
{
int fd,i,n,pid,seqno;
char buf[MAXBUF+1];
pid=getpid();
if((fd=open(SEQ_NO_FILE,2))<0)
{ perror("open"); exit(1); }
for(i=0;i<5;i++)
{
my_lock();
lseek(fd,0L,0);
if((n=read(fd,buf,MAXBUF))<=0)
perror("read");
buf[n]='\0';
sscanf(buf,"%d\n",&seqno);
printf("pid=%d,seqno=%d\n",pid,seqno);
seqno++;
sprintf(buf,"%3d",seqno);
lseek(fd,0L,0);
n=sizeof(buf);
if(write(fd,buf,n)!=n)
perror("write");
sleep(3);
my_unlock();
}
}
9)Client/Server Socket & Internet Programming
//Socket::Client
//Chef Server
#include
#include
#include
#include
main()
{
int serverfd,clientfd,serverlen,clientlen;
struct sockaddr_un serveraddr;
struct sockaddr_un clientaddr;
struct sockaddr* serversockaddrptr,* clientsockaddrptr;
signal(SIGCHLD,SIG_IGN);
serversockaddrptr=(struct sockaddr *) &serveraddr;
serverlen=sizeof(serveraddr);
clientsockaddrptr=(struct sockaddr *) &clientaddr;
clientlen=sizeof(clientaddr);
serverfd=socket(AF_UNIX,SOCK_STREAM,0);
serveraddr.sun_family=AF_UNIX;
strcpy(serveraddr.sun_path,"recipe");
unlink("recipe");
bind(serverfd,serversockaddrptr,serverlen);
listen(serverfd,5);
while(1)
{
clientfd=accept(serverfd,clientsockaddrptr,&clientlen);
if(fork()==0)
{
writerecipe(clientfd);
close(clientfd);
exit(0);
}
else
close(clientfd);
}
}
writerecipe(fd)
{
static char* line1="RAMANUJA_AKASH";
static char* line2="GUNTUR";
write(fd,line1,strlen(line1)+1);
write(fd,line2,strlen(line2)+1);
}
//Cook Client
#include
#include
main()
{
int chieffd,clientfd,serverlen,result;
struct sockaddr_un serveraddr;
struct sockaddr* serversockaddrptr=(struct sockaddr *)&serveraddr;
serverlen=sizeof(serveraddr);
clientfd=socket(AF_UNIX,SOCK_STREAM,0);
serveraddr.sun_family=AF_UNIX;
strcpy(serveraddr.sun_path,"recipe");
do
{
result=connect(clientfd,serversockaddrptr,serverlen);
if(result==-1)
sleep(1);
}while(result==-1);
readrecipe(clientfd);
close(clientfd);
exit(0);
}
readrecipe(fd)
{
char str[200];
while(readline(fd,str))
printf("%s\n",str);
}
int readline(int fd,char *str)
{
int n;
do
{
n=read(fd,str,1);
}while(n>0 && *str++!=NULL);
return (n>0);
}
//Client
#include
#include
#include
#define sport 6891
main()
{
int sid;
struct sockaddr_in server;
char buf1[32],buf2[32];
if((sid=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("SOCKET");
exit(0);
}
else
printf("socket created successfully\n");
server.sin_family=AF_INET;
server.sin_port=htons(sport);
server.sin_addr.s_addr=INADDR_ANY;
if(connect(sid,&server,sizeof(server))<0)
{
perror("connect");
exit(1);
}
strcpy(buf1,"this is client!!!");
if(send(sid,buf1,sizeof(buf1),0)<0)
{
perror("send");
exit(2);
}
if(recv(sid,buf2,sizeof(buf2),0)<0)
{
perror("recv");
exit(3);
}
printf("message recieved is %s\n",buf2);
}
//Server
#include
#include
#include
#include
#define sport 6891
main()
{
int sid,namelen,new_l,new_sid;
struct sockaddr_in server,client;
char buf[32];
sid=socket(AF_INET,SOCK_STREAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(sport);
server.sin_addr.s_addr=INADDR_ANY;
if(bind(sid,&server,sizeof(server))<0)
{
perror("bind");
exit(0);
}
printf("the assigned port is %d\n",htons(server.sin_port));
if(listen(sid,1)!=0)
{
perror("listen");
exit(1);
}
namelen=sizeof(client);
if((new_sid=accept(sid,&client,&namelen))==-1)
{
perror("accept");
exit(2);
}
if(recv(new_sid,buf,sizeof(buf),0)<0)
{
perror("recv");
exit(3);
}
if(send(new_sid,buf,sizeof(buf),0)<0)
{
perror("send");
exit(4);
}
}
tcp:
#include
#include
#include
#include
main()
{
int sid,namelen,ns;
unsigned long int sport;
struct sockaddr_in server;
struct sockaddr_in client;
char buf[32];
printf("enter connection port number \n");
scanf("%ld",&sport);
sid=socket(AF_INET,SOCK_STREAM,0);
if(sid==1)
{
printf("socket not created\n");
exit(0);
}
else
if("socket created succesfuly\n");
server.sin_family=AF_INET;
server.sin_port=htons(sport);
server.sin_addr.s_addr=inet_addr("10.1.5.56");
if(bind(sid,(struct sockaddr *)&server,sizeof(server))<0)
{
printf("binding error\n");
exit(1);
}
if (listen(sid,1)!=0)
{
printf("listen is failed\n");
exit(2);
}
namelen=sizeof(client);
if(ns=accept(sid,(struct sockaddr *)&client,&namelen)==-1)
{
printf("failed to accept client request\n");
exit(3);
}
if(recv(ns,buf,sizeof(buf),0)==-1)
{
printf("not getting client data\n");
exit(4);
}
printf("enter message from client\n");
puts(buf);
printf("enter message from server\n");
scanf("%s",buf);
if(send(ns,buf,sizeof(buf),0)<0)
{
printf("failed to reply back\n");
exit(5);
}
}
client
#include
#include
#include
#include
main()
{
int sid,namelen,ns;
unsigned long int sport;
struct sockaddr_in server;
struct sockaddr_in client;
char buf[32];
printf("enter connection port number \n");
scanf("%ld",&sport);
sid=socket(AF_INET,SOCK_STREAM,0);
if(sid==1)
{
printf("socket not created\n");
exit(0);
}
else
if("socket created succesfuly\n");
server.sin_family=AF_INET;
server.sin_port=htons(sport);
server.sin_addr.s_addr=inet_addr("10.1.5.56");
if(bind(sid,(struct sockaddr *)&server,sizeof(server))<0)
{
printf("binding error\n");
exit(1);
}
if (listen(sid,1)!=0)
{
printf("listen is failed\n");
exit(2);
}
namelen=sizeof(client);
if(ns=accept(sid,(struct sockaddr *)&client,&namelen)==-1)
{
printf("failed to accept client request\n");
exit(3);
}
if(recv(ns,buf,sizeof(buf),0)==-1)
{
printf("not getting client data\n");
exit(4);
}
printf("enter message from client\n");
puts(buf);
printf("enter message from server\n");
scanf("%s",buf);
if(send(ns,buf,sizeof(buf),0)<0)
{
printf("failed to reply back\n");
exit(5);
}
}
"shell77.c" 57L, 1085C written
[y7it930@rvrlinuxserver y7it930]$ cc shell77.c
[y7it930@rvrlinuxserver y7it930]$