0% found this document useful (0 votes)
92 views

Unix

The program takes a directory name as a command line argument and displays the names of files within that directory that have more than one hard link. It uses the readdir() and stat() functions to iterate through the directory contents, obtain file status information, and check the link count of each file, printing the names of files with a link count greater than one.

Uploaded by

Mythri Phani
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Unix

The program takes a directory name as a command line argument and displays the names of files within that directory that have more than one hard link. It uses the readdir() and stat() functions to iterate through the directory contents, obtain file status information, and check the link count of each file, printing the names of files with a link count greater than one.

Uploaded by

Mythri Phani
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

2 : Program using system calls :create,open,read,write,close,stat,fstat,fseek .

Source code :

#include<stdio.h>
#include<sys/types.h>
#include<errno.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>

int main(int argc,char *argv[]) {

int size,fd;
char buf;
char *msg="welcome to mca......\n";
if(argc != 2)
{
write(STDERR_FILENO,msg,strlen(msg));
exit(0);
}
if((fd=open(argv[1],O_RDONLY))==-1)
{
if(errno == ENOENT)
{
//printf(stderr,"%s\n",stderr(errno));
exit(1);
}
else {
perror(argv[1]);
exit(2);
}
}
lseek(fd,1,SEEK_CUR);
while(lseek(fd,-2,SEEK_CUR)>=0)
{
if(read(fd,&buf,1)!=1) {
perror("read");
exit(3);
}
if(write(STDOUT_FILENO,&buf,1)!=1) {
perror("write");
exit(4);

}
}
close(fd);
exit(5);
return 0;
}

OUTPUT :

3 : Program to perform inter process communication using pipes .

Source code :

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
int main()
{
int pfds[2];
char buff[300];
pipe(pfds);
if(!fork())
{
printf("child:writing tompipe\n");
write(pfds[1],"test",5);
printf("child: exiting \n ");
exit(0);
}
else
{
printf("parent is reading\n");
read(pfds[0],buff,strlen(buff));
printf("parent : read %s \n",buff);
wait(NULL);
}
return 0;
}

OUTPUT :

8 : Write a shell script for sorting,searching,insertion,delection and displaying of elements in a list .

Source code :

clear
i=1
echo Enter the size of array :
read n
echo Enter $n elements :
while [ $i -le $n ]
do
read a[$i]
i=`expr $i + 1`
done
while true
do
i=1
j=1
clear
echo 1. Sort
echo 2. Search
echo 3. Insert
echo 4. Delete
echo 5. Display
echo 6. Exit
echo Enter your choice :
read ch
case $ch in
1)
while [ $i -le $n ]
do
j=1
while [ $j -le $n ]
do

if [ ${a[$i]} -lt ${a[$j]} ]


then
t=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$t
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
;;

2)
echo Enter the element to search :
flag=0
read e
while [ $i -le $n ]
do
if [ $e -eq ${a[$i]} ]
then
flag=1

break

else

flag=0

fi

i=`expr $i + 1`

done

if [ $flag -eq 0 ]
then

echo The element not found

else

echo The element found

fi

;;

3)

echo Enter the element to insert


read e
echo Enter the position to insert
read pos
if [ $pos -gt `expr $n + 1` ]
then
echo Wrong Position
fi
if [ $pos -eq `expr $n + 1` ]

then
n=`expr $n + 1`
a[$n]=$e
else
n=`expr $n + 1`
p=`expr $n - $pos`
n1=$n
while [ $p -gt 0 ]
do
n2=`expr $n1 - 1`
a[$n1]=${a[$n2]}
n1=`expr $n1 - 1`
p=`expr $p - 1`
done
a[$pos]=$e
fi
;;

4)
echo Enter the position to be deleted
read pos
if [ $pos -gt $n ]
then
echo Wrong Position
else
p=`expr $n - $pos`
while [ $p -ge 0 ]
do
po=`expr $pos + 1`
a[$pos]=${a[$po]}
pos=`expr $pos + 1`

p=`expr $p - 1`
done

n=`expr $n - 1`

fi

;;

5)

i=1
while [ $i -le $n ]
do
echo ${a[$i]}
i=`expr $i + 1`
done
;;
6)
exit
;;

esac
read key
done

OUTPUT :
9 : Create two processes to run a for loop,which adds 1 to n,say one process adds odd and other for
even

Source code :

#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>

int main() {
pid_t child;
child=fork();
int i,n=7,even=0,odd=0;
if(child == 0) {
for(i=1;i<=n;i=i+2)
odd=odd+i;
printf("\nodd is :%d\n",odd);

}
else {
printf("\n");
for(i=2;i<=n;i=i+2)
even=even+i;
printf("\nevne numb :%d\n",even);
}
}
OUTPUT :

13. Write a program demonstrating semaphore operation on a shared file for reading but not
writing .

Source code :

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main()
{
int sem;
char a='5';
sem=semget(1212,1,IPC_CREAT|0666);
if(sem<0)
printf("semaphore not created");
semctl(sem,0,SETVAL,6);
struct sembuf sem_op;
FILE *fd;
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem,&sem_op,1);
fd=fopen("/home/bhanup/honey.c","a");
if(fd<0)
printf("not possible");
else
{
putc('\n',fd);
putc(a,fd);
fclose(fd);
}
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem,&sem_op,1);
}

OUTPUT :

16 : Write a program which reads a source file name and destination file using command line
arguments and then converts into specified formet (either in upper or lower o inverse of each).

Source code :

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
//#include<apue.h>
int main(int argc,char *argv[]) {
FILE *fm,*fn;
char ch,choi;
printf("chnage it in uper or not(y/n) :");
choi=getchar();
if((fm=fopen(argv[1],"r"))==NULL) {
printf("\nerror..\n");
exit(0);
}
fn=fopen(argv[2],"w");
if(fn == NULL) {
printf("error...../");
exit(1);
}
while(1) {
ch=fgetc(fm);
if(feof(fm)) break;
if(choi == 'y') {
ch=toupper(ch);
}
else {
ch=tolower(ch);
}
fputc(ch,fn);
}
fclose(fm);
fclose(fn);
return 0;
}

OUTPUT :
17 : Write a program which takes a set of filenames along with the command line and print them
based on their size in bytes either in ascending or descending

Source code :

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>

int main(int argc,char * argv[]) {


char ch;
int x=1,k,size,i,lar,c[10];
FILE *fp;
struct stat st;
c[0]=0;
for(i=1;i<argc;i++) {
stat(argv[i],&st);
size=st.st_size;
c[i]=size;
}
while(x != argc) {
lar=1;
for(k=1;k<(argc-1);k++) {
if(c[lar]<c[k+1]) {
lar=k+1;
}
}
fp=fopen(argv[lar],"r");
if(fp == NULL) {
printf("file does not exist....\n");
}
else {
printf("the size of file is :%d\n",c[lar]);
ch=fgetc(fp);
while(ch != EOF) {
printf("%c",ch);
ch=fgetc(fp);
}
}
c[lar]=0;
x++;
}
}

OUTPUT :

22. Write a program to demonstrate the working of


simple signal handler that catches either of the two user defined signals and prints the signal
number .

Source code :

#include<stdio.h>
#include<signal.h>
#include<stdlib.h>
void catch_mysignal(int signo)
{
printf("SEGMENTATION PROBLEM \n SIGNAL NO IS : %d\n",signo);
exit(1);
}
main()
{
int a=10,b=0,c;
signal(SIGFPE,catch_mysignal);
c=a/b;
}

OUTPUT :

25 : Write a shell script which works similar to the wc command. This script can receive the ption
-l, -w, -c to indicate whether number of lines/ words/characters .

Source code :

echo "enter the fiel name to count lines,words,characters :"


read fname
while true
do
echo "1:character 2:words 3:lines 4:exit"
echo "enter ur choice :"
read ch
if [ $ch -eq 1 ]
then
echo total no.of characters ; wc -c $fname
elif [ $ch -eq 2 ]
then
echo total no.of words ; wc -w $fname
elif [ $ch -eq 3 ]
then
echo total no.of lines ; wc -l $fname
elif [ $ch -eq 4 ]
then
break
fi
sleep 5
done
OUTPUT :

26.A : Write a program to print prime numbers between x and y.

Source code :

echo "enter the starting number..........."


read x
echo "enter the ending number........"
read y

i=$x;
j=1;
c=0;

while [ $i -le $y ]
do
c=0;
for j in $(seq 1 $i)
do
q=`expr $i % $j`;
if [ $q -eq 0 ]
then
c=`expr $c + 1`
fi
done
if [ $c -eq 2 ]
then
echo "$i";
fi
i=`expr $i + 1`;
done

OUTPUT :

26.B : Write a shell script which deletes all lines containing the word .

Source code :

if [ $# -eq 0 ]
then
echo "enter one r more file names as arguments.........../"
exit
fi
echo "enter the word to delete :"
read word
for file in $*
do
sed "/$word/d" $file | tee tmp
mv tmp $file
done

OUTPUT :

27 : Write a shell script which deletes all lines conntaining the word “UNIX” in the files supplied as
arguments to this shell script.

Source code :

if [ $# -eq 0 ]
then
echo "please enter one r more filenames as args :"
exit
fi
for file in $*
do
sed -i '/unix/d' $file
done
OUTPUT :

28. Write a shell script which displays a list of all files in the current directory to which you have
read, write and execute permissions .

Source code :

echo "enter file name"


read file
[ -w $file ] && W="Write = yes" || W="Write = No"
[ -x $file ] && X="Execute = yes" || X="Execute = No"
[ -r $file ] && R="Read = yes" || R="Read = No"
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"

OUTPUT :
30 : Write a shell script rename each file in the directory >

Source code :

clear
for i in *
do
if [ -f $i ]
then
echo ;mv $i $i.$$
fi
done

OUTPUT :
31. Write a program which demonstrates the shared memory functions .

Source code :

/*SERVER PROGRAM*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
char c;
char *s,*shm;
key_t key;
key=56787;
shmid=shmget(key,SHMSZ,IPC_CREAT|0666);
printf("shmid=%d\n",shmid);
if(shmid==-1)
{
perror("shmget");
exit(1);
}
shm=shmat(shmid,NULL,0);
s=shm;
for(c='a';c<='z';c++)
*s++=c;
s=NULL;
}

/* CLIENT PROGRAM */

#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
char c;
char *s,*shm;
key_t key;
key=56787;
shmid=shmget(key,SHMSZ,0666);
if(shmid==-1)
{
perror("shmget");
exit(1);
}
shm=shmat(shmid,NULL,0);
for(s=shm;*s!='\0';s++)
{
putchar(*s);
putchar('\t');
}
printf("\n");
}

OUTPUT :
18 : Write a program which takes directory name aloge the command ine and displays names of the
file which are having more than one link .

Source code :

#include&lt;stdio.h&gt;
#include&lt;unistd.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;dirent.h&gt;
#include&lt;string.h&gt;
#include&lt;sys/types.h&gt;
#include&lt;sys/stat.h&gt;
int main(int argc,char **argv)
{
struct stat x;
struct dirent *dir;
DIR *d;
int no_of_files=0,i=0,j=0;
long *size,*ino,l;
char *name=NULL,**file;
if(argc==1)
{
printf(&quot;\n usage : %s &lt;directoryname&gt;&quot;,argv[0]);
exit(1);
}
if((d=opendir(argv[1]))==NULL)
{
printf(&quot;\n error in opening directory&quot;);
exit(2);
}
while(dir=readdir(d))
{
name=(char *)realloc(name,strlen(argv[1]));
sprintf(name,&quot;%s/%s&quot;,argv[1],dir-&gt;d_name);

stat(name,&amp;x);
if(S_ISREG(x.st_mode))
no_of_files++;
}
size=(long *)malloc(no_of_files *sizeof(long));
ino=(long *)malloc(no_of_files *sizeof(long));
file=(char **)malloc(no_of_files *sizeof(char *));
rewinddir(d);
while((dir=readdir(d)))
{
name=(char *)realloc(name,strlen(argv[1]));
sprintf(name,&quot;%s/%s&quot;,argv[1],dir-&gt;d_name);
stat(name,&amp;x);
if(S_ISREG(x.st_mode))
{
size[i]=x.st_size;
ino[i]=dir-&gt;d_ino;
file[i]=dir-&gt;d_name;
i++;
}
}
for(i=0;i&lt;no_of_files-1;i++)
{
for(j=0;j&lt;no_of_files-i- 1;j++)
{
if(ino[j]&gt;ino[j+1])
{
long t;
t=ino[j];

ino[j]=ino[j+1];
ino[j+1]=t;
t=size[j];
size[j]=size[j+1];
size[j+1]=t;
name=file[j];
file[j]=file[j+1];
file[j+1]=name;
}
}
}
l=0;
printf(&quot;\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- &quot;);
printf(&quot;\n Inode \t Files which r having more than one link \t size in bytes&quot;);
printf(&quot;\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- &quot;);
for(i=0;i&lt;no_of_files;i++)
{
if(ino[i]==l || ino[i]==ino[i+1])
{
if(ino[i]!=l)
{
l=ino[i];
printf(&quot;\n %ld \t&quot;,l);
}
printf(&quot; %s &quot;,file[i]);
if(ino[i]!=ino[i+1])
printf(&quot;\t\t\t\t\t %ld &quot;,size[i]);} }printf(&quot;\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -
-- -- -- -- -- -- -- -- -- -- - \n&quot;);
exit(0);

You might also like