Showing posts with label IPC in linux. Show all posts
Showing posts with label IPC in linux. Show all posts

Monday, March 21, 2016

Difference between shared memory and message queue

Both shared memory and message queues can be used to exchange information between processes.The difference is in how they are used.

Shared memory is exactly what you'd think: it's an area of storage that can be read and written by more than one process.
 It provides no inherent synchronization; in other words, it's up to the programmer to
 ensure that one process doesn't clobber another's data.
  But it's efficient in terms of throughput: reading and writing are relatively fast operations.

A message queue is a one-way pipe: one process writes to the queue,
and another reads the data in the order it was written until an end-of-data condition occurs.
When the queue is created, the message size (bytes per message, usually fairly small)
and queue length (maximum number of pending messages) are set.
Access is slower than shared memory because each read/write operation is typically a single message.
But the queue guarantees that each operation will either processes an entire message successfully or fail
 without altering the queue. So the writer can never fail after writing only a partial message, and
 the reader will either retrieve a complete message or nothing at all.

Simple wrapper examples for networking code

Following are few wrappers used for networking code.. Mainly for sockets

int nRead(int fd, char* ptr, int nBytes)
{
    int nleft = nbytes ;
    int nread ;
   
    while (nleft>0)
    {
        nread = read(fd, ptr, nleft) ;
        if (nread == 0)
            break ;
           
        if (nread<0)
            return nread ;
           
        nleft = nleft - nread ;
       
        ptr = ptr+nread ;
    }
   
    return nbytes - nleft ;
}
   

int nWrite(int fd, const char* ptr, int nBytes)
{
    int nleft = nBytes ;
    int nwrite ;
   
    while (nleft >= 0)
    {
        nwrite = write(fd, ptr, nleft) ;
        if (nwrite <=0)
        nleft = nleft - nwrite ;
        ptr = ptr + nwrite ;
    }
    return nBytes - nleft ;
}
           
int setNonBlocking(int fd)
{
    int flags ;
    if (flags = fcntl(fd, F_GETFL) < 0)
    {
        perror("GETFL") ;
        return -1 ;
    }
   
    if (fcntl(fd, F_SETFL | flags) < 0)
    {
        perror("SETFL") ;
        return -1 ;
    }

    return 0 ;
}

Thursday, March 17, 2016

IPC part -1

IPC's are the mechanism used to communicate between 2 or more processes.
Before going further lets understand why processes need to communicate.
1) Because large amount of cpu time will be used if everything is controlled by a single process.
2) User need to wait, till one task need to complete.
3) IF the kernel is not preemptive, high priority task will wait till completion of current task.

So we need to communication. There are many links available on net why process need communication.

But what are the different ways to communicate process each other. How to identify process A need to talk to process B. Of course your mind will think we can use PID to communicate. Right and wrong.
Some questions about communication.
1) what does it mean process to communicate actually?
2) what happens the process which is waiting for data to arrive to process.
3) Lets not use any communication. We are happy with our own single process to do the task all.

To answer above, processes communicate via some simple mechanism. What they communicate. Ideally sharing the resources nothing but communication. Resources can be anything like file descriptors, data(global, static), signals, list of open files etc. All these will be shared to other processes to make the program efficient, smart and fast.

To share these we need to take care about synchronization. A big problem for processes to communicate.
Lets take standard scenario of client/server applications. These are 2 processes, client created by client.c and server created by server.c.

The use of these 2 programs is to write the data on the standard output. Server program will create a nasty funny string "Why it is working" and write on a buffer..Consider buffer is now standard ipc mechanism. Now client will come up and read the contents of buffer and prints on the screen. The data is then removed from buffer. Thats all. End of story.

Some simple problems
1) What happens if buffer is not free/not available.
2) What happens if client reads data from buffer
3) what happens if buffer is full.
4) what happens when client is reading, but server is writing.
5) etc..
So buffer and points 2 and 3 are important. point is just about the different mechanism for communication and rest are for synchronization.

It is boring, we are not yet started ipc's.  :-(
1) pipes
2) message queues
3) shared memory
4) sockets
5) signals

Above are used for IPC. Why there are so many. Again it depends on the way to communicate like
1) Within proceses like program called fork/vfork
2) Whether process is interested to only read or write
3) whether to communicate 2 systems over a network.
4) etc

The best, easy and fastest IPC is shared memory. Because processes are directly accessed memory very faster without copying into user space.