srik.in
user

srik.in

Srik.in is the personal website of Srikanth Eswaran who lives in Bangalore, India.


Featured

software development, epoch, unix timeval, itimer, setitimer, getitimer, linux, unix, timers, signals, signal handler, usage, sigalrm, real timer, virtual prof man, bangalore, bengaluru, technology, blog, mktime, tm to tm, time_t, programming, opengroup

Tm To Epoch Time Conversion

Posted on .

Linux provides a friendly way of entering time in terms of details such as day month year, seconds, hours, minutes etc. The structure in question is called “tm” and is as shown below:

struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}

However for comparisons, this time has to be further converted to the standard UNIX EPOCH time discussed in earlier articles. So for this, a function called mktime does this necessary conversion and provides a result in the form time_t which is in effect an epoch time. Two epoch times can be compared with relational operators (<, >, = =, !=) and we can decide which of the two input times is greater than the other or lesser than that other, etc.

An example from the opengroup is provided here for reference

What day of the week is July 4, 2001?

#include <stdio.h>
#include <time.h>

struct tm time_str;

char daybuf[20];

int main(void)
{
    time_str.tm_year = 2001 - 1900;
	time_str.tm_mon = 7 - 1;
	time_str.tm_mday = 4;
	time_str.tm_hour = 0;
	time_str.tm_min = 0;
	time_str.tm_sec = 1;
	time_str.tm_isdst = -1;
	if (mktime(&time_str) == -1)
		(void)puts("-unknown-");
	else {
		(void)strftime(daybuf, sizeof(daybuf), "%A", &time_str);
		(void)puts(daybuf);
	}
	return 0;
}

Its worthy to note here that the call can return a value if -1 (casted to time_t) if in case the resultant output cannot be expressed in the form of an epoch time.

You can try out the function mktime with a few input examples for the struct tm, on your own, to see the results for yourself. Try some invalid values as well.

Technorati Tags: Software Development, Epoch, Unix epoch, Software Development, Timeval, ITimer, SetITimer, GetITimer, Linux, Unix, Timers, Signals, Signal handler, Usage, Sigalrm, Real timer, Virtual timer, Prof timer, Man, Bangalore, Bengaluru, Technology, Blog, Mktime, Tm to epoch, Tm, Time_t, Linux, Programming, Timers, Opengroup

Featured

software development, timers, setitimer, interval timer, linux, c, timer handler

Interval Timers :: Setitimer Linux Function Usage

Posted on .

The setitimer call in linux is used to set the timer provided to a process by the kernel to a specific value. When it expires, a designated callback can be called. I had a situation where I needed one such timer which would alert my module once expired. But some things about setitimer are best noted here:

The set interval timer function in linux has the following signature:

SYNOPSIS

#include <sys/time.h>

int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);

It is used to create a timer which will alert the process by calling a callback once the timer expires. If you look at the following piece of code

int SetTimerAndStart(funcPtr pFunc, long int TimerValue)
{
int result = 0;
long int timervalue = 1;

struct itimerval value, ovalue, pvalue;
int which = ITIMER_REAL;

struct sigaction sact;
volatile double count;
time_t t;

sigemptyset( &sact.sa_mask );
sact.sa_flags = 0;
sact.sa_handler = pFunc;
sigaction( SIGALRM, &sact, NULL );

getitimer( which, &pvalue );

//Set a real time interval timer here
value.it_interval.tv_sec = TimerValue; /* seconds */
value.it_interval.tv_usec = 0; /* milliseconds */
value.it_value.tv_sec = TimerValue; /* reset to seconds */
value.it_value.tv_usec = 0; /* reset to milliseconds */

result = setitimer( which, &value, &ovalue );
return result;
}

this function takes in a function pointer of the form void (*ptr)(int sig) and a long integer value which is set to the tv_sec variable within the function.

Now I made a call to this function with the following

int ret = SetTimerAndStart(HandlerFunc, 5)

Handler func was of type void HandlerFunc(int) and I wanted the timer for 5 seconds. On a bare look at this code everything seems fine since it means I am asking for a timer with timeout of 5 seconds. But did this work for me?

The answer is no. The code core dumped when it was run leaving me confounded for a while. It is then that I realized the rather very silly error in the call which was causing a problem. It was the value 5 being passed as a long int.

The correct call to be made was as shown below:

long int value = 5; int ret = SetTimerAndStart(HandlerFunc, value);

Now I ll leave you to analyse why the former did not work, and why the latter call worked. But remember: BOTH CALLS DID COMPILE! More posts will be following on this topic later!

Technorati Tags: Software Development, Timers, Setitimer, Interval timer, Linux, C, Timer handler

Featured

software development, timeval, itimer, setitimer, getitimer, linux, unix, timers, signals, signal handler, usage, sigalrm, real timer, virtual prof man, bangalore, bengaluru, technology, blog

Usage of timers - what you need to know about them?

Posted on .

I had written an earlier article on Timers in Linux. In this article I share with you a slideshow on what is possible with timers and what is not achievable, so that you may clearly understand how timers can be used during system programming under a linux host or linux target based environment.


The following points are worth noting about timers

  • The time is stored relative to a certain date in history (Jan 1, 1970), so when you read the time, its always a number relative to this date. This is called EPOCH time or the UNIX EPOCH time.
  • Each process is allocated one timer, this is called per process timer.
  • iTimers which are of three types: REAL, VIRTUAL and PROF
  • how to initialize countdown timers and use them
  • iTimerVal and timeval structures
  • it_interval, it_value, tv_sec, tv_usec variables and their possible values
  • using the signal handler function to handle timer expiry signals
  • how to set signal handlers while using timers
  • types of signals that can be sent to process or across processes (SIGALRM)

So its now easy for you to write a small timer program in linux and compile it using the following :

gcc timer_example.c p-o timer_example

run it using

./timer_example

and check your logs to see if the timers did expire properly! As you would be aware the timer is a per process timer which means within a process, if you require timeouts at different intervals it has to be an enhancement over a single timer. So you could expire a single timer every say 1 second or 10 milliseconds or whatever value you want (at a granular level) and provide a specific interrupt to the process or other processes every “N” seconds or milliseconds where “N” is a multiple of the it_value variable within the timeval structure.

Try it out yourself and be rest assured it ll work like a charm.

Technorati Tags: Software Development, Timeval, ITimer, SetITimer, GetITimer, Linux, Unix, Timers, Signals, Signal handler, Usage, Sigalrm, Real timer, Virtual timer, Prof timer, Man, Bangalore, Bengaluru, Technology, Blog

Featured

india inc, leadership, skills, soft aggressiveness, assertiveness, software development, language, coding, design, platforms, standards, itu, ieee, data display debugger, monta vista, wind river, vxworks, psos, qnx, nucleus, rtos, semaphores, pipes, shared memory, map, files, ipc, tools, vision ice, click, probe, st connect, linux, real time, c, c++, windows 2000, nt, os, sdk, interviews, candidates, india, bangalore, bengaluru, it, proactiveness, attentiveness, friendliness, innovativeness, presentation, communication, service companies, projects, customers, candidates

Skill Sets Of Candidates For Interviews – The Indian Angle

Posted on .

I was called for checking out a few candidates for their suitability for my project when I decided to put in this blog post on what candidates need to have to make it into a team of engineers on a core project. This post comes after having the chance to screen through atleast 100+ resumes of different sorts from different companies in Bangalore.

What an interviewer looks for in a candidate can be classified into some basic categories.

Technical skills

  • Language/Coding skills – Here we look for a basic understanding of the language used in coding a particular project. The most common languages that we look for in an embedded systems perspective would be C/C++. In some cases depending on the project requirement this would change
  • Design skills – A simple problem (which involves design) is presented to the candidate, and he is expected to bring out a solution in the form of pseudo code or block diagrams or even sequence diagrams at times, depending on the nature of the problem posed. Here the expectation is to understand from the candidate the way in which he approaches this problem, more than to find out whether the end solution is right or not.
  • Platform knowledge – No one single individual can be an expert in terms of the platforms he has worked with unless the candidate has put in a consistent 8-10 years of work in that field of study. Hence what is expected out of say a 3-4 yrs experienced individual is to see how much of platform influence he has had in his projects that he has worked on during this tenure. This might be either direct work on the platform, or work on layers higher than the platform that use the platform to a great extent. Arguably, one should understand that there are so many different architectures, and so many different flavours of chipsets and peripherals which everyone may not be and is not required to be aware of.
  • Standards knowledge – Candidates are these days also generally assessed for their knowledge on standards and/or protocols as specified by bodies such as the IEEE, ITU, etc. Sometime 10 years back, this was a luxury work for people to do in India, but not anymore. The advances in technology have now made it possible for anyone anywhere in the world to access these standards and derive knowledge out of them. Of course this is not a strict requirement, but exposure to standards during the course of the candidates projects is considered a plus. What is of importance here is that the trait of a person who knows how to read a standard and derive designs/conclusions from the standard is considered an art well learnt by him.
Featured

Codecs and container formats

Posted on .

I found this snippet from the Video-LAN-Streaming HowTo document about Muxers and Codecs which clearly explains the possible video encoding formats, the container formats, the differences between container formats and codecs, etc. I am reproducing it here for reference and better understanding:

What is a codec ?

To fully understand the VideoLAN solution, you must understand the difference between a codec and a container format A codec is a compression algorithm, used to reduce the size of a stream. There are audio codecs and video codecs.MPEG-1, MPEG-2, MPEG-4, Vorbis, DivX, … are codecs

What is a container format ?

To start off, think of a container format as a standard shipping box. You get a box in the mail and you think, “Cool! What’s inside.” You don’t really care about the box itself, you care about what’s in that box. The problem? You can’t see into the box. So what do you do? You get a knife and cut it open. A container format follows this same basic idea. It contains one or several streams already encoded by codecs. Very often, there is an audio stream and a video one. AVI, Ogg, MOV, ASF, MP4 … are container formats. The streams contained can be encoded using different codecs. In a perfect world, you could put any codec in any container format. Unfortunately, there are some incompatibilities. You can find a matrix of possible codecs and container formats on the features page (http://www.videolan.org/streaming/features.html)

Encoding a video

This is the first step where you are going to create the shipping box. First you need to encode your file. That means that a file, wheter it is an audio, video file, is compressed to another format that normally takes up less physical drive space than the previous format. Common video encoding methods are DivX, MPEG-1, MPEG-2, MPEG-4 … most common audio encoding method is MP3 or ogg-vorbis. Then you have to mux (or multiplex). This means basically a process where separate parts of the video (or streams) are joined together into one file.

Playing a video

Now that you have your box, you need to open it before to see the content. That’s exactly what VLC will do. To decode a stream, VLC first demuxes it. This means that it reads the container format and separates audio, video, and subtitles, if any. Demuxing files doesn’t weaken the video nor audio quality, it doesn’t do anything for these data streams, it justs simply saves them into separate files, each containing one element of the original file. Then, each of these are passed decoders that do the mathematical processing to decompress the streams.

There is a particular thing about MPEG

  • MPEG is a codec. There are several versions of it, called MPEG-1, MPEG-2, MPEG-4, …
  • MPEG is also a container format, sometimes refered to as MPEG System.

There are several types of MPEG: ES, PS, and TS. When you play an MPEG video from a DVD, for instance, the MPEG stream is actually composed of several streams (called Elementary Streams, ES): there is one stream for video, one for audio, another for subtitles, and so on. These different streams are mixed together into a single Program Stream (PS). So, the .VOB files you can find in a DVD are actually MPEG-PS files. But this PS format is not adapted for streaming video through a network or by satellite, for instance. So, another format called Transport Stream (TS) was designed for streaming MPEG videos through such channels.

Technorati Tags: Software Development, Technology Standards, MPEG, Codecs, Container formats, Standards, Compression, Divx, Video, Audio, Subtitles, Transport stream, Program stream, Elementary stream, Muxers, Demuxers, VLC, AVI, Ogg, MOV, ASF, MP4, MPEG-1, MPEG-2, MPEG-4, Vorbis, DivX, VOB, Video-LAN-Streaming, HowTo