Saturday, 13 August 2016

Threads for linux kernel


All threads in a process share all memory segments except stack segment. Each thread has its own private stack segment.

Linux has a unique implementation of threads.To the Linux kernel, there is no concept of a thread. Linux implements all threads as standard processes.The Linux kernel does not provide any special scheduling semantics or data structures to represent threads. Instead, a thread is merely a process that shares certain resources with other processes. Each thread has a unique task_struct and appears to the kernel as a normal process threads just happen to share resources, such as an address space, with other processes.


Threads are created the same as normal tasks, with the exception that the clone() system call is passed flags corresponding to the specific resources to be shared:
clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
The previous code results in behavior identical to a normal fork() , except that the address space, filesystem resources, file descriptors, and signal handlers are shared. In other words, the new task and its parent are what are popularly called threads. In contrast, a normal fork() can be implemented as
clone(SIGCHLD, 0);
And vfork() is implemented as
clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);

 User thread vs kernel thread:
-User threads are fast & run in user space. Kernel is not aware of user level threads. User threads are run by threads library in user space.
-Kernel threads are slow & run in kernel space. Kernel manages  kernel threads by its own.
-User threads can not take advantage of multiprocessing while kernel level threads can. Means kernel threads run parallelly on different cores.

fork() vs vfork()

fork()
The only overhead incurred by fork() is the duplication of the parent’s page tables
and the creation of a unique process descriptor for the child.

vfork()
The vfork() system call has the same effect as fork() , except that the page table entries
of the parent process are not copied.

Thursday, 21 July 2016

Important links

http://www.geeksforgeeks.org/virtual-functions-and-runtime-polymorphism-in-c-set-1-introduction/

http://www.programmerinterview.com/index.php/c-cplusplus/pure-virtual-function/

Sunday, 17 July 2016

Generate configure & Makefile in linux

If you are not using a installer for installing any software in linux, you will need to follow these 3 steps:
User is always provided with 'configure' & 'Makefile' scripts.
1) ./configure
2) make
3) make install

1)Configuration File
Configuration file checks whether all dependencies required for program/software installation are installed on your system. It generates Makefile using Makefile.in.
 2)make (build your project)
make use Makefile script to build project. Makefile contains all the information how the project should be compiled like compiler, paths, file dependencies etc.
3)make install
After building the project make install only copies final  executable files to destination paths like PATH. It also copies man page in corresponding man pages directory.

Autogenerate Configuration file in linux

Configuration script uses Makefile.in to generate Makefile which are further used by make  to build the project.

Writing configuration files is a tidy job. Hence, we are going to auto-generate configuration files.
For auto-generating configuration files we need to use m4 macro based package autotools which consist of autoconfig, automake etc.

Lets get started with a small project to auto-generate config files.
1) Install autotools using:
apt-get update
apt-get install autoconfig

Also install m4 macro processor:

Downloading m4:

Run the command below,
wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.10.tar.gz

Extracting files from the downloaded package:

tar -xvzf m4-1.4.10.tar.gz
Now, enter the directory where the package is extracted.
cd m4-1.4.10

Configuring m4:

./configure --prefix=/usr/local/m4
Replace "/usr/local/m4" above with the directory path where you want to copy the files and folders. Note: check for any error message.

Compiling m4:

make
2) First we need to write a basic C program main.c:

#include<stdio.h>
int main()
{
    printf("Hello World!\n");
    return 0;
}

3)We need to write a script configure.ac :
AC_INIT([helloworld], [0.5], [anything@myname.com]) AM_INIT_AUTOMAKE AC_PROG_CC AC_CONFIG_FILES([Makefile]) AC_OUTPUT

Briefs:
Initializing autoconf:
AC_INIT initialize autoconf & initialize our target project "helloworld" with version ".5" & maintainer information "anything@myname.com".

Initialize automake:
AM_INIT_AUTOMAKE It is used to initialize automake which we will use to generate Makefile from Makefile.in .
Makefile.in is a file enlisting the sources to be used by configuration file for generating final Makefile.

Adding Project dependencies:
AC_PROG_CC In our proect we only need a C compiler. So, this line only checks for a C compiler in our system.
WE can also add other dependencies using AC_PATH_PROG for a given program at user PATH

Inform configure file to use Makefile 
AC_CONFIG_FILES([Makefile]) This macro tells autoconf that configure file should use Makefile.in to generate Makefile .

Display script output
AC_OUTPUT This macro displays script output to screen.

4) Creating Makefile.in
Makefile.in is a big & complex file, So, we generate Makefile.in using Makefile.am . Automake will use Makefile.am to generate Makefile.in .

Makefile.am :

AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = helloworld helloworld_SOURCES = main.c

Brief:
AUTOMAKE_OPTIONS = foreign 
AUTOMAKE_OPTIONS specifies project layout. As we are not using standard GNU project, hence we are using foreign project.

bin_PROGRAMS = helloworld
This specifies target project to be helloworld.

helloworld_SOURCES = main.c
ProgramName_SOURCES specifies source files for helloworld project. We can add multiple source files.

5) Combining  all
Now we have configure.ac & Makefile.am
a)Set m4 environment to use autotools:
aclocal
b) Now we run autoconf to convert configure.ac to configure & automake to convert Makefile.am to Makefile.
autoconf
automake --add-missing

Using autotools we have finally generated 'configure' & 'Makefile' scripts. Now lets build our project:

./configure
make

Run:
./helloworld
= Hello World!


Thursday, 5 May 2016

Process & context switching

Process:
When we execute a program, it is loaded into memor by OS & a process is created to show its running instance. Moreover OS allocates address space to each process with different address segments(stack, heap, code, data).

context switching:
Operating System executes a process at a time and but it continously switch between all running processes to execute them, this is called context switching. This context switching is so fast that although at a time only one process is executing but it will give a illusion that all are running in parallel.
Suppose you are doing coding in eclipse and also listening to songs in a VLC Player on same system. It will seems to us that oth “eclispe” and “music player” process are running in parallel but acually only one process is executing at a time by OS. To manage this OS assigns states to each process.
A Process has following states,
1.) New
When a process is in creation mode then its in new State. As soon as its created completely, OS will move it into Ready State.
2.) Ready
When a process is ready to be executed then OS puts it in a ready queue with other ready processes. When context switching happens, then OS need to pick another process to execute. Then OS picks the next process to run from this queue. Therefore, from this state process will move into Running State.
3.) Running
When a process is running i.e. OS is executing its instructions. From here 3 things can happen,
  • If process gets blocked on any system API or I/O operation then OS moves this process to Blocked State
  • If timeout happens then OS moves this process to Ready Queue and change is State to Ready.
  • If Process ends then it moves to Done State
In first two  scenarios Context Switching happens i.e. OS picks another process from ready queue to be in Running State
4.) Blocked
When a process is waiting for any event like I/O event or on signal event or stuck on any system call then its moved in to Blocked State. Once free from this wait then process will move to ready state.
5.) Done
When a process is finished.

Sunday, 1 May 2016

Buffering in linux

There are 3 types of buffering:
i)Block buffered
many characters are saved upto a buffer size  written to destination as a block
ii)Line buffered
characters are saved till a new line character is found  & written to destination
iii)unbuffered
the output appears on destination as soon as it is written

By default BUFSIZ is used for any stream if buffer is null or not specified.

fflush can be used to flush out a stream:
stdout - write the data in buffer to output stream
stdin - discards any buffered data in stream

return 0 on success 

setvbuf(FILE *stream, char *buff, mode, sizeof(buff))