Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

IPC

IT is transfer of data among process.so this is called IPC

| - A pipe permits one way communication.

Types of interprocess communication

1-Shared memory – It permits process to read and write message from a specified
memory.

2-pipes –It permits sequential communication


Two types of pipe
1-UNNAMED
is an example of an “unnamed pipe”. The pipe exists only inside the kernel and cannot be
accessed by processes that created it, in this case, the bash shell. For those who don't
already know, a parent process is the first process started by a program that in turn
creates separate child processes that execute the program.

2-NAMED-The other sort of pipe is a “named” pipe, which is sometimes called a FIFO.
FIFO stands for “First In, First Out” and refers to the property that the order of bytes
going in is the same coming out. The “name” of a named pipe is actually a file name
within the file system. Pipes are shown by ls as any other file with a couple of differences

% ls -l fifo1
prw-r--r-- 1 andy users 0 Jan 22 23:11 fifo1|

The p in the leftmost column indicates that fifo1 is a pipe. The rest of the permission bits
control who can read or write to the pipe just like a regular file. On systems with a
modern ls, the | character at the end of the file name is another clue, and on Linux
systems with the color option enabled, fifo| is printed in red by default.

3- MAPPED MEMORY-This is same but it is associated with the file in the file-system.

4-Socket communication-This support communication between unrelated process.

SHARED MEMORY-This is the fastest IPC .


Shared memory allows two or more processes to access the same memory as if they all
called malloc and were returned pointers to the same actual memory.

At the same time two process can not write to the shared memory .So to avoid these
condition .Semapfore has been introduced.
Shmget –this is the command to allocate the memory .
Different parameters of shmget

1-first parameter is an integer segment key which indicates which segment to create.

2-its second parameter is the the number of byte in the segment .getpagesize();

3-The third parameter is the flag or bitwise .That speicifies the option to shmget

The different flags are


1-IPC_CREATE – This flag indicated that new segment should be created..

2-IPC_EXCL-This flag is used with IPC_CREATE and to avoid use of the same key .If
the key is same the creation of shmget .It will fail to create.

3-Mode flag-this flag is 9 bit which .In which we will mention the permission state.For
this we use the header <sys/stat.h>
like - S_IRUSR and S_IWUSR specify read and write permissions for the owner of the
shared memory segment, and S_IROTH and S_IWOTH specify read and write
permissions
for others.

int segment_id = shmget (shm_key, getpagesize (),


IPC_CREAT | S_IRUSR | S_IWUSER);

Attachment and deattachment

To make shared memory segment available we must attach it to the segement .Using the
command “shmat”.

1-The first parameter to pass with this fun is the ID returned by the shmget.

2-The second argument is a pointer that specifies where in your process’s address
space you want to map the shared memory; if you specify NULL, Linux will choose
an available address.

3-The third argument is the flag

(i)SHM_RND indicates that the address specified for the second parameter should be
rounded down to a multiple of the page size. If you don’t specify this flag, you
must page-align the second argument to shmat yourself.

(ii) SHM_RDONLY indicates that the segment will be only read, not written..
if every thing is fine then it will return the address of the attached memory

When whole process is over we will deattach the shared memory by passing the address
returned by it.

Controlling and Deallocating Shared Memory:-

shmctl (segment_id, IPC_STAT, &shmbuffer);

1-First argument is the shared memory identifier

2-To obatain information about shared memory pass IPC_STAT as the argument and
pointer to struct shmid_ds.

3-To remove the shared memory argument pass IPC_RMID and NULL as the third
parameter.

SEMAPHORE LEFT- GO THROUGH IT LATER

MAPPED MEMORY
1-This method allow a number of process to communicate via shared file.

2-Mapped memory can be used for the inter process communication.

3-Mapped memory forms an association between a file and a process’s memory.


Linux splits the file into page-sized chunks and then copies them into virtual memory
pages so that they can be made available in a process’s address space.Thus, the process
can read the file’s contents with ordinary memory access. It can also modify the file’s
contents by writing to memory.This permits fast access to files.

4-MAPPING AN ORDINARY FILE

(i)The first parameter is the address at which you like linux to map the files into your process address
space.NULL values in this palce allows linux to decide an available start address.

(ii)The second argument is the length of the map in byte.

(iii)The third argument specifes the protection on the mapped address range . Either we can mention bit
strucuture of our own or some specified . like
1-PROT_READ.
2-PROT_WRITE.
3-PROT_EXEC.
(iv)The fourth argument is the flag value that specifes additional value.
Fourth argument is bit wise or these constraints
(1)MAP_FIXED-If you specify this flag, Linux uses the address you request to map
the file rather than treating it as a hint.This address must be page-aligned.
(2) MAP_PRIVATE—Writes to the memory range should not be written back to the
attached file, but to a private copy of the file. No other process sees these writes.
This mode may not be used with MAP_SHARED.
(3) MAP_SHARED—Writes are immediately reflected in the underlying file rather than
buffering writes. Use this mode when using mapped memory for IPC.This
mode may not be used with MAP_PRIVATE.

(v)The fifith argument is the file descriptor opened to the file to be opened .

(vi)This one is the offset from the beginning of the file from which to start the map.

PIPE- It allows unidirectional communication


2-They are serial devices.

3-A pipe is used to communicate between two threads in a single process or between
parent and child.

4-It is created in shell

5- ls | less

In a shell, the symbol | creates a pipe. For example, this shell command causes the
shell to produce two child processes, one for ls and one for less.

6- Shell also creates a pipe connecting the standard output of ls process to atandard input
of less process

7-Pipes capacity is limited

8-******************************************************************

Creating pipes

Communication between parent and child

A call to pipe creates file descriptor which are valid only within that process and its
children. A process’s file descriptors cannot be passed to unrelated processes.
Pipes can connect only related process.
FIFO –(First in first out)

This process is also PIPE that has name in the filesystem .Any process can open it and
close it .the process on either end of the pipe need not be related to each other.This one is
also called named pipe.

We can make fifo by giving “mkfifo”


When we will give “ls” the first character will be ‘P’

Creating FIFO programmatically

1-First argument is the path at which to create the fifo.


2- second parameter is the pipes owner. Group owner or others

3-A pipe must have a reader and writer .So the permission include both read and write
operation.
4- If the pipe can not be created then it will return –1.
5- Include two header when you are creating <sys/types.h> ,<sys/stat.h>.

6- to communicate in FIFO one process must write and other should read..
For example, to write a buffer of data to a FIFO using low-level I/O routines, you
could use this code:
int fd = open (fifo_path, O_WRONLY);
write (fd, data, data_length);
close (fd);

To read a string from the FIFO using C library I/O functions, you could use
this code:
FILE* fifo = fopen (fifo_path, “r”);
fscanf (fifo, “%s”, buffer);
fclose (fifo);

8-A fifo can have multiple reader and writer.


SOCKET
Socket Concept – We you create a socket .One must specify three parameters
1-Communication style .
2- namespace.
3-protocol.

COMMUNICATION STYLE-This controls how the socket treats the data and specifies
the number of communication partners.

(i)Connection style gurantee delivery of all the data in the order in which they were sent.

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

Creating And Destroying

The socket and close function creates and destroy socket respectively .

When you create a socket specify three socket choice


1-namespace – For namespace use PF_LOCAL,PF_UNIX .PF stands for protocol
families.

2- communication style .-For communication uses SOCK_.


Use SOCK_STREAM FOR CONNECTION STYLE. Or USE SOCK_DGRAM for a
data gram connection.

3-protocol-different protocol by which we send the data .


CALLING CONNECT-

1-To create connection between two socket .Client call connect specifying the server
address.
2-Client initiate the connection and server waits for the connection to accept .
3-The client calls to connect to the server specified by the second argument .
4-The third argument is the length, in bytes, of the address structure
pointed to by the second argument.

SERVER-
Bind ,listen ,accept

Following are the argument


1- Its first argument is the socket file descriptor.
2- The second argument is the pointer to the socket address structure.
3- Third is the length in byte of the address structure.
4- When an address is bound to connection style socket .It must invoke listen to
indicate that this is server.
5- Different arguments of listen are
(i)Its first argument is the file descriptor .
(ii)Second argument specifies how many pending connection are queued .If the
queue is full .Then additional connection will be rejected .

6-The server accepts connection request from client by invoking accept .So different
parameters it passes are
(i)The socket file descriptor .
(ii)The second points to the socket address structure which is filled with client socket
address .
(iii)The third argument is the length in byte of the socket address structure.
(iv)The server can use the client address to know that they really want to communicate
with the client .
(v)The call to accept creates a new socket for communicating with the client and
return the corresponding file descriptor .

LOCAL SOCKET
Socket connecting on the same computer can use the local namespace represented by the
synonyms PF_LOCAL and PF_UNIX. They are called local socket or UNIX domain
socket
1-The socket name is specified in struct sockaddr_un.
2-The socket’s name is specified in struct sockaddr_un.
3-must set the sun_family field to AF_LOCAL.indicating that this is local namespace.

4-The sun_path field specifies the filename to use.

5-The actual length of struct sockaddr_un should be computed using the SUN_LEN
macro.

You might also like