编程介的小学生 2017-10-12 15:05 采纳率: 20.5%
浏览 1604
已采纳

Fork() Makes Trouble

Description

Consider the following C program under Redhat 9.0:

1 #include < stdio.h >

2 #include < unistd.h >

3 #include < sys/wait.h >

4 #define N ???

5 int main(void)

6 {

7 int i;

8 int ProcessID;

9 int A;

10 A=0;

11 for (i=0;i < N;i++)

12 {

13 printf("Loop %d: Process ID=%d\n", i, getpid());

14 A=A+7;

15 while ((ProcessID=fork())== -1);

16 if (ProcessID==0)

17 {

18 printf("Process ID=%d, A=%d\n", getpid(), A);

19 }

20 else

21 {

22 wait(NULL);

23 }

24 }

25 }

The fork() function in line 15 makes a copy of the current process, returns pid of the son to the father process, and returns zero to the son process. After invoking fork(), the son process prints out a message in line 18 and continues with the next loop, while the father process waits for the end of the son in line 22. Remember that once been forked, the father process and the son process are independent, i.e having different local variables, and executed IN PARALLEL (but in the programs, since the father process always waits for its son's termination just after invoking fork(), actually there is often only one running process).

You are going to print the i-th line of the program's output. Assume the pid of the original process is 1000, and pids for new generated processes are increased by 1.
Input

The first line of the input is a single integer t (1 <= t <= 20), the number of test cases. Each of the following line contains two integers n and i (1 <= n <= 15), the value of N in line 4 and which line of the program's output to print.
Output

For each input, print the i-th line of the program's output. It is confirmed that the i-th line always exists.
Sample Input

3
4 1
4 6
4 19
Sample Output

Loop 0: Process ID=1000
Process ID=1003, A=21
Loop 2: Process ID=1009
Hint

Here is the whole output of n = 4:

Loop 0: Process ID=1000
Process ID=1001, A=7
Loop 1: Process ID=1001
Process ID=1002, A=14
Loop 2: Process ID=1002
Process ID=1003, A=21
Loop 3: Process ID=1003
Process ID=1004, A=28
Loop 3: Process ID=1002
Process ID=1005, A=28
Loop 2: Process ID=1001
Process ID=1006, A=21
Loop 3: Process ID=1006
Process ID=1007, A=28
Loop 3: Process ID=1001
Process ID=1008, A=28
Loop 1: Process ID=1000
Process ID=1009, A=14
Loop 2: Process ID=1009
Process ID=1010, A=21
Loop 3: Process ID=1010
Process ID=1011, A=28
Loop 3: Process ID=1009
Process ID=1012, A=28
Loop 2: Process ID=1000
Process ID=1013, A=21
Loop 3: Process ID=1013
Process ID=1014, A=28
Loop 3: Process ID=1000
Process ID=1015, A=28

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误