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

/ 6,495评论 / 48642阅读 / 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. Dennis Wise说道:

    Me encanta cómo destacas la importancia del liderazgo efectivo en la gestión de un club de fútbol https://cardiofit.mystrikingly.com/

  2. Paul Sparks说道:

    Go88 là một sân chơi trực tuyến vô cùng hấp dẫn, nơi bạn có thể thỏa sức khám phá các trò chơi đa dạng và thú vị go88

  3. Herbert Benson说道:

    Did you know there’s 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

  4. Milton Byrd说道:

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

  5. Lily Moran说道:

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

  6. Virginia Casey说道:

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

  7. Bess Rose说道:

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

  8. Eunice Alvarado说道:

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

  9. Lena Santiago说道:

    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

  10. Rodney Sims说道:

    Were you aware 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

  11. Bess Mason说道:

    Appreciate the detailed information. For more, visit 減重

  12. Timothy Reed说道:

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

  13. Carrie Snyder说道:

    Were you aware there’s 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

  14. Seth Reyes说道:

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

  15. Olga Patterson说道:

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

  16. Mildred Lewis说道:

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

  17. Ricky Ball说道:

    Did you know there’s 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

  18. Rebecca Clark说道:

    Have you heard 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

  19. Chester Sanchez说道:

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

  20. warung makan说道:

    I relish, cause I found exactly what I used to be taking a look for.
    You’ve ended my four day long hunt! God Bless you man. Have a nice day.
    Bye

  21. Leah Newman说道:

    Thanks for the insightful write-up. More like this at Go to this site

  22. Harvey Becker说道:

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

  23. Janie Hamilton说道:

    Did you know 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

  24. Stanley Spencer说道:

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

  25. Jerry Johnson说道:

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

  26. Charles Brown说道:

    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

  27. Jeanette Webb说道:

    Have you heard 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

  28. Lewis Griffin说道:

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

  29. Lenora Wells说道:

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

  30. Ronnie Tyler说道:

    Did you know there’s 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

  31. Augusta Byrd说道:

    Have you heard 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

  32. Edna Fletcher说道:

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

  33. Brent Daniels说道:

    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

  34. Jessie Dunn说道:

    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

  35. Edith Gardner说道:

    Were you aware there’s 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

  36. Mattie Adams说道:

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

  37. Hokicoy Gacor说道:

    What’s up friends, how is all, and what you wish for to say on the topic
    of this piece of writing, in my view its truly
    remarkable for me.

  38. Patrick Norris说道:

    Were you aware there’s 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

  39. sv388说道:

    Hi there just wanted to give you a quick heads up. The
    text in your article seem to be running off the screen in Internet explorer.
    I’m not sure if this is a format issue or something to do
    with web browser compatibility but I figured I’d post to
    let you know. The style and design look great though!
    Hope you get the problem fixed soon. Many thanks

  40. Sean Bowers说道:

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

  41. Elva Owen说道:

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

  42. Ernest Johnson说道:

    Were you aware there’s 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

  43. Andre Ruiz说道:

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

  44. Fannie Schultz说道:

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

  45. Blake Baldwin说道:

    Tus consejos son muy prácticos y aplicables a cualquier club de fútbol Análisis

  46. Billy Gibson说道:

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

  47. Adelaide Pena说道:

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

  48. Willie Myers说道:

    Were you aware there’s 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

  49. Kate Sandoval说道:

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

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

发表回复

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