Unix
Unix
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 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 :
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 :
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
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
else
fi
;;
3)
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>
OUTPUT :
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 :
Source code :
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 :
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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<dirent.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
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("\n usage : %s <directoryname>",argv[0]);
exit(1);
}
if((d=opendir(argv[1]))==NULL)
{
printf("\n error in opening directory");
exit(2);
}
while(dir=readdir(d))
{
name=(char *)realloc(name,strlen(argv[1]));
sprintf(name,"%s/%s",argv[1],dir->d_name);
stat(name,&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,"%s/%s",argv[1],dir->d_name);
stat(name,&x);
if(S_ISREG(x.st_mode))
{
size[i]=x.st_size;
ino[i]=dir->d_ino;
file[i]=dir->d_name;
i++;
}
}
for(i=0;i<no_of_files-1;i++)
{
for(j=0;j<no_of_files-i- 1;j++)
{
if(ino[j]>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("\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- ");
printf("\n Inode \t Files which r having more than one link \t size in bytes");
printf("\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- ");
for(i=0;i<no_of_files;i++)
{
if(ino[i]==l || ino[i]==ino[i+1])
{
if(ino[i]!=l)
{
l=ino[i];
printf("\n %ld \t",l);
}
printf(" %s ",file[i]);
if(ino[i]!=ino[i+1])
printf("\t\t\t\t\t %ld ",size[i]);} }printf("\n -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -
-- -- -- -- -- -- -- -- -- -- - \n");
exit(0);