0% found this document useful (0 votes)
117 views112 pages

Linux Programming (R15a0527)

This document provides an overview of a course on Linux Programming. It includes 4 units that cover Linux utilities, files and directories, processes, signals, and interprocess communication. Some key topics include file handling utilities, shell scripting with bash, system calls for file I/O, process creation and termination, signals, and IPC mechanisms like pipes, message queues, semaphores, shared memory, and sockets. The objectives are to develop skills in Unix systems programming, make effective use of Unix utilities and shell scripting, and write network programs using sockets.
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)
117 views112 pages

Linux Programming (R15a0527)

This document provides an overview of a course on Linux Programming. It includes 4 units that cover Linux utilities, files and directories, processes, signals, and interprocess communication. Some key topics include file handling utilities, shell scripting with bash, system calls for file I/O, process creation and termination, signals, and IPC mechanisms like pipes, message queues, semaphores, shared memory, and sockets. The objectives are to develop skills in Unix systems programming, make effective use of Unix utilities and shell scripting, and write network programs using sockets.
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/ 112

DIGITAL NOTES

ON
LINUX PROGRAMMING

B.TECH III- YEAR – I-SEM


(2018-19)

DEPARTMENT OF INFORMATION TECHNOLOGY

MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY


(Autonomous Institution – UGC, Govt. of India)
(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 Certified)
Maisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, INDIA.

LINUX PROGRAMMING Page 1


MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY

III Year B.Tech. IT - I Sem L T/P/D C


5 1/-/- 4

(R15A0527)LINUX PROGRAMMING
Objectives:
 To develop the skills necessary for Unix systems programming including file system
programming, process and signal management, and interprocess communication.
 To make effective use of Unix utilities and Shell scripting language such as bash.
 To develop the basic skills required to write network programs using Sockets.

UNIT I
Linux Utilities-File handling utilities, Security by file permissions, Process utilities, Disk utilities,
Networking commands, Filters, Text processing utilities and Backup utilities.
Sed-Scripts, Operation, Addresses, Commands, Applications, awk- Execution, Fields and Records,
Scripts, Operation, Patterns, Actions, Associative Arrays, String and Mathematical functions, System
commands in awk, Applications.
Shell programming with Bourne again shell(bash)- Introduction, shell responsibilities, pipes and
Redirection, here documents, running a shell script, the shell as a programming language, shell meta
characters, file name substitution, shell variables, command substitution, shell commands, the
environment, quoting, test command, control structures, arithmetic in shell, shell script examples,
interrupt processing, functions, debugging shell scripts.

UNIT II
Files and Directories- File Concept, File types, File System Structure, file metadata-Inodes, kernel
support for files, system calls for file I/O operations- open, create, read, write, close, lseek, dup2,file
status information-stat family, file and record locking-lockf and fcntl functions, file permissions -
chmod, fchmod, file ownership-chown, lchown, fchown, links-soft links and hard links – symlink, link,
unlink. Directories-Creating, removing and changing Directories-mkdir, rmdir, chdir, obtaining
current working directory-getcwd, Directory contents, Scanning Directories-opendir, readdir,
closedir, rewinddir, seekdir, telldir functions.

UNIT III
Process – Process concept, Kernel support for process, process identification, process hierarchy,
process states, process control - process creation, waiting for a process, process termination, zombie
process, orphan process, system call interface for process management-fork, vfork, exit, wait,
waitpid, exec family, system, I/O redirection
Signals – Introduction to signals, Signal generation and handling, Kernel support for signals, Signal
function, unreliable signals, reliable signals, kill, raise , alarm, pause, abort, sleep functions.

UNIT IV
Interprocess Communication - Introduction to IPC, IPC between processes on a single computer
system,IPC between processes on different systems, pipes-creation, IPC between related processes
using unnamed pipes, FIFOs-creation, IPC between unrelated processes using FIFOs (Named
pipes),differences between unnamed and named pipes, popen and pclose library functions.Message
Queues- Kernel support for messages, APIs for message queues, client/server example.Semaphores-
Kernel support for semaphores, APIs for semaphores, file locking with semaphores.

LINUX PROGRAMMING Page 2


UNIT V
Shared Memory- Kernel support for shared memory, APIs for shared memory, shared memory
example.
Sockets- Introduction to Berkeley Sockets, IPC over a network, Client-Server model, Socket address
structures (Unix domain and Internet domain),Socket system calls for connection oriented protocol
and connectionless protocol, example-client/server programs-Single Server-Client connection,
Multiple simultaneous clients, Comparison of IPC mechanisms.

TEXT BOOKS:
1. Unix System Programming using C++, T.Chan, PHI.
2. Unix Concepts and Applications, 4th Edition, Sumitabha Das, TMH,2006.
3. Unix Network Programming, W.R.Stevens, PHI

REFERENCE BOOKS:
1. Linux System Programming, Robert Love, O’Reilly, SPD, rp-2007.
2. Unix for programmers and users, 3rd Edition, Graham Glass, King Ables, Pearson2003,
3. Advanced Programming in the Unix environment, 2nd Edition, W.R.Stevens, Pearson .
4. System Programming with C and Unix, A.Hoover, Pearson.

Outcomes:
 Students will be able to use Linux environment efficiently
 Solve problems using bash for shell scripting
 Work confidently in Unix/Linux environment

LINUX PROGRAMMING Page 3


MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY

INDEX

S. No Topic Page no
Unit
Linux Utilities-File handling utilities, Security by file
1 7-12
permissions, Process utilities, Disk utilities,
Networking commands Filters, Text processing utilities and
2 Backup utilities. 13-18

3 Sed-Scripts, Operation, Addresses, Commands, Applications 19-21


I
awk- Execution, Fields and Records, Scripts, Operation,
4 22-23
Patterns, Actions, Associative Arrays
String and Mathematical functions, System commands in awk,
5 Applications. 24-43

Shell programming with Bourne again shell(bash)- Introduction,


shell responsibilities, pipes and
6 Redirection, here documents, running a shell script, the shell as 44-49
a programming language, shell meta characters, file name
substitution
shell variables, command substitution, shell commands, the
50-56
environment, quoting, test command, control structures
II Files and Directories- File Concept, File types, File System
7 57-59
Structure, file metadata-Inodes,

kernel support for files, system calls for file I/O operations-
8 59-62
open, create, read, write, close, lseek, dup2
file and record locking-lockf and fcntl functions, file
9 permissions - chmod, fchmod, file ownership-chown, lchown, 63-65
fchown, links-soft links and hard links – symlink, link, unlink
Directories-Creating, removing and changing Directories-mkdir,
rmdir, chdir, obtaining current working directory-getcwd, 66-71
Directory contents
Process – Process concept, Kernel support for process, process
identification, process hierarchy, process states, process control
10 72-74
- process creation, waiting for a process, process termination,
III zombie process
orphan process, system call interface for process management-
11 fork, vfork, exit, wait, waitpid, exec family, system, I/O 75-80
redirection
Signals – Introduction to signals, Signal generation and
handling, Kernel support for signals, Signal function, unreliable
81-86
signals, reliable signals, kill, raise , alarm, pause, abort, sleep
functions
Interprocess Communication - Introduction to IPC, IPC between
IV processes on a single computer system,IPC between processes
12 87-90
on different systems, pipes-creation, IPC between related
processes using unnamed pipes

LINUX PROGRAMMING Page 4


FIFOs-creation, IPC between unrelated processes using FIFOs
(Named pipes),differences between unnamed and named pipes,
91-92
popen and pclose library functions.Message Queues- Kernel
support for messages, APIs for message queues
client/server example.Semaphores-Kernel support for
93-94
semaphores, APIs for semaphores, file locking with semaphores

Shared Memory- Kernel support for shared memory, APIs for


13 95-99
shared memory, shared memory example
V
Sockets- Introduction to Berkeley Sockets, IPC over a network,
14 100-107
Client-Server model, Socket address structures
Socket system calls for connection oriented protocol and
connectionless protocol, Multiple simultaneous clients, 108-110
Comparison of IPC mechanisms

LINUX PROGRAMMING Page 5


MALLA REDDY COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY

UNIT-1

LINUX PROGRAMMING Page 6


LINUX PROGRAMMING Page 7
LINUX PROGRAMMING Page 8
LINUX PROGRAMMING Page 9
LINUX PROGRAMMING Page 10
LINUX PROGRAMMING Page 11
LINUX PROGRAMMING Page 12
LINUX PROGRAMMING Page 13
LINUX PROGRAMMING Page 14
LINUX PROGRAMMING Page 15
LINUX PROGRAMMING Page 16
LINUX PROGRAMMING Page 17
LINUX PROGRAMMING Page 18
LINUX PROGRAMMING Page 19
LINUX PROGRAMMING Page 20
LINUX PROGRAMMING Page 21
LINUX PROGRAMMING Page 22
LINUX PROGRAMMING Page 23
LINUX PROGRAMMING Page 24
LINUX PROGRAMMING Page 25
LINUX PROGRAMMING Page 26
LINUX PROGRAMMING Page 27
LINUX PROGRAMMING Page 28
LINUX PROGRAMMING Page 29
LINUX PROGRAMMING Page 30
LINUX PROGRAMMING Page 31
LINUX PROGRAMMING Page 32
LINUX PROGRAMMING Page 33
LINUX PROGRAMMING Page 34
LINUX PROGRAMMING Page 35
LINUX PROGRAMMING Page 36
LINUX PROGRAMMING Page 37
LINUX PROGRAMMING Page 38
LINUX PROGRAMMING Page 39
LINUX PROGRAMMING Page 40
LINUX PROGRAMMING Page 41
LINUX PROGRAMMING Page 42
LINUX PROGRAMMING Page 43
LINUX PROGRAMMING Page 44
LINUX PROGRAMMING Page 45
LINUX PROGRAMMING Page 46
LINUX PROGRAMMING Page 47
LINUX PROGRAMMING Page 48
LINUX PROGRAMMING Page 49
LINUX PROGRAMMING Page 50
LINUX PROGRAMMING Page 51
LINUX PROGRAMMING Page 52
LINUX PROGRAMMING Page 53
LINUX PROGRAMMING Page 54
LINUX PROGRAMMING Page 55
Unit II – Files and Directories
Working with Files

In this chapter we learn how to create, open, read, write, and close files.

UNIX File Structure

In UNIX, everything is a file.


Programs can use disk files, serial ports, printers and other
devices in the exactly the same way as they would use a file.
Directories, too, are special sorts of files.
Directories

As well as its contents, a file has a name and 'administrative


information', i.e. the file's creation/modification date and its
permissions.

The permissions are stored in the inode, which also


contains the length of the file and where on the disc it's
stored.

A directory is a file that holds the inodes


and names of other files. Files are
arranged in directories, which also contain
subdirectories.
A user, neil, usually has his files stores in a 'home' directory, perhaps /home/neil.

LINUX PROGRAMMING Page 56


Files and Devices

Even hardware devices are represented (mapped) by files in UNIX. For


example, as
root, you mount a CD-ROM drive as a file,

$ mount -t iso9660 /dev/hdc /mnt/cd_rom


$ cd /mnt/cd_rom

Low-level File Access

Each running program, called a process, has associated with it a number of file
descriptors.
LINUX PROGRAMMING Page 57
When a program starts, it usually has three of these descriptors already
opened. These are: The write system call arranges for the first n bytes
bytes from buf to be written to the file associated with the file descriptor
files.

With this knowledge, let's write our first program, simple_write.c:

dup and dup2

The dup system calls provide a way of duplicating a file descriptor, giving
two or more, different descriptors that access the same file.

The Standard I/O Library

The standard I/O library and its header file stdio.h, provide a versatile
interface to low-level I/O system calls.

Three file streams are automatically opened when a program is


started. They are stdin, stdout, and stderr.

Now, let's look at:

fopen

LINUX PROGRAMMING Page 58


The fopen library function is the analog of the low level open system call.

fopen opens the file named by the filename parameter and associates a stream with it.
The mode
parameter specifies how the file is to be opened. It's one of the following strings:

If successful, fopen returns a non-null FILE * pointer.


fread
The fread library function is used to read data from a file stream. Data is read
into a data buffer given by ptr from the stream, stream.
Fwrite
The fwrite library call has a similar interface to fread. It takes data records from the
specified
data buffer and writes them to the output stream.
fclose

The fclose library function closes the specified stream, causing any unwritten data to be
written.

Fflush

The fflush library function causes all outpstanding data on a file stream to be written
immediately.
fseek

The fseek function is the file stream equivalent of the lseek system call.
It sets the position in the stream for the next read or write on that stream.

LINUX PROGRAMMING Page 59


fgetc, getc, getchar

The fgetc function returns the next byte, as a character, from a file
stream. When it reaches the end of file, it returns EOF.
The getc function is equivalent to fgetc, except that you can
implement it as a macro. The getchar function is equivalent to
getc(stdin) and reads the next character from the standard input.
fputc, putc, putchar

The fputc function writes a character to an output file stream. It returns the
value it has written, or EOF on failure.
The function putc is quivalent to fputc, but you may implement it as a macro.
The putchar function is equivalent to putc(c,stdout), writing a single
character to the standard output.
fgets, gets

The fgets function reads a string from an input file stream. It writes characters to
the string pointed to by s until a newline is encountered, n-1 characters have been
transferred or the end of file is reached. Formatted Input and Output
There are library functions for producing output in a controlled fashion.
printf, fprintf and sprintf

The printf family of functions format and output a variable number of


arguments of different types. Ordinary characters are passed unchanged into
the output. Conversion specifiers cause printf to fetch and format additional
argumetns passed as parameters. They are start with a %.
For example

LINUX PROGRAMMING Page 60


which produces, on the standard output:

Some numbers: 1, 2, and 3

Here's another example:

This produces:

Hello Miss A Mathew, aged 6.5


Field specifiers are given as numbers immediatley after
the % character in a conversion specifier. They are used
to make things clearer.

The printf function returns an integer, the number of characters written.

scanf, fscanf and sscanf

LINUX PROGRAMMING Page 61


The scanf family of functions work in a similar way to the printf group,
except that thye read items from a stream and place vlaues into variables.

The format string for scanf and friends contains both


ordinary characters and conversion specifiers.

Here is a simple example:

The call to scanf will succeed and place 1234 into the variable num given either if the
following inputs

Other conversion specifiers are:

Given the input line,

this call to scanf will correctly scan four items:

LINUX PROGRAMMING Page 62


\
In general, scanf and friends are not highly regarded, for three reasons:

Other library functions use either stream paramters or the standard streams stdin,
stdout, stderr

LINUX PROGRAMMING Page 63


LINUX PROGRAMMING Page 64
LINUX PROGRAMMING Page 65
LINUX PROGRAMMING Page 66
LINUX PROGRAMMING Page 67
LINUX PROGRAMMING Page 68
LINUX PROGRAMMING Page 69
LINUX PROGRAMMING Page 70
LINUX PROGRAMMING Page 71
UNIT-III PROCESSES AND SIGNALS

Viewing Processes

We can see what processes are running by using the ps command. Here is
some sample output:

LINUX PROGRAMMING Page 72


The PID column gives the PIDs, the TTY column shows which terminal
started the process, the STAT column shows the current status, TIME gives
the CPU time used so far and the COMMAND column shows the command
used to start the process.
Let's take a closer look at some of these:

The initial login was performed on virtual console number one (v01).
The shell is running bash. Its status is s, which means sleeping. Thiis is
because it's waiting for the X Windows sytem to finish.

X Windows was started by the command startx. It won't finished until we exit from
X. It too is sleeping.
The fvwm is a window manager for X, allowing other programs to be started and windows to
be arranged on t screen

This process represents a window in the X Windows system. The shell, bash, is running in
the new window. T window is running on a new pseudo terminal (/dev/ptyp0) abbreviated
pp0.

This is the EMACS editor session started from the shell mentioned above. It uses the pseudo
terminal

This is a clock program started by the window manager. It's in the


middle of a one- minute wait between updates of the clock hands.
System Processes
Let's look at some other processes running on this Linux system.
The output has been abbreviated for clarity:

LINUX PROGRAMMING Page 73


Here we can see one very important process indeed:

In general, each process is started by another, known as its parent process. A process so
started is known as a
child process.
When UNIX starts, it runs a single program, the prime ancestror and process
number one: init. One such example is the login procedure init starts the
getty program once for each terminal that we can use to long in.
These are shown in the ps output like this:

Process Scheduling
One further ps output example is the entry for the ps command itself:

This indicates that process 192 is in a run state (R) and is executing the command ps- ax.
We can set the process priority using nice and adjust it using renice, which
reduce the priority of a process by 10. High priority jobs have negative values.
Using the ps -l (forlong output), we can view the priority of processes. The
value we are interested in is shown in the NI (nice) column:

Here we can see that the oclock program is running with a default nice
value. If it had been stated with the command,

it would have been allocated a nice value of +10.


We can change the priority
How It Works
In the first example, the program calls system with the string "ps -
ax", which executes the ps program. Our program returns from the
call to system when the ps command is finished.
In the second example, the call to system returns as soon as the shell
command finishes. The shell returns as soon as the ps program is started,
just as would happen if we had typed,

LINUX PROGRAMMING Page 74


at a shell prompt.
Replacing a Process Image
There is a whole family of related functions grouped under the exec heading. They differ in
the way that they start processes and present program arguments.

LINUX PROGRAMMING Page 75


LINUX PROGRAMMING Page 76
LINUX PROGRAMMING Page 77
LINUX PROGRAMMING Page 78
LINUX PROGRAMMING Page 79
LINUX PROGRAMMING Page 80
LINUX PROGRAMMING Page 81
LINUX PROGRAMMING Page 82
LINUX PROGRAMMING Page 83
LINUX PROGRAMMING Page 84
LINUX PROGRAMMING Page 85
LINUX PROGRAMMING Page 86
UNIT-IV
Interprocess communication, Message
Queues and Semaphores

Interprocess Communication
IPC between processes on
a single computer system
IPC between processes on

different systems

Pipes- creation
IPC between related processes using unnamed pipes
FIFOs- creation, IPC between unrelated processes using FIFOs(named pipes)
Differences Between
Unnamed
And Named
Pipes popen
& pclose
library
functions.
Message Queues-

Kernel support for


messages

Semaphores-Kernel support for semaphores


APIs for semaphores
File locking with Semaphores

Introduction to IPC

Interprocess Communication- “Interprocess


communication(IPC) is the transfer of data among different
processes”.

LINUX PROGRAMMING Page 87


Interprocess communication (IPC) includes thread synchorization and
data exchange between threads beyond the process boundaries. If
threads belong to the same process, they execute in the same address
space, i.e. they can access global (static) data or heap directly, without
the help of
the operating system. However, if threads belong to different processes,
they cannot access each others address spaces without the help of the
operating system.

There are two fundamentally different approaches in IPC:

 processes are residing on the same computer


 processes are residing on different computers

The first case is easier to implement because processes can share


memory either in the user space or in the system space. This is equally
true for uniprocessors and multiprocessors.

In the second case the computers do not share physical memory, they
are connected via I/O device(for example serial communication or
Ethernet). Therefore the processes residing in different computers can
not use memory as a means for communication.

IPC between processes on a Single System

Most of this chapter is focused on IPC on a single computer system,


including four general approaches:

 Shared memory
 Messages
 Pipes
 Sockets

The synchronization objects considered in the previous chapter


normally work across the process boundaries (on a single computer
system). There is one addition necessary however: the synchronization
objects must be named. The handles are generally private to the
process, while the object names, like file names, are global and known
to all processes.

IPC between processes on different systems

IPC between processes on different systems

IPC is Inter Process Communication, more of a technique to share data across


different processes

LINUX PROGRAMMING Page 88


within one machine, in such a way that data passing binds the coupling of different
processes.
The first, is using memory mapping techniques, where a memory
map is created, and others open the memory map for
reading/writing...
The second is, using sockets, to communicate with one
another...this has a high overhead, as each process would have to
open up the socket, communicate across... althougheffective
The third, is to use a pipe or a named pipe, a very good example

PIPES:

A pipe is a serial communication device (i.e., the data is read in the


order in which it was written),which allows a unidirectional
communication. The data written to end isreadbackfromtheotherend.

The pipe is mainly used to communicate between two threads in a


single process or between parent and child process. Pipes can only
connect the related process. In shell,
thesymbolcanbeusedtocreateapipe.

In pipes the capacity of data is limited. (i.e.) If the writing process is


faster than the reading process which consumes the data, the pipe
cannot store the data. In this situation the writer process will block
until more capacity becomes available. Also if the reading process tries
to read data when there is no data to read, it will be blocked until the
data becomes available. By this, pipes automatically synchronize the
two process.

Creatingpipes:
The pipe() function provides a means of passing data between two
programs and also allows to read and write the data.

#include<unistd.h>
int pipe(int file_descriptor[2]);

pipe()function is passed with an array of file descriptors. It will fill the


array with new file descriptors and returns zero. On error, returns -1 and
sets the errno to indicate the reason of
failure.

The file descriptors are connected in a way that is data written to file_
descriptor[1] can be read back from the file_descriptor[0].

(Note: As this uses file descriptors and not the file streams, we must use
read and write system calls to access the data.)

Pipes are originally used in UNIX and are made even more powerful in Windows
95/NT/2000.

LINUX PROGRAMMING Page 89


Pipes are implemented in file system. Pipes are basically files with
only two file offsets: one for reading another for writing. Writing to a
pipe and reading from a pipe is strictly in FIFO manner. (Therefore
pipes are also called FIFOs).

For efficiency, pipes are in-core files, i.e. they reside in memory
instead on disk, as any other global data structure. Therefore pipes
must be restricted in size, i.e. number of pipe blocks must be limited.
(In UNIX the limitation is that pipes use only direct blocks.)Since the
pipes have a limited size and the FIFO access discipline, the reading
and writing processes are synchronized in a similar manner as in case
of message buffers. The access functions for pipes are the same as for
files: WriteFile() and ReadFile().

Pipes used as standard input and output:

We can invoke the standard programs, ones that don’t expect a file

The purpose of dup call is to open a new file descriptor, which will
refer to the same file as an existing file descriptor. In case of dup, the
value of the new file descriptor is the lowest number available. In
dup2 it is same as, or the first available descriptor greater than the
parameter file_descriptor_2.

We can pass data between process by first closing the file descriptor 0
and call is made to dup. By this the new file descriptor will have the
number 0.As the new descriptor is the duplicate of an existing one,
standard input is changed to have the access. So we have created two
file descriptors for same file or pipe, one of them will be the standard
input.

(Note: The same operation can be performed by using the fcntl()


function. But compared to this dup and dup2 are more efficient)

Named pipes (FIFOs)


-------------------
Similar to pipes, but allows for communication
between unrelated processes. This is done by naming
the communication channel and making it permanent.

Like pipe, FIFO is the unidirectional data stream.


FIFO creation:
int mkfifo ( const char *pathname, mode_t mode );
- makes a FIFO special file with name pathname.

LINUX PROGRAMMING Page 90


(mode specifies the FIFO's permissions,
as common in UNIX-like file systems).
- A FIFO special file is similar to a pipe, except that it is created in
a different way. Instead of being an anonymous
communications channel, a FIFO special file is
entered into the file system by calling mkfifo()

Once a FIFO special file has been created, any


process can open it for reading or writing, in
the same way as an ordinary file.

A First-in, first-out(FIFO) file is a pipe that has a name in the


filesystem. It is also called as named pipes.

Creation of FIFO:
We can create a FIFO from the command line and within a program.

To create from command line we can use either mknod or mkfifo commands.
$ mknod filename p
$ mkfifo filename
(Note: The mknod command is available only n older versions,
you can make use of mkfifo in new versions.)

To create FIFO within the program we can use two system


calls. They are, #include<sys/types.h>
#include<sys/stat.h>

int mkfifo(const char


*filename,mode_t mode);
int
mknod(const
char
*filename,
mode_t
mode|S_IFIF
O,(dev_t) 0);
Accessing FIFO:
Let us first discuss how to access FIFO in command line using file
commmands. The useful feature of named pipes is, as they appear in
the file system, we can use them in commands.

We can read from the FIFO(empty)


$ cat < /tmp/my_fifo
Now, let us write to the FIFO.
$ echo "Simple!!!" > /tmp/my_fifo
(Note: These two commands should be executed in different terminals
because first command will be waiting for some data to appear in the
FIFO.)
Pipe processing:(popen &pclose library functions)

LINUX PROGRAMMING Page 91


The process of passing data between two programs can be done
with the help of popen() and pclose() functions.

#include<stdio.h>
FILE
*popen(co
nst char
*command
, const char
*open-
mode);
int pclose(FILE *stream_to_close);
popen():
The popen function allows a program to invoke another program as a
new process and either write the data to it or to read from it. The
parameter command is the name of the program to run. The
open_mode parameter specifies in which mode it is to be invoked, it
can be only either "r" or "w". On failure popen() returns a NULL
pointer. If you want to perform bi-directional communication you have
to use two pipes.

pclose():
By using pclose(), we can close the filestream associated with popen()
after the process started by it has been finished. The pclose() will
return the exit code of the process, which is to be closed. If the process
was already executed a wait statement before calling pclose, the exit
status will be lost because the process has been finished. After closing
the filestream, pclose() will wait for the child process to terminate.

Messagequeue:
This is an easy way of passing message between two process. It
provides a way of sending a block of data from one process to
another. The main advantage of using this is, each block of data is
considered to have a type, and a receiving process receives the blocks
of data having different type values independently.

Creation and accessing of a message queue:


You can create and access a message queue
using the msgget() function.
#include<sys/msg.h>
int msgget(key_t key,int msgflg);

The first parameter is the key value, which specifies the particular
message queue. The special constant IPC_PRIVATE will create a
private queue. But on some Linux systems the message queue may
not actually be private.

The second parameter is the flag value, which takes nine permission flags.

Adding a message:
LINUX PROGRAMMING Page 92
The msgsnd() function allows to add a message to a
message queue. #include<sys/msg.h>
int msgsnd(int msqid,const void *msg_ptr ,size_t msg_sz,int msgflg);

The first parameter is the message queue identifier returned from an msgget
function.

The second parameter is the pointer to the message to be sent. The third
parameter is the size of the message pointed to by msg_ptr. The fourth
parameter, is the flag value controls what happens if either the current
message queue is full or within the limit. On success, the function
returns 0 and a copy of the message data has been taken and placed on
the message queue, on failure -1 is returned.
Retrieving a message:
The smirch() function retrieves message from the message queue.

#include<sys/msg.h>
int msgsnd(int msqid,const void *msg_ptr
,size_t msg_sz,long int msgtype ,int msgflg); The second parameter is a pointer t

The fourth parameter allows a simple form of reception priority. If its


value is 0,the first available message in the queue is retreived. If it is
greater than 0,the first message type is retrived. If it is less than 0,the
first message that has a type the same a or less than the absolute value
of msgtype is retrieved.

On success, msgrcv returns the number on bytes placed in the receive


buffer, the message is copied into the user-allocated buffer and the
data is deleted from the message queue. It returns -1 on error.
#include<sys/msg.h>
int
msgctl(i
nt
msgid,in
t
comman
d, struct
msqid_d
s *buf);

The second parameter takes the values as given below:

1.) IPC_STAT - Sets the data in the msqid_ds to reflect the values
associated with the message queue.

2.) IPC_SET - If the process has the permission to do so, this sets the
values associated with the message queue to those provided in the
msgid_ds data structure.

3.) IPC_RMID-Deletes the message queue.

LINUX PROGRAMMING Page 93


(Note: If the message queue is deleted while the process is
writing in a msgsnd or msgrcv function, the send or receive
function will fail.

LINUX PROGRAMMING Page 94


UNIT-V

Shared Memory:
Shared memory is a highly efficient way of data sharing between the running
programs. It allows two unrelated processes to access the same logical
memory. It is the fastest form of IPC because all processes share the same
piece of memory. It also avoidscopyingdataunnecessarily.

As kernel does not synchronize the processes, it should be handled by the user.
Semaphore can also be used to synchronize the access to shared memory.

Usageofsharedmemory:
To use the shared memory, first of all one process should allocate the segment,
and then each process desiring to access the segment should attach the
segment. After accessing the segment, each process should detach it. It is also
necessary to deallocate thesegmentwithoutfail.

Allocating the shared memory causes virtual pages to be created. It is


important to note that allocating the existing segment would not create new
pages, but will return theidentifierfortheexistingpages.

All the shared memory segments are allocated as the integral multiples of the
system's page size, which is the number of bytes in a page of memory.

Unix kernel support for shared memory

 There is a shared memory table in the kernel address space that keeps
track of all shared memory regions created in the system.
 Each entry of the tables store the followingdata:

1. Name
2. Creator user ID and group ID.
3. Assigned owner user ID and group ID.
4. Read-write access permission of the region.
5. The time when the last process attached to the region.
6. The time when the last process detached from the region.
7. The time when the last process changed control data of the region.
8. The size, in no. of bytes of the region.

UNIX APIs for

shared memory

shmget

 Open and create a shared memory.

 Function prototype:

LINUX PROGRAMMING Page 95


#include<sys/types.h>
#include<sys/ipc.h> #include<sys/shm.h>

int shmget ( key_t key, int size, int flag );

 Function returns a positive descriptor if it succeeds or -1 if it fails.

Shmat

 Attach a shared memory to a process virtual address space.


 Function prototype:

void * shmat ( int shmid, void *addr, int flag );

 Function returns the mapped virtual address of he shared memory if it


succeeds or -1 ifit fails.

Shmdt

 Detach a shared memory from the process virtual address space.


 Function prototype:

 Function returns 0 if it succeeds or -1 if it fails.

Shmctl

 Query or change control data of a shared memory or delete thememory.

 Function prototype:

#inc

lude

int shmctl ( int shmid, int cmd, struct shmid_ds *buf );

Function returns 0 if it succeeds or -1 if it fails.

Shared memory Example

//shmry1.c

TEXT_

SZ

2048

struct

LINUX PROGRAMMING Page 96


shared_

use_st
{
int written_by_you;
char some_text[TEXT_SZ];
};

int main()
{
int running = 1;
void
*shared_memor
y = (void *)0;
struct
shared_use_st
*shared_stuff;
int shmid;
srand(
(unsigned
int)getpid() );
shmid =
shmget(
(key_t)1234,
sizeof(struct
shared_use_st
), 0666
|IPC_CREAT
);

if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory =
shmat(shmid,(void *)0, 0);
if (shared_memory ==
(void *)-1)
{
fprintf(stderr,
"shmat failed\n");
exit(EXIT_FAILU
RE);
}

printf("Memory
Attached at
%x\n",
(int)shared_mem

LINUX PROGRAMMING Page 97


ory);

shared_stuff =
(struct shared_use_st
*) shared_memory;
shared_stuff-
>written_by_you =
0; while(running)

{
if(shared_stuff->written_by_you)
{

printf("You Wrote: %s",


shared_stuff->some_text);

sleep( rand() %4 );
shared_stuff->written_by_you = 0;

if
(strncmp(shared_st
uff->some_text,
"end", 3)== 0)
{
running = 0;
}
}
}

if (shmdt(shared_memory) == -1)

{
fprintf(stderr,
"shmdt
failed\n");
exit(EXIT_F
AILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "failed to delete\n");
exit(EXIT_FAILURE);
}

exit(E
XIT_
SUC
CESS
);

LINUX PROGRAMMING Page 98


}

.
h
>

#include<sys/shm.h>

#define
TEXT_
SZ
2048
struct
shared_
use_st
{
int written_by_you;
char some_text[TEXT_SZ];
};

int main()
{
int running =1
void *shared_memory = (void *)0; struct shared_use_st *shared_stuff;

int shmid;

shmid
=shmget( (key_t)1234, sizeof(struct
shared_use_st),
0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr,
"shmget
failed\n");
exit(EXIT_FA
ILURE);
}

shared_mem
ory=shmat(s
hmid, (void
*)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr,
"shmat failed\n");
exit(EXIT_FAILU
RE);
}

LINUX PROGRAMMING Page 99


printf("Memory Attached at %x\n", (int)
shared_memory); shared_stuff = (struct
shared_use_st *)shared_memory;
while(running)
{
while(shared_stuff->written_by_you== 1)
{
sleep(1);
printf("waiting for client. .. \n");
}
printf("Ent
er Some
Text: ");
fgets
(buffer,
BUFSIZ,
stdin);
strncpy(shared_stuff-
>some_text, buffer,
TEXT_SZ);
shared_stuff->written_by_you = 1;
if(strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
if (shmdt(shared_memory) == -1)
{
fprintf(stderr,
"shmdt
failed\n");
exit(EXIT_F
AILURE);
}
exit(
EXIT
_SU
CCE
SS);
}

The shmry1.c program will create the segment using shmget() function and
returns the identifier shmid. Then that segment is attached to its address space
using shmat() function.

The structure share_use_st consists of a flag written_by_you is set to 1 when data is


available. When it is set, program reads the text, prints it and clears it to show it has read the
data. The string end is used to quit from the loop. After this the segment is detached and
deleted.

The shmry2.c program gets and attaches to the same memory segment. This is possible
LINUX PROGRAMMING Page 100
with the
help of same key value 1234 used in the shmget() function. If the
written_by_you text is set, the process will wait until the previous process
reads it. When the flag is cleared, the data is written and sets the flag. This
program too will use the string "end" to terminate. Then the segment is
detached.

5.2 Sockets

A socket is a bidirectional communication device that can be used to


communicate withanother process on the same machine or with a process
running on other machines.Sockets are the only interprocess communication
we‟ll discuss in this chapter thatpermit communication between processes on
different computers. Internet programs such as Telnet, rlogin, FTP, talk, and
the World Wide Web use sockets.

For example, you can obtain the WWW page from a Web server using
theTelnet program because they both use sockets for network
communications.To open a connection to a WWW server at
www.codesourcery.com, use telnet www.codesourcery.com 80.The magic
constant 80 specifies a connection to the Web server programming running
www.codesourcery.com instead of some other process.Try typing GET / after
the connection is established.This sends a message through the socket to the
Web server, which replies by sending the home page‟s HTML source and then
closing the connection—for example:

% telnet
www.codesourcer
y.com 80 Trying
206.168.99.1...
Connected to
merlin.codesourcery.com
(206.168.99.1). Escape character is
„^]‟.
GET /
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1”>
...
3. Note that only Windows NT can create a named pipe;Windows 9x
programs can form only client connections.
4. Usually, you‟d use telnet to connect a Telnet server for remote logins.
But you can also use telnet to connect to a server of a different kind and
then type comments directly at it.

Introduction to Berkeley sockets


Berkeley sockets (or BSD sockets) is a computing library with an application
programming interface (API) for internet sockets and Unix domain sockets,
used for inter-process communication (IPC).

LINUX PROGRAMMING Page 101


This list is a summary of functions or methods provided by the Berkeley
sockets API library:
 socket() creates a new socket of a certain socket type, identified by
an integer number, and allocates system resources to it.
 bind() is typically used on the server side, and associates a socket
with a socket address structure, i.e. a specified local port number and
IP address.
 listen() is used on the server side, and causes a bound TCP socket to enter
listening state.
connect() is used on the client side, and assigns a free local port number to a socket. In case of
a TCP socket, it causes an attempt to establish a new TCP connection.

 accept() is used on the server side. It accepts a received incoming


attempt to create a new TCP connection from the remote client, and
creates a new socket associated with the socket address pair of this
connection.
 send() and recv(), or write() and read(), or sendto() and recvfrom(),
are used for sending and receiving data to/from a remote socket.
 close() causes the system to release resources allocated to a socket.
In case of TCP, the connection is terminated.
 gethostbyname() and gethostbyaddr() are used to resolve host names
and addresses. IPv4 only.
 select() is used to pend, waiting for one or more of a provided list of
sockets to be ready to read, ready to write, or that have errors.
 poll() is used to check on the state of a socket in a set of sockets. The
set can be tested to see if any socket can be written to, read from or if
an error occurred.
 getsockopt() is used to retrieve the current value of a particular
socket option for the specified socket.
 setsockopt() is used to set a particular socket option for the specified socket.

IPC
over a
networ
k
Socket
Concep
ts
When you create a socket, you must specify three parameters:
 communication style,
 namespace,
 protocol.
A communication style controls how the socket treats transmitted data and specifies
the number of communication partners.When data is sent through a socket, it
is ackaged into chunks called packets.The communication style determines
how these
packets are handled and how they are addressed from the sender to the receiver.

Connection styles guarantee delivery of all packets in the order


they were sent. If packets are lost or reordered by problems in the

LINUX PROGRAMMING Page 102


network, the receiver automatically requests their retransmission
from the sender.
A connection-style socket is like a telephone call:The addresses of the sender
and receiver are fixed at the beginning of the communication
when the connection is established.

Datagram styles do not guarantee delivery or arrival order.


Packets may be lost or reordered in transit due to network
errors or other conditions. Each packet must be labeled with its
destination and is not guaranteed to be delivered.The system
guarantees only “best effort,” so packets may disappear or
arrive in a different order than shipping.
A datagram-style socket behaves more like postal mail.The sender
specifies the receiver‟s address for each individual message.

A socket namespace specifies how socket addresses are written. A socket


address identifies one end of a socket connection. For example, socket
addresses in the “local namespace” are ordinary
filenames. In “Internet namespace,” a socket address is composed of the Internet address
(also known as an Internet Protocol address or IP address) of a host attached to the network
and a port number.The port number distinguishes among multiple sockets on the same host.A
protocol specifies how data is transmitted. Some protocols are TCP/IP, the primary
networking protocols used by the Internet; the AppleTalk network protocol; and the UNIX
local communication

Client-server datagram socket — example


To experiment with datagram sockets in the UNIX domain we will
write a client/server application where:
 the client takes a number of arguments on its command line and send
them to the server using separate datagrams
 for each datagram received, the server converts it to uppercase and
send it back to the client
 the client prints server replies to standard output
For this to work we will need to bind all involved sockets to pathnames.

Client-server datagram socket example — protocol

#includ
e
<ctype
.h>
#includ
e
<sys/un
.h>
#includ
e
<sys/so
cket .h>
#includ
e

LINUX PROGRAMMING Page 103


<unistd
.h>
#includ
e"
helpers
.h"
#define SRV_SOCK_PATH "
/tmp/uc_srv_socket " #define
CLI_SOCK_PATH " /tmp/ uc_cl
i_socket .%ld " #define MSG_LEN 10
#include "uc�proto .h"
int main( int argc ,
char *argv [ ] ) {
struct sockaddr_un
srv_addr , cl i_addr ;
int srv_fd , i ;
s
s
i
z
e
_
t

b
y
t
e
s

s
o
c
k
l
e
n
_
t

l
e
n

;
char buf [MSG_LEN] ;
i f ( ( srv_fd = socket (AF_UNIX ,
SOCK_DGRAM, 0) ) < 0) err_sys ( " socket
error " ) ;

LINUX PROGRAMMING Page 104


memset(&srv_addr , 0, sizeof (
struct sockaddr_un ) ) ; srv_addr .
sun_family = AF_UNIX ;
strncpy ( srv_addr . sun_path ,
SRV_SOCK_PATH, sizeof (
srv_addr . sun_path ) �1) ;
i f ( access ( srv_addr .
sun_path , F_OK) == 0)
unlink ( srv_addr . sun_path
);
i f ( bind ( srv_fd , ( struct
sockaddr * ) &srv_addr , sizeof (
struct sockaddr_un ) ) < 0)
err_sys ( " bind error " ) ;
for ( ; ; ) {
len = sizeof ( struct sockaddr_un ) ;
i f ( ( bytes = recvfrom( srv_fd ,
buf , MSG_LEN, 0, ( struct
sockaddr * ) &cl i_addr , &len ) )
< 1) err_sys ( " recvfrom error " )
;
pr int f ( " server received %ld
bytes from %s\n" , ( long) bytes
, cl i_addr . sun_path ) ;
for ( i = 0; i < bytes ; i ++)
buf [ i ] = toupper ( (
unsigned char ) buf [ i ] ) ; i f
( sendto ( srv_fd , buf , bytes
, 0,
( struct sockaddr * ) &cl
i_addr , len ) != bytes )
err_sys ( " sendto error " ) ;

#include "uc�proto .h"


int main( int argc ,
char *argv [ ] ) {
struct sockaddr_un
srv_addr , cl i_addr ;
int srv_fd , i ;
s
i
z
e
_
t

l
e
n

LINUX PROGRAMMING Page 105


s
s
i
z
e
_
t

b
y
t
e
s

;
char
resp
[MS
G_LE
N] ; i
f(
argc
< 2)
er r_qui t ( "Usage : uc�c l ient MSG. . . " ) ;
i f ( ( srv_fd = socket (AF_UNIX ,
SOCK_DGRAM, 0) ) < 0) err_sys ( " socket
error " ) ;
memset(&cl i_addr , 0, sizeof (
struct sockaddr_un ) ) ; cl i_addr .
sun_family = AF_UNIX ;
snpr int f ( cl i_addr . sun_path , sizeof ( cl
i_addr . sun_path ) , CLI_SOCK_PATH, (
long) getpid ( ) ) ;
i f ( bind ( srv_fd , ( struct
sockaddr * ) &cl i_addr , sizeof (
struct sockaddr_un ) ) == �1)
err_sys ( " bind error " ) ;

Notes:
the server is persistent and processes one datagram at a time, no matter
the client rocess, i.e. there is no notion of connection messages larger
than 10 bytes are silently truncated

Socket address structures(UNIX domain &

Internet domain) UNIX domain Sockets:

We now want to give an example of stream sockets. To do so, we can


longer remain in the abstract of general sockets, but we need to pick a
domain. We pick the UNIX domain. In the UNIX domain, addresses are
LINUX PROGRAMMING Page 106
pathnames. The corresponding Cstructure is sockaddr_un: struct
sockaddr_un {
sa_fami ly_t sun_family ; /* = AF_UNIX */
char sun_path[108] ; /* socket
pathname, NULL�terminated */
}
The field sun_path contains a regular pathname, pointing to a special file of
type socket (. pipe) which will be created at bind time.
During communication the file will have no content, it is used only as a
rendez-vous point between processes.

Internet-Domain Sockets

UNIX-domain sockets can be used only for communication between two


processes on the same computer. Internet-domain sockets, on the other hand,
may be used to connect processes on different machines connected by a
network.
Sockets connecting processes through the Internet use the Internet namespace represented by
PF_INET.The most common protocols are TCP/IP.The Internet Protocol (IP), a low-level
protocol, moves packets through the Internet, splitting and rejoining the packets, if necessary.
It guarantees only “best-effort” delivery, so packets may vanish or be reordered during
transport. Every participating computer is specified using a unique IP number.

LINUX PROGRAMMING Page 107


The Transmission Control Protocol (TCP), layered on top of IP, provides reliable connection-
ordered transport. It permits telephone-like connections to be established between computers and
ensures that data is delivered reliably and inorder.
DNS Names
Because it is easier to remember names than numbers, the Domain Name Service (DNS)
associates names such as www.codesourcery.com with computers‟ unique IP numbers. DNS is
implemented by a worldwide hierarchy of name servers, but you don‟t need to understand DNS
protocols to use Internet host names in your programs.
Internet socket addresses contain two parts: a machine and a port number.This information is
stored in a struct sockaddr_in variable. Set the sin_family field to AF_INET to indicate that this
is an Internet namespace address.The sin_addr field stores the Internet address of the desired
machine as a 32-bit integer IP number.A port number distinguishes a given machine‟s different
sockets. Because different machines store multibyte values in different byte orders, use htons to
convert the port number to
network byte order. See the man page for ip for more information.To convert human-readable
hostnames, either numbers in standard dot notation (such as 10.0.0.1) or DNS names (such as
www.codesourcery.com) into 32-bit IP numbers, you can use gethostbyname.This returns a
pointer to the struct hostent structure; the h_addr field contains the host‟s IP number.

System Calls
Sockets are more flexible than previously discussed communication techniques.These
are the system calls involving sockets:
socket—Creates a socket
closes—Destroys a socket
connect—Creates a connection between two sockets
bind—Labels a server socket with an address
listen—Configures a socket to accept conditions
accept—Accepts a connection and creates a new socket for the connection
Sockets are represented by file descriptors.

Creating and Destroying Sockets


Sockets are IPC objects that allow to exchange data between processes running:
either on the same machine (host), or on different ones over a network.
The UNIX socket API first appeared in 1983 with BSD 4.2. It has been finally standardized for
the first time in POSIX.1g (2000), but has been ubiquitous to every UNIX implementation since
the 80s.
The socket API is best discussed in a network programming course,which this one is not. We
will only address enough general socketconcepts to describe how to use a specific socket family:
UNIXdomain sockets.

Connection Oriented Protocol


Client-server setup
Let‟s consider a typical client-server application scenario — no matter if they are located on the
same or different hosts.
Sockets are used as follows:
each application: create a socket
idea: communication between the two applications will flow through an imaginary “pipe” that
will connect the two sockets together
server: bind its socket to a well-known address
we have done the same to set up rendez-vous points for other IPC objects.

LINUX PROGRAMMING Page 108


e.g. FIFOs
client: locate server socket (via its well-known address) and “initiate communication”1 with the
server.

Socket options:
In order to tell the socket to get the information about the packet destination, we should call
setsockopt().
setsockopt() and getsockopt() - set and get options on a
socket. Both methods return 0 on success and -1 on error.
Prototype: int setsockopt(int sockfd, int level, int optname,...
There are two levels of socket options:
To manipulate options at the sockets API level: SOL_SOCKET
To manipulate options at a protocol level, that protocol number should be used;
for example, for UDP it is IPPROTO_UDP or SOL_UDP (both are equal 17) ; see
include/linux/in.h and include/linux/socket.h
● SOL_IP is 0.
● There are currently 19 Linux socket options and one another on option for BSD
compatibility.
● There is an option called IP_PKTINFO.

We will set the IP_PKTINFO option on a socket in the following example.


// from /usr/include/bits/in.h
#define IP_PKTINFO 8 /* bool */
/* Structure used for IP_PKTINFO. */

returns a new file descriptor with a numerical value equal to or greater than the integer newfd.

The call,

returns the file descriptor flags as defined in fcntl.h.

The call,

is used to set the file descriptor flags, usually just FD_CLOEXEC.

The calls,

LINUX PROGRAMMING Page 109


respectively get and set the file status flags and access modes.

5.9 Comparision of IPC mechanisms.


IPC mechanisms are mianly 5 types
1. pipes:it is related data only send from one pipe output is giving to another pipe input toshare
resouses pipe are used drawback:itis only related process onlycommunicated
2. message queues:message queues are un related process are also communicate with message
queues.
3. sockets:sockets also ipc it is comunicate clients and server
with socket system calls connection oriented and connection less also
4. PIPE: Only two related (eg: parent & child) processess can be communicated. Data reading
would be first in first out manner.
Named PIPE or FIFO : Only two processes (can be related or unrelated) can communicate. Data
read from FIFO is first in first out manner.
5. Message Queues: Any number of processes can read/write from/to the queue. Data can be
read selectively. (need not be in FIFO manner)
6. Shared Memory: Part of process's memory is shared to other processes. other processes can
read or write into this shared memory area based on the permissions. Accessing Shared memory
is faster than any other IPC mechanism as this does not involve any kernel level
switching(Shared memory resides on user memory area).
Semaphore: Semaphores are used for process synchronisation. This can't be used for bulk data tran
between processes.

LINUX PROGRAMMING Page 110


LINUX PROGRAMMING Page 111
LINUX PROGRAMMING Page 112

You might also like