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

LP Lab Manual

This document contains a syllabus for Linux programming courses at the Department of Computer Science & Engineering at JNTU. It lists 27 topics to be covered over 10 weeks. The topics include writing shell scripts to perform tasks like displaying lines between given line numbers in a file, deleting lines containing a specified word, and checking file permissions. It also includes writing programs in C and awk to emulate Unix commands like cat, ls, and sort, as well as programs demonstrating processes, pipes, sockets and shared memory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

LP Lab Manual

This document contains a syllabus for Linux programming courses at the Department of Computer Science & Engineering at JNTU. It lists 27 topics to be covered over 10 weeks. The topics include writing shell scripts to perform tasks like displaying lines between given line numbers in a file, deleting lines containing a specified word, and checking file permissions. It also includes writing programs in C and awk to emulate Unix commands like cat, ls, and sort, as well as programs demonstrating processes, pipes, sockets and shared memory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 

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.

Week​ ​2-​ ​(29-07-17)


5.​ ​ ​ ​Write​ ​a​ ​shell​ ​script​ ​that​ ​accepts​ ​a​ ​list​ ​of​ ​file​ ​names​ ​as​ ​its​ ​arguments,​ ​counts​ ​and
2. reports​ ​the​ ​occurrence​ ​of​ ​each​ ​word​ ​that​ ​is​ ​present​ ​in​ ​the​ ​first​ ​argument​ ​file​ ​on​ ​other
argument​ ​files.

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.

Week​ ​3-​ ​(05-08-17)


​ ​8.​ ​ ​ ​Write​ ​an​ ​awk​ ​script​ ​to​ ​count​ ​the​ ​number​ ​of​ ​lines​ ​in​ ​a​ ​file​ ​that​ ​do​ ​not​ ​contain
​ ​ ​ ​ ​ ​3. 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.

LINUX​ ​PROGRAMMING  
 
​ ​1
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

Week​ ​4-​ ​(19-08-17)


​ ​ ​11.​ ​ ​ ​Implement​ ​in​ ​C​ ​the​ ​following​ ​UNIX​ ​commands​ ​using​ ​System​ ​calls
4. ​ ​ ​ ​A.​ ​cat​ ​ ​ ​ ​ ​ ​ ​ ​B.​ ​mv.
​ ​12.​ ​ ​Write​ ​a​ ​C​ ​program​ ​to​ ​list​ ​files​ ​in​ ​a​ ​directory.

Week​ ​5-​ ​(26-08-17)


13.​ ​ ​ ​Write​ ​a​ ​C​ ​program​ ​to​ ​emulate​ ​the​ ​UNIX​ ​ls​ ​–l​ ​command.
5.
14.​ ​ ​ ​Write​ ​a​ ​C​ ​program​ ​to​ ​list​ ​for​ ​every​ ​file​ ​in​ ​a​ ​directory,​ ​its​ ​inode​ ​number​ ​and​ ​file
name.
15.​ ​ ​ ​Write​ ​a​ ​C​ ​program​ ​that​ ​demonstrates​ ​redirection​ ​of​ ​standard​ ​output​ ​to​ ​a​ ​file.
Ex:​ ​ls​ ​>​ ​f1.

Week​ ​6-​ ​(16-09-17)


16.​ ​ ​Write​ ​a​ ​C​ ​program​ ​to​ ​create​ ​a​ ​child​ ​process​ ​and​ ​allow​ ​the​ ​parent​ ​to​ ​display
6. “parent”​ ​and​ ​ ​the​ ​child​ ​to​ ​display​ ​“child”​ ​on​ ​the​ ​screen.

17.​ ​ ​Write​ ​a​ ​C​ ​program​ ​to​ ​create​ ​a​ ​Zombie​ ​process.

18.​ ​ ​ ​Write​ ​a​ ​C​ ​program​ ​that​ ​illustrates​ ​how​ ​an​ ​orphan​ ​is​ ​created.

Week​ ​7-​ ​(23-09-17)


19.​ ​ ​ ​ ​Write​ ​a​ ​C​ ​program​ ​that​ ​illustrates​ ​how​ ​to​ ​execute​ ​two​ ​commands​ ​concurrently
7. with​ ​a​ ​command​ ​pipe.
​ ​Ex:​ ​-​ ​ls​ ​–l​ ​|​ ​sort.
20.​ ​Write​ ​C​ ​programs​ ​that​ ​illustrate​ ​communication​ ​between​ ​two​ ​unrelated​ ​processes
using​ ​named​ ​pipe.

21. Write a C program in which a parent writes a message to a pipe and the child reads
the​ ​message.

Week​ ​8-​ ​(07-10-17)


22. Write a C program (sender.c) to create a message queue with read and write
8. permissions​ ​to​ ​write​ ​3​ ​messages​ ​to​ ​it​ ​with​ ​different​ ​priority​ ​numbers.

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 

Week​ ​9-​ ​(21-10-17)


9. 24.​ ​Write​ ​a​ ​C​ ​program​ ​that​ ​illustrates​ ​suspending​ ​and​ ​resuming​ ​processes​ ​using
signals.
​ ​(Using​ ​Semaphores).
25.​ ​ ​Write​ ​client​ ​and​ ​server​ ​programs​ ​(using​ ​c)​ ​for​ ​interaction​ ​between​ ​server​ ​and
client​ ​processes​ ​using​ ​Unix​ ​Domain​ ​sockets.
10. Week​ ​10-​ ​(28-10-17)
26.​ ​ ​Write​ ​client​ ​and​ ​server​ ​programs​ ​(using​ ​c)​ ​for​ ​interaction​ ​between​ ​server​ ​and
client​ ​processes​ ​using​ ​Internet​ ​Domain​ ​sockets.

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”

echo​ ​"enter​ ​the​ ​filename"


read​ ​fname
echo​ ​"enter​ ​the​ ​starting​ ​line​ ​number"
read​ ​s
echo​ ​"enter​ ​the​ ​ending​ ​line​ ​number"
read​ ​n
sed​ ​-n​ ​$s,$n\p​ ​$fname​ ​|​ ​cat​ ​>​ ​newline
cat​ ​newline

OUTPUT:
$​ ​cat​ ​file
line​ ​number​ ​1
line​ ​number​ ​2
line​ ​number​ ​3
line​ ​number​ ​4
line​ ​number​ ​5
line​ ​number​ ​6
line​ ​number​ ​7

$​ ​chmod​ ​u+x​ ​prog1.sh


$​ ​./prog1.sh
enter​ ​the​ ​filename
file
enter​ ​the​ ​starting​ ​line​ ​number
2
enter​ ​the​ ​ending​ ​line​ ​number
4
line​ ​number​ ​2
line​ ​number​ ​3
line​ ​number​ ​4

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

$​ ​chmod​ ​u+x​ ​prog2.sh


$​ ​./prog2.sh​ ​file
sh​ ​prog2.sh​ ​file
Enter​ ​word
3
File​ ​content​ ​before​ ​delete​ ​the​ ​lines
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 

File​ ​content​ ​after​ ​delete​ ​the​ ​lines


line​ ​number​ ​1
line​ ​number​ ​2
line​ ​number​ ​4
line​ ​number​ ​5
line​ ​number​ ​6

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.

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​Aim:​ ​To​ ​find​ ​the​ ​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)

echo​ ​"Enter​ ​a​ ​number"


read​ ​n
f=1
a=$n
while​ ​[​ ​$n​ ​-ne​ ​0]
do
f=`expr​ ​$f​ ​\*​ ​$n`
n=`expr​ ​$n​ ​-​ ​1`
done
echo​ ​"Factorial​ ​of​ ​$a​ ​is​ ​$f"
Output:
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​Enter​ ​a​ ​number
5
Factorial​ ​of​ ​5​ ​is​ ​120

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

!/[aeiou]​ ​/​ ​{++n}


END​ ​{​ ​printf​ ​"the​ ​number​ ​of​ ​lines​ ​that​ ​don't​ ​contain​ ​vowels​ ​is​ ​:%d",​ ​n}

OUTPUT

$​ ​cat​ ​test1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
ccccccccccccccccccccccccccccccccccc
ddddddddddddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

$​ ​awk​ ​-f​ ​eight.awk​ ​test1


the​ ​number​ ​of​ ​lines​ ​that​ ​don't​ ​contain​ ​vowels​ ​is​ ​:3

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

$​ ​awk​ ​-f​ ​nine.awk​ ​test2


The​ ​no​ ​of​ ​characters​ ​is​ ​58
​ ​no​ ​of​ ​words​ ​is​ ​12
​ ​no​ ​of​ ​lines​ ​is​ ​3​ ​in​ ​a​ ​file

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 

$./a.out​ ​file1​ ​file2


$cat​ ​file2
Hai​ ​Hello

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.

Aim:​ ​To​ ​ ​emulate​ ​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 

-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8808​ ​Oct​ ​23​ ​11:22​ ​prg13


-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8936​ ​Oct​ ​23​ ​11:28​ ​prg14
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8824​ ​Oct​ ​23​ ​12:12​ ​prg15
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8864​ ​Oct​ ​23​ ​12:16​ ​prg16
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​9016​ ​Oct​ ​23​ ​12:19​ ​prg17
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8864​ ​Oct​ ​23​ ​12:41​ ​prg18
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​9072​ ​Oct​ ​23​ ​15:19​ ​prg19
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8880​ ​Oct​ ​23​ ​10:19​ ​prog10
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​478​ ​Oct​ ​28​ ​19:22​ ​prog10.c
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8816​ ​Oct​ ​23​ ​10:25​ ​prog11a
-rw-rw-rw-​ ​1​ ​kmit​ ​ngit​ ​ ​301​ ​Oct​ ​23​ ​10:25​ ​prog11a.c
-rwxrwxr-x​ ​1​ ​kmit​ ​ngit​ ​8760​ ​Oct​ ​23​ ​10:29​ ​prog11b
-rw-rw-rw-​ ​1​ ​kmit​ ​ngit​ ​ ​277​ ​Oct​ ​23​ ​10:28​ ​prog11b.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​1238​ ​Oct​ ​23​ ​10:39​ ​prog11.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​297​ ​Oct​ ​23​ ​11:10​ ​prog12.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​329​ ​Oct​ ​23​ ​11:22​ ​prog13.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​347​ ​Oct​ ​23​ ​11:29​ ​prog14.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​244​ ​Oct​ ​23​ ​12:12​ ​prog15.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​347​ ​Oct​ ​23​ ​12:16​ ​prog16.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​376​ ​Oct​ ​23​ ​12:41​ ​prog18.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​736​ ​Oct​ ​23​ ​15:19​ ​prog19.c
-rw-rw-r--​ ​1​ ​kmit​ ​ngit​ ​ ​141​ ​Oct​ ​23​ ​10:39​ ​temp2

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

FILE​ ​NAME​ ​ ​ ​INODE​ ​NUMBER


.. 4195164
file2.c 4195164
. 4195164
file1.c 4195164

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.

Aim:​ ​To​ ​create​ ​a​ ​child​ ​process

#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

Aim:​ ​To​ ​create​ ​a​ ​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

$​ ​parent​ ​with​ ​3799

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.

Aim:​​ ​to​ ​create​ ​an​ ​orphan​ ​process

​ ​#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

AIM:​​ ​Implementing​ ​Pipes

D​ESCRIPTION​:

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​A​ ​pipe​ ​is​ ​created​ ​by​ ​calling​ ​a​ ​pipe()​ ​function.


​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​int​ ​pipe(int​ ​filedesc[2]);
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​It​ ​returns​ ​a​ ​pair​ ​of​ ​file​ ​descriptors​ ​filedesc[0]​ ​is​ ​open​ ​for​ ​reading​ ​and​ ​filedesc[1]​ ​is
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​open​ ​for​ ​writing.​ ​This​ ​function​ ​returns​ ​a​ ​0​ ​if​ ​ok​ ​&​ ​-1​ ​on​ ​error.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

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


{
​ ​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]);

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.

AIM:​​ ​Implementing​ ​IPC​ ​using​ ​a​ ​FIFO​ ​(or)​ ​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;

​ ​ ​ ​ ​char​ ​*​ ​myfifo​ ​=​ ​"/tmp/myfifo";

​ ​ ​ ​ ​mkfifo(myfifo,​ ​0666);

​ ​ ​ ​ ​char​ ​str1[80],​ ​str2[80];

​ ​ ​ ​ ​while​ ​(1)

​ ​ ​ ​ ​{

​ ​ ​ ​ ​ ​ ​ ​ ​fd1​ ​=​ ​open(myfifo,O_RDONLY);

​ ​ ​ ​ ​ ​ ​ ​ ​read(fd1,​ ​str1,​ ​80);

​ ​ ​ ​ ​ ​ ​ ​ ​printf("User1:​ ​%s\n",​ ​str1);

​ ​ ​ ​ ​ ​ ​ ​ ​close(fd1);

LINUX​ ​PROGRAMMING  
 
​ ​33
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

​ ​ ​ ​ ​ ​ ​ ​ ​fd1​ ​=​ ​open(myfifo,O_WRONLY);

​ ​ ​ ​ ​ ​ ​ ​ ​fgets(str2,​ ​80,​ ​stdin);

​ ​ ​ ​ ​ ​ ​ ​ ​write(fd1,​ ​str2,​ ​strlen(str2)+1);

​ ​ ​ ​ ​ ​ ​ ​ ​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;

​ ​ ​ ​ ​char​ ​*​ ​myfifo​ ​=​ ​"/tmp/myfifo";

​ ​ ​ ​ ​mkfifo(myfifo,​ ​0666);

​ ​ ​ ​ ​char​ ​arr1[80],​ ​arr2[80];

​ ​ ​ ​ ​while​ ​(1)

LINUX​ ​PROGRAMMING  
 
​ ​34
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

​ ​ ​ ​ ​{

​ ​ ​ ​ ​ ​ ​ ​ ​fd​ ​=​ ​open(myfifo,​ ​O_WRONLY);

​ ​ ​ ​ ​ ​ ​ ​ ​fgets(arr2,​ ​80,​ ​stdin);

​ ​ ​ ​ ​ ​ ​ ​ ​write(fd,​ ​arr2,​ ​strlen(arr2)+1);

​ ​ ​ ​ ​ ​ ​ ​ ​close(fd);

​ ​ ​ ​ ​ ​ ​ ​ ​fd​ ​=​ ​open(myfifo,​ ​O_RDONLY);

​ ​ ​ ​ ​ ​ ​ ​ ​read(fd,​ ​arr1,​ ​sizeof(arr1));

​ ​ ​ ​ ​ ​ ​ ​ ​printf("User2:​ ​%s\n",​ ​arr1);

​ ​ ​ ​ ​ ​ ​ ​ ​close(fd);

​ ​ ​ ​ ​}

​ ​ ​ ​ ​return​ ​0;

Output:

$cc​ ​-o​ ​server​ ​Process2.c

$cc​ ​-o​ ​client​ ​Process1.c

Now​ ​run​ ​these​ ​two​ ​executable​ ​files​ ​in​ ​two​ ​terminals

Terminal-1 Terminal-2

$​ ​./server $​ ​./client

hi user1:​ ​hi

LINUX​ ​PROGRAMMING  
 
​ ​35
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

user2:​ ​hello hello

welcome user1:​​ ​welcome

to​ ​KMIT to?

User2:​​ ​to? user1:​​ ​to​ ​KMIT

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>

int​ ​main​ ​(void)


{
int​ ​pc[2],cp[2];
char​ ​req[100],res[200];
strcpy(res,"response​ ​is​ ​");
pipe(pc);
pipe(cp);
int​ ​pid​ ​=​ ​fork();
if(pid​ ​==​ ​0)
{
close(pc[1]);
close(cp[0]);
read(pc[0],req,100);
printf("Parent:​ ​%s\n",req);
strcat(res,req);
write(cp[1],res,500);
}
else
{
close(pc[0]);
close(cp[1]);
scanf("%[^\n]",req);
write(pc[1],req,100);
read(cp[0],res,500);
printf("Child:​ ​%s​ ​\n",res);
}
}

OUTPUT:

LINUX​ ​PROGRAMMING  
 
​ ​37
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

$​ ​cc​ ​-o​ ​prg21​ ​prog21.c


$​ ​./prg21
hi​ ​welcome​ ​to​ ​kmit
Parent:​ ​hi​ ​welcome​ ​to​ ​kmit
Child:​ ​response​ ​is​ ​hi​ ​welcome​ ​to​ ​kmit

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>

void​ ​sig_handler(int​ ​signo)


{
​ ​ ​if​ ​(signo​ ​==​ ​SIGINT)
​ ​ ​ ​ ​printf("received​ ​SIGINT\n");
}

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

Step​ ​1:​ ​Start​ ​the​ ​program


Step​ ​2:​ ​The​ ​server​ ​creates​ ​a​ ​socket​ ​ ​and​ ​waits​ ​for​ ​a​ ​request​ ​for​ ​connection.
Step​ ​3:​ ​If​ ​client​ ​starts​ ​we​ ​are​ ​successful​ ​in​ ​connecting​ ​to​ ​the​ ​server.
Step 4: The server receives a character from the client, increments it, and returns it. The server
continues​ ​and​ ​waits​ ​for​ ​the​ ​next​ ​client.

A​ ​SIMPLE​ ​LOCAL​ ​CLIENT

/*The​ ​necessary​ ​include​ ​and​ ​set​ ​up​ ​variables*/

#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;

struct​ ​ ​sockaddr_un​ ​address;

int​ ​ ​result;

char​ ​ch​ ​=’A’;

LINUX​ ​PROGRAMMING  
 
​ ​45
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

/*​ ​create​ ​a​ ​socket​ ​for​ ​the​ ​client*/

sockfd​ ​ ​=​ ​ ​socket(AF_UNIX,​ ​ ​SOCK_STREAM,​ ​ ​0);

/*​ ​name​ ​the​ ​cocket​ ​as​ ​agreed​ ​with​ ​the​ ​server*/

address.sun_family=AF_UNIX;

strcpy(address.sun_family​ ​ ​=​ ​ ​AF_UNIX;

len​ ​ ​=​ ​ ​sizeof(address);

/*connect​ ​your​ ​socket​ ​to​ ​the​ ​server’s​ ​socket:*/

result​ ​=​ ​connect(sockfd,​ ​(struct​ ​ ​sockaddr*)&address,​ ​ ​len);

if​ ​(result​ ​==​ ​-1)​ ​ ​{

perror(“oops:client”);

exit(1);

/*you​ ​can​ ​now​ ​read​ ​and​ ​write​ ​via​ ​sockfd:*/

write(sockfd,​ ​ ​&ch,​ ​ ​1);

read(sockfd,​ ​ ​&ch,​ ​ ​1);

printf(“char​ ​from​ ​server​ ​ ​=​ ​%c\n”,​ ​ ​ch);

exit(0);

A​ ​SIMPLE​ ​LOCAL​ ​SERVER

/*The​ ​necessary​ ​include​ ​and​ ​set​ ​up​ ​variables*/

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()

int​ ​ ​server_sockfd,​ ​ ​client_sockfd;

int​ ​ ​server_len,​ ​client_len;

struct​ ​ ​sockaddr_un​ ​ ​server_address;

struct​ ​ ​sockaddr_un​ ​ ​client_address;

/*remove​ ​any​ ​old​ ​sockets​ ​and​ ​create​ ​an​ ​unnamed​ ​socket​ ​for​ ​server*/

unlink​ ​ ​(“server_socket”);

server_sockfd​ ​ ​=​ ​ ​socket​ ​(AF_UNIX,​ ​ ​SOCK_STREAM,​ ​ ​0);

/*name​ ​the​ ​socket​ ​*/

server_address.sun_family​ ​ ​=​ ​ ​AF_UNIX;

strcpy(server_address.sun_path,​ ​ ​“server_socket”);

server_len​ ​ ​=​ ​ ​sizeof​ ​(server_address);

bind​ ​(server_sockfd,​ ​ ​(struct​ ​ ​sockaddr​ ​*)&server_address,​ ​ ​server_len);

/*create​ ​a​ ​connection​ ​queue​ ​and​ ​wait​ ​for​ ​clients*/

listen(server_sockfd,​ ​ ​5);

LINUX​ ​PROGRAMMING  
 
​ ​47
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

while(1)​ ​ ​ ​{

char​ ​ch;

printf(“server​ ​waiting\n”);

/*accept​ ​a​ ​connection*/

client_len​ ​ ​=​ ​ ​sizeof(client_address);

client_sockfd​ ​ ​=​ ​ ​accept​ ​(server_sockfd,

struct​ ​ ​sockaddr​ ​*)&client_address,​ ​&client_len);

/*read​ ​and​ ​write​ ​to​ ​client​ ​on​ ​client_sockfd*/

read(client_sockfd,​ ​ ​&ch,​ ​ ​1);

ch++;

write(client_sockfd,​ ​ ​&ch,​ ​ ​1);

close(client_sockfd);

$​ ​./client1​ ​&​ ​./client1​ ​&​ ​./client1​ ​&

[2]​ ​23412

[3]​ ​ ​23413

[4]​ ​ ​23414

server​ ​waiting

char​ ​from​ ​server​ ​ ​=​ ​ ​B

server​ ​waiting

char​ ​from​ ​server​ ​ ​=​ ​ ​B

LINUX​ ​PROGRAMMING  
 
​ ​48
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

server​ ​waiting

char​ ​from​ ​server​ ​ ​=​ ​ ​B

server​ ​waiting

[2] Done client1

[3]- Done client1

[4]+ Done client1

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>

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


{
​ ​ ​ ​ ​int​ ​listenfd​ ​=​ ​0,​ ​connfd​ ​=​ ​0;
​ ​ ​ ​ ​struct​ ​sockaddr_in​ ​serv_addr;

​ ​ ​ ​ ​char​ ​sendBuff[1025];
​ ​ ​ ​ ​time_t​ ​ticks;

​ ​ ​ ​ ​listenfd​ ​=​ ​socket(AF_INET,​ ​SOCK_STREAM,​ ​0);


​ ​ ​ ​ ​memset(&serv_addr,​ ​'0',​ ​sizeof(serv_addr));
​ ​ ​ ​ ​memset(sendBuff,​ ​'0',​ ​sizeof(sendBuff));

​ ​ ​ ​ ​serv_addr.sin_family​ ​=​ ​AF_INET;


​ ​ ​ ​ ​serv_addr.sin_addr.s_addr​ ​=​ ​htonl(INADDR_ANY);
​ ​ ​ ​ ​serv_addr.sin_port​ ​=​ ​htons(5000);

​ ​ ​ ​ ​bind(listenfd,​ ​(struct​ ​sockaddr*)&serv_addr,​ ​sizeof(serv_addr));

​ ​ ​ ​ ​listen(listenfd,​ ​10);

​ ​ ​ ​ ​while(1)
​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​connfd​ ​=​ ​accept(listenfd,​ ​(struct​ ​sockaddr*)NULL,​ ​NULL);

LINUX​ ​PROGRAMMING  
 
​ ​50
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

​ ​ ​ ​ ​ ​ ​ ​ ​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>

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


{
​ ​ ​ ​ ​int​ ​sockfd​ ​=​ ​0,​ ​n​ ​=​ ​0;
​ ​ ​ ​ ​char​ ​recvBuff[1024];
​ ​ ​ ​ ​struct​ ​sockaddr_in​ ​serv_addr;

​ ​ ​ ​ ​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;
​ ​ ​ ​ ​}

LINUX​ ​PROGRAMMING  
 
​ ​51
DEPARTMENT​ ​OF​ ​COMPUTER​ ​SCIENCE​ ​& 
ENGINEERING 

​ ​ ​ ​ ​memset(&serv_addr,​ ​'0',​ ​sizeof(serv_addr));

​ ​ ​ ​ ​serv_addr.sin_family​ ​=​ ​AF_INET;


​ ​ ​ ​ ​serv_addr.sin_port​ ​=​ ​htons(5000);

​ ​ ​ ​ ​if(inet_pton(AF_INET,​ ​argv[1],​ ​&serv_addr.sin_addr)<=0)


​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​printf("\n​ ​inet_pton​ ​error​ ​occured\n");
​ ​ ​ ​ ​ ​ ​ ​ ​return​ ​1;
​ ​ ​ ​ ​}

​ ​ ​ ​ ​if(​ ​connect(sockfd,​ ​(struct​ ​sockaddr​ ​*)&serv_addr,​ ​sizeof(serv_addr))​ ​<​ ​0)


​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​printf("\n​ ​Error​ ​:​ ​Connect​ ​Failed​ ​\n");
​ ​ ​ ​ ​ ​ ​ ​return​ ​1;
​ ​ ​ ​ ​}

​ ​ ​ ​ ​while​ ​(​ ​(n​ ​=​ ​read(sockfd,​ ​recvBuff,​ ​sizeof(recvBuff)-1))​ ​>​ ​0)


​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​recvBuff[n]​ ​=​ ​0;
​ ​ ​ ​ ​ ​ ​ ​ ​if(fputs(recvBuff,​ ​stdout)​ ​==​ ​EOF)
​ ​ ​ ​ ​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​printf("\n​ ​Error​ ​:​ ​Fputs​ ​error\n");
​ ​ ​ ​ ​ ​ ​ ​ ​}
​ ​ ​ ​ ​}

​ ​ ​ ​ ​if(n​ ​<​ ​0)


​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​printf("\n​ ​Read​ ​error​ ​\n");
​ ​ ​ ​ ​}

​ ​ ​ ​ ​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

You might also like