【问题标题】:Process Controlling过程控制
【发布时间】:2012-09-17 06:19:15
【问题描述】:

所以我正在尝试完成我的编程任务,这里是:

Write a C/C++ program (call it string invert) that takes a
string argument from the command line and outputs the string in reversed order. Here comes
the twist: Each process can output at most one character. If you want to output more than a
single character, you must fork off one or more processes in order to do that, and each of the
forked processes in turn outputs a single character. After the call to program string invert
with the command line argument, the output should appear, and no more processes should
be running, in addition to the shell. Test your program on any UNIX/LINUX machine, and
turn in the source code as part of the written assignment. (The source code should be at
most a few lines long.)

我可以轻松地读取和反转字符串,没问题。问题是当它说“每个进程最多可以输出一个字符”时是什么意思。我什至不明白那是什么意思。我不需要任何代码,我相信一旦我理解我就可以自己做。我只需要有人解释一下这是什么意思。

【问题讨论】:

    标签: c++ c unix fork


    【解决方案1】:

    假设您有输入“abcd”,那么您的程序应该为每个字符生成一个进程。所以第一个进程将返回“d”,第二个进程将返回“c”,依此类推。该作业可能是对您对同步过程的理解程度的测试。

    【讨论】:

      【解决方案2】:

      每个进程都应该只打印 ONE 字符。示例:

      $yourProgamm sample
      

      通常你会遍历示例字符串,并简单地通过调用cout 来打印每个字符。但是,您应该每个进程只打印一个字符。意思是你输出e,一切都很好。但是,如果您再次运行循环以输出l,则相同的进程会打印第二个字符。

      所以你必须为每个字符fork一个进程,让那个进程打印那个一个字符并继续你的循环。请务必与join 同步,否则您可能会得到随机顺序输出(我想这是分配的要点,只需在没有它的情况下运行几次即可了解我的意思)。

      【讨论】:

        猜你喜欢
        • 2015-05-01
        • 1970-01-01
        • 2013-05-22
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-19
        • 1970-01-01
        相关资源
        最近更新 更多