Linux Programming (R15a0527)
Linux Programming (R15a0527)
ON
LINUX PROGRAMMING
(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.
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
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
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
UNIT-1
In this chapter we learn how to create, open, read, write, and close files.
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.
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 and its header file stdio.h, provide a versatile
interface to low-level I/O system calls.
fopen
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:
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.
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
This produces:
The call to scanf will succeed and place 1234 into the variable num given either if the
following inputs
Other library functions use either stream paramters or the standard streams stdin,
stdout, stderr
Viewing Processes
We can see what processes are running by using the ps command. Here is
some sample output:
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
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,
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-
Introduction to IPC
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.
Shared memory
Messages
Pipes
Sockets
PIPES:
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]);
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.
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().
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.
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.)
#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.
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
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.
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.
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.
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.
shared memory
shmget
Function prototype:
Shmat
Shmdt
Shmctl
Function prototype:
#inc
lude
//shmry1.c
TEXT_
SZ
2048
struct
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
shared_stuff =
(struct shared_use_st
*) shared_memory;
shared_stuff-
>written_by_you =
0; while(running)
{
if(shared_stuff->written_by_you)
{
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
);
.
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);
}
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 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
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.
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.
#includ
e
<ctype
.h>
#includ
e
<sys/un
.h>
#includ
e
<sys/so
cket .h>
#includ
e
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 " ) ;
l
e
n
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
Internet-Domain Sockets
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.
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.
returns a new file descriptor with a numerical value equal to or greater than the integer newfd.
The call,
The call,
The calls,