C-创建两个获得50个唯一整数的过程 [英] C - Creating two processes which obtains 50 unique integers

查看:80
本文介绍了C-创建两个获得50个唯一整数的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图弄清楚此作业的逻辑.有人可以帮忙吗?根据我的理解,我必须创建2个进程,该进程从给定文件中读取N,并将N数增加1.我不了解的部分是当这两个进程都为奇数时,我如何为这两个进程分配奇数和偶数整数做完全一样的事情.

So I am trying to figure out the logic for this homework assignment. Can someone help? From what I understand I have to create 2 processes which reads the N from the given file and increment that N number by 1. The part I don't understand is how I assign odd and even integers to these 2 processes when both of them are doing the exact same thing.

写一个简单的序列号系统,通过它两个进程P1和P2可以分别获得50个唯一的整数,这样一个进程接收所有的奇数,而另一个接收所有的偶数.使用fork()调用创建P1和P2.给定一个包含单个数字的文件F,每个进程必须执行以下步骤:

一种.打开F.
b.从文件中读取序列号N.
C.关闭F.
d.输出N和过程的PID(在屏幕上或测试文件中).
e. N增加1
F.打开F.
G.将N写入F.
H.冲洗F.
一世.关闭F

Write a simple sequence-number system through which two processes, P1 and P2, can each obtain 50 unique integers, such that one receives all the odd and the other all the even numbers. Use the fork() call to create P1 and P2. Given a file, F, containing a single number, each process must perform the following steps:

a. Open F.
b. Read the sequence number N from the file.
c. Close F.
d. Output N and the process' PID (either on screen or test file).
e. Increment N by 1
f. Open F.
g. Write N to F.
h. Flush F.
i. Close F

推荐答案

这两个过程可以相似,但不完全相同.在每个进程中,都有一个循环,该循环从文件中读取N.在一个过程中,如果N为奇数,则继续,但是如果N为偶数,请睡一秒钟,然后返回循环的开始.

The two processes can be similar but not exactly the same. In each process have a loop at the start that reads N from the file. In one process, if N is odd then continue, but if N is even, sleep for a second say then go back to the start of the loop.

在另一个过程中,反转上面的奇数/偶数.

In the other process, reverse the odd/even above.

这篇关于C-创建两个获得50个唯一整数的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆