Linux Lab
Linux Lab
PROGRAMMING
LAB MANUL
1)Write a shell script that accepts a filename , starting and ending line number as
arguments and display all the lines between the given line numbers
output: $ sh f1.sh
enter file name
f1.txt
starting line no
2
Ending line no
3
the line between 2 and 3
second
third
2) Write a shell script that deletes all lines containing a specified word in one or more files supplied
as arguments to it.
echo “list of files which have read,write and execute permissions in a current
directory”
for file in *
do
if [ -r $file –a –w $file –a –x $file ]
then
echo “$file”
fi
done
input:
$chmod a+rwx f1.txt
$chmod a+rwx f1.sh
$chmod a+rwx f2.sh
input:
$vi f1.txt
first
second
third
$vi f2.txt
four
five
six
seven
input :
$mkdir cse
$cd cse
$vi a.txt
$vi b.txt
output: $ sh f7.sh
enter the number
5
factorial of 5
is 120
8) Write an awk script to count the number of lines in a file that do not contain vowels.
BEGIN{
printf " the no of lines not contain vowels are "
}!/[aA]|[eE]|[iI]|[oO]|[uU]/{
k++
}
END{
printf "%d \n",k
}
total
words: 3
characters: 16
lines 4
10 Write a c program that makes a copy of a file using standard I/O and system calls
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
FILE *fds,*fdd;
char ch;
fds = fopen(argv[1], "r");
fdd = fopen(argv[2], "w");
while(!feof (fds))
putc(getc(fds),fdd);
fclose(fds);
fclose(fdd);
}
Output
$ vi f10.c
$ cc f10.c
$ ./a.out f1.txt f2.txt
11 Implement in C the following UNIX commands using System calls
A. cat B. ls C. mv
a cat
#include<stdio.h>
#include<stdlib.h>
main(int argc, char *argv[])
{
FILE *fp;
int i;
for(i=1;i<argc;i++)
{
fp = fopen (argv[i], "r");
while (!feof(fp))
putc(getc(fp),stdout);
fclose(fp);
}
}
Output
$ vi f11a.c
$ cc f11a.c
$ ./a.out xyz.txt
linux
programming
11 b ls
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<stdlib.h>
#include<sys/types.h>
int main(int argc ,char **argv)
{
DIR *dp;
struct dirent *dirp;
char p[8];
if(argc==1)
strcpy(p,".");
else
{
strcpy(p,argv[1]);
}
if((dp=opendir(p))==NULL)
{
printf("can't open directoey");
exit(0);
}
while((dirp=readdir(dp))!=NULL)
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
return 1;
}
Output
$ vi f11b.c
$ cc f11b.c
$ ./a.out
f2.txt
a.out
f11a.c
xyz.txt
f11b.c
f10.c
f1.txt
f11c.c
11 c mv
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
FILE *fps,*fpd;
fps = fopen(argv[1], "r");
fpd = fopen(argv[2], "w");
while (!feof(fps))
putc(getc(fps),fpd);
fclose(fpd);
}
Output
$ vi f11c.c
$ cc f11c.c
$ ./a.out f1.txt f2.txt
12. Write a program that takes one or more file/directory names as command line input and
reports the following information on the file.
A. File type. B. Number of links.
C. Time of last access. D. Read, Write and Execute permissions.
for i in $*
do
if [ -d $i ]
then
fi
if [ -f $i ]
then
fi
file $i
ls -l $i
ln $i
if [ -x $i -a -w$i -a -r $i ]
then
else
fi
done
output
no.of links
no.of links
[meher@localhost cprog]$
13 Write a C program to emulate the UNIX ls –l command.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
if ( pid < 0 )
{ //fail
printf("\nFork failed\n");
exit (-1);
else if ( pid == 0 )
{ //child
else
{ //parent
printf("\nchild complete\n");
exit (0);
}
Output
total 96
#include<unistd.h>
#include<stdlib.h>
#include<dirent.h>
int main(int argc,char **argv)
{
DIR *dp;
struct stat buf;
ino_t ino;
struct dirent *dirp;
if(argc!=2)
{
printf("usage:dirname");
exit(1);
}
if((dp=opendir(argv[1]))==NULL)
perror("open:");
printf("\n inode filename");
printf("\n----------------");
while((dirp=readdir(dp))!=NULL)
{
stat(dirp->d_name,&buf);
ino=buf.st_ino;
printf("\n %ld %s",ino,dirp->d_name);
}
return 1;
}
Output
$ vi f14.c
$ cc f14.c
$ ./a.out abc
inode filename
----------------
131123 .
131170 b.txt
131080 ..
131169 a.txt
15 Write a C program that demonstrates redirection of standard output to a file.
Ex: ls > f1
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char d[50];
if(argc==2)
bzero(d,sizeof(d));
strcat(d,"ls ");
strcat(d,"> ");
strcat(d,argv[1]);
system(d);
else
Output
^z
^C
abc
a.out
a.txt
b.txt
f1
f10.c
f11a.c
f11b.c
f11c.c
f12.c
f12.sh
f13.c
f14.c
f15.c
f16.c
f17.c
f18.c
f1.txt
f2
f2.txt
lsf1.c
lssort.c
orphan.c
PID:3451
PID:3452
PPID:3451
pqr.txt
p.txt
xyz.txt
zombie.c
16. Write a C program to create a child process and allow the parent to display “parent” and
the child to display “child” on the screen.
#include<stdio.h>
main ()
{
int childpid;
childpid = fork();
if(childpid==0)
$ cc f16.c
$ ./a.out
child process
parent process
17 Write a C program to create a Zombie process.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int id ,x ,y;
id=fork();
if(id==0)
{
x=getpid();
y=getppid();
printf("Child process 1>PID:%d \n2>PPID:%d",x,y);
sleep(5);
printf("\nchild terminated");
}
else{
x=getpid();
y=getppid();
printf("Parent process 1>PID:%d \n2>PPID:%d",x,y);
sleep(20);
wait(&id);
printf("parent terminated");
}
return(0);
}
Output
$ vi zombie.c
$ cc zombie.c
$ ./a.out
Parent process 1>PID:3419
Child process 1>PID:3420
2>PPID:3419
child terminated2>PPID:3321parent terminated
18 Write a C program that illustrates how an orphan is created.
#include<stdio.h>
main()
{
int id;
id=fork();
if(id==0)
{
printf("Child has started: %d\n ",getpid());
printf("Parent of this child : %d\n",getppid());
sleep(10);
printf("child");
}
else {
printf("Parent has started: %d\n",getpid());
printf("Parent of the parent proc : %d\n",getppid());
}
child is alive and parent is dead child child is alive and parent is dead
19. Write a C program that illustrates how to execute two commands concurrently with a
command pipe.
Ex: - ls –l | sort
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
perror("pipe failed");
exit(1);
if(!fork())
close(1);
dup(pfds[1]);
else
Output
total 92
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#nclude<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
int main()
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
perror("pipe");
exit(1);
write(pfds[1],"test",5);
read(pfds[0],buf,5);
printf("read\"%s\"\n" ,buf);
}
Output
read"test"
[meher@localhost cprog]$
21 Write a C program to create a message queue with read and write permissions to write 3
messages to it with different priority numbers.
Program:
#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct msgbuf
long mtype;
char mtext[50];
mobj={15,"HELLO"};
int main()
if(fd==-1||msgsnd(fd,&mobj,strlen(mobj.mtext)+1,IPC_NOWAIT))
perror("message error");
return 0;
Output:
$ vi mq.c
$ cc mq.c
$ ipcs
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[50];
}
mobj={15,”HELLO”};
main()
{
int fd;
fd=msgget(100,IPC_CREAT|IPC_EXCL|0642);
if(msgrcv(fd,&mobj,50,20,MSG_NOERROR)>0)
printf(“\n %5s \n”,mobj.mtext);
else
printf(“\n message receive error \n “);
return 0;
}
output:
$vi mqr.c
$cc mqr.c
$ipcs
------ Message Queues --------
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main(void)
key_t key;
int semid;
if((key==ftok("s.txt","j"))==-1)
perror("ftok");
exit(1);
if((semid=semget(key,1,IPC_CREAT|0666))==-1)
perror("semget");
exit(1);
if(semctl(semid,0,SETVAL,0)==-1)
{
perror("smctl");
exit(1);
return 0;
Output:
$vi 23.c
$cc 23.c
#include<sys/types.h>
#include<signal.h>
//suspend the process(same as hitting crtl+z)
kill(pid,SIGSTOP);
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define NUM_LOOPS 20
int semid;
int child_pid;
int i;
int rc;
semid=semget(IPC_PRIVATE,1,0600);
if(semid==-1)
perror("main:semget");
exit(1);
}
rc =semctl(semid,0,SETVAL,0);
child_pid=fork ();
switch(child_pid)
case -1:
perror("fork");
exit(1);
case 0:
for (i=0;i<NUM_LOOPS;i++)
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(semid,&sem_op,1);
fflush(stdout);
sleep(3);
break;
default:
for(i=0;i<NUM_LOOPS;i++)
{
printf("producer:%d\n",i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(semid,&sem_op,1);
sleep(2);
if(rand( )>3*(RAND_MAX/4))
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,NULL);
break;
return 0;
consumer:'0'
producer:1
consumer:'1'
producer:2
consumer:'2'
producer:3
producer:4
consumer:'3'
producer:5
consumer:'4'
producer:6
producer:7
consumer:'5'
producer:8
consumer:'6'
producer:9
producer:10
consumer:'7'
producer:11
consumer:'8'
producer:12
26. Write client and server programs (using c) for interaction between server and client
processes using Unix Domain sockets.
Server.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
close(connection_fd);
return 0;
}
int main(void)
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
unlink("./demo_socket");
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(bind(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("bind() failed\n");
return 1;
}
if(listen(socket_fd, 5) != 0)
{
printf("listen() failed\n");
return 1;
}
while((connection_fd = accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
child = fork();
if(child == 0)
{
/* now inside newly created connection handling process */
return connection_handler(connection_fd);
}
Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(connect(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}
close(socket_fd);
return 0;
}
27. Write client and server programs (using c) for interaction between server and client
processes using Internet Domain sockets.
Server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
char sendBuff[1025];
time_t ticks;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
Client.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
28 Write a C program that illustrates inter process communication using shared memory.
Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc,char *argv[])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[]=”hello world”;
key=ftok(“.”,’s’);
if((shmid=shmget(key,SEGSIZE,IPC_CREAT|IPC_EXCL|0666))==-1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror(“shmget”);
exit(1);
}
}
else
{
printf(“ creating a new shared memory seg \n “);
printf(“SHMID:%d”,shmid);
}
system(“ipcs -m”);
if((segptr=shmat(shmid,0,0))==(char*)-1)
{
perror(“shmat”);
exit(1);
}
printf(“writing data to shared memory…\n “);
strcpy(segptr,buff);
printf(“DONE \n”);
printf(“reading data from shared memory…\n”);
printf(“removed successfully”);
}
Output
$vi 28.c
$cc 28.c
$ ./a.out
DONE
DATA:-hello world
DONE
removed successfully[