首页 > 技术相关, 读书笔记 > 程序/进程的前世今生

程序/进程的前世今生

2012年2月8日 发表评论 阅读评论

在这篇文章中,将介绍下程序从源码,到目标文件,到二进制码,再到装载,运行以及退出的整个过程,简称程序/进程的前世今生。

首先,区分一下程序和进程的概念。程序是一个静态的概念,而进程是一个动态的概念。程序一般是指从源码到二进制码这些过程的实体(勉强简称为前世),而进程则是从装载,到执行,到推出的实体(勉强简称为今生)。维基百科对两个概念的定义为:

程序的定义:

计算机程序或者软件程序(通常简称程序)是指一组指示计算机或其他具有讯息处理能力装置每一步动作的指令,通常用某种程序设计语言编写,运行于某种目标体系结构上。

进程的定义:

行程(英语:Process,中国大陆译作进程,台湾译作行程)是计算机中已执行程序的实体。行程本身不会执行,是线程的容器。程式本身只是指令的集合,行程才是程式(那些指令)的真正执行。

对于上面进程的定义,个人觉得有点问题(不仅仅是大陆台湾叫法不同的问题),我认为应该是”进程是计算机已执行程序的实体。对于多线程程序,程序可能存在多个同时执行的指令流和控制流,称为线程。程序本身知识指令的集合,进程才是那些指令的真正执行”。

下面,就开始介绍程序/进程的前世今生,由于内容较多,在这里只是介绍前世今生的各个阶段,各个阶段的详细介绍,后面有空的话,并且觉得有必要的话,会单独写一文。不废话,转入正题,程序/进程大致需要经过以下阶段完成其前世今生(以C语言以及Linux进程为例):

  • 源码阶段:表现形式为符合某种程序语言设计语言语法的文本。如大名鼎鼎的C语言以及其上大名鼎鼎helloworld.c源码:
    #include <stdio.h>
    
    void main(void)
    {
        printf("Hello World\n");
    }
  • 编译阶段:这阶段的作用是将程序从源码转化成特定体系结构特定指令集的二进制文件的过程,这个阶段一般由编译器完成,如Linux下gcc可以通过”gcc helloworld.c -o helloworld”完成签署helloword.c的编译。编译是一个很复杂的过程,不是一步完成的,可以分成很多步,事实上,gcc程序就是由一系列工具组成的,不通工具负责不同编译的不通阶段,大致分为一下阶段(这里简略介绍下,详细介绍可能会另开一文):
    1. 预处理:主要包括宏替换,文件包含,以及特殊符号等的处理
    2. 编译:主要包括词法分析和语法分析以及优化。此阶段完成后将得到一个临时汇编文件进而根据指令编码转化成二进制的目标文件。目标文件一般以.o结尾。
    3. 链接:链接过程主要完成的是不通源文件之间函数调用的链接以及不通目标文件地址的统一编码,从而得到真正的可执行文件。链接分为两种,静态链接和动态链接。
  • 加载阶段:程序的加载(装载)是通过操作系统的程序加载器将程序加载到内存并建立起进程的过程(需要强调的是,加载器不会把整个程序加载进去,很多都是动态加载的,一般都是只会将起始地址更新到进程控制块中)。详细的过程另文说明。
  • 执行阶段:就是执行程序的指令,不过其中会碰到各种内部的例外(如缺页)以及外部的中断(如IO),需要处理。暂时不详细介绍。
  • 进程退出:这个阶段是指进程被杀死时的行为,主要包括释放内存,释放打开的文件等操作。

 



本文作者: Sigma    在新浪微博关注SigmaSigmaWeibo    RSS订阅本博客
本文链接: http://www.sigma.me/2012/02/08/%e7%a8%8b%e5%ba%8f%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%89%8d%e4%b8%96%e4%bb%8a%e7%94%9f.html
本博客采用知识共享署名—非商业性-禁止演绎使用3.0协议进行许可,转载请保留作者和原文链接。

  1. 2019年3月6日06:18 | #1

    Hi there, its good post concerning media print, we all understand media is a
    wonderful source of data.

  2. 2019年3月6日17:18 | #2

    We absolutely love your blog and find nearly all of your post’s to
    be exactly what I’m looking for. Do you offer guest writers
    to write content for yourself? I wouldn’t mind writing a post or elaborating on most of the
    subjects you write regarding here. Again, awesome site!

  3. 2019年3月6日17:58 | #3

    Touche. Sound arguments. Keep up the great work.

  4. 2019年3月6日19:18 | #4

    Hello are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and create my own. Do you need any coding expertise to make your own blog?

    Any help would be greatly appreciated!

  5. 2019年3月7日04:35 | #5

    Hi there, just wanted to say, I liked this blog post.

    It was inspiring. Keep on posting!

  6. 2019年3月7日04:40 | #6

    I do consider all of the ideas you’ve introduced in your post.
    They are really convincing and can definitely work.
    Nonetheless, the posts are very brief for newbies.
    May you please prolong them a little from next time?
    Thank you for the post.

  7. 2019年3月8日10:17 | #7

    Good day I am so grateful I found your webpage, I really found you by accident,
    while I was searching on Askjeeve for something else, Anyhow I am here now and would just like to say
    many thanks for a incredible post and a all round interesting blog
    (I also love the theme/design), I don’t have time
    to go through it all at the minute but I have book-marked it and also added in your RSS feeds, so when I
    have time I will be back to read more, Please do keep up the great work.

  8. 2019年3月8日22:27 | #8

    Its like you read my mind! You appear to know a lot about
    this, like you wrote the book in it or something. I think that you
    could do with a few pics to drive the message
    home a little bit, but other than that, this is great blog.
    A great read. I will definitely be back.

  9. 2019年3月9日13:42 | #9

    Just desire to say your article is as astonishing. The clearness for your put up is just spectacular and i could suppose you’re an expert on this subject.
    Well with your permission allow me to seize your feed to stay updated with coming near near post.
    Thanks one million and please continue the rewarding work.

  10. 2019年3月9日14:41 | #10

    whoah this weblog is fantastic i love studying your articles.
    Stay up the good work! You recognize, a lot of persons are searching round for this info,
    you can aid them greatly.

  11. 2019年3月10日10:02 | #11

    I think this is one of the most significant information for me.
    And i am glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really nice : D.
    Good job, cheers

  12. 2019年3月10日10:31 | #12

    A fascinating discussion is worth comment. I do think that you need to publish
    more about this topic, it may not be a taboo matter but generally
    people don’t discuss such subjects. To the next!
    Cheers!!

  13. 2019年3月12日03:05 | #13

    What’s up to every body, it’s my first go to see of this weblog; this
    weblog contains awesome and genuinely excellent material for
    visitors.

  14. 2019年3月12日04:07 | #14

    This is a topic that is close to my heart… Cheers!
    Exactly where are your contact details though?

  15. 2019年3月12日07:15 | #15

    Yes! Finally something about ig.

  16. 2019年3月12日18:59 | #16

    I do accept as true with all of the ideas you’ve offered to your post.
    They are really convincing and will certainly work.
    Still, the posts are too brief for beginners. May you please prolong them a bit from subsequent time?
    Thank you for the post.

  17. 2019年3月13日03:00 | #17

    Thanks for ones marvelous posting! I seriously enjoyed reading it, you will
    be a great author.I will make sure to bookmark your
    blog and will eventually come back someday. I want to encourage yourself to continue your great work, have a nice morning!

  18. 2019年3月13日08:03 | #18

    Hi there, just became alert to your blog through Google, and found that it’s truly informative.

    I’m going to watch out for brussels. I’ll appreciate
    if you continue this in future. Lots of people will be benefited from your writing.
    Cheers!

  19. 2019年3月13日09:47 | #19

    Its such as you read my mind! You seem to understand
    a lot about this, like you wrote the guide in it or something.
    I think that you could do with a few % to power the message home
    a bit, however other than that, this is great blog.
    A great read. I will certainly be back.

  20. 2019年3月13日15:44 | #20

    I do believe all of the ideas you have offered for your post.
    They are very convincing and can certainly work.
    Nonetheless, the posts are too quick for beginners. Could
    you please extend them a little from subsequent time?

    Thank you for the post.

  21. 2019年3月14日06:54 | #21

    Great goods from you, man. I have consider your stuff prior to and you are just extremely magnificent.

    I really like what you have got right here, certainly
    like what you’re saying and the way in which you say it.
    You’re making it enjoyable and you continue to care for to stay it wise.
    I can’t wait to learn far more from you. This is really a
    great site.

  22. 2019年3月14日09:30 | #22

    Hi there i am kavin, its my first time to commenting anywhere,
    when i read this piece of writing i thought i could also make
    comment due to this good piece of writing.

  23. 2019年3月14日14:45 | #23

    Hello there, You have done an incredible job.
    I’ll certainly digg it and personally suggest to my friends.
    I am confident they will be benefited from this site.

  24. 2019年3月15日02:16 | #24

    Everything is very open with a really clear clarification of the issues.
    It was really informative. Your site is very useful. Thanks
    for sharing!

  25. 2019年3月15日02:40 | #25

    Hello, i feel that i saw you visited my website thus i got here
    to go back the want?.I’m trying to in finding issues to improve my
    site!I guess its adequate to use a few of your ideas!!

  26. 2019年3月15日04:25 | #26

    Undeniably imagine that which you said. Your favourite justification appeared to be on the web the easiest thing to be mindful of.

    I say to you, I certainly get irked even as other people consider concerns that they just don’t recognise about.
    You managed to hit the nail upon the top as well as defined out the entire
    thing with no need side effect , people could take a signal.
    Will likely be back to get more. Thanks

  27. 2019年3月15日06:28 | #27

    Great blog here! Also your site loads up very fast!
    What web host are you using? Can I get your affiliate
    link to your host? I wish my web site loaded up as quickly as yours lol

  28. 2019年3月15日09:31 | #28

    What’s up to all, it’s actually a pleasant for me to visit this web page,
    it consists of valuable Information.

  29. 2019年3月15日09:53 | #29

    Howdy I am so excited I found your weblog, I really found you by
    accident, while I was searching on Yahoo for something else, Anyhow I am here now and would just like to say cheers for a incredible post and a
    all round thrilling blog (I also love the theme/design), I don’t have time to read through it all at
    the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read more, Please do
    keep up the excellent b.

  30. 2019年3月15日13:22 | #30

    My partner and I stumbled over here coming from a different website and thought I should
    check things out. I like what I see so now i’m following you.
    Look forward to going over your web page for a second time.

  31. 2019年3月15日15:44 | #31

    The other day, while I was at work, my cousin stole my apple ipad and tested to
    see if it can survive a 25 foot drop, just so she can be a youtube sensation. My iPad
    is now destroyed and she has 83 views. I know this is entirely off topic but I had
    to share it with someone!

  32. 2019年3月16日00:10 | #32

    Valuable information. Fortunate me I found your site unintentionally, and I am shocked why this twist of fate didn’t
    happened in advance! I bookmarked it.

  33. 2019年3月16日06:18 | #33

    This article is in fact a good one it assists new internet people, who are wishing in favor of blogging.

  34. 2019年3月16日11:43 | #34

    We are trusted Mississauga Heating and Cooling
    Experts.

  35. 2019年3月16日11:57 | #35

    Hello to every one, the contents present at this
    website are actually amazing for people experience, well, keep up the nice work
    fellows.

  36. 2019年3月17日00:59 | #36

    Magnificent web site. Lots of helpful information here.

    I am sending it to a few friends ans also sharing in delicious.
    And of course, thank you to your effort!

  37. 2019年3月18日12:28 | #37

    Hello, I enjoy reading through your article post. I wanted
    to write a little comment to support you.

  38. 2019年3月18日23:51 | #38

    Does your site have a contact page? I’m having problems locating it but, I’d like to shoot you an e-mail.
    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it develop over time.

  39. 2019年3月19日07:51 | #39

    Hey there! I know this is kinda off topic but I was wondering which blog platform are you using for this website?

    I’m getting fed up of WordPress because I’ve had issues with hackers
    and I’m looking at alternatives for another platform. I would be awesome if you could
    point me in the direction of a good platform.

  40. 2019年3月19日13:16 | #40

    Hi there to all, for the reason that I am actually keen of reading this weblog’s post to be updated daily.
    It includes pleasant data.

  41. 2019年3月19日19:21 | #41

    Do you mind if I quote a couple of your posts as long as I provide credit
    and sources back to your site? My website is in the
    very same area of interest as yours and my visitors would truly benefit from some of the information you provide here.
    Please let me know if this alright with you. Regards!

  42. 2019年3月20日18:12 | #42

    When someone writes an paragraph he/she maintains the
    idea of a user in his/her brain that how a user can be aware of it.

    So that’s why this post is outstdanding. Thanks!

  43. 2019年3月20日19:59 | #43

    Thanks for another informative website. Where else could I get
    that type of info written in such an ideal way? I’ve a venture that I am
    just now operating on, and I’ve been at the glance out for such information.

  44. 2019年3月20日22:23 | #44

    What’s up, all the time i used to check weblog posts here in the early hours in the break of
    day, since i like to learn more and more.

  45. 2019年3月20日23:47 | #45

    Take into account investments that provide speedy annuities.

  46. 2019年3月21日04:58 | #46

    Hey there, I think your blog might be having browser compatibility issues.
    When I look at your blog in Firefox, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just wanted to give
    you a quick heads up! Other then that, amazing blog!

  47. 2019年3月21日10:33 | #47

    I like what you guys are up too. This sort of clever work and reporting!
    Keep up the wonderful works guys I’ve you guys to blogroll.

  48. 2019年3月21日21:01 | #48

    Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get
    a lot of spam remarks? If so how do you stop it,
    any plugin or anything you can advise? I get so much lately it’s driving me mad so any help is very much appreciated.

  49. 2019年3月21日21:59 | #49

    I’m truly enjoying the design and layout of your website.

    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you
    hire out a developer to create your theme? Superb work!

  50. 2019年3月22日04:13 | #50

    Why visitors still use to read news papers when in this technological globe the whole thing is available on web?

  51. 2019年3月22日05:29 | #51

    Unquestionably believe that which you said.
    Your favorite justification appeared to be
    on the internet the simplest thing to be aware
    of. I say to you, I certainly get irked while people think about worries that they plainly
    do not know about. You managed to hit the nail upon the top as well as defined out the whole thing without
    having side-effects , people can take a signal. Will likely be back to get more.
    Thanks

  52. 2019年3月22日13:58 | #52

    I’d like to find out more? I’d love to find out some
    additional information.

  53. 2019年3月23日01:57 | #53

    Great blog you have here.. It’s difficult to find
    good quality writing like yours these days. I seriously appreciate people like you!
    Take care!!

  54. 2019年3月23日02:10 | #54

    I don’t even know how I ended up here, but I thought this
    post was great. I don’t know who you are but
    definitely you’re going to a famous blogger if you aren’t already 😉 Cheers!

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

无觅相关文章插件,快速提升流量