Contributors

Tuesday, September 28, 2010

Mutex vs semaphore

Both are synchronization objects but they carry great difference in the way they are used and implemented. While semaphore do have counting and binary semaphore as it types, many call binary semphore is similar to mutex. It is in the sense of using set or clear of a bit mechanism but they are not really the same.

Main difference between mutex and semaphore is,
+ a mutex when acquired by a thread needs to released in the same thread.
+ a sempahore can be released from another thread which has not acquired the semaphore
+ then comes the counting semphore where you define count and the number of waiting threads

One more important fact is both semaphore and mutex are not to be used in the ISRs as they are not automic mostly which might result in deadlocks. In ISRs, spinlocks are used with automic flag enabled.

I would like to see few comments/inputs here adding or debating on the usage.

Futex

One of the commonly heard topics when its comes to operating system internals or sharing of resources. So what does these objects do ?? SYNCHRONIZE access of re-entrant code or shared resource.

The commonly known and used synchronization objects are mutex, semaphores, spinlock, condition variables and there are events, futex and others like critical section. Futex, many might not have used this much or heard about it so let me throw some light on it.

- Futex also know as fast userspace mutex which is defined by a memory which is shared between different processes.
- Futex is similar to a semaphore where a counter a incremented or decremented atomically.
- Futex is a integer which can be only accessed atomically and processes share this integer over mmap or shared memory.
- Advantage of futex is it is limited to userspace and so like other synchronization object it doesnt need any user to kernel interaction when used in userspace.

*Will sure revisit here with more updates.

Thursday, September 23, 2010

Zombie vs Orphan

A process is called Zombie on following instances

+ If the parent process has missed the completion(SIGCHLD) or exit of the a child process.
+ when the child process has exited but if the entries in the process table is intact.
+ Zombie process is also know as defunct process.
+ Zombie process in certain cases is desirable as the parent process would not allot the same process id to the new child process.


A process becomes Orphan,

+ when a parent process has got completed or terminated, and child process is still alive
+ orphan process is inherited by init, till the completion of orphan process
+ Orphan process results in wasting of resources

Select vs Poll

The first thing that comes into my mind when I hear about select and poll are asychronous. Select and Poll calls are used to monitor multiple fds, streams or sockets for a specific time interval provided as a argument. Select is BSD derived and Poll is System V derived.

Select and poll allows a process/program to wait for one or more fds of a set of fds to be set/ready for I/O.

The number of fds on which the select call can wait is defined by the FD_SETSIZE variable. The FD_SET is based on the bit mask.

--> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);

Other associated calls
FD_ZERO(fd_set *) - Initializes an fd_set to be empty
FD_CLR(int fd, fd_set *) - Removes the associated fd from the fd_set
FD_SET(int fd, fd_set *) - Adds the associated fd to the fd_set
FD_ISSET(int fd, fd_set *) - Returns a nonzero value if the fd is in fd_set

The number of fds are not limited by FD_SETSIZE in Poll and it depends on the number of malloc/calloc that can done successfully on the pollfd structure.

--> int poll(struct pollfd fds[ ], nfds_t nfds, int timeout);

Wednesday, September 22, 2010

Spinlock

1. Spinlocks are used in ISRs and in SMP systems [symmetric multi core processor].
2. Can be made atomic in context.
3. Implemented by using a bit in integer and set or test of the bit.
4. While using spinlocks disabling the interrupts avoid deadlocks.
5. Disabling interrupts avoids pre-emption from ISR and higher priority tasks.
6. Spin locks with interrupts disabled are slower as the processor never goes into sleep and waits for spinlock object to complete and keeps the interrupts pending or lost.
7. Let’s take case of protecting structure that is updated in only process context and not on interrupts in this case spin_lock() is a benefit

Ex) spin_unlock_bh other available spin_lock(). Spin_unlock(), spin_unlock_saveirq(), spin_lock_irq(),spin_unlock_irqrestore

Hashing

Advantage of Hashing over other algorithms is its faster and it handles constantly changing unsorted data easily by taking range of keys values and transforms into key index.

One common issue seen in hashing is collision and techniques used to avoid collision are
1. Open addressing
2. Separate chaining.

Adios

Tuesday, September 21, 2010

mmap

When it comes to File I/O, everyone knows about opening a file using fopen and other file operation commands. But the linux kernel do provide an alternate to standard file I/O, which is allows the file to be mapped into memory and can be accessed like a chunk of memory.
mmap is command which is used to map the file into memory. mmap can be used for inter process communication, where a file can be mmap'ed and read/write can be done from a process with proper synchronisation object.

mmap would not fine deal when dealing with larger files as it needs to find a contigious block of address in process address space that is large enough fit the entire range of the file. It even becomes a bigger problem if the address space is fragmented.

mmap allows the operating system to read the file as pages based on paging mechanism, which is optimized for high I/O by reading based on page boundaries and size. When the requested data i.e] address space is not available in memory, the OS generates a page fault and fetches the page from actual page.

Friday, August 6, 2010

Creating a shared library

Creating a shared library:
The shared library is one of the types of libraries which we use in Linux. Generally, the files with extension '.so' represents Shared library object. Here, I am going to explain about how to create a shared library and how to invoke it in our programs.

The file a.c is our library's C source file.
a.c:
#include "head.h"
 void printfn(void)
{
    int a = 3, b = 2;
    int c = 9;
    printf("\nThe power of %d to %d is %d\n", a, b, c);
}

The Head.h file is the header file where we have the declaration of the functions which are defined in the library.

Head.h:
void printfn(void);

The d.c file is the C source of the executable which we are going to generate which is going to use our shared library.

d.c:
#include
#include "head.h"

int main ()
{
    printfn();
  return 0;
}

The new.sh file has the definitions to include LD_LIBRARY_PATH variable which is essential for linking the shared library with the executable. The users who does not have admin permissions, will not be able to set LD_LIBRARY_PATH environment variable which is needed to locate the shared library.

New.sh:
#!/bin/sh
export LD_LIBRARY_PATH=/temp:$LD_LIBRARY_PATH
exec /temp/output_file $

The Makefile has the definition for generating our shared library.

Makefile:
LIBNAME     = mylib
SOLIBVER    = 1.0

RM  = rm
AR  = ar
RANLIB  = ranlib
CC  = gcc
LN  = ln

SHAREDLIB   = lib$(LIBNAME).so

LSRCS       = a.c

LOBJS       = $(LSRCS:.c=.o)
SHOBJS      = $(LSRCS:.c=.od)

%.od: %.c
    $(CC) -fPIC -c -o $@ $<

all:: shared

shared: $(SHAREDLIB).$(SOLIBVER)

$(SHAREDLIB).$(SOLIBVER): $(SHOBJS)
    $(CC) -o $@ -shared -Wl,-soname,$@ $^
    $(LN) -s $@ $(SHAREDLIB)

clean::
    $(RM) $(SHOBJS) $(SHAREDLIB).$(SOLIBVER) $(SHAREDLIB)


Steps:
The following is the list of steps which we need to do to execute our program after linking our generated shared library.
1)     
      1) Make
2)      2)  gcc –o output_file d.c
3)      3) chmod 777 new.sh
4)      4) ./new.sh