<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EmbLogic &#187; hvsingh.in</title>
	<atom:link href="https://www.emblogic.com/blog/author/hvsingh-in/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.emblogic.com/blog</link>
	<description>Embedded System and ARM Training</description>
	<lastBuildDate>Tue, 03 Mar 2020 13:00:06 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Pointers</title>
		<link>https://www.emblogic.com/blog/04/pointers-6/</link>
		<comments>https://www.emblogic.com/blog/04/pointers-6/#comments</comments>
		<pubDate>Tue, 07 Apr 2015 12:58:47 +0000</pubDate>
		<dc:creator><![CDATA[hvsingh.in]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.emblogic.com/blog/?p=12532</guid>
		<description><![CDATA[Pointers are just variables like any other variables we declare, the only difference is that, the so called normal variables store some sort of data like an integer,a character,a floating point number, on the other hand a pointer stores a &#8230; <a href="https://www.emblogic.com/blog/04/pointers-6/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Pointers are just variables like any other variables we declare, the only difference is that, the so called normal variables store some sort of data like an integer,a character,a floating point number, on the other hand a pointer stores a logical address belonging to the data or stack segment (at which some data is stored or is to be stored).</b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>A pointer provides us the ability to access and modify data from anywhere. Pointers allow faster access to memory. </b></span></span></address>
<address class="western" style="font-style: normal"> </address>
<address class="western"><strong><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><span style="font-style: normal"><span style="text-decoration: underline"><b>Pointer declaration:</b></span></span></span></span></strong></address>
<address class="western"> </address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>The &#8216;*&#8217; operator is used to declare a pointer for e.g. int *ptr or char *ptr. In the former case ptr is a pointer which would hold the memory location of an integer similarly in the latter ptr is a pointer which would store the memory location of a character. Its important to note that the datatype preceding the identifier is not the datatype of the identifier its the datatype of the value that would be stored or is already present at the location ptr is pointing to.</b></span></span></address>
<address class="western"> </address>
<address class="western"><strong><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><span style="font-style: normal"><span style="text-decoration: underline"><b>Pointer Initialization:</b></span></span></span></span></strong></address>
<address class="western"> </address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Its important to remember that a pointer when declared does not store a valid logical address, rather it holds a garbage value and such pointers are called wild pointers and are very dangerous as they may be pointing to a location where some data is already stored and you might end up overwriting it. So its essential to give a valid address to the pointer (You shouldn&#8217;t worry too much about this as the compiler would give you a segmentation fault at run time if you try to access a wild pointer, but its a good habit to initialize the pointer as soon as you declare it).</b></span></span></address>
<address class="western" style="font-style: normal"> </address>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>There are many ways to initialize a pointer:</b></span></span></address>
<ul>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Suppose you declare a pointer which points to an integer value, like int *ptr, now this pointer doesn&#8217;t have a valid address. We can use the address of another variable and assign this address to our pointer like</b></span></span></address>
</li>
</ul>
<address class="western" style="font-style: normal;padding-left: 210px"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>int variable1;</b></span></span></address>
<address class="western" style="font-style: normal;padding-left: 210px"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>ptr = &amp;variable1;</b></span></span></address>
<address class="western" style="margin-bottom: 0.2in;font-style: normal;padding-left: 60px"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>&#8216;&amp;&#8217; ampersand is used to get memory location of the integer variable. </b></span></span></address>
<ul>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Another way is dynamic memory allocation. In this technique pointer is assigned a valid address at run time from the heap segment. This is the best way to assign addresses to pointers as it gives us the flexibility of deciding the amount of memory we want our pointer to access and the type of data we will store in the block of memory. Dynamic allocation of memory is done using malloc(), calloc(),and realloc() system calls.</b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>For e.g. lets assign an address to ptr </b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>                     ptr = (int*)malloc(sizeof(int) * 10);</b></span></span></address>
</li>
</ul>
<address class="western" style="margin-left: 0.49in;margin-bottom: 0.2in;padding-top: 0in;padding-right: 0in;padding-bottom: 0in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Here, we type cast malloc to tell the compiler that we want a memory block that stores integers and we use the sizeof() function to find the size of &#8216;int&#8217; our system and multiply it by the no. of integers we want to store in this block. ptr would store the starting address of this block of memory.</b></span></span></address>
<ul>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>This last method is specific to character arrays</b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>for e.g. char *ptr = “Hello world”</b></span></span></address>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>&#8216;ptr&#8217; is a pointer which would store the address at which a character is to be stored. In our e.g. ptr would hold the address at which &#8216;H&#8217; is stored using this address we access the entire string.</b></span></span></address>
</li>
</ul>
<address class="western" style="padding: 0in 0in 0in 30px;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Note that the string will be automatically terminated by NULL character. Another point to remember is that this is a declaration for a const char* that means the string pointed to by ptr cannot be changed. An  attempt to modify this string will not result in a compile time error, rather at run time a segmentation fault will occur. A better option is to initialise ptr as :</b></span></span></address>
<address class="western" style="font-style: normal;padding-left: 30px"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>                     const char *ptr = &#8220;Hello World&#8221;;</b></span></span></address>
<address class="western" style="padding: 0in 0in 0in 30px;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Modifying ptr now would give compile time error, which is better because both statements give the same result, a const char array, so a compile time error saves us time and reduces the confusion.</b></span></span></address>
<address class="western" style="font-style: normal"> </address>
<address class="western"><strong><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><span style="font-style: normal"><span style="text-decoration: underline"><b>Reading Complicated Pointer Declarations:</b></span></span></span></span></strong></address>
<address class="western"> </address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Reading complex pointer declarations can become a nightmare especially for people who are new to C. So, I&#8217;ve added an e.g. w.r.t this topic just to complete my share social service for this week.</b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>We&#8217;ll use the following e.g. to understand how we should interpret complicated pointer declarations.</b></span></span></address>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>int*(*(*ptr[]) () )[] </b></span></span></address>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>We should start from the identifier(and the innermost brace) and move right then left until we reach an end brace. So here we start from ptr:</b></span></span></address>
<ul>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>ptr is the identifier we can straight away conclude that ptr is an array (on moving right) of (on moving left) pointers, after square brackets the innermost braces close. So we&#8217;re at *ptr[] </b></span></span></address>
</li>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Next on moving right ptr is an array of pointers to a (on moving right) function (then moving left) that returns a pointer. At this point we&#8217;re at (int*(*ptr[])() )</b></span></span></address>
</li>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Finally the outermost braces give us : ptr is an array of pointers to a function returning pointer to (on moving right) an array of integer pointers(on moving left).</b></span></span></address>
</li>
</ul>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b> I&#8217;ll take another e.g. and explain it in more detail to make things clear:</b></span></span></address>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b> char (*(*ptr())[])()</b></span></span></address>
<ul>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Using our convention we start from ptr, now moving right we see that ptr is a function , then we reach the end of the innermost brace but we still don&#8217;t know what this function returns to find out we move to the left of ptr and find &#8216;*&#8217; operator so we conclude that ptr is a function that returns a pointer. At the end of this we&#8217;re at: *ptr() (ptr is a function returning a pointer), but we don&#8217;t know what the returning pointer points to. </b></span></span></address>
</li>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Going out of the inner most braces on the right we see square brackets so that means the pointer returned by ptr would point to an array, now on moving left we find what this array stores. It stores pointers. So at this stage we have : *(*ptr())[] (ptr is a function returning a pointer to an array of pointers). Remember we don&#8217;t know the pointers in the array point to.</b></span></span></address>
</li>
<li>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Now to the outermost braces on moving right its clear that the pointers in the array each point to a function and on moving left we find the return type of these functions is a char type variable. </b></span></span></address>
</li>
</ul>
<address class="western" style="margin-left: 0.49in;border: none;padding: 0in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>So we can conclude that ptr is a function returning a pointer to an array of pointers which point to functions that return char type variables. </b></span></span></address>
<address class="western" style="margin-left: 0.49in;border: none;padding: 0in;font-style: normal"> </address>
<address class="western" style="margin-bottom: 0.2in;border: none;padding: 0in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>I&#8217;ll leave a few exercises and there answers so that you can become accustomed to this method.</b></span></span></address>
<ol>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>*(*(*arr[3]))[3]</b></span></span></address>
</li>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>*(*arr[5])( ))</b></span></span></address>
</li>
<li>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>char(*ptr)( )</b></span></span></address>
</li>
</ol>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>Answers:</b></span></span></address>
<ol>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>&#8216;arr&#8217; is an array ( of size 3 ) of pointers each of which points to a pointer that points to an array ( of size 3 ) of pointers.( Remember if you encounter nothing on the right simply move to the left)</b></span></span></address>
</li>
<li>
<address class="western" style="font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>&#8216;arr&#8217; is an array ( of size 5 ) of pointers each of which points to a function that returns a pointer.</b></span></span></address>
</li>
<li>
<address class="western" style="margin-bottom: 0.2in;font-style: normal"><span style="font-family: Liberation Serif,serif"><span style="font-size: medium"><b>&#8216;ptr&#8217; is pointer to a function that returns a char type variable.</b></span></span></address>
</li>
</ol>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/04/pointers-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why is Sigaction() a better option than Signal()</title>
		<link>https://www.emblogic.com/blog/02/why-is-sigaction-a-better-option-than-signal/</link>
		<comments>https://www.emblogic.com/blog/02/why-is-sigaction-a-better-option-than-signal/#comments</comments>
		<pubDate>Wed, 11 Feb 2015 13:16:50 +0000</pubDate>
		<dc:creator><![CDATA[hvsingh.in]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.emblogic.com/blog/?p=12136</guid>
		<description><![CDATA[First of all lets see what a signal is. A signal is a software interrupt sent by the kernel to a foreground process to report an exceptional situation or report a case where the process is trying to access an &#8230; <a href="https://www.emblogic.com/blog/02/why-is-sigaction-a-better-option-than-signal/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>First of all lets see what a signal is. A signal is a software interrupt sent by the kernel to a foreground process to report an exceptional situation or report a case where the process is trying to access an invalid memory location ( e.g. a segmentation fault is reported after SIGSEGV signal is generated when we try to access an invalid location) or any other asynchronous event.</p>
<p>There are a total of 64 signals defined in Linux. These have been divided into various sections :</p>
<ol>
<li>Program error signals</li>
<li>Termination signals</li>
<li>Alarm signals</li>
<li>Asynchronous I/O signals</li>
<li>Job control signals</li>
<li>Operation error signals</li>
<li>Miscellaneous signals</li>
</ol>
<p>All these are defined in header file signal.h.</p>
<p>When generated these signals would execute there default behavior. But we can change this by redefining what the signal should do when its generated. This new definition is called a handler.</p>
<p>NOTE : SIGKILL and SIGSTOP can&#8217;t be caught or handled.</p>
<p>The signal system call &#8211; int signal(<strong>int</strong> signum, <strong>sighandler_t</strong> handler) is used to register a signal handler which would be invoked when the signum signal is generated.</p>
<p>Using signal() system call has some major drawbacks, of which the most significant is that its behavior varies across different versions of UNIX and Linux. Others include:</p>
<ul>
<li>Undefined behavior if the signal handler is already running because signals are not blocked while the current handler is executing.</li>
<li>We can&#8217;t pass arguments to the handler.</li>
<li>No information about the origins of the signal.</li>
<li>The signal function generally resets back to its default behavior, which is more often then not termination of the current process. Now, suppose a signal is generated and between the time the signal is generated and the handler  re-installs its definition, another instance of the signal occurs. In this case the default behavior which is generally termination of process, would happen.</li>
</ul>
<p>So, I think its safe to say that signal() is a fairly primitive and unsafe way to handle incoming signals.</p>
<p>Contrary to signal(), sigaction() provides a vast variety of options while handling signals but, with added complexity.</p>
<p>This is the prototype for sigaction() system call &#8211; int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)</p>
<p>The 1st argument takes the signal which is to be handled, the 3rd argument is used to define the previous behavior of the signal. The 2nd argument is a pointer to sigaction which is a kernel data structure its defined as:</p>
<p>struct sigaction {<br />
void     (*sa_handler)(int);<br />
void     (*sa_sigaction)(int, siginfo_t *, void *);<br />
sigset_t   sa_mask;<br />
int        sa_flags;<br />
void     (*sa_restorer)(void);<br />
};<br />
&#8211; sa_handler specifies the action to be associated with signum. This function receives  the  signal no. as its only argument.</p>
<p>&#8211; Parameters in sa_sigaction i.e void* is used to send arguments to the handler and siginfo_t is a structure which gives information about the whereabouts of the signal and other critical information. Its defined as:</p>
<p>siginfo_t {<br />
int      si_signo;    /* Signal number */<br />
int      si_errno;    /* An errno value */<br />
int      si_code;     /* Signal code */<br />
int      si_trapno;   /* Trap number that caused<br />
hardware-generated signal<br />
(unused on most architectures) */<br />
pid_t    si_pid;      /* Sending process ID */<br />
uid_t    si_uid;      /* Real user ID of sending process */<br />
int      si_status;   /* Exit value or signal */<br />
clock_t  si_utime;    /* User time consumed */<br />
clock_t  si_stime;    /* System time consumed */<br />
sigval_t si_value;    /* Signal value */<br />
int      si_int;      /* POSIX.1b signal */<br />
void    *si_ptr;      /* POSIX.1b signal */<br />
int      si_overrun;  /* Timer overrun count; POSIX.1b timers */<br />
int      si_timerid;  /* Timer ID; POSIX.1b timers */<br />
void    *si_addr;     /* Memory location which caused fault */<br />
long     si_band;     /* Band event (was int in<br />
glibc 2.3.2 and earlier) */<br />
int      si_fd;       /* File descriptor */<br />
short    si_addr_lsb; /* Least significant bit of address<br />
(since kernel 2.6.32) */<br />
}</p>
<p>&#8211; sigset_t sa_mask is a set of signals to be blocked during execution of the signal handler.   This is called signal masking. The blocked signals are sent to the sigpending queue, sigpending is a kernel structure defined in signal.h which is used to queue all the signals raised in the kernel space and are blocked in the user space.</p>
<p>&#8211; sa_flags specify a set of flags which modify the behavior of the signal. For e.g. if SA_SIGINFO then sa_sigaction specifies the signal handling function for signum not sa_handler. This is used when we want to define our own handler. List of all the flags is :</p>
<ul>
<li>SA_NOCLDSTOP</li>
<li>SA_NOCLDWAIT</li>
<li>SA_NODEFER</li>
<li>SA_ONSTACK</li>
<li>SA_RESETHAND</li>
<li>SA_RESTART</li>
<li>SA_SIGINFO</li>
</ul>
<p>&#8211; sa_restorer should not be by the user, its used internally by the Linux kernel while handling a pending unblocked signal.</p>
<p>Its quiet obvious that sigaction() allows more precise and safe handling of signals along with portability and takes care of the anomalies or loop holes created while using signal() system call.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/02/why-is-sigaction-a-better-option-than-signal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
