[linux操作系统实验]进程的创建(有时程序输出在cmd提示之后?)

/ 6,496评论 / 48653阅读 / 0点赞

问题


实验具体内容

实验内容

编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示'a',子进程分别显示字符'b'和字符'c'。试观察记录屏幕上的显示结果,并分析原因。

涉及的系统调用

…..
pid=fork( );
if  (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process! \n");
else printf("Fork fail!\n");
……
fork( )调用前
…..
pid=fork( );
if (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process!\n ");    
else printf("Fork fail!\n");
……
 …..
pid=fork( );
if (! pid) printf("I'm the child process!\n");
else if (pid>0) printf("I'm the parent process!\n ");
else printf("Fork fail!\n");
……
fork( )调用后

源代码

#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        cout << "<< coolight | [软件202,2006300052,郑泳坤]" << endl;
        int fork_num = fork();
        if(fork_num == -1){
                cout << "<< fork Error" << endl;
        }else if(fork_num == 0){ //子进程
                fork_num = fork();
                if(fork_num == -1){
                        cout << "<< fork Error" << endl;
                }else if(fork_num == 0){ //子进程
                        sleep(0.5);
                        cout << "c" << endl;
                }else if(fork_num > 0){
                        sleep(0.5);
                        cout << "b" << endl;
                }
        }else if(fork_num > 0){
                sleep(0.5);
                cout << "a" << endl;
        }
        return 0;g
}

运行结果截图

思考

  1. Loretta Stevens说道:

    Have you heard there is federal aid provided for self-employed people affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  2. Katie Willis说道:

    Were you aware there is federal aid provided for self-employed workers hit by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  3. Were you aware there’s federal aid available for self-employed individuals affected by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  4. Ernest Gonzalez说道:

    Did you know there is federal aid provided for self-employed individuals affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  5. Nicholas Wood说道:

    Were you aware there is federal aid provided for self-employed individuals affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  6. Georgie Roy说道:

    Were you aware there is federal aid provided for self-employed people impacted by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  7. Steve Morris说道:

    Have you heard there is federal aid offered to self-employed people hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  8. Leon Pierce说道:

    Have you heard there is federal aid offered to self-employed people affected by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  9. Ruby Morgan说道:

    Were you aware there is federal aid available for self-employed people hit by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  10. Addie Leonard说道:

    Were you aware there is federal aid provided for self-employed workers hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  11. Were you aware there’s federal aid provided for self-employed people affected by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  12. Luke Barber说道:

    Did you know there is federal aid available for self-employed people impacted by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  13. Emilie Duncan说道:

    Did you know there’s federal aid provided for self-employed workers affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  14. Flora Scott说道:

    Did you know there’s federal aid offered to self-employed people hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  15. Luella Dunn说道:

    Did you know there is federal aid provided for self-employed workers impacted by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  16. Dollie Andrews说道:

    Were you aware there is federal aid available for self-employed workers hit by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  17. Samuel Osborne说道:

    Were you aware there’s federal aid provided for self-employed individuals hit by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  18. Nathaniel Harmon说道:

    Have you heard there is federal aid available for self-employed people hit by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  19. Cecelia Munoz说道:

    Did you know there’s federal aid provided for self-employed workers impacted by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  20. Jose Craig说道:

    Have you heard there’s federal aid available for self-employed workers affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  21. Casino说道:

    Pretty! This has been a really wonderful post. Thank you for supplying this info.

  22. naga169说道:

    A person necessarily help to make significantly posts I’d state.
    That is the very first time I frequented your website page and thus far?

    I surprised with the research you made to create this particular publish
    amazing. Excellent task!

  23. Edwin Mullins说道:

    Did you know there’s federal aid provided for self-employed workers hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  24. Bess Santos说道:

    Have you heard there is federal aid available for self-employed individuals affected by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  25. Elijah Morris说道:

    Did you know there is federal aid provided for self-employed individuals hit by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  26. Rodney Pittman说道:

    Have you heard there’s federal aid provided for self-employed people affected by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  27. Beatrice Lane说道:

    Were you aware there’s federal aid offered to self-employed individuals affected by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  28. Willie Jefferson说道:

    Have you heard there’s federal aid available for self-employed workers impacted by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  29. Juan Palmer说道:

    Were you aware there is federal aid offered to self-employed workers affected by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  30. Lydia Black说道:

    Have you heard there is federal aid available for self-employed workers affected by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  31. Celia Nunez说道:

    Estoy impresionado con tu conocimiento sobre la gestión de club deportivo Aprenda más aquí

  32. Hokicoy Gacor说道:

    Hello there! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
    genuinely enjoy reading through your articles. Can you recommend
    any other blogs/websites/forums that go over the same topics?
    Thanks a lot!

  33. Ida Harris说道:

    Did you know there’s federal aid available for self-employed people hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  34. winning303说道:

    Keep this going please, great job!

  35. Bertie Garza说道:

    Did you know there’s federal aid available for self-employed workers impacted by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  36. Justin Moreno说道:

    Did you know there is federal aid offered to self-employed people hit by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  37. Ricky Kim说道:

    Did you know there is federal aid offered to self-employed people hit by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  38. Betty Sutton说道:

    Were you aware there is federal aid available for self-employed workers hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  39. Anthony Leonard说道:

    Were you aware there is federal aid offered to self-employed individuals hit by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

  40. Blake Evans说道:

    Did you know there is federal aid offered to self-employed people impacted by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  41. Vera Hampton说道:

    Did you know there is federal aid available for self-employed people hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  42. Florence Boyd说道:

    Thanks for the insightful write-up. More like this at https://raindrop.io/amulosvyml/bookmarks-46073332

  43. sv388说道:

    What’s up colleagues, its enormous article about tutoringand completely defined, keep it up all the time.

  44. Iva Turner说道:

    Have you heard there’s federal aid offered to self-employed workers hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  45. Bertha Casey说道:

    Have you heard there is federal aid available for self-employed individuals impacted by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  46. Fannie Fisher说道:

    Did you know there’s federal aid available for self-employed people affected by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  47. Travis Hodges说道:

    Have you heard there is federal aid provided for self-employed individuals affected by the pandemic? The SETC Tax Credit gives up to $32,220 per person setc tax credit

  48. Jimmy Ruiz说道:

    Have you heard there is federal aid provided for self-employed individuals hit by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  49. Christian Dunn说道:

    Did you know there is federal aid available for self-employed workers impacted by the pandemic? The SETC Tax Credit provides up to $32,220 per person setc tax credit

  50. Lucinda Brewer说道:

    Were you aware there’s federal aid available for self-employed workers impacted by the pandemic? The SETC Tax Credit offers up to $32,220 per person setc tax credit

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注