LP Lab Manual
LP Lab Manual
ENGINEERING
JNTU SYLLABUS
S.No Topic
Week1- (22-07-17)
1. Write a shell script that accepts a file name, starting and ending line numbers as
1. arguments and displays all the lines between the given line numbers.
2. Write a shell script that deletes all lines containing a specified word in one or more
files supplied as arguments to it.
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.
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.
6. Write a shell script to list all of the directory files in a directory.
7. Write a shell script to find factorial of a given integer.
10. Write a c program that makes a copy of a file using standard I/O and system calls.
LINUX PROGRAMMING
1
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
17. Write a C program to create a Zombie process.
18. Write a C program that illustrates how an orphan is created.
21. Write a C program in which a parent writes a message to a pipe and the child reads
the message.
23. Write a C program (receiver.c) that receives the messages (from the above message
queue as specified in (22)) and displays them.
LINUX PROGRAMMING
2
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
27. Write C programs to perform the following: One process creates a shared memory
segment and writes a message(”HelIo”) into it.Another process opens the shared
memory segment and reads the message. “HelIo”).lt will then display the
message(”HelIo”) to standard output device.
LINUX PROGRAMMING
3
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
4
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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.
“gedit prog1.sh”
OUTPUT:
$ cat file
line number 1
line number 2
line number 3
line number 4
line number 5
line number 6
line number 7
LINUX PROGRAMMING
5
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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.
gedit prog2.sh
Script:
echo "Enter word"
read word
echo "File content before delete the lines"
cat $filename
echo "\nFile content after delete the lines"
sed -e /$word/d $filename
OUTPUT:
$ cat file
line number 1
line number 2
line number 3
line number 4
line number 5
line number 6
LINUX PROGRAMMING
6
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
7
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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.
“gedit prog3.sh”
Script:
echo "List of Files which have Read, Write and Execute Permissions in Current
Directory"
for file in *
do
if [ -r $file -a -w $file -a -x $file ]
then
echo $file
fi
done
OUTPUT:
$ sh prog3.sh
List of Files which have Read, Write and Execute Permissions in Current
Directory
prog1.sh
prog2.sh
LINUX PROGRAMMING
8
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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
im: To write a shell script that receives any number of file names as arguments checks if
A
every argument supplied is a file or a directory
prog4.sh
Script:
for x in $*do
if [ -d $x ]
then
echo " given name ‘$x’ is directory"
elif [ -f $x ]
then
echo " given name is file: $x"
lines=` wc -l < $x `
echo "No of lines in file are : $lines"
else
echo " given name is not a file or a directory"
fi
done
OUTPUT:
$sh prog4.sh / /home file prog1.sh
given name ‘/’ is directory
given name ‘/home’ is directory
given name is file: file
No of lines in file are : 8
given name is file: prog1.sh
No of lines in file are : 8
LINUX PROGRAMMING
9
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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.
prog5.sh
Script:
if [ $# -ne 2 ]
then
echo "Error : Invalid number of arguments."
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "Word = $a, Count = `grep -c "$a" $2`"
done
Output :
$ cat file
line number
$ cat file1
line number 1
line number 2
line number 3
line number 4
$ sh prog5.sh file file1
Word = line, Count = 4
Word = number, Count = 4
LINUX PROGRAMMING
10
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
11
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
6. Write a shell script to list all of the directory files in a directory.
Aim: To list all of the directory files in a directory
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
KMIT
List of all files in the directoty
CSE.txt
ECE.txt
LINUX PROGRAMMING
12
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
7. Write a shell script to find factorial of a given integer.
Script:
# !/bin/bash
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`echo $fact\* $num|bc`
let num--
done
echo "Factorial of $n is $fact"
(or)
LINUX PROGRAMMING
13
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
8. Write an awk script to count the number of lines in a file that do not contain vowels.
vi eight.awk
OUTPUT
$ cat test1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccc
ddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
LINUX PROGRAMMING
14
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
9. Write an awk script to find the number of characters, words and lines in a file.
Script
$ cat nine.awk
{nc=nc+length($0);nw=nw+NF}
END { printf "The no of characters is %d \n no of words is %d \n no of lines is %d in a
file",nc,nw,NR}
OUTPUT
$ cat test2
aaaa bbbb cccc dddd
eeee ffff gggg hhhh
iiii jjjj kkkk llll
LINUX PROGRAMMING
15
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
10. Write a c program that makes a copy of a file using standard I/O and system calls
Aim : To write an awk script to find the number of characters, words and lines in a file.
vi a1.c
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
int read_fd;
int write_fd;
struct stat stat_buf;
off_t offset = 0;
/* Open the input file. */
read_fd = open (argv[1], O_RDONLY);
/* Stat the input file to obtain its size. */
fstat (read_fd, &stat_buf);
/* Open the output file for writing, with the same permissions as the
source file. */
write_fd = open (argv[2], O_WRONLY | O_CREAT, stat_buf.st_mode);
/* Blast the bytes from one file to the other. */
sendfile (write_fd, read_fd, &offset, stat_buf.st_size);
/* Close up. */
close (read_fd);
close (write_fd);
return 0;
}
OUTPUT
$cat>file1
Hai
Hello
$cat file1
Hai
Hello
$gcc a1.c
LINUX PROGRAMMING
16
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
17
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
11. Implement in C the following UNIX commands using System calls
A. cat B. mv
prog11.c
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int main(){
int fd1, fd2;
char buf[1024];
int nread,ch;
char src[20],dest[20];
printf("1. cat\n2. mv\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter filename to display\n");
scanf("%s",src);
fd1 = open (src, O_RDONLY);
printf("The contents of copied file are\n");
while ((nread = read (fd1, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close(fd1);
break;
case 2:
printf("Enter filename to move\n");
scanf("%s",src);
printf("Enter filename to store\n");
scanf("%s",dest);
fd1 = open(src, O_RDONLY);
fd2 = open(dest, O_CREAT|O_WRONLY|O_TRUNC,0666);
while((nread=read(fd1, buf, sizeof(buf))) > 0)
{
write(fd2, buf, nread);
}
close(fd2);
close(fd1);
nread=remove(src);
LINUX PROGRAMMING
18
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
if(nread==0)
{
printf("%s renamed to %s\n",src,dest);
}
fd1 = open(dest, O_RDONLY);
while((nread=read(fd1, buf, sizeof(buf))) > 0)
{
write(1, buf, nread);
}
break;
default:
printf("Wrong Choice\n");
}
}
OUTPUT 1:
cc prog11.c -o prg11
./prg11
1. cat
2. mv
Enter your choice
1
Enter filename to display
file
The contents of copied file are
LAB Programs
lp1.sh
lp2.sh
lp3.sh
lp4.sh
lp5-1.sh
lp5.sh
OUTPUT2:
./prg11
1. cat
2. mv
Enter your choice
2
Enter filename to move
LINUX PROGRAMMING
19
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
file
Enter filename to store
file5
file renamed to file5
LAB Programs
lp1.sh
lp2.sh
lp3.sh
lp4.sh
lp5-1.sh
lp5.sh
LINUX PROGRAMMING
20
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
12. Write a C program to list files in a directory
Source Code:
#include<stdio.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
void main()
{
DIR *dp;
struct dirent *p;
char dname[20];
printf("Enter the directory name:");
scanf("%s",dname);
dp=opendir(dname);
printf("List of Files\n");
while((p=readdir(dp))!=NULL)
{
printf("%s\n",p->d_name);
}
}
Output:
.
file1
file2
file3
file4
..
LINUX PROGRAMMING
21
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
22
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
13. Write a C program to emulate the UNIX ls –l command.
Source code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int pid;
pid = fork();
if ( pid < 0 )
{
printf("\nFork failed\n");
exit (-1);
}
else if ( pid == 0 )
{
execlp ("/bin/ls", "ls", "-l", NULL );
}
else
{
wait (NULL);
printf("\nchild complete\n");
exit (0);
}
}
OUTPUT:
total 208
-rw-rw-r-- 1 kmit ngit 144 Oct 23 12:12 file1
-rw-rw-r-- 1 kmit ngit 196 Oct 23 14:52 file15
-rw-rw-r-- 1 kmit ngit 57 Oct 28 19:29 file5
-rw-rw-r-- 1 kmit ngit 141 Oct 23 14:31 file.txt
-rwxrwxr-x 1 kmit ngit 9032 Oct 23 10:39 prg11
-rwxrwxr-x 1 kmit ngit 8880 Oct 23 11:10 prg12
LINUX PROGRAMMING
23
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
24
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
14. Write a C program to list for every file in a directory, its inode number and file name.
Source Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
main()
{
DIR *dp;
struct dirent *p;
char dname[20];
struct stat x;
printf("Enter the directory name:");
scanf("%s",dname);
dp=opendir(dname);
printf("\n FILE NAME\t INODE NUMBER\n");
while((p=readdir(dp))!=NULL)
{
printf("%s\t %ld\n",p->d_name,x.st_ino);
}
}
OUTPUT:
-bash-3.2$ cc inode.c -o inode
-bash-3.2$ ./inode
Enter the directory name:lunix
LINUX PROGRAMMING
25
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
15. Write a C program that demonstrates redirection of standard output to a file.
Ex: ls > f1.
Source code:
#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,"> ");
strcat(d,argv[1]);
system(d);
}
else
printf("\nInvalid No. of inputs");
}
OUTPUT:
$cc file.c
$./a.out file1
$cat file1
a.out file5 prg12 prg16 prog10 prog11b prog13.c prog18.c
LINUX PROGRAMMING
26
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
pid=fork();
if(pid<0)
{
perror("error in fork:");
}
if(pid==0)
{
printf("\nchild process..");
}
if(pid>0)
{
printf("\nparent process..");
}
}
Execution:
$gcc child.c -o child
$./child
Child
parent
Conclusion:
A c program is successfully executed to display child process as “child” parent process
as “parent”.
LINUX PROGRAMMING
27
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
17. Write a C program to create a Zombie process.
If child terminates before the parent process then parent process with out child is called
zombie process
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
if(fork()>0)
{
printf("parent with %d",getpid());
sleep(10);
}
}
Execution:
$gcc zombie.c –o zombie
$./ zombie &
$ps Z
LABEL PID TTY STAT TIME COMMAND
user_u:system_r:unconfined_t 3769 pts/1 Ss 0:00 bash
user_u:system_r:unconfined_t 3799 pts/1 S 0:00 ./zom
user_u:system_r:unconfined_t 3800 pts/1 Z 0:00 [zom] <defunct>
user_u:system_r:unconfined_t 3801 pts/1 R+ 0:00 ps Z
Conclusion:
A c programme is successfully executed to create zombie process.
OUTPUT:
LINUX PROGRAMMING
28
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
If we run the preceding program with ./fork2 & and then call the ps program after the
child has finished
but before the parent has finished, we’ll see a line such as this. (Some systems may say
<zombie>
rather than <defunct>.)
$ ps –al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
004 S 0 1273 1259 0 75 0 - 589 wait4 pts/2 00:00:00 su
000 S 0 1274 1273 0 75 0 - 731 schedu pts/2 00:00:00 bash
000 S 500 1463 1262 0 75 0 - 788 schedu pts/1 00:00:00 oclock
000 S 500 1465 1262 0 75 0 - 2569 schedu pts/1 00:00:01 emacs
000 S 500 1603 1262 0 75 0 - 313 schedu pts/1 00:00:00 fork2
003 Z 500 1604 1603 0 75 0 - 0 do_exi pts/1 00:00:00 fork2 <defunct>
000 R 500 1605 1262 0 81 0 - 781 - pts/1 00:00:00 ps
LINUX PROGRAMMING
29
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
18. Write a C program that illustrates how an orphan is created.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("child process...");
printf("process id=%d,parentid=%d",getpid(),getppid());
sleep(15);
printf("termination of child");
}
if(pid>0)
{
printf("parent process:%d",getpid());
printf("termination of parent.");
}
}
Execution:
$gcc orphan.c –o orphan
$./orphan
Parent process:3395
Termination of parent
Child process..
Child id:3396 parent id:3395
Termination of child...
LINUX PROGRAMMING
30
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
19. Write a C program that illustrates how to execute two commands concurrently with a
command pipe.
Ex: - ls –l | sort
DESCRIPTION:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
LINUX PROGRAMMING
31
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("execl");
}
}
OUTPUT:
prog11.c
prog12.c
prog13.c
prog14.c
prog15.c
prog16.c
prog18.c
prog19.c
temp2
test.txt
LINUX PROGRAMMING
32
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
20. Write C programs that illustrate communication between two unrelated processes
using named pipe.
Process1.c
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
int fd1;
mkfifo(myfifo, 0666);
while (1)
{
close(fd1);
LINUX PROGRAMMING
33
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
close(fd1);
}
return 0;
Process2.c
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
int fd;
mkfifo(myfifo, 0666);
while (1)
LINUX PROGRAMMING
34
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
{
close(fd);
close(fd);
}
return 0;
Output:
Terminal-1 Terminal-2
$ ./server $ ./client
hi user1: hi
LINUX PROGRAMMING
35
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
36
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
21.Write a C program in which a parent writes a message to a pipe and the child reads the
message.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
OUTPUT:
LINUX PROGRAMMING
37
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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.
MesageQsender.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 255
struct mesgbuf
{
long type;
char mtext[MAX];
};
char buff[MAX];
main()
{
int mid,fd,n,count=0;
struct mesgbuf mesg;
if((mid=msgget(1006,IPC_CREAT | 0666))<0)
LINUX PROGRAMMING
38
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
{
printf("\n Can‟t create Message Q");
exit(1);
}
printf("\n Queue id:%d", mid);
mesg.type=6;
fd=open("fact",O_RDONLY);
while(read(fd,buff,25)>0)
{
strcpy(mesg.mtext,buff);
if(msgsnd(mid, &mesg, sizeof(struct mesgbuf), 0)== -1)
printf("\n Message Write Error");
}
if((mid=msgget(1006,0))<0)
{
printf("\n Can‟t create Message Q");
exit(1);
}
while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>0)
write(1,mesg.mtext,n);
count++;
if((n== -1)&&(count==0))
printf("\n No Message Queue on Queue:%d",mid);
}
OUTPUT:
LINUX PROGRAMMING
39
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
$ cat fact
hi
hello
KMIT
$ cc -o sender MesageQsender.c
$ ./sender
Queue id:65538
23.Write a C program (receiver.c) that receives the messages (from the above message
queue as specified in (22)) and displays them
MesageQrecvr.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 255
struct msgque
{
long type;
char mtext[MAX];
};
int main(void)
LINUX PROGRAMMING
40
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
{
struct msgque buf;
int msqid;
key_t key;
key= 1006;
if ((msqid = msgget(key, 0644)) == -1) {
perror("msgget");
exit(1);
}
if (msgrcv(msqid, &buf, sizeof(struct msgque), 6, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("Receiver: \"%s\"\n", buf.mtext);
exit(0);
}
OUTPUT
$ cc -o receiverMesageQrecvr.c
$ ./receiver
Receiver: "hi
hello
KMIT
"
LINUX PROGRAMMING
41
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
LINUX PROGRAMMING
42
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
24 .Write a C program that illustrates suspending and resuming processes using signals.
Program.c
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
int main(void)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
while(1)
sleep(1);
return 0;
}
OUTPUT:
$ cc -o signal Program.c
$ ./signal
^Creceived SIGINT //press ctrl+c
^Creceived SIGINT //press ctrl+c
^Creceived SIGINT //press ctrl+c
^Z //press ctrl+z
[2]+ Stopped ./signal
$ bg //Suspended
[2]+ ./signal &
$ fg //Resumed
^Creceived SIGINT
LINUX PROGRAMMING
43
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
^Creceived SIGINT
^Creceived SIGINT
^Z
[2]+ Stopped ./signal
Note: Like this you can suspend the process and resume the process.
LINUX PROGRAMMING
44
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
25. Write client and server programs (using c) for interaction between server and client
processes using Unix Domain sockets.
Algorithm
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main()
int sockfd;
int len;
int result;
LINUX PROGRAMMING
45
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
address.sun_family=AF_UNIX;
perror(“oops:client”);
exit(1);
exit(0);
LINUX PROGRAMMING
46
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
int main()
/*remove any old sockets and create an unnamed socket for server*/
unlink (“server_socket”);
strcpy(server_address.sun_path, “server_socket”);
listen(server_sockfd, 5);
LINUX PROGRAMMING
47
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
while(1) {
char ch;
printf(“server waiting\n”);
ch++;
close(client_sockfd);
[2] 23412
[3] 23413
[4] 23414
server waiting
server waiting
LINUX PROGRAMMING
48
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
server waiting
server waiting
LINUX PROGRAMMING
49
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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;
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
LINUX PROGRAMMING
50
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
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>
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
LINUX PROGRAMMING
51
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
return 0;
}
LINUX PROGRAMMING
52
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
27. Write a C program that illustrates two processes communicating using shared
memory.
shmwr.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define NOT_READY (-1)
#define FILLED (0)
#define TAKEN (1)
struct Memory {
int status;
int data[4];
};
void main(int argc, char *argv[])
{
pid_t pid;
key_t ShmKEY;
int ShmID, i;
struct Memory *ShmPTR;
ShmKEY = ftok("./", 'x');
ShmID = shmget(ShmKEY, sizeof(struct Memory),
IPC_CREAT | 0666);
ShmPTR = (struct Memory *) shmat(ShmID, NULL, 0);
ShmPTR->status = NOT_READY;
for (i = 0; i < 4; i++)
{
ShmPTR->data[i] = atoi(argv[i+1]);
printf("%d ",ShmPTR->data[i]);
}
printf("\nfinished\n");
}
shmrd.c
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
LINUX PROGRAMMING
53
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
#include <sys/ipc.h>
#include <sys/shm.h>
#define NOT_READY (-1)
#define FILLED (0)
#define TAKEN (1)
struct Memory {
int status;
int data[4];
};
int ShmID;
pid_t pid;
key_t ShmKEY;
struct Memory *ShmPTR;
void main(void)
{
ShmKEY=ftok("./", 'x');
ShmID = shmget(ShmKEY, sizeof(struct Memory), 0666);
ShmPTR = (struct Memory *) shmat(ShmID, NULL, 0);
ShmPTR->status = TAKEN;
printf("%d %d %d %d\n", ShmPTR->data);
shmdt((void *) ShmPTR);
exit(0);
}
Output:
$cc -o write shmwr.c
$cc -o read shmrd.c
Termianl-1 Terminal-2
$ ./write KMIT $ ./read
KMIT KMIT
finished
LINUX PROGRAMMING
54