Final Linux Programming Lab Manual
Final Linux Programming Lab Manual
Final Linux Programming Lab Manual
Contents
Page
S.No Name of the Program
no
Write a shell script that accepts a file name, starting and ending line numbers as
1 3
arguments and displays all the lines between the given line numbers.
Write a shell script that deletes all lines containing a specified word in one or
2 4
more files supplied as arguments to it.
Write a shell script that displays a list of all the files in the current directory to
3 5
which the user has read, write and execute permissions.
Write a shell script that receives any number of file names as arguments checks if
4 every argument supplied is a file or a directory and reports accordingly. 6
Whenever the argument is a file, the number of lines on it is also reported.
Write a shell script that accepts a list of file names as its arguments, counts and
5 reports the occurrence of each word that is present in the first argument file on 7
other argument files
Write an awk script to count the number of lines in a file that do not contain
8 9
vowels.
9 Write an awk script to find the number of characters, words and lines in a file. 10
Write a c program that makes a copy of a file using standard I/O and system calls
10 11
Write a C program to create a child process and allow the parent to display
21 parent and the child to display child on the screen. 35
Write a C program to create a message queue with read and write permissions to
22 36
write 3 messages to it with different priority numbers.
Write a C program that receives the messages (from the above message queue as
23 37
specified in (22)) and displays them.
Write client and server programs (using c) for interaction between server and
25 40
client processes using Unix Domain sockets.
Write client and server programs (using c) for interaction between server and
26. 44
client processes using Internet Domain sockets.
1. Write a shell script that accepts a file name, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
Aim: To Write a shell script that accepts a file name, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
Script:
$ awk NR<2 || NR> 4 {print $0} 5 lines.dat
I/P: line1
line2
line3
line4
line5
O/P: line1
line5
or
#!/bin/sh
read file
head -1 $file
tail -1 $file
cat $file
2. Write a shell script that deletes all lines containing a specified word in one or more
files supplied as arguments to it.
Aim: To write a shell script that deletes all lines containing a specified word in one or more
files supplied as arguments to it.
Script:
3. Write a shell script that displays a list of all the files in the current directory to which
the user has read, write and execute permissions.
Aim: To write a shell script that displays a list of all the files in the current directory to
which the user has read, write and execute permissions.
Script:
echo "List of Files which have Read, Write and Execute Permissions in Current
Directory"
for file in *
do
then
echo $file
fi
done
4. Write a shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory and reports accordingly. Whenever
the argument is a file, the number of lines on it is also reported
Aim: To write a shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory
Script:
read file
wc l $file
wc w $file
wc c $file
5. Write a shell script that accepts a list of file names as its arguments, counts and
reports the occurrence of each word that is present in the first argument file on other
argument files.
Aim : To write a shell script that accepts a list of file names as its arguments, counts and
reports the occurrence of each word that is present in the first argument file on other
argument files.
Script:
read file
if [ -d $file ]
then
elif [ -f $file ]
then
else
Script:
# !/bin/bash
echo"enter directory name"
read dir
if[ -d $dir]
then
echo"list of files in the directory"
ls $dir
else
echo"enter proper directory name"
fi
Output:
Enter directory name
Atri
List of all files in the directoty
CSE.txt
ECE.txt
Script:
echo Factorial
read f
fact=1
factorial=1
do
fact=`expr $fact + 1`
done
Output:
Enter a 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.
#!/bin/bash
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels are: ",count}' $file
Output
9. Write an awk script to find the number of characters, words and lines in a file.
Aim : To write an awk script to find the number of characters, words and lines in a file.
Script:
BEGIN{print "record.\t characters \t words"}
#BODY section
len=length($0)
total_len+=len
print(NR,":\t",len,":\t",NF,$0)
words= words+NF
END{
print("\n total")
output:
$ vi ex
this is an example of awk
counting vowels
not containing in lines
$ awk f count.awk ex
total
characters : 63
words: 12
lines : 3
10. Write a c program that makes a copy of a file using standard I/O and system calls
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
char buffer[100];
0700)) == -1)){
exit(1);
exit(3);
$ cat del
unix is os
dos is also os
here using unix
unix is powerful os
$ cc lp10.c
$ cat exdel
unix is os
dos is also os
here using unix
unix is powerful os
A. cat
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
output:
$cc cat.c
$./a.out sample
c.mv
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[] )
{
int i,fd1,fd2;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s",file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777);
while(i=read(fd1,buf,1)>0)
write(fd2,buf,1);
remove(file1);
close(fd1);
close(fd2);
}
output:
$cc mv.c
$cat > ff
hello
hai
$./a.out ff ff1
$cat ff
cat:ff:No such file or directory
$cat ff1
hello
hai
#include <dirent.h>
#include <stdio.h>
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
cc prog12.c
./a.out
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid; //process id
pid = fork(); //create another process
if ( pid < 0 )
{ //fail
printf(\nFork failed\n);
exit (-1);
}
else if ( pid == 0 )
{ //child
execlp ( /bin/ls, ls, -l, NULL ); //execute ls
}
else
{ //parent
wait (NULL); //wait for child
printf(\nchild complete\n);
exit (0);
}
}
SAMPLE OUTPUT:
cc prog13.c
./a.out
14. Write a C program to list for every file in a directory, its inode number and file name.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main(int argc, char *argv[])
{
char d[50];
if(argc==2)
{
bzero(d,sizeof(d));
strcat(d,"ls ");
strcat(d,"-i ");
strcat(d,argv[1]);
system(d);
}
else
printf("\nInvalid No. of inputs");
}
output:
$cc prog14.c
./a.out hello
Output:
$ gcc o prog15.out prog15.c
$ls
downloads documents listing.c listing.out prog15.c
prog15.out
$ cat > f1
^z
$./prog15.out f1
$cat f1
downloads
documents
listing.c
listing.out
std.c
std.out
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>
#include<string.h>
#include<stdlib.h>
main()
{
int childpid;
if (( childpid=fork())<0)
{
printf("cannot fork");
}
else if(childpid >0)
{
}
else
printf(Child process);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int childpid;
if (( childpid=fork())<0)
{
printf("cannot fork");
}
else if(childpid >0)
{
Printf(child process);
exit(0);
}
else
{
wait(100);
printf(parent process);
}
}
#include<stdio.h>
main()
{
int id;
printf("Before fork()\n");
id=fork();
if(id==0)
{
printf("Child has started: %d\n ",getpid());
printf("Parent of this child : %d\n",getppid());
printf("child prints 1 item :\n ");
sleep(25);
printf("child prints 2 item :\n");
}
else
{
printf("Parent has started: %d\n",getpid());
printf("Parent of the parent proc : %d\n",getppid());
}
printf("After fork()");
}
19. Write a C program that illustrates how to execute two commands concurrently with a
command pipe.
Ex: - ls l | sort
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int fd[2],pid,k;
k=pipe(fd);
if(k==-1)
perror("pipe");
exit(1);
pid=fork();
if(pid==0)
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp(argv[1],argv[1],NULL);
perror("execl");
else
wait(2);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("execl");
Output:
cc prog19.c
./a.out ls sort
20. Write C programs that illustrate communication between two unrelated processes
using named pipe.
#include<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);
}
printf("writing to file descriptor #%d\n", pfds[1]);
write(pfds[1],"test",5);
printf("reading from file descriptor #%d\n ", pfds[0]);
read(pfds[0],buf,5);
printf("read\"%s\"\n" ,buf);
}
21. 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>
#include <sys/wait.h> /* contains prototype for wait */
#include<stdlib.h>
int main(void)
{
int pid;
int status;
printf("Hello World!\n");
pid = fork( );
if(pid == -1) /* check for error in fork */
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
}
Output:
$gcc prog21.c
$: ./a.out
Hello World!
I am the child process.
I am the parent process
22. Write a C program to create a message queue with read and write permissions to write
3 messages to it with different priority numbers.
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int qid,len,i;
char s[15];
struct
{
long mtype;
char mtext[15];
}message,buff;
qid=msgget((key_t)10,IPC_CREAT|0666);
if(qid==-1)
{
perror("message queue create failed");
exit(1);
}
for(i=1;i<=3;i++)
{
printf("Enter the message to send \n");
scanf("%s",s);
strcpy(message.mtext,s);
message.mtype=i;
len=strlen(message.mtext);
if(msgsnd(qid,&message,len+1,0)==-1)
{
perror("message failed \n");
exit(1);
}
}
}
$cc prog22.c
$ ./a.out
hi
hello
how
23. Write a C program that receives the messages (from the above message queue as
specified in (22)) and displays them.
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int qid,len,i;
char s[15];
struct
{
long mtype;
char mtext[15];
}buff;
qid=msgget((key_t)10,IPC_CREAT|0666);
if(qid==-1)
{
perror("message queue create failed");
exit(0);
}
for(i=1;i<=3;i++)
{
if(msgrcv(qid,&buff,15,i,0)==-1)
{
perror("message failed \n");
exit(0);
}
printf("Message received from sender is %s \n",buff.mtext);
}
}
$ cc prog23.c
$ ./a.out
24. Write a C program that illustrates suspending and resuming processes using signals.
#include <stdio.h>
#include <ospace/unix.h>
int child_function()
{
while (true) // Loop forever.
{
Printf("Child loop\n");
os_this_process::sleep( 1 );
}
return 0; // Will never execute.
}
int main()
{
os_unix_toolkit initialize;
os_process child ( child function ); // Spawn child.
os_this_process::sleep( 4 );
printf("child.suspend()\n");
child.suspend();
printf("Parent sleeps for 4 seconds\n");
os_this_process::sleep (4);
printf("child.resume()");
child.resume ();
os_this_process::sleep (4);
printf("child.terminate()");
child.terminate ();
printf("Parent finished");
return 0;
}
Output:
Child loop
Child loop
Child loop
Child loop
Child loop
child.suspend()
Parent sleeps for 4 seconds
child.resume()
Child loop
Child loop
Child loop
Child loop
child.terminate()
Child loop
Parent finished
25. 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);
}
close(socket_fd);
unlink("./demo_socket");
return 0;
}
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;
}
26. 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));
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;
}
27. Write a C program that illustrates two processes communicating using shared memory.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<sys/shm.h>
#define shm_size 1024
int main(int argc,char * argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if(argc>2)
{
fprintf(stderr,usage:stdemo[data_to_writte]\n);
exit(1);
}
if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)
{
perror(shmget);
exit(1);
}
data=shmat(shmid,(void *)0,0);
if(data==(char *)(-1))
{
perror(shmat);
exit(1);
}
if(argc==2)
printf(writing to segment:\%s\\n,data);
if(shmdt(data)==-1)
{
perror(shmdt);
exit(1);
}
return 0;
}
Input:
#./a.out koteswararao
Output:
writing to segment koteswararao