Seconds Since the “Epoch”

I was supposed to write a RT (Real Time) logging which doesn’t call a single Linux CALL.
I had only seconds from 1st Jan 1970 (Called Eposh).

A value that approximates the number of seconds that have elapsed since the Epoch. A Coordinated Universal Time name (specified in terms of seconds (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time represented as seconds since the Epoch, according to the expression below.

If the year is <1970 or the value is negative, the relationship is undefined. If the year is>=1970 and the value is non-negative, the value is related to a Coordinated Universal Time name according to the C-language expression, where tm_sec,  tm_min,  tm_hour,  tm_yday,  and  tm_year are all integer types:

```tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
```

The relationship between the actual time of day and the current value for seconds since the Epoch is unspecified.

How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined. As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

Note:
The last three terms of the expression add in a day for each year that follows a leap year starting with the first leap year since the Epoch. The first term adds a day every 4 years starting in 1973, the second subtracts a day back out every 100 years starting in 2001, and the third adds a day back in every 400 years starting in 2001. The divisions in the formula are integer divisions; that is, the remainder is discarded leaving only the integer quotient.

You can convert epoch Seconds to current time please look at this LINK.

Xenomai Timer

Xenomai Timer :Xenomai has two time sources: the sytem timer, which counts the number of nanoseconds since 1970, and a hardware dependent high resolution counter which counts the time since an unspecified point in time (usually the system boot time). This hardware dependent high resolution counter is called “tsc” on a PC, and gave its name to Xenomai native API calls.rt_timer_tsc returns the value of this hardware dependent high-resolution counter.
rt_timer_info returns the same thing in the tsc member of the RT_TIMER_INFO structure, and the value of the system timer at exactly the same time as when the high-resolution counter was read.

This allows to have a correspondence between the two time sources.

rt_alarm_inquire is not related to this and returns some information
about a given alarm. Now, if you allow me, a little advice for the implementation of a “timer library”: you could be tempted to create only one periodic alarm object with Xenomai, and to manage a timer list yourself. Don’t do this. Creating an alarm object for each timer library object make Xenomai aware of the existence of all your application timers, this has several

– it allows Xenomai to use its anticipation algorithm for all your timers
– if you are concerned about the scalability of Xenomai timers list
management, you can check the options in the “Scalability” menu of

Xenomai POSIX skin supports two clocks:
CLOCK_REALTIME maps to the nucleus system clock, keeping time as the amount of time since the Epoch, with a resolution of one system clock tick.

CLOCK_MONOTONIC maps to an architecture-dependent high resolution counter, so is suitable for measuring short time intervals. However, when used for sleeping (with clock_nanosleep()), the CLOCK_MONOTONIC clock has a resolution of one system clock tick, like the CLOCK_REALTIME clock.

Semaphore and Critical section

Before understanding semaphore we should first discuss the critical section.
critical section is a piece of code that can be executed by two or more process at a time. Because of the simultaneous access of code our data might get inconsistent. To avoid this inconsistency we use synchronization methods.

so semaphore is one of the synchronization technique. It is a locking mechanism which is use to provide a lock for the access of critical section. If a process wants to access the critical section it has to acquire the lock first and free the lock once it has completed their work. When one process is already having the lock and other process try to acquire the lock then that process has to wait for the time till the lock is freed by previous process.

suppose we have total n number of same object and for that we have n number of lock. if a process try to acquire a lock and lock is available then the value of lock will be decreased by one or if lock is not available then that process has to wait till the time any lock is available. we can understand this by following example.

total number of objects = 3

total number of locks available =3

IPv6 Compression

Hi, Please follow the link. It compresses the IPv6 address. In this file it also have to validate the IPv6 address and also It have a function which can Expand the Compressed IP.

Regex to validate the Email

Here i am sharing a regex to validate the Email.

This is an standard version of regex.

`^([a-zA-Z0-9\!\#\\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)(?:\.[A-Za-z0-9\!\#\\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+)*@([a-zA-Z0-9]([\-]?[a-zA-Z0-9]+)*\.)+([a-zA-Z0-9]{0,6})\\$`

It can validate email like

Valid Emails

```arungupta@gmail.com
arun+gupta+ramjiki+@gmail.com
a.little.lengthy.but.fine@dept.example.com
disposable.style.email.with+symbol@example.com
other.email-with-dash@example.com
arun@daiict.ac.in
arun_gupta@gmail.com```

Invalid Emails

```me@
@example.com
me.@example.com
.me@example.com
me@example..com
me.example@com
me\@example.com```

Counting Digits from given Range

Imagine you sell those metallic digits used to number houses, locker doors, hotel rooms, etc. You need to find how many of each digit to ship when your customer needs to number doors/houses:
1 to 100
51 to 300
1 to 2,000 with zeros to the left
The obvious solution is to do a loop from the first to the last number, convert the counter to a string with or without zeros to the left, extract each digit and use it as an index to increment an array of 10 integers.
I wonder if there is a better way to solve this, without having to loop through the entire integers range.
Thanx to mathematician’s Post that helped me to understand the complexity of problem, i just understood the equation and converted it to code. Enjoy !

Minimum cost flow House coloring with three colors

There are a row of houses, each house can be painted with three colors red, blue and green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. You have to paint the houses with minimum cost. How would you do it?

Note: Painting house-1 with red costs different from painting house-2 with red. The costs are different for each house and each color.

Approach:
Dynamic Programming solution:
we can paint the i’th house with blue, red or green.

```Enter No. of Houses to paint : 6
1 4 5
1 3 9
6 7 2
9 6 4
3 5 2
6 7 4

Minimum Cost to paint all houses are 20
```
```#include <stdio.h>
#include <stdarg.h>
#define COLOR 3
#define MAX(a,b) ((a) > (b) ? a : b)
#define MIN(a,b) ((a) < (b) ? a : b)
#define R 0
#define B 1
#define G 2
#define Color 3

int min( int num, ... )
{
va_list arguments;
int min=0;
int tmp=0;
/* Initializing arguments to store all values after num */
va_start ( arguments, num );
/* Sum all the inputs; we still rely on the function caller to tell us how
* many there are */
min = va_arg ( arguments, int );
//printf("\n1st|%d|\n",min);
for ( int x = 1; x < num; x++ )
{
tmp = va_arg(arguments,int);
min = MIN(min,tmp);
}
va_end ( arguments );
// Cleans up the list
//printf("\nMin is %d", min);
return min;
}
typedef struct {
int  color;
int cost;
//struct nodee *root;
}node;
int main()
{
node cost;
int house = 3;
printf("Enter No. of Houses to paint : ");
scanf("%d",&house);
int color[house][COLOR];
//	int ji=0;
for(int i=0; i<house;++i){
for(int j=0; j<COLOR;++j){
scanf("%d",&color[i][j]);
//				color[i][j]= ++ji;
}

}
//cost.cost=color[R];cost.cost=color[B];cost.cost=color[G];
for(int i= 0; i< Color ;i++){
cost[i].cost=color[i];
cost[i].color = i;
} // Initial Submission of values, and colour
//	printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost.cost,cost.color,cost.cost,cost.color,cost.cost,cost.color);
//A.color = R; B.color = B; C.color = G;
int tmp_B=0,tmp_G=0,tmp_R=0;
//printf("\nR_cost\t%d\tB_cost\t%d\tG_cost\t%d\n",R_cost,B_cost,G_cost);
for(int i=1; i<house;++i)
{
for(int j= 0; j< Color ;j++){
if(cost[j].color == R){
if(color[i][B] < color[i][G]){
cost[j].cost = cost[j].cost + color[i][B];
cost[j].color = B;
//cost[j].root = malloc(sizeof(struct node));
//cost[j].root->next = 0;
//cost[j].root->x = color[i][B];
}else{
cost[j].cost = cost[j].cost + color[i][G];
cost[j].color = G;
}
}else if(cost[j].color == B){
if(color[i][R] < color[i][G]){
cost[j].cost = cost[j].cost + color[i][R];
cost[j].color = R;
}else{
cost[j].cost = cost[j].cost + color[i][G];
cost[j].color = G;
}
}else{
//Case for G
if(color[i][R] < color[i][B]){
cost[j].cost = cost[j].cost + color[i][R];
cost[j].color = R;
}else{
cost[j].cost = cost[j].cost + color[i][B];
cost[j].color = B;
}
}
//			printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost.cost,cost.color,cost.cost,cost.color,cost.cost,cost.color);
}
/*
if(A.color == )
//R_cost = min(2,color[i][B],color[i][G])+R_cost;
if(color[i][B] < color[i][G]){

tmp_B = R_cost + color[i][B];
}else{
tmp_G = R_cost + color[i][G];
}

//B_cost = min(2,color[i][R],color[i][G])+B_cost;
if(color[i][R] < color[i][G]){

tmp_R = B_cost + color[i][R];
}else{
tmp_G = B_cost + color[i][G];
}
//G_cost = min(2,color[i][B],color[i][R])+G_cost;
if(color[i][R] < color[i][B]){

tmp_R = G_cost + color[i][R];
}else{
tmp_B = G_cost + color[i][B];
}
R_cost = tmp_R;
B_cost = tmp_B;
G_cost = tmp_G;
//printf("\n G %d:%d",color[i][B],color[i][R]);
*/
//			printf("\nA.cost\t%d(%d)\tB.cost\t%d(%d)\tC.cost\t%d(%d)\n",cost.cost,cost.color,cost.cost,cost.color,cost.cost,cost.color);
}
printf("\nMinimum Cost to paint all houses are %d\n",min(3,cost.cost,cost.cost,cost.cost));
}

```

First Version of RegextoWord

Hello Guys. I have been working in Perl from last 5 moths and while writing Regular Expressions i face difficulty to understand or write the Regular expressions. Well now it’s ok, but i thought can we covert the RegEx to understandable language.
So i started an project, here is the first version of the RegExtowordV1.0.

It can accept(Parse) Strings like

```^arun
^[arun]
^[a-z]
^[A-Z]
^[a-zA-Z0-9]
^[a-zA-Z0-9]Arun
^[a-zA-Z0-9]A\^\%run
[arun]
(A|R|U|N)
```

Basically in first version it can parse string (any word , digit special character, ^ ,[] () ).

Increment decrement problem in c

```Hello guys let's we have an syntax and related values let's see how this works. Evaluate the given syntax
z = ++x + y-- - ++y - x-- - x-- - ++y - x--
where x = 7 y = -3```
```z = ((((((++x + y--) - ++y) - x--) - x--) - ++y) - x--)
so first(++x + y--) will be evaluated
(++x + y--)
8 + (-3)        x=8 y = -4
z = (((((5 - ++y) - x--) - x--) - ++y) - x--)
(5 - ++y)
5 - ( -3)  x=8 y = -3

z = ((((8 - x--) - x--) - ++y) - x--)
(8 - x--)
8 - 8      x =7 y = -3
z = (((0- x--) - ++y) - x--)
(0- x--)
0 - 7      x =6 y = -3
z = (((-7 - ++y) - x--)
(-7 - ++y)
-7 - (-2)  x = 6 y = -2
z = (-5 - x--)
-5 -6       x =5 y -2
z = -11```

Can’t find a usable init.tcl in the following directories

sometimes while installing ns2  this error “application-specific initialization failed: Can’t find a usable init.tcl in the following directories: ” sucks , we don’t know what to do.

I have a solution.

application-specific initialization failed: Can’t find a usable init.tcl in the following directories:/root/ns2/ns-allinone-2.34/lib/tcl8.4 /usr/local/lib/tcl8.4 /usr/lib/tcl8.4 /usr/local/library /usr/library /usr/tcl8.4.18/library /tcl8.4.18/library /root/ns2/ns-allinone-2.34/lib/tcl8.4

or at local folder too

There are some solution please try may be they can help:

Solution 1:
1) Go to location root-usr-local-bin by giving following command in terminal
cd /usr/local/bin
2) There you would find the ns file. We just need to remove it by giving following command
rm ns
3) Thats it, you are done. Now your ns starts running successfully.

Solution 2:

This error can happen if you make any changes to the “ns-allinone-2.34/” directory :
Renaming the directory, moving the directory to another location,
i.e. anything that will change the “/home/arungupta2008/ns-allinone-2.34/tcl8.4.18/lib/” path.
Or when changes were made in /home/arungupta2008/ns-allinone-2.34/tcl8.4.18/lib/
Note :
The binary ‘ns’ is hard coded to know the location of it’s libraries, tclsh8* :
/home/arungupta2008/ns-allinone-2.34/tcl8.4.18/bin/, /home/arungupta2008/ns-allinone-2.34/tcl8.4.18/lib/

Workarounds, fixes :
1) cd ns-allinone-2.34/tcl8.4.18/ && ln -s lib/ library
2) Reinstall ns-allinone-2.34  2. The problem is because, ns executable is also at /usr which is conflicting.

Note : while removing symlinks first make that file removable then , you can remove that file  for Ex.     chmod 755 ns