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

/ 7,549评论 / 53609阅读 / 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. Узнай все о операция по увеличению члена москва увеличить член цена

  2. Everything is very open with a very clear clarification of the challenges. It was definitely informative. Your site is useful. Thank you for sharing.

  3. Узнай все о септопластика носа цены в москвевыравнивание перегородки носа

  4. Néon garage说道:

    I absolutely love your site.. Very nice colors & theme. Did you develop this website yourself? Please reply back as I’m hoping to create my own blog and would like to find out where you got this from or exactly what the theme is called. Kudos.

  5. алкоголизм вывод из запоя краснодар [url=http://cah.forum24.ru/?1-19-0-00000461-000-0-0-1730745053]http://cah.forum24.ru/?1-19-0-00000461-000-0-0-1730745053[/url] .

  6. Some felt they weren’t learning anything, and the concept of four extra years of faculty, or even two, held little enchantment.

  7. 6-laptop W/L/D endgame database from Martin Fierz utilized by all programs.

  8. Cruise ship passengers solely have one yr to file go well with.

  9. Edward Winter (1998), A Chess Idealist.

  10. Metzger, Charles H. (1962).

  11. The expansion in vaccine eligibility was made attainable because the tempo of vaccinations picked up.

  12. In case you don’t reside in a type of states, they won’t consider your application for this position.

  13. The Trilateral Fee is apparently real.

  14. On March 22, 2020, Philadelphia Mayor Jim Kenney issued a stay-at-house order for town, set to take effect the next day at 8:00 am.

  15. After Barr used the Drive to turn the Imperial stormtroopers present against them, the Ninth Sister was betrayed by the Sixth Brother, who severed her proper foot and left her behind to die so that he might escape, although she managed to outlive.

  16. Wally Henschel was born in a Jewish household.

  17. Thomas Fixico Funeral services for Thomas Fixico will probably be held Monday, Dec.

  18. Army Non-public First Class Lawrence Boudreaux, was introduced with the highest French meritorious army award for his actions during World Warfare II.

  19. Earlier than it turned right into a manifesto of types, this submit was meant to be an expanded version of a comment I left beneath a Fediverse publish by Binyamin Inexperienced.

  20. I took each team in turn and added up the present league position of each side they’ve performed this season, divided by the number of video games performed.

  21. The problem exhibits economy in its development: no greater force is employed than that required to render the issue sound (that’s, to ensure that the problem’s supposed resolution is certainly an answer and that it’s the issue’s only resolution).

  22. It additionally introduced a level of tactical considering to augmentation use that makes the system work.

  23. 雨樋 読み方说道:

    Morning Submit. 30 April 1859.

  24. Dennis MEYERS, Pastor of the Assembly of God Church, Fletcher, and Rev.

  25. As soon as Black captured White’s h-pawn in the 115th transfer, only seven pieces remained on the board, and the game could possibly be present in endgame tablebases as a theoretical draw.

发表回复

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