Archive for April, 2009

Linux进程的初理解

Wednesday, April 29th, 2009

前段时间曾经得知一句话“All life is problem solving”。自我感觉这样的一句话诠释生活,多多少少显得有些凄凉。毕竟生活也有暖的一面,也有放下一切享受闲暇时光的那份心。有时候一首歌,一次美好的谈话,就能让我忘记生活中那些令人头痛的问题。生活也有些时候是无忧无虑的美好,而不都是“problem solving”。

在学习新知识的过程中,我觉得解决问题是获取新知识的最好的途径。单纯的抱着一本装满新知识的书去看,没有足够的知识背景,很难产生共鸣。也就很难将期间的知识掌握。如果带着待解决的问题去读书,不仅读的效率提高,也很容易与书中所讲产生共鸣。问题解决的同时,知识也就基本掌握了。最近一直在做嵌入式Linux下的应用程序编写,期间问题很多,多半在 APUE 的帮助下得以解决。虽然至今看了不足APUE 的1/20的知识,能够掌握一些是一些:)

提起进程,对我这个初次接触操作系统编程的新手而言是个抽象的概念。入门的文章是IBM上的一篇技术文章:对话 UNIX : UNIX 进程。这篇文章写的通俗易懂,而且有一些实践的技巧,很值得一读。当然进程的知识不只这些,还需继续学习 :)

关于进程的两个概念:
一、如上图所示,是Unix一个进程的概念化模型。一个进程就像一个容器,有属于自己的数据、应用程序的副本、打开的文件集(设备),以及自己的环境变量集,当进程 A 刚出现时,系统会对它们进行初次捕捉。

二、每个新的 Unix进程都是某个现有进程的产物。另外,每个新进程(“子”进程)是对“父”进程的克隆体(至少有一瞬间是如此),直到“子”进程继续独立执行为止。

进程的创建过程:
每个新的 Unix 进程都是某个现有进程的产物,进程的创建是一个 克隆 -> 独立 的过程。所以在创建进程的时候,要尽量保证父进程所占有的系统资源较少。克隆后不会产生系统资源不足的情况。建议在应用程序开发时,用资源小的进程作进程管理。减少进程创建过程中的资源浪费。

上图描述了进程创建的过程:
1、在 Unix 中,fork() 系统调用被用来产生新的进程。当程序 A (Program A) 执行指令 13 (Instruction 13) fork() 时,系统会立即创建进程 A 的一个精确克隆版本,并将其命名为进程 Z (Process Z) 。Z 具有和 A 相同的环境变量、相同的内存内容、相同的程序状态,打开的文件也一样。图 3 显示的是进程 A 生成进程 Z后,进程 A 和 Z 的状态。
2、进程 Z 是从进程 A 停止的地方开始执行的。也就是说,此后进程 Z 从指令 14 (Instruction 14) 处开始执行。进程 A 会在同一指令位置继续执行。
3、一般来说,指令 14 处的编程逻辑将测试当前的进程是子进程还是父进程,也就是说,进程 Z 和进程 A 中的指令 14 分别判定这两个进程是否为其他进程的后代或祖先。为了以示区别,fork() 系统调用在子进程中返回 0,但返回给父进程的却是进程 Z 的进程 ID。
4、在上次测试之后,进程 A 和进程 Z 会出现差异,每个进程会采用单独的代码路径,就像路上出现岔道,每一个都会走上不同的分枝。生成一个新进程的流程更多地被称为分叉,这就像两位旅行者走到了路上的岔道。因此,系统调用被命名为 fork()。

通过学习system()函数学习进程操作:
#include
#include
#include
#include

int system(const char * cmdstring)

{
pid_t pid;
int status;

if(cmdstring == NULL){
return (1);
}

if((pid = fork())<0){
status = -1;
}
else if(pid = 0){

execl(”/bin/sh”, “sh”, “-c”, cmdstring, (char *)0);
-exit(127);
}

else{

while(waitpid(pid, &status, 0) < 0){
if(errno != EINTER){
status = -1;
break;
}
}
}

return status;
}

You’ve got to find what you love

Sunday, April 12th, 2009

Steve Jobs
Commencement Address at Stanford University

今天学习了 Steve Jobs (CEO of Apple Computer) 2005年6月在斯坦福大学的演讲,并且深深的喜欢上了。你可能对下面的资源也感兴趣:演讲稿的英文记录,演讲的音频文件,演讲的中文字幕视频

“That’s it. No big deal. Just three stories. ” 演讲从这句话开始。Steve开始讲述也许是他生命中最重要的三个故事,并在每一个故事之后描述了自己对故事本身的理解和感受。我想这些体会是Steve 对他如何创造出辉煌事业,迎来幸福生活的总结。我在这里记录下来Steve 的这些体会,试图留下这些故事和这些感受给我的感动。

Steve 对第一个故事 “about connecting the dots” 的理解:
Again, you can’t connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something — your gut, destiny, life, karma, whatever. Because belive in the dots will connect down the road will give you the confidence to follow your heart, even when it leads you off the well-worn path, and that will make all the difference.

Steve 对第二个故事”about love and loss”的理解:
I’m pretty sure none of this would have happened if I hadn’t been fired from Apple. It was awful tasting medicine, but I guess the patient needed it. Sometimes life hits you in the head with a brick. Don’t lose faith. I’m convinced that the only thing that kept me going was that I loved what I did. You’ve got to find what you love. And that is as true for your work as it is for your lovers. Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven’t found it yet, keep looking. Don’t settle. As with all matters of the heart, you’ll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking, Don’t settle.

Steve 对第三个故事”about death”的理解:
Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma — which is living with the results of other people’s thinking. Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.

“Stay Hungry. Stay Foolish.” Steve 对年轻一代的忠告:

Stewart and his team put out several issues of The Whole Earth Catalog, and then when it had run its course, they put out a final issue. It was the mid-1970s, and I was your age. On the back cover of their final issue was a photograph of an early morning country road, the kind you might find yourself hitchhiking on if you were so adventurous. Beneath it were the words: “Stay Hungry. Stay Foolish.” It was their farewell message as they signed off. Stay Hungry. Stay Foolish. And I have always wished that for myself. And now, as you graduate to begin anew, I wish that for you.