<?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; msiddarth</title>
	<atom:link href="https://www.emblogic.com/blog/author/msiddarth/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>E14: Project: Multiple Data Compression using Iterative technique</title>
		<link>https://www.emblogic.com/blog/09/e14-project-multiple-data-compression-using-iterative-technique/</link>
		<comments>https://www.emblogic.com/blog/09/e14-project-multiple-data-compression-using-iterative-technique/#comments</comments>
		<pubDate>Mon, 03 Sep 2012 11:21:18 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=5171</guid>
		<description><![CDATA[Dear E14 trainees, Update/issues regarding MDC project has to be posted on this thread only on daily basis.]]></description>
				<content:encoded><![CDATA[<p>Dear E14 trainees,</p>
<p>Update/issues regarding MDC project has to be posted on this thread only on daily basis.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/09/e14-project-multiple-data-compression-using-iterative-technique/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>E14: Assignment 01</title>
		<link>https://www.emblogic.com/blog/08/e14-assignment-01/</link>
		<comments>https://www.emblogic.com/blog/08/e14-assignment-01/#comments</comments>
		<pubDate>Fri, 31 Aug 2012 11:33:51 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Data Structures with C]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=5137</guid>
		<description><![CDATA[Dear E14 Trainees, Update the status/issues of Assignment 01 through this post only.]]></description>
				<content:encoded><![CDATA[<p>Dear E14 Trainees,</p>
<p>Update the status/issues of Assignment 01 through this post only.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e14-assignment-01/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>E12: project: Multiple Data Compression Using Iterative Technique</title>
		<link>https://www.emblogic.com/blog/08/e12-project-multiple-data-compression-using-iterative-technique/</link>
		<comments>https://www.emblogic.com/blog/08/e12-project-multiple-data-compression-using-iterative-technique/#comments</comments>
		<pubDate>Thu, 30 Aug 2012 03:07:26 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=5112</guid>
		<description><![CDATA[Dear E12 Trainees, Update the status/issues pertaining  to MDC project on daily basis through this post only.]]></description>
				<content:encoded><![CDATA[<p>Dear E12 Trainees,</p>
<p>Update the status/issues pertaining  to MDC project on daily basis through this post only.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e12-project-multiple-data-compression-using-iterative-technique/feed/</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
		<item>
		<title>E12: Assignment No.1</title>
		<link>https://www.emblogic.com/blog/08/e12-assignment-no-1/</link>
		<comments>https://www.emblogic.com/blog/08/e12-assignment-no-1/#comments</comments>
		<pubDate>Wed, 22 Aug 2012 10:27:26 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=4955</guid>
		<description><![CDATA[Dear E12 Trainees, Update the status of Assignment 1 of Data structures with C on this post only.]]></description>
				<content:encoded><![CDATA[<p>Dear E12 Trainees,</p>
<p>Update the status of Assignment 1 of Data structures with C on this post only.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e12-assignment-no-1/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>E14: Introduction to Shell Script Assignment</title>
		<link>https://www.emblogic.com/blog/08/e14-introduction-to-shell-script-assignment/</link>
		<comments>https://www.emblogic.com/blog/08/e14-introduction-to-shell-script-assignment/#comments</comments>
		<pubDate>Tue, 21 Aug 2012 09:40:56 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=4922</guid>
		<description><![CDATA[Dear E14 Trainees, Update the status of Shell Script assignment as well as any kind of issues has to be updated on this post only.]]></description>
				<content:encoded><![CDATA[<p>Dear E14 Trainees,</p>
<p>Update the status of Shell Script assignment as well as any kind of issues has to be updated on this post only.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e14-introduction-to-shell-script-assignment/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>E12: Shell Script Assignment</title>
		<link>https://www.emblogic.com/blog/08/e12-shell-script-assignment/</link>
		<comments>https://www.emblogic.com/blog/08/e12-shell-script-assignment/#comments</comments>
		<pubDate>Fri, 17 Aug 2012 08:09:41 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=4810</guid>
		<description><![CDATA[Dear E12 Trainees, Update the status of shell script assignment and any issues regarding shell script has to be posted on this thread.]]></description>
				<content:encoded><![CDATA[<p>Dear E12 Trainees,</p>
<p>Update the status of shell script assignment and any issues regarding shell script has to be posted on this thread.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e12-shell-script-assignment/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>E8: Multiple Data Compression Project</title>
		<link>https://www.emblogic.com/blog/08/e8-multiple-data-compression-project/</link>
		<comments>https://www.emblogic.com/blog/08/e8-multiple-data-compression-project/#comments</comments>
		<pubDate>Fri, 17 Aug 2012 06:03:17 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Data Structures with C]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=4805</guid>
		<description><![CDATA[Dear E8 Trainees, Update the status of Multiple Data Compression project and any kind of issues has to be posted through this thread only.]]></description>
				<content:encoded><![CDATA[<p>Dear E8 Trainees,</p>
<p>Update the status of Multiple Data Compression project and any kind of issues has to be posted through this thread only.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/08/e8-multiple-data-compression-project/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Information: Regarding Signal Number 32 and 33</title>
		<link>https://www.emblogic.com/blog/07/information-regarding-signal-number-32-and-33/</link>
		<comments>https://www.emblogic.com/blog/07/information-regarding-signal-number-32-and-33/#comments</comments>
		<pubDate>Thu, 19 Jul 2012 05:37:59 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Data Structures with C]]></category>
		<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=3957</guid>
		<description><![CDATA[Signal              Number           Default Action                  Remarks SIGWAITING        32                    Ignore        Cocurrancy signal used by threads library SIGLWP             33                     Ignore        Inter LWP signal used by thread library Where LWP: light &#8230; <a href="https://www.emblogic.com/blog/07/information-regarding-signal-number-32-and-33/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Signal              Number           Default Action                  Remarks</p>
<p><span style="font-family: Palatino-Roman"><code>SIGWAITING</code></span>        32                    Ignore        Cocurrancy signal used by threads library</p>
<p>SIGLWP             33                     Ignore        Inter LWP signal used by thread library</p>
<p>Where LWP: light weight process</p>
<p>For further information you can access man pages by giving the command</p>
<p>man 7 signal</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/07/information-regarding-signal-number-32-and-33/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IPC mechanism: exit() status</title>
		<link>https://www.emblogic.com/blog/05/ipc-mechanism-exit-status/</link>
		<comments>https://www.emblogic.com/blog/05/ipc-mechanism-exit-status/#comments</comments>
		<pubDate>Mon, 21 May 2012 11:43:40 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=2893</guid>
		<description><![CDATA[When a program exits, it can return to the parent process a small amount of information about the cause of termination, using the exit status. This is a value between 0 and 255 that the exiting process passes as an &#8230; <a href="https://www.emblogic.com/blog/05/ipc-mechanism-exit-status/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>When a program exits, it can return to the parent process a small amount of information about the cause of termination, using the exit status. This is a value between 0 and 255 that the exiting process passes as an argument to <code>exit</code>.</p>
<p>Normally you should use the exit status to report very broad information about success or failure. You can&#8217;t provide a lot of detail about the reasons for the failure, and most parent processes would not want much detail anyway.</p>
<p>There are conventions for what sorts of status values certain programs should return. The most common convention is simply 0 for success and 1 for failure. Programs that perform comparison use a different convention: they use status 1 to indicate a mismatch, and status 2 to indicate an inability to compare. Your program should follow an existing convention if an existing convention makes sense for it.</p>
<p>A general convention reserves status values 128 and up for special purposes. In particular, the value 128 is used to indicate failure to execute another program in a subprocess. This convention is not universally obeyed, but it is a good idea to follow it in your programs.</p>
<p><strong>Warning:</strong> Don&#8217;t try to use the number of errors as the exit status. This is actually not very useful; a parent process would generally not care how many errors occurred. Worse than that, it does not work, because the status value is truncated to eight bits. Thus, if the program tried to report 256 errors, the parent would receive a report of 0 errors—that is, success.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/05/ipc-mechanism-exit-status/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Linux InterProcess Communication-IV: Duplicate Process</title>
		<link>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iv-duplicate-process/</link>
		<comments>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iv-duplicate-process/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 05:30:31 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=2283</guid>
		<description><![CDATA[LINUX implements through the fork() and exec() system calls an elegant two-step mechanism for process creation and execution. fork() is used to create the image of a process using the one of an existing one, and exec is used to &#8230; <a href="https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iv-duplicate-process/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> LINUX implements through the <tt><span style="font-family: Bitstream Charter,serif">fork()</span></tt> and <tt><span style="font-family: Bitstream Charter,serif">exec()</span></tt> system calls an elegant two-step mechanism for process creation and execution. </span></span></p>
<ul>
<li><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is used to create the image of a process using the one of an existing one, and</span></span></li>
<li><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exec</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is used to execute a program by overwriting that image with the program&#8217;s one. </span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> This separation allows to perform some interesting housekeeping actions in between, as we&#8217;ll see in the following lectures.</span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">A call to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> of the form:</span></span></p>
<pre><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;unistd.h&gt; </span></span>
      <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t fork(void);</span></span></pre>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"><strong>Return Value:</strong> </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.</span></span></p>
<p align="JUSTIFY"><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">Errors</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">:</span></span></p>
<ul>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">EAGAIN fork() cannot allocate sufficient memory to copy the parent&#8217;s page tables and allocate a task structure for the child. </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">EAGAIN It was not possible to create a new process because the caller&#8217;s RLIMIT_NPROC resource limit was encountered. To exceed this limit, the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability. </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight.</span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> It will creates (if it succeeds) a new process, which a child of the caller&#8217;s, and is an exact copy of the (parent) caller itself. By exact copy we mean that it&#8217;s image is a physical bitwise copy of the parent&#8217;s (in principle, they </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">do not</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> share the image in memory: though there can be exceptions to this rule, we can always thing of the two images as being stored in two separate and protected address spaces in memory, hence a manipulation of the parent&#8217;s variables won&#8217;t affect the child&#8217;s copies, and vice versa). The only visible differences are in the PCB, and the most relevant (for now) of them are the following: </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The two processes obviously have two different process id.s. (pid). In a C program process id.s are conveniently represented by variables of </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> type, the type being defined in the </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">sys/types.h</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> header. </span></span></p>
<ul>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">In LINUX the PCB of a process contains the id of the process&#8217;s parent, hence the child&#8217;s PCB will contain as parent id (ppid) the pid of the process that called </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">, while the caller will have as ppid the pid of the process that spawned it. </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">The child process has its own copy of the parent&#8217;s file descriptors. These descriptors reference the same under-lying objects, so that files are shared between the child and the parent. This makes sense, since other processes might access those files as well, and having them already open in the child is a time-saver. </span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> call returns in both the parent and the child, and both resume their execution from the statement immediately following the call. One usually wants that parent and child behave differently, and the way to distinguish between them in the program&#8217;s source code is to test the value returned by </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">. This value is 0 in the child, and the child&#8217;s pid in the parent. Since </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> returns -1 in case the child spawning fails, a catch-all C code fragment to separate behaviours may look like the following: </span></span></p>
<pre><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;sys/types.h&gt;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;stdlib.h&gt;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;errno.h&gt;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;stdio.h&gt;</span></span>

<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">int main()</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
 <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t childpid;</span></span>
 <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">childpid=fork();</span></span>
 <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">switch(childpid)</span></span>
 <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">case -1:</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fprintf(stderr,"ERROR: %s\n", sys_errlist[errno]);</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit(1);</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">break;</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">case 0:</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">/* Child's code goes here */</span></span>
 <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf(“ Child: My id is: %d and my parents id is: %d\n”,getpid(),getppid());</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">break;</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">default:</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf(“ Parent: My id is: %d and my child id is: %d\n”,getpid(),childpid);</span></span>
       <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">break;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span></pre>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> Note that a child (i.e. a process whatsoever, since they are all children of some other process, with the exception of processes 0, </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">swapper</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> and 1, </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">init</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">) cannot use the value returned by </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">fork()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> to know its pid, since this is always 0 in the child. A system call named </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">getpid()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is provided for this purpose, and another one, named </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">getppid()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is used to ask the system about the parent&#8217;s id. Both functions take no arguments and return the requested value in </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> type, or -1 in case of failure. </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> In the above program , a system call to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is made in case of failure, which causes the program to abort . We&#8217;ll see later that the </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> call returns the lower 8 bits of its argument (1, in the above example) to a waiting parent process, which can use them to determine the child&#8217;s exit status and behave accordingly. The usual convention is to exit with 0 on correct termination, and with a meaningful (for the parent) error code on abort. </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> It is often the case that a parent process must coordinate its actions with those of its children, maybe exchanging with them various kind of messages. UNIX defines several sophisticated inter-process communication (IPC) mechanisms, the simplest of which is a parent&#8217;s ability to test the termination status of its children. A synchronization mechanism is provided via the </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> system call, that allows a parent to sleep until one of its children exits, and then get its exit status. This call actually comes in three flavors, </span></span></p>
<ul>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">one simply called </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> and common to all version of UNIX (that i know of), </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">one called </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">waitpid()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">, which is a POSIX extension, and </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">one called </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait3()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">, and it&#8217;s a BSD extension. </span></span></li>
</ul>
<p align="JUSTIFY"><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">General Syntax of wait() system call is:</span></span></strong></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include&lt;sys/types.h&gt;<br />
#include &lt;sys/wait.h&gt; </span></span></p>
<p><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t wait(int *</span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">status</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">); </span></span></p>
<h2 align="JUSTIFY"><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">DESCRIPTION</span></span></strong></h2>
<p><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The </span></span><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child has already exited by the time of the call (a so-called &#8220;zombie&#8221; process), the function returns immediately. Any system resources used by the child are freed. </span></span></p>
<p><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"><strong>Return Value: </strong></span></span></p>
<p><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">The process ID of the child which exited, or zero if </span></span><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">WNOHANG</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> was used and no child was available, or -1 on error (in which case </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">errno</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is set to an appropriate value). </span></span></p>
<p><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">Errors:</span></span></strong></p>
<ul>
<li><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">ECHILD</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> : if the process specified in </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> does not exist or is not a child of the calling process. (This can happen for one&#8217;s own child if the action for SIGCHLD is set to SIG_IGN. See also the LINUX NOTES section about threads.) </span></span></li>
<li><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">EINVAL</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> : if the </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">options</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> argument was invalid. </span></span></li>
<li><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">EINTR</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> : if </span></span><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">WNOHANG</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> was not set and an unblocked signal or a </span></span><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">SIGCHLD</span></span></strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> was caught. </span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"><br />
Here&#8217;s an example call to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">: a program spawns two children, then waits for their completion and behaves differently according to which one is finished. Try to compile and execute it (no need to type: you can cut and paste from your web browser&#8230;). </span></span></p>
<pre><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;sys/types.h&gt;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;sys/wait.h&gt;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">#include &lt;stdio.h&gt;</span></span>

<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">int main(int argc, char *argv[])</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid_t whichone, first, second;</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">int howmany;</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">int status;</span></span>

    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">if ((first=fork())==0) /* Parent spawns 1st child */</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("Hi, I am the first child, and my id is %d\n",getpid());</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">sleep(10); /* Sleep 10 sec, then exit */</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit(0); </span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">else if (first == -1)</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">perror("1st fork: something went bananas\n");</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit(1);</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">else if ((second=fork())==0) /* Parent spawns 2nd child */</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("Hiya, I am the second child, and my id is %d\n",getpid());</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">sleep(15); /* Sleep 15 sec, then exit */</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit(0); </span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">else if (second == -1)</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">perror("2nd fork: something went bananas\n");</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit(1);</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span>

    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("This is the parent\n");</span></span>

    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">howmany=0; </span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">while (howmany &lt; 2) /* Wait twice */</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">{</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">whichone=wait(&amp;status);</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">howmany++;</span></span>

        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">if (whichone==first)</span></span>
           <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("First child exited ");</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">else</span></span>
           <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("Second child exited ");</span></span>

        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">if ((status &amp; 0xffff)==0)</span></span>
           <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("correctly\n");</span></span>
        <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">else</span></span>
           <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">printf("uncorrectly\n");</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span>
    <span style="font-family: Bitstream Charter,serif"><span style="font-size: small">return 0;</span></span>
<span style="font-family: Bitstream Charter,serif"><span style="font-size: small">}</span></span></pre>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The parent enters into the loop; waiting for the children&#8217;s completion. The </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> system call blocks the caller process until one of its immediate children (not children&#8217;s children, or other siblings) terminates, and then returns the pid of the terminated process. The argument to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is the address on an integer variable or the NULL pointer. If it&#8217;s not NULL, the system writes 16 bits of status information about the terminated child in the low-order 16 bits of that variable. Among these 16 bits, the </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">higher</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> 8 bits contain the </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">lower</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> 8 bits of the argument the child passed to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> while the </span></span><em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">lower</span></span></em><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> 8 bits are all zero if the process exited correctly, and contain error information if not (see the </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait(2)</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> man page for details). Hence, if a child exits with 0 all those 16 bits are zero. To reveal if this is actually the case we test the bitwise AND expression </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">(status &amp; 0xffff)</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">, which evaluates as an integer whose lower 16 bits are those of </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">status</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">, and the others are zero. If it evaluates to zero, everything went fine, otherwise some trouble occurred. Try changing the argument passed to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">exit()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> in one of the children. </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The Posix and BSD extensions to </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> are useful when a parent must not block waiting for children, but still wants to know about the children&#8217;s termination status values via the wait mechanism. We&#8217;ll treat only the Posix </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">waitpid()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> call, and you are referred to the man page for the BSD call. </span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">The </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">waitpid()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> call is declared as follows in the </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">sys/wait.h</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> header: </span></span></p>
<pre><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"><strong>pid_t waitpid(pid_t pid, int *statptr, int options);</strong></span></span></pre>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> Here the meaning of the the return value and of the pointer to the status </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">statptr</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> is exactly the same in </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">wait()</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">. However this call allows to specify which children should be waited for and how. Specifically, the first argument </span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> specifies the process(es) that must be waited for. The relevant (for now) cases are: </span></span></p>
<ul>
<li><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid == -1</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">: all children are waited for; </span></span></li>
<li><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">pid &gt; 0</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">: it specifies the pid of a single child that should be waited for (an error occurs if that process does not exist or is not one of the caller&#8217;s children); </span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: small"> The relevant (for now) value for the third argument is a constant called WNOHANG, that causes the function not to suspend the caller&#8217;s execution if status is not immediately available for one of the child processes. This allows to implement a loop in which the parent can do something useful and periodically poll the children&#8217;s status as well.</span></span></p>
<p align="JUSTIFY"><strong><span style="font-family: Bitstream Charter,serif"><span style="font-size: small">Sources</span></span></strong></p>
<ul>
<li><a href="http://www.cim.mcgill.ca/%7Efranco/OpSys-304-427/lecture-notes/node16.html">http://www.cim.mcgill.ca/~franco/OpSys-304-427/lecture-notes/node16.html</a></li>
<li><a href="http://linux.about.com/od/commands/l/blcmdl2_wait.htm">http://linux.about.com/od/commands/l/blcmdl2_wait.htm</a></li>
<li>info pages of linux operating system</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iv-duplicate-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Linux InterProcess Communication-III: Duplicate Process</title>
		<link>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iii-duplicate-process/</link>
		<comments>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iii-duplicate-process/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 05:40:18 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=2131</guid>
		<description><![CDATA[The Exec family calls To create a new process, which initially is a near duplicate of its parent process . Often, the new process immediately executes a new program. The act of creating a new process is called forking, and &#8230; <a href="https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iii-duplicate-process/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><strong>The Exec family calls</strong></span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> To create a new process, which initially is a near duplicate of its parent process . Often, the new process immediately executes a new program. The act of creating a new process is called forking, and this functionality is provided by the fork( ) system call. Two acts—first a fork, to create a new process, and then an exec, to load a new image into that process are thus required to execute a new program image in a new process. </span></span></p>
<p align="JUSTIFY">
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> The <em>exec</em> family of functions shall replace the current process image with a new process image. The new image shall be constructed from a regular, executable file called the <em>new process image file</em>. There shall be no return from a successful <em>exec</em>, because the calling process image is overlaid by the new process image.</span></span></p>
<p align="JUSTIFY"> <span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">There is no single exec function; instead, there is a family of exec functions built on a single system call. List of exec() family system calls are:</span></span></p>
<ul>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execl(const char *path, const char *arg, &#8230;);</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execlp(const char *file, const char *arg, &#8230;);</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execle(const char *path, const char *arg,&#8230;, char * const envp[]);</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execv(const char *path, char *const argv[]);</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execvp(const char *file, char *const argv[]);</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int execvpe(const char *file, char *const argv[],char *const envp[]);</span></span></li>
</ul>
<p align="JUSTIFY"> <span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">When a C-language program is executed as a result of this call, it shall be entered as a C-language function call as follows:</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> int main (</span></span></tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>int argc, char *argv</em></span></span><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">[]);</span></span></tt></pre>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">where <em>argc</em> is the argument count and <em>argv</em> is an array of character pointers to the arguments themselves.</span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The <em>argv</em> array are each terminated by a null pointer. The null pointer terminating the <em>argv</em> array is not counted in <em>argc</em>. The arguments specified by a program with one of the <em>exec</em> functions shall be passed on to the new process image in the corresponding <em>main</em>() arguments. The argument <em>path</em> points to a pathname that identifies the new process image file.</span></span></p>
<p><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> The argument <em>file</em> is used to construct a pathname that identifies the new process image file. If the <em>file</em> argument contains a slash character, the <em>file</em> argument shall be used as the pathname for this file. Otherwise, the path prefix for this file is obtained by a search of the directories passed as the environment variable <em>PATH</em></span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> Signals set to the default action (SIG_DFL) in the calling process image shall be set to the default action in the new process image. Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling process image shall be set to be ignored by the new process image. Signals set to be caught by the calling process image shall be set to the default action in the new process image</span></span></p>
<ul>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">After a successful call to any of the <em>exec</em> functions, any functions previously registered by <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/atexit.html"><em>atexit</em>()</a> are no longer registered.</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Any shared memory segments attached to the calling process image shall not be attached to the new process image</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Any named semaphores open in the calling process shall be closed as if by appropriate calls to <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/sem_close.html"><em>sem_close</em>()</a>.</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Any blocks of typed memory that were mapped in the calling process are unmapped,</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Memory locks established by the calling process via calls to <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/mlockall.html"><em>mlockall</em>()</a> or <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/mlock.html"><em>mlock</em>()</a> shall be removed</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">All open message queue descriptors in the calling process shall be closed, as described in <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/mq_close.html"><em>mq_close</em>()</a>.</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The new process image shall inherit the CPU-time clock of the calling process image.</span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The new process shall inherit at least the following attributes from the calling process image:</span></span></p>
<ul>
<li>Read and write descriptor created via pipes.</li>
<li></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Nice value </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Process ID</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Parent process ID</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Process group ID</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Session membership</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Real user ID</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Real group ID</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Supplementary group IDs</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Time left until an alarm clock signal (see </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/alarm.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>alarm</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">()</span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">)</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Current working directory</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Root directory</span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">File mode creation mask </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">File size limit </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Process signal mask </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Pending signal </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Resource limits </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Controlling terminal </span></span></li>
<li><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Interval timers</span></span></li>
</ul>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> Upon successful completion, the <em>exec</em> functions shall mark for update the <em>st_atime</em> field of the file. If an <em>exec</em> function failed but was able to locate the process image file, whether the <em>st_atime</em> field is marked for update is unspecified.</span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><strong>Lets see a the some examples of  exec() family calls.</strong></span></span></p>
<p align="JUSTIFY"> <span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><strong>Using execl()</strong></span></span></p>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example executes the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command, specifying the pathname of the executable ( </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><strong>/bin/ls</strong></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">) and using arguments supplied directly to the command to produce single-column output.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execl ("/bin/ls", "ls", "-1", (char *)0);</span></span></tt></pre>
<h5 align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Using execle()</span></span></h5>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example is similar to <a href="http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html#tag_03_130_06_01">Using execl()</a>. In addition, it specifies the environment for the new process image using the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>env</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> argument.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>

<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execle ("/bin/ls", "ls", "-l", (char *)0, env);</span></span></tt></pre>
<h5 align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Using execlp()</span></span></h5>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example searches for the location of the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command among the directories specified by the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>PATH</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> environment variable.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execlp ("ls", "ls", "-l", (char *)0);</span></span></tt></pre>
<h5 align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Using execv()</span></span></h5>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example passes arguments to the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command in the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>cmd</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> array.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>

<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">char *cmd[] = { "ls", "-l", (char *)0 };</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execv ("/bin/ls", cmd);</span></span></tt></pre>
<h5 align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Using execve()</span></span></h5>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example passes arguments to the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command in the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>cmd</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> array, and specifies the environment for the new process image using the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>env</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> argument.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>

<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">char *cmd[] = { "ls", "-l", (char *)0 };</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execve ("/bin/ls", cmd, env);</span></span></tt></pre>
<h5 align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">Using execvp()</span></span></h5>
<p align="JUSTIFY"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">The following example searches for the location of the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command among the directories specified by the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>PATH</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> environment variable, and passes arguments to the </span></span><a href="http://pubs.opengroup.org/onlinepubs/009604499/utilities/ls.html"><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>ls</em></span></span></a><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> command in the </span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"><em>cmd</em></span></span><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium"> array.</span></span></p>
<pre><tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">#include &lt;unistd.h&gt;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">int ret;</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">char *cmd[] = { "ls", "-l", (char *)0 };</span></span></tt>
<tt><span style="font-family: Bitstream Charter,serif"><span style="font-size: medium">ret = execvp ("ls", cmd);</span></span></tt></pre>
<p align="JUSTIFY"> Return Value:</p>
<p align="JUSTIFY">If one of the <em>exec</em> functions returns to the calling process image, an error has occurred; the return value shall be -1, and <em>errno</em> shall be set to indicate the error.(Refer man pages)</p>
<p align="JUSTIFY">In our next article we will cover some more system calls like exit(),kill() and using fork and exec() family functions.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-iii-duplicate-process/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Understanding Linux InterProcess Communication-II: Process Management</title>
		<link>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-ii-process-management/</link>
		<comments>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-ii-process-management/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 06:56:30 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=2083</guid>
		<description><![CDATA[Before we should discuss about PIPES and FIFOs. There are is a difference between an &#8220;Inter-process Communication Mechanism&#8221; and an &#8220;Inter-process Communication Resource/Facility&#8221;, though it is very difficult to draw a line between them and differentiate between them. Pipes and &#8230; <a href="https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-ii-process-management/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Before we should discuss about PIPES and FIFOs. There are is a difference between an &#8220;Inter-process Communication Mechanism&#8221; and an &#8220;Inter-process Communication Resource/Facility&#8221;, though it is very difficult to draw a line between them and differentiate between them.</p>
<p>Pipes and FIFOs are &#8220;Inter-process Communication Mechanisms&#8221; while semaphores, message queues and shared memory segments are &#8220;Inter-process Communication Resources&#8221;.</p>
<p>The best way to remember the difference between the two is: Inter-process Communication Mechanisms emphasize &#8220;how and why&#8221; data communication occurs between two User Mode processes, while on the other hand, Inter-process Communication Resources define the same objective, but in a more polished manner, by implementing the functionality through programming interfaces (and most of the times, using rather complex ones!).</p>
<p>To understand the concept of PIPES and FIFOS we have to look how a process is created in linux opearing system.</p>
<p>Linux is a very dynamic system with constantly changing computing needs. The representation of the computational needs of Linux centers around the common abstraction of the process. Processes can be short-lived (a command executed from the command line) or long-lived (a network service). For this reason, the general management of processes and their scheduling is very important.</p>
<p>From user-space, processes are represented by process identifiers (PIDs). From the user&#8217;s perspective, a PID is a numeric value that uniquely identifies the process. A PID doesn&#8217;t change during the life of a process, but PIDs can be reused after a process dies, so it&#8217;s not always ideal to cache them.</p>
<p>In user-space, you can create processes in any of several ways. You can execute a program (which results in the creation of a new process) or, within a program, you can invoke a fork or exec system call.</p>
<p><strong>Process representation</strong></p>
<p>Within the Linux kernel, a process is represented by a rather large structure called task_struct. This structure contains all of the necessary data to represent the process, along with a plethora of other data for accounting and to maintain relationships with other processes (parents and children).</p>
<p>A full description of the task_struct is beyond the scope of this article, but a portion of task_struct is shown here. This code contains the specific elements this article explores. Note that task_struct resides in ./linux/include/linux/sched.h.</p>
<p>A small portion of task_struct is re</p>
<p>struct task_struct {<br />
volatile long state;<br />
void *stack;<br />
unsigned int flags;<br />
int prio, static_prio;<br />
struct list_head tasks;<br />
struct mm_struct *mm, *active_mm;<br />
pid_t pid;<br />
pid_t tgid;<br />
struct task_struct *real_parent;<br />
char comm[TASK_COMM_LEN];<br />
struct thread_struct thread;<br />
struct files_struct *files;<br />
&#8230;<br />
};</p>
<p><strong>Process Management</strong></p>
<p>Now, let&#8217;s explore how you manage processes within Linux. In most cases, processes are dynamically created and represented by a dynamically allocated task_struct. One exception is the init process itself, which always exists and is represented by a statically allocated task_struct. You can see an example of this in ./linux/arch/i386/kernel/init_task.c.</p>
<p>Lets take an example of process, If you have two terminal windows showing on your screen, then you are probably running the same terminal program twice—you have two terminal processes. Each terminal window is probably running a shell; each running shell is another process. When you invoke a command from a shell, the corresponding program is executed in a new process; the shell process resumes when that process completes.</p>
<p>All processes in Linux are collected in two different ways. The first is a hash table, which is hashed by the PID value; the second is a circular doubly linked list. The circular list is ideal for iterating through the task list.</p>
<p>Process IDs are 16-bit numbers that are assigned sequentially by Linux as new processes are created.<br />
Every process also has a parent process (except the special init process).Thus, you can think of the processes on a Linux system as arranged in a tree, with the init process at its root. The parent process ID, or ppid, is simply the process ID of the process’s parent.</p>
<p>When referring to process IDs in a C program, always use the pid_t typedef, which is defined in &lt;sys/types.h&gt;. A program can obtain the process ID of the process it’s running in with the getpid() system call, and it can obtain the process ID of its parent process with the getppid() system call.<br />
Lets see an example of getpid() and getppid()</p>
<p>#include &lt;stdio.h&gt;<br />
#include &lt;unistd.h&gt;<br />
int main ()<br />
{<br />
printf (“The process ID is %d\n”, (int) getpid ());<br />
printf (“The parent process ID is %d\n”, (int) getppid ());<br />
return 0;<br />
}</p>
<p>If you invoke this program several times, a different process ID is reported because each invocation is in a new process. However, if you invoke it every time from the same shell, the parent process ID (that is, the process ID of the shell process) is the same.</p>
<p><strong>Viewing Active Processes</strong></p>
<p>The ps command displays the processes that are running on your system. The GNU/Linux version of ps has lots of options because it tries to be compatible with  versions of ps on several other UNIX variants. These options control which processes are listed and what information about each is shown.</p>
<p>root@localhost~]#% ps<br />
PID        TTY     TIME         CMD<br />
21693   pts/8   00:00:00   bash<br />
21694   pts/8   00:00:00    ps</p>
<p><strong>Killing a Process</strong></p>
<p>You can kill a running process with the kill command. Simply specify on the command line the process ID of the process to be killed.<br />
<strong>Creating Processes</strong><br />
Two common techniques are used for creating a new process.</p>
<ul>
<li>The first is relatively simple but should be used sparingly because it is inefficient and has considerably security risks using system().</li>
<li>The second technique is more complex but provides greater flexibility, speed, and security using fork() and exec().</li>
</ul>
<p><strong>Using system() call</strong></p>
<p>The system function in the standard C library provides an easy way to execute a command from within a program, much as if the command had been typed into a shell.<br />
In fact, system creates a subprocess running the standard Bourne shell (/bin/sh) and hands the command to that shell for execution.</p>
<p>For example, in our program we invoke the ls command to display the contents of the root directory, as if you typed ls -l / into a shell.</p>
<p>#include &lt;stdlib.h&gt;<br />
int main ()<br />
{<br />
int return_value;<br />
return_value = system (“ls -l /”);<br />
return return_value;<br />
}</p>
<p>As the system function uses a shell to invoke your command, it’s subject to the features,  imitations, and security flaws of the system’s shell. You can’t rely on the availability of any particular version of the Bourne shell.</p>
<p><strong>Using fork and exec</strong></p>
<p>Linux provides one function, fork, that makes a child process that is an exact copy of its arent process. Linux provides another set of functions, the exec family, that causes a particular process to cease being an instance of one program and to instead become an instance of another program.</p>
<p>To spawn a new process, you first use fork to make a copy of the current process.Then you use exec to transform one of these processes into an instance of the program you want to spawn.</p>
<p><strong>Calling fork()</strong><br />
General Syntax for fork() call is:</p>
<p>#include &lt;unistd.h&gt;</p>
<p>pid_t fork(void);</p>
<p>fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:</p>
<ul>
<li>The child has its own unique process ID, and this PID does not match the ID of any existing process group.</li>
<li>The child’s parent process ID is the same as the parent’s process ID.</li>
<li>The child does not inherit its parent’s memory locks.</li>
<li>Process resource utilizations and CPU time counters are reset to zero in<br />
the child.</li>
<li>The child’s set of pending signals is initially empty.</li>
<li>The child does not inherit semaphore adjustments from its parent.</li>
<li>The child does not inherit timers from its parent</li>
</ul>
<p>The child process is created with a single thread — the one that called fork(). The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables, and other pthreads objects.</p>
<p>The child inherits copies of the parent’s set of open file descriptors. Each file descriptor in<br />
the child refers to the same open file description (see open(2)) as the corresponding file<br />
descriptor in the parent. This means that the two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the description of F_SETOWN and F_SETSIG in fcntl(2)).</p>
<p>The child inherits copies of the parent’s set of open message queue descriptors (see<br />
mq_overview(7)). Each descriptor in the child refers to the same open message queue description as the corresponding descriptor in the parent. This means that the two descriptors share the same flags (mq_flags).</p>
<p>So how do the two processes differ? First, the child process is a new process and therefore has a new process ID, distinct from its parent’s process ID. One way for a program to distinguish whether it’s in the parent process or the child process is to call getpid. However, the fork function provides different return values to the parent and child processes—one process “goes in” to the fork call, and two processes “come out,” with different return values.The return value in the parent process is the process ID of the child.The return value in the child process is zero. Because no process ever has a process ID of zero, this makes it easy for the program whether it is now running as the parent or the child process.<br />
#include &lt;stdio.h&gt;<br />
#include &lt;sys/types.h&gt;<br />
#include &lt;unistd.h&gt;<br />
int main ()<br />
{<br />
pid_t child_pid;<br />
printf (“the main program process ID is %d\n”, (int) getpid ());<br />
child_pid = fork ();<br />
if (child_pid != 0)<br />
{<br />
printf (“this is the parent process, with id %d\n”, (int) getpid ());<br />
printf (“the child’s process ID is %d\n”, (int) child_pid);<br />
}<br />
else<br />
printf (“this is the child process, with id %d\n”, (int) getpid ());<br />
return 0;<br />
}</p>
<p>In our next article we will cover exec families and also combining both exec and fork.</p>
<p>Sources:</p>
<ul>
<li>linux man pages</li>
<li>Advance Linux Programming by Mark Mitchell, Jeffrey Oldham,<br />
and Alex Samuel</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/03/understanding-linux-interprocess-communication-ii-process-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding InterProcess Communication in Linux: Introduction to Interprocess Communication</title>
		<link>https://www.emblogic.com/blog/03/understanding-interprocess-communication-in-linux-introduction-to-interprocess-communication/</link>
		<comments>https://www.emblogic.com/blog/03/understanding-interprocess-communication-in-linux-introduction-to-interprocess-communication/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 05:33:19 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=2025</guid>
		<description><![CDATA[Before we dive deep into the subject matter, there are a few things that we should understand: Let&#8217;s understand the actual meaning of: IPC. IPC is an abbreviation that stands for &#8220;Inter-process Communication&#8221;. It denotes a set of system calls &#8230; <a href="https://www.emblogic.com/blog/03/understanding-interprocess-communication-in-linux-introduction-to-interprocess-communication/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Before we dive deep into the subject matter, there are a few things that we should understand:</p>
<p>Let&#8217;s understand the actual meaning of: IPC. IPC is an abbreviation that stands for &#8220;Inter-process Communication&#8221;. It denotes a set of system calls that allows a User Mode process to:</p>
<ul>
<li>Synchronize itself with other processes by means of &#8216;Semaphores&#8217;.</li>
<li>Send messages to other processes or receive messages from them.</li>
<li>Share a memory area with other processes.</li>
</ul>
<p>IPC was introduced in a development UNIX variant called &#8220;Columbus Unix&#8221; and IPC was introduced in a development UNIX variant called &#8220;Columbus Unix&#8221; and later adopted by AT&amp;T&#8217;s System III. It is now commonly found in most UNIX systems, including GNU/Linux.</p>
<p><strong>Need of Inter process communication:</strong></p>
<p>There are several reasons for providing an environment that allows process cooperation:</p>
<ul>
<li>Information sharing</li>
<li>Speedup</li>
<li>Modularity</li>
<li>Convenience</li>
</ul>
<p>System V IPC is more heavyweight than BSD mmap, and provides three methods of communication:</p>
<ul>
<li>message queues,</li>
<li>semaphores, and</li>
<li>shared segments.</li>
</ul>
<p>Like BSD mmap, System V IPC uses files to identify shared segments. Unlike BSD, System V uses these files only for naming. Their contents have nothing to do with the initialization of the shared segment. IPC data structures are created dynamically when a process requests an IPC Resource, i.e. a semaphore, a message queue, or a shared memory segment.</p>
<p><strong>What is Inter Process Communication means?</strong></p>
<p>The mechanism in which User Mode processes synchronize themselves and exchange data is referred to as &#8220;Inter-process Communication (IPC)&#8221; in UNIX Systems (that includes Linux too).</p>
<p><strong>But in what way exactly do terms like: Semaphores, Shared Memory and Message Queues relate to IPC?</strong><br />
Semaphores, Shared Memory and Message Queues do relate to IPC in a very special way, since Semaphores, Shared Memory and Message Queues are &#8220;Inter-process Communication Resources&#8221; or &#8220;Inter-process Communication Facilities&#8221;, and different in the way they represent IPC from &#8220;Inter-process Communication Mechanisms&#8221; like Pipes and FIFOs. Semaphores, Shared Memory and Message Queues are System V (AT&amp;T System V.2 release of UNIX) IPC facilities, and they represent wrapper functions that have been developed and inserted in suitable libraries to harness the energy and beauty of IPC mechanisms.</p>
<p>Data sharing among processes can be obtained by storing data in temporary files protected by locks. But this mechanism is never implemented as it proves costly since it requires accesses to the disk filesystem. For that reason, all UNIX Kernels include a set of system calls that supports process communications without interacting with the filesystem.</p>
<p>Application programmers have a variety of needs that call for different communication mechanisms. Some of the basic mechanisms that UNIX systems, GNU/Linux is particular has to offer are:</p>
<ul>
<li>Pipes and FIFOs: Mainly used for implementing producer/consumer interactions among processes. Some processes will fill the pipe with data while others will extract from it.</li>
<li>Semaphores: Here we refer to (NOT the POSIX Realtime Extension Semaphores applied to Linux Kernel Threads), but System V semaphores which apply to User Mode processes. Used for locking critical sections of code.</li>
<li>Message Queues: To set up a message queue between processes is a way to exchange short blocks (called messages) between two processes in an asynchronous way.</li>
<li>Shared Memory: A mechanism (specifically a resource) applied when processes need to share large amounts of data in an efficient way.</li>
</ul>
<p>In our next article, we will cover understanding of PIPES and FIFOs with example.</p>
<p>Source:</p>
<ul>
<li>http://linux.omnipotent.net/article.php</li>
<li>http://en.wikipedia.org/wiki/Inter-process_communication</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/03/understanding-interprocess-communication-in-linux-introduction-to-interprocess-communication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solution: How to Create a man page?</title>
		<link>https://www.emblogic.com/blog/12/solution-how-to-create-a-man-page/</link>
		<comments>https://www.emblogic.com/blog/12/solution-how-to-create-a-man-page/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 08:28:33 +0000</pubDate>
		<dc:creator><![CDATA[msiddarth]]></dc:creator>
				<category><![CDATA[Embedded Linux]]></category>
		<category><![CDATA[Linux Internals and System Programming]]></category>

		<guid isPermaLink="false">http://emblogic.org/blog/?p=1436</guid>
		<description><![CDATA[Step 1: type in: &#8220;vi myapp.1&#8243; (yes, it&#8217;s a &#8220;one&#8221; at the end it&#8217;s NOT an &#8220;L&#8221;). Step 2: Type it in exactly the way it&#8217;s given, maintaining all the spaces and every word of it. .TH MYAPP 1 .SH &#8230; <a href="https://www.emblogic.com/blog/12/solution-how-to-create-a-man-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><strong>Step 1:</strong> type in: &#8220;vi myapp.1&#8243; (yes, it&#8217;s a &#8220;one&#8221; at the end it&#8217;s NOT an &#8220;L&#8221;).</p>
<p><strong>Step 2:</strong> Type it in exactly the way it&#8217;s given, maintaining all the spaces and every word of it.</p>
<p>.TH MYAPP 1</p>
<p>.SH NAME myapp \- A Demonstration application.</p>
<p>.SH SYNOPSIS .B myapp [\-option ...]</p>
<p>.SH DESCRIPTION  \fImyapp\fP is a complete application that  does nothing useful.</p>
<p>It was written for demonstration purposes for http://www.linux.com</p>
<p>.SH OPTIONS It doesn&#8217;t have any, but let&#8217;s pretend, to make this template complete:</p>
<p>.TP .BI \-option If there was an option, it would not be -option.</p>
<p>.SH RESOURCES myapp uses almost no resources.</p>
<p>.SH DIAGNOSTICS The program should provide an approximate position  where the cat ought to be found. The return value is in terms  of time, distance and direction.</p>
<p>.SH SEE ALSO The only other program we know with this little  functionality is the ubiquitous hello world application.</p>
<p>.SH COPYRIGHT myapp is Copyright (c) 2011-2012 M.Siddarth</p>
<p>This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.</p>
<p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.</p>
<p>You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.</p>
<p>.SH BUGS There were a few bugs, but my naughty cat ate all of them.<br />
.SH AUTHORS XXXXX<br />
E-mail : xxxx@xxx.org</p>
<p><strong>Step 3:</strong> Okay, after typing in everything, check once more, and then save and exit. Let&#8217;s get a custom manual page running first! Now, we have the source to the manual page ready, so we can process it with groff. The groff command commonly produces ASCII (American Standard Code for Information Interchange) text (-Tascii) or PostScript (-Tps) output. The -man option in the command line tells groff that it&#8217;s a manual page that we want to create. So, type in at the command prompt (from within the same directory where the myapp.1 file resides): &#8220;groff -Tascii -man myapp.1&#8243; and press enter.</p>
<p><strong>Step 4:</strong> We need to install it in the proper directory for using from the command line. So, for installing our custom man page, first we need to convert it into a .gz file and then install it in /usr/share/man/man1 directory. But first, don&#8217;t forget to create a backup of our text file &#8220;myapp.1&#8243;. Name the backup &#8220;myapp.1.bak&#8221;. We need this backup in case we need to create future manual pages, or alter the already created one (when it&#8217;s converted into a .gz file and installed into the /usr/share/man/man1 directory. So type in (from within the directory that contains the file myapp.1): &#8220;cp myapp.1 myapp.1.bak&#8221; and press enter.</p>
<p><strong>Step 5:</strong> The backup created, now type in: &#8220;gzip myapp.1&#8243; and press enter. This creates a file myapp.1.gz that now needs to be installed into the /usr/share/man/man1 directory for future use.</p>
<p><strong>Step 6:</strong> Type in: &#8220;cp myapp.1.gz /usr/share/man/man1&#8243; and press enter. This will successfully install the custom manual page and prepare it for further use as when required.</p>
<p><strong> Step 7</strong>: Now for using the manual page, from the command line, just type in: &#8220;man myapp&#8221; and press enter. The first time someone asks for this manual page, the man command will automatically format and display it.</p>
<p>We started out with a text file called &#8220;myapp.1&#8243;, processed it using the &#8220;groff&#8221; utility, then converted the &#8220;myapp.1&#8243; into &#8220;myapp.1.gz&#8221; and finally installed it within /usr/share/man/man1 directory. These are the only steps that one needs to follow for creating his/her own manual pages in Linux. And for future use just use the template file for creating more manual pages. <strong></strong></p>
<p><strong>One question that needs to be asked is: Hey, why did you create that compressed file using &#8220;gzip&#8221;? What&#8217;s the use of this step? </strong></p>
<p>Who can provide the answer?</p>
]]></content:encoded>
			<wfw:commentRss>https://www.emblogic.com/blog/12/solution-how-to-create-a-man-page/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
