OnSwipe redirect code

Monday, August 31, 2009

Should Indian farmer think of his family's hunger or the country's?

This is the type of condition the typical Indian farmer currently is in. When I say typical Indian farmer, I am referring to those lean hard working people who sweat out the whole day and yet just have a hand to mouth existence, that too if they are lucky. I do not have to substantiate the last part of my previous statement as the state of our farmer is well known. No matter how hard the farmer tries, how hard he struggles, the first person to gain from that is the middle-man, with whom the farmers are compelled to park their annual produce. The reason for that is another story in itself, probably some other time. Anyways, focusing on the topic of this post, let me try and tell you why I think farmers today are faced with this question.

We all know Indian population is growing faster than any other thing in the country with an unbeatable consistency. This directly corresponds to heavily increased demand & consumption of food. Good news is that the country's agriculture is able to meet this rising demand pretty well, thanks to the technology. With improved variety of seeds and fertilizers, increased proliferation of irrigation, some education to farmers, better knowledge of weather behavior and so and so forth, the throughput per acre of land has increased, in almost every variety of cultivation. So the farmer now has more goods to sell every year and get more revenue. But sadly this is has not transformed into increased income to the farmer yet because of the ever increasing input costs. The seeds have improved, but they cost more, a lot more. The fertilizers are better, they reduce the time to grow, but they really push the farmer deep into the pit of debt. Its the same story with irrigation equipments, modern cultivation methodologies, etc.. (Government is trying various policies and subsidies, but still the costs are higher and have increased substantially). As a result the farmer is still poor and leads a hand to mouth existence. Not only that, he now has to invest a higher amount initially (which most farmes do by taking a loan from the middle-man) and hence is taking a bigger risk, because in case of a failed crop he has a much bigger debt to repay. The trade off / inequality / tragedy is clear here. The farmer is the one who is providing the country's ever increasing population with the food supply, by putting on more risk on himself and yet he earns the same as he has been earning. All that he can think of doing is to have proper meals for himself and his family, in other words - bare survival. If he starts thinking of proper education for his children, or a proper house in place of the generations old dilapidated building, he invariably has to go for a loan again. In such a situation it is but natural for the farmer to think that whether the extra risk that he is taking every year is really required? If we put ourselves in his position and think about the question the answer stands out clearly as NO. And fortunately or unfortunately this is what the Indian farmer is thinking now.

He is thinking of securing his home first. He is not thinking about the national crisis it may cause and he can't be expected to think about that either. But the government has started to see this. It also has realized that if the farmer goes his way the country will soon be facing a huge food crisis and huge imports will be inevitable. I am not sure if imports is the right word here, it might as well become begging. With us (our country) in a such needy state all our policies will be influenced either by the one who will be lending us the food or the west (Europe and US) in general. That might bring a stop to the magnificent growth picture being painted everywhere in the country now. The government knows this all and it obviously can't let this happen because anything like this will instantaneously put them out of voters' favor. So the government has already started taking measures to counter this. Currently it is not very aggressive in its approaches. It is trying to convince the farmers to grow more and work towards increasing the yield instead of just thinking of survival. Though it does not appear plausible, we might just see a rule mandating a minimum yield/unit of land coming from the government. Or to be farmer friendly we might just see government going in for huge amounts of subsidies. I do not know how the government will handle this but I think the best way is to make arrangements for farmers to get a fair value for their produce, which means getting rid of middle-men. That is a very hard thing as every activity of the farmer is linked to the middle-man and hence there has to be an alternate system in place to replace the middle-men. Lets hope that the government will think of something that will feed both the country and the farmer well and equally.

Saturday, August 15, 2009

Return value from system() is not reliable

Recently I happen to use this nifty utility available on the Linux platform to perform some maintenance work, a bit of housekeeping, before my application starts up. The need for this is not really important. Essentially, the environment needed to be configured for my application to start up and start functioning properly. This, ideally, should have been done by a configuration/setup script, probably written in Python or a similar programming language, which are meant for such tasks. But unfortunately I did have that privilege and I had to do every bit of it from my C-program. The task were simple and very regular, like clearing a workspace directory, setting appropriate permissions and the like. The initial thought was to use the dirent family of functions aka OS system calls to read the filesystem and modify it programatically. But doing that whole thing was a big PITA. Hence I took the easy route and simply used the system() function, which will execute shell commands.

The problem with this easy approach is that tracking the operation's success is really hard. The system() man page says that the function will return the actual value returned by the command that we pass to it to be executed. But sadly this is not how things are, at least on the Linux 2.6 machine on which I am developing and running my code. The return value from this system() function is highly unreliable. In fact the man page also puts it in there, but in a very subtle way. Here is a quote from the man page:
     The <b>system</b>() function returns the exit status of the shell as returned by
<b><a href="http://www.manpagez.com/man/2/waitpid/">waitpid(2)</a></b>, or -1 if an error occurred when invoking <b><a href="http://www.manpagez.com/man/2/fork/">fork(2)</a></b> or
<b><a href="http://www.manpagez.com/man/2/waitpid/">waitpid(2)</a></b>. A return value of 127 means the execution of the shell
failed.


I am not fully clear with the process semantics, but from my observations when I execute a command using this system() function the command would have executed successfully, as in, the corresponding operation would have been completed, and yet the return value would be -1, telling me that the execution of the command has failed somewhere but it does not tell me where. For example, if it is a command to clear a directory and create some other directory structure there, all these tasks would be completed. The old directory would be gone and the new ones created. I see that when I just navigate to that location from the command line, but system() would have returned -1. I initially was checking the return value to handle the failures and was taking some fail safe steps. But all that was happening even when it was all good. The logs repeatedly told me that the operations were failing where as it was all good actually.

The reason for this is not known to me. It probably lies in the quote from the man page that I have put above. As it says -1 can be returned for any of the errors, be it error from fork or wait. Now if the error was from wait, which I am guessing is the case, it makes a little sense. I think the fork and exec went through properly and the command performed the required action without any error. But later the wait failed and the system() could not collect the exit status and hence returned -1. This is the only thing that I can think of. Nevertheless bottom line is, Return value from system() is not reliable.