Sei sulla pagina 1di 19

Program 1:

clear
echo "
PROGRAM TO DEMONSTRATE FILE COPY"
flag=0
while [ 1 ]
do
echo "Enter first file : "
read file1
if [ -f $file1 ]
then
while [ 1 ]
do
echo "Enter second file "
read file2
if [ -f $file2 ]
then
echo "File aleady exist want to over write it(Y/N)"
read yn
else
#cp $file1 $file2
#cat $file1 | cat>$file2
clear
echo "
PROGRAM TO DEMONSTRATE FILE COPY"
flag=0
while [ 1 ]
do
echo "Enter first file : "
read file1
if [ -f $file1 ]
then
while [ 1 ]
do
echo "Enter second file "
read file2
if [ -f $file2 ]
then
echo "File aleady exist want to over write it(Y/N)"
read yn
else
#cp $file1 $file2
#cat $file1 | cat>$file2
cat < $file1 > $file2
flag=1
echo
echo
echo "file copied sucessfully"

echo "file contents of $file2 is : "


cat $file2
break
fi
if [ $yn == "n" -o $yn == "N" ]
then
continue
else
#cp $file1 $file2
cat $file1 | cat>$file2
flag=1
echo "file copied sucessfully"
echo "file contents of $file2 is : "
cat $file2
break
fi
done
break
else
echo "Source file is not found "
continue
fi
if flag=1
then
echo
echo
echo "File copied sucessfully"
echo
break
fi
done
output:
PROGRAM TO DEMONSTRATE FILE COPY
Enter first file :
file4
Enter second file
file8
file copied sucessfully
file contents of file8 is :
veena
[exam11@localhost poornima]$
-------------------------------------------------------------------------

----------------------------------------------------------------------C program 2:
#include<stdio.h>
#include<signal.h>
//#include<sys/types.h>
#include<ctype.h>
//#include<unistd.h>
#include<stdlib.h>
#define MAX_PROCESS 5
int array[MAX_PROCESS];
void header();
void create_process();
void kill_process();
header();
create_process();
kill_process();
exit(0);
}
void header()
{
system("clear");
printf("\n CREATE 5 CHILD PROCESS AND KILL THEM!!!\n\n\n");
return;
}
void create_process()
{
int i;
for(i=0;i<MAX_PROCESS;i++)
{
switch(array[i]=fork())
{
case -1:
printf("fork fails");
break;
case 0:
//printf("\n parent id %d",getppid());
for(;;);
break;
default:
break;
}
}

return;
}
void kill_process()
{
char choice;
int i,j;
printf("\n kill in forward or reverse order f or r:");
scanf("%c",&choice);
printf("\n");
if(choice=='f')
{
printf("killing child in forward direction \n");
for(i=0;i<MAX_PROCESS;i++)
{
printf("\n killing child %d \n",array[i]);
kill(array[i],SIGKILL);
}
}
else
{
printf("\n killing child in backward direction\n");
for(i=MAX_PROCESS-1;i>=0;i--)
{
printf("killing child %d \n",array[i]);
kill(array[i],SIGKILL);
}
}
return;
}
Output:
CREATE 5 CHILD PROCESS AND KILL THEM!!!

child number 0 has PID 4174


child number 1 has PID 4175
child number 2 has PID 4176
child number 3 has PID 4177
child number 4 has PID 4178

kill in forward or reverse order f or r:r


killing child in backward direction
killing child 4178
killing child 4177
killing child 4176
killing child 4175
killing child 4174
[exam8@localhost ~]$
---------------------------------------------------------Program 3:
clear
echo "PROGRAM TO STORE DATA IN A FILE AND COMPRESS IT"
echo "-----------------------------------------------"
while true
do
echo
echo
echo "1. Create a file"
echo "2. Compress the file"
echo 3. uncmpress the file
echo "4. Exit"
echo
echo "Enter your choice"
read ch
case $ch in
1)
echo "Enter file name to create : "
read file1
while true
do
if [ -f $file1 ]
then
echo "File already existing"
echo "you want to overwrite the same file (y/n)?"
read op
if [ $op == "y" -o $op == "Y" ]
then
echo "enter the content to overwrite $file1"
cat>$file1
echo "file overwrite is successfull "
break
else

echo "enter another new file :"


read file1
fi
else
echo "enter the contents to write into a file $file1 "
cat>$file1
echo "file created successfull"
break
fi
done
;;
2)
while true
do
echo "enter the file to compress"
read c
if [ -f $c ]
then
zip zipped $c
echo "$file1 is zipped"
ls -l z*
break
else
echo "$c is not existing"
continue
break
fi
done
;;
3)
echo "enter the file to uncompress"
read u
if [ -f $u ]
then
unzip $u
ls
else
echo "file to uncompress is not existing"
break
fi
;;
4)
exit
;;

esac
done
~
Output:
PROGRAM TO STORE DATA IN A FILE AND COMPRESS IT
----------------------------------------------1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
1
Enter file name to create :
computer
PROGRAM TO STORE DATA IN A FILE AND COMPRESS IT
----------------------------------------------1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
3
enter the file to uncompress
zipped.zip
Archive: zipped.zip
replace nalina1? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace nalina2? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace nalina3? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace mouse? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace file1? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace computer? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: computer
anju
childprocess.sh file15
mca2
nalina3
signals.c wall.sh
a.out
compress.sh
filecopy.sh mouse
nalina5
signals.sh zipped.zip
apple
computer
file.sh
nalina
newfork.c str
PROGRAM TO STORE DATA IN A FILE AND COMPRESS IT
-----------------------------------------------

1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
1
Enter file name to create :
comp
enter the contents to write into a file comp
computer science...file created successfull
1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
2
enter the file to compress
comp
adding: comp (stored 0%)
comp is zipped
-rw-r--r-- 1 exam8 exam8 1583 Nov 21 15:20 zipped.zip
1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
3
enter the file to uncompress
comp
Archive: comp
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of comp or
comp.zip, and cannot find comp.ZIP, period.
anju
childprocess.sh file1
mca1
nalina2 security.sh test1Hello

a.out
comp
file15
mca2
nalina3 signals.c test1.sh
apple
compress.sh
filecopy.sh mouse
nalina5 signals.sh wall.sh
aps
computer
file.sh
nalina
newfork.c str
zipped.zip
childprocess dirnalina
ipc.c
nalina1 pavi.sh str.sh
childprocess.c exefork.c
mango
nalina1.sh pavithra test1
1. Create a file
2. Compress the file
3. uncmpress the file
4. Exit
Enter your choice
4
[exam8@localhost ~]$

-------------------------------------------------------------Program4..
clear
echo "PROGRAM TO DEMONSTRATE TO CONTROL THE PROCESS AND
ASSIGN SECURITY"
echo
echo
echo "Enter file name :"
read file
if [ -f $file ]
then
while true
do
ls -l $file
echo "Assign File Access Permission"
echo "1 Symbolic Mode"
echo "2 Absolute Mode"
echo "3 Exit"
echo "Enter choice : "
read ch
case $ch in
1)
echo "Symbolic Mode"

echo "-------------"
echo "CATEGORY u:USER g:Group o: OWNER"
read category
echo "OPERATION +:ASSIGN -:REMOVE =:ABSOLUTE"
read operation
echo "PERMISSION r:READ w:WRITE x:EXECUTE"
read permission
chmod $category$operation$permission $file
;;
2)
echo "Absolute Mode"
echo "-------------"
echo "PERMISSION for User 0,1,2,3,4,7"
read user
echo "PERMISSION for Owner 0,1,2,3,4,7"
read owner
echo "PERMISSION for others 0,1,2,3,4,7"
read other
chmod $user$owner$other $file
;;
3)
exit
;;
*)
echo "Not a valid option"
continue
esac
done
else
echo "No such file or directory"
fi
~
~
Output:
PROGRAM TO DEMONSTRATE TO CONTROL THE PROCESS AND ASSIGN
SECURITY
Enter file name :
anju
-rw-rw-r-- 1 exam6 exam6 12 Nov 21 14:44 anju
Assign File Access Permission
1 Symbolic Mode

2 Absolute Mode
3 Exit
Enter choice :
1
Symbolic Mode
------------CATEGORY u:USER g:Group o: OWNER
uo
OPERATION +:ASSIGN -:REMOVE =:ABSOLUTE
-=
PERMISSION r:READ w:WRITE x:EXECUTE
x
---xrw---x 1 exam6 exam6 12 Nov 21 14:44 anju
Assign File Access Permission
1 Symbolic Mode
2 Absolute Mode
3 Exit
Enter choice :
2
Absolute Mode
------------PERMISSION for User 0,1,2,3,4,7
2
PERMISSION for Owner 0,1,2,3,4,7
3
PERMISSION for others 0,1,2,3,4,7
4
--w--wxr-- 1 exam6 exam6 12 Nov 21 14:44 anju
Assign File Access Permission
1 Symbolic Mode
2 Absolute Mode
3 Exit
Enter choice :
3
[exam6@localhost ~]$
----------------------------------------------------------------------------Program 5
clear
echo "PROGRAM TO CREATE THE DIRECTORY AND FILES AND TO TRANSFER
THE FILES"
while true
do
echo "Enter first directory name "
read dir1
if [ -d $dir1 ]

then
echo "Directory with this name already exist give another name "
continue
else
mkdir $dir1
break
fi
done
while true
do
echo "Enter second directory name "
read dir2
if [ -d $dir2 ]
then
echo "Directory with this name already exist give another name "
continue
else
mkdir $dir2
break
fi
done
echo "Enter 5 files name "
read f1
read f2
read f3
read f4
read f5
cd $dir1
touch $f1 $f2 $f3 $f4 $f5
cd ..
echo "Moving of files from $dir1 to $dir2 "
mv ../pavi/$dir1/* ../pavi/$dir2
echo "5 files moved sucessfully"
cd $dir2
ls
output:
PROGRAM TO CREATE THE DIRECTORY AND FILES AND TO TRANSFER
THE FILES
Enter first directory name
move1
Enter second directory name
move2
Enter 5 files name

1
2
3
4
5
Moving of files from move1 to move2
5 files moved sucessfully
1 2 3 4 5
[exam6@localhost pavi]$
-------------------------------------------------------C Program 6:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int fd[2],nbytes;
int pid,childpid;
char string[]="Hellow world";
char readbuffer[20];
pipe(fd);
childpid=fork();
if(childpid==0)
{
close(fd[0]);
write(fd[1],string,(strlen(string)+1));
kill(childpid,-9);
exit(0);
}
else
{
close(fd[1]);
nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
printf("\nReceived string is :%s\n",readbuffer);
}
return 0;
}
~

~
~
~
"ipc.c" 34L, 699C written
[exam8@localhost ~]$ cc ipc.c
[exam8@localhost ~]$ ./a.out
Received string is :Hellow world
-----------------------------------------------------------C Program 7:
#include<stdio.h>
main()
{
int pid;
char * fname;
printf("IMPLEMENTATION OF EXEC, FOR EXIT AND WAIT
COMMANDS\n\n\n");
printf("date : (TO BE PRINTED BY CHILD PROCESS)\n");
pid=fork();
switch(pid)
{
case -1:
printf("\n FORK FAILED................");
exit(1);
case 0:
printf("\n CLILD PROCESS ID IS %d\n",getpid());
execl("/bin/date","date",(char *)0);
break;
default:
break;
}
printf("\n PARENT PROCESS ID IS %i\n",getppid());
}
"fork.c" [New] 25L, 499C written
[exam8@localhost ~]$ cc fork.c
[exam8@localhost ~]$ ./a.out
IMPLEMENTATION OF EXEC, FOR EXIT AND WAIT COMMANDS
date : (TO BE PRINTED BY CHILD PROCESS)
CLILD PROCESS ID IS 4529
Fri Nov 21 15:44:22 IST 2014
PARENT PROCESS ID IS 4470

--------------------------------------------------------------------------

------------------------------------------------------------------------------C Program 8.
#include<signal.h>
#include<stdio.h>
void mysig(int sig)
{
if(sig==SIGALRM)
write(1,"\nAlarm signal caught\n",22);
if(sig==SIGCHLD)
write(1,"\nDeath of child signal caught\n",31);
if(sig==SIGFPE)
{
write(1,"\nSIGFPE signal caught\n",23);
exit(1);
}
if(sig==SIGPIPE)
write(1,"\nSIGPIPE signal caught\n",24);
}
main(int args,char*argv[])
{
int pipes[2],n=5,m=5,bytes;
int chpid,pid,status;
char readbuffer[30];
signal(SIGALRM,mysig);
signal(SIGCHLD,mysig);
signal(SIGFPE,mysig);
signal(SIGPIPE,mysig);
}
main(int args,char*argv[])
{
int pipes[2],n=5,m=5,bytes;
int chpid,pid,status;
char readbuffer[30];
signal(SIGALRM,mysig);
signal(SIGCHLD,mysig);
signal(SIGFPE,mysig);

signal(SIGPIPE,mysig);
write(1,"\nDemonstration of catching signals : ",36);
write(1,"\nSetting up an alarm after 2 sec : ",39);
alarm(50);
write(1,"\nSetting conditions for SIGPIPE");
if((pipe(pipes)<0))
{
write(1,"\nError createing pipes\n",22);
exit(1);
}
if((chpid==fork())<0)
{
wirte(1,"\nError creating child process\n",30);
exit(1);
}
if(chpid==0)
{
close(pipes[0]);
sleep(10);
}
write(pipes[1],"hello",5);
close(pipes[1]);
bytes=read(pipes[0],readbuffer,30);
sleep(2);
printf("\nString is %s",readbuffer);
kill(chpid,SIGILL);
sleep(2);
n=n/m;
}
Output:
"prog5.c" 63L, 1199C written
[exam5@localhost ~]$ cc prog5.c
[exam5@localhost ~]$ ./a.out
Demonstration of catching signals :
Setting up an alarm after 2 sec :

Se
Setting conditions for SIGPIPE
Error
String is hello
Death of child signal caught
String is hello[exam5@localhost ~]$
Program 9:
clear
echo -n "Enter number : "
read n
len=$(echo $n | wc -c)
len=`expr $len - 1`
echo "Your number $n in words is: "
for (( i=1; i<=$len; i++ ))
do
# get one digit at a time
digit=$(echo $n | cut -c $i)
# use case control structure to find digit equivalent in words
case $digit in
0) echo -n "zero " ;;
1) echo -n "one " ;;
2) echo -n "two " ;;
3) echo -n "three " ;;
4) echo -n "four " ;;
5) echo -n "five " ;;
6) echo -n "six " ;;
7) echo -n "seven " ;;
8) echo -n "eight " ;;
9) echo -n "nine " ;;
esac
echo
done
~
Output:
Enter number : 34501
Your number 34501 in words is:
three
four
five
zero
one
[exam3@localhost ~]$

Program 10:
while true
do
read
clear
echo " 1 write "
echo " 2 read "
echo " 3 wall "
echo " 4 grep "
echo " 5 chmod "
echo " 6 exit "
echo " Enter u r ch "
read ch
case $ch in
1) echo enter username
read uname
write $uname
;;
2) echo " enter the number "
read n
echo $n
;;
3) echo " enter the message and press ctrl+d "
wall
;;
4) echo " enter the pattern "
read p
echo enter the file name
read file
grep $p $file
;;
5) echo " enter the file name "
read fname
echo Before changing
ls -l $fname
echo after changes
chmod 777 $fname
ls -l $fname
;;
6)exit
;;

esac
done
output:
---------------------------------

Potrebbero piacerti anche