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

/ 6,496评论 / 48648阅读 / 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. Callie Roberts说道:

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

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

  3. Mable Bowers说道:

    Estoy emocionado de implementar estos secretos de los atletas de élite en mi entrenamiento. Gracias por compartir recursos adicionales a través de Rutinas de ejercicio , donde puedo aprender más y mejorar mi rendimiento deportivo

  4. Sally Dawson说道:

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

  5. Leonard Lindsey说道:

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

  6. Hokicoy Gacor说道:

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

  7. Tillie Maxwell说道:

    Me encanta el pasto San Agustín por su aspecto exuberante y resistencia. ¡Gracias por compartir esta información! Ahora sé dónde encontrarlo, en https://pastosantacruz.com/contacto/

  8. Lily Johnston说道:

    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

  9. Olivia Day说道:

    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

  10. Gregory Manning说道:

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

  11. Eleanor Burns说道:

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

  12. Mable Mason说道:

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

  13. Ophelia Bennett说道:

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

  14. Alta Lee说道:

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

  15. Blanche Hoffman说道:

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

  16. Gary Gutierrez说道:

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

  17. 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

  18. Steve Becker说道:

    Nettruyen là nơi tuyệt vời để thỏa mãn đam mê truyện tranh của bạn Duyệt qua trang web manga này nhé

  19. Flora Jimenez说道:

    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

  20. Winifred Shaw说道:

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

  21. Alma Medina说道:

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

  22. Sophia Barrett说道:

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

  23. Amy Bradley说道:

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

  24. Louise Rodriquez说道:

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

  25. Lottie Taylor说道:

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

  26. Jesse Davis说道:

    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

  27. Anne Fox说道:

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

  28. Ricardo Gardner说道:

    I felt like I entered a different world during my desert safari adventure with Cameldesertsafaridubai quad bike hire company Dubai

  29. Gerald Lee说道:

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

  30. Angel Rodgers说道:

    Este artículo me ha dado ideas frescas y novedosas para mejorar la gestión de mi club de fútbol https://www.instapaper.com/read/1693655853

  31. Lilly Sutton说道:

    Estoy impresionado con tu conocimiento sobre la gestión de club deportivo Escuela de Fútbol de Mareo

  32. Alvin Reese说道:

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

  33. Jose Riley说道:

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

  34. Cordelia Patrick说道:

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

  35. Darrell Greer说道:

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

  36. Ray Briggs说道:

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

  37. Agnes Hanson说道:

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

  38. Timothy McDaniel说道:

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

  39. Elijah Lewis说道:

    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

  40. Roger Flores说道:

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

  41. Ricky Russell说道:

    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

  42. Gertrude Wright说道:

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

  43. Harold Palmer说道:

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

  44. Philip Henry说道:

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

  45. Mittie Perez说道:

    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

  46. Derek Ruiz说道:

    Estoy impresionado por cómo los atletas de élite encuentran el equilibrio perfecto entre el entrenamiento y la recuperación. Gracias por compartir estos secretos y proporcionar recursos adicionales a través de Recuperación

  47. Louisa Tucker说道:

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

  48. Katie Sanchez说道:

    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

  49. Francis Erickson说道:

    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

  50. Noah Drake说道:

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

发表回复

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