我必須編寫使用pipe()
來創建進程的程序。fork(),pipe()和exec()進程創建和通信
我的第一個任務是編寫一個父進程,它使用fork()
函數生成四個子進程。
一旦fork()
成功,將子進程替換爲另一個進程rover1, rover2, rover3, and rover4
,儘管它們都具有相同的代碼。
過程的功能如下。
每個子進程最初都有自己的編號。它從父母收到一個新號碼。使用以下公式它創建其自己的新的數目如下,並將其轉發到父:
mynumber = (3 * mynumber + 4 * numberreceived)/7
該過程繼續直到母體發送,該系統是穩定的消息。父母也有其初始號碼。它接收所有的孩子的數量和計算它的新號碼如下:
mynumber = (3 * mynumber + numbers sent by all the children)/7
父將這個號碼發送到它的所有兒童。這個過程將繼續,直到父母發現其號碼不再改變。那時它會告訴孩子系統已經變得穩定。
這就是我所做的,但我的教授說我必須使用exec()來執行子進程,並用另一個子進程替換子進程。我不知道如何使用exec()。你能幫我解決這個問題嗎?
我只附加第一代子代。
// I included stdio.h, unistd.h stdlib.h and errno.h
int main(void)
{
// Values returned from the four fork() calls
pid_t rover1, rover2, rover3, rover4;
int parentnumber, mynumber1, mynumber2, mynumber3, mynumber4;
int childownnumber1 = 0, status = 1, childownnumber2 = 0,
childownnumber3 = 0, childownnumber4 = 0, numberreceived = 0;
printf("Enter parent number: ");
printf("%d", parentnumber);
printf("Enter each children number");
printf("%d %d %d %d", mynumber1, mynumber2, mynumber3, mynumber4);
// Create pipes for communication between child and parent
int p1[2], p2[2];
// Attempt to open pipe
if (pipe(p1) == -1) {
perror("pipe call error");
exit(1);
}
// Attempt to open pipe
if (pipe(p2) == -1) {
perror("pipe call error");
exit(1);
}
// Parent process generates 4 child processes
rover1 = fork();
// if fork() returns 0, we're in the child process;
// call exec() for each child to replace itself with another process
if (rover1 == 0) {
for(; numberreceived != 1;) {
close(p1[1]); // Close write end of pipe
close(p2[0]); // Close read end of second pipe
// Read parent's number from pipe
read(p1[0], &numberreceived, sizeof(int));
if (numberreceived == 1) {
// System stable, end child process
close(p1[0]);
close(p2[1]);
_exit(0); // End child process
}
mynumber1 = (int)((3*mynumber1 + 4*numberreceived)/7.0);
printf("\nrover1 number: ");
printf("%i", mynumber1);
// Write to pipe
write(p2[1], &mynumber1, sizeof(int));
}
}
/* Error:
* If fork() returns a negative number, an error happened;
* output error message
*/
if (rover1 < 0) {
fprintf(stderr,
"can't fork, child process 1 not created, error %d\n",
errno);
exit(EXIT_FAILURE);
}
}
是一個什麼樣的子進程都應該包含在'roverX'可執行文件中做的功能? – user470379 2011-02-23 16:55:03
@ user470379-是的,我想。 – cool 2011-02-23 17:00:01