gpt4 book ai didi

c - C 中的递归斐波那契 : Fork()

转载 作者:行者123 更新时间:2023-11-30 16:40:52 25 4
gpt4 key购买 nike

我正在尝试实现一个递归斐波那契程序,该程序使用 fork() 和带有 getopt 的消息队列来设置用于打印第 n 个斐波那契数和“m”计算阈值的命令行选项。我的程序即将完成,但我的输出有问题。我的一个条件似乎工作正常,但另一个条件似乎尝试输出这两种条件。我相信这是因为 fork() 的属性。发生这种情况是因为我们不知道子进程何时产生吗?

生成文件:

# make all: compiles and links all files
# make test1: runs executable for prob1 (n=6 m=6)
# make test2: runs executable for prob1 (n=6 m=3)
# make clean: cleans up .o and *~ files
#
# Options:
# -F => nth sequence
# -S => computing threshold
#
################################################################################

CC = gcc
CFLAGS = -g -Wall -Werror -c
OBJ = main.o fib_seq.o

################################ Make All ####################################

# Compiles all files
all: main fib_seq
$(CC) $(OBJ) -o myfib

# Compiles object files
main: main.c
$(CC) $(CFLAGS) $@.c

fib_seq: fib_seq.c
$(CC) $(CFLAGS) $@.c

################################ Test ##########################################

test1: myfib
./myfib -F 6 -S 6

test2: myfib
./myfib -F 6 -S 3

############################# Clean ##########################################

clean:
$(RM) *.o *~ myfib* $(SO)

主要:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>

int fib_seq(int x);

int main(int argc, char **argv) {
// Definitions
int x=0, fib=0;
int c, n, m, i;
int Fflag, Sflag;
pid_t pid;

// interprocess communication
const int size=4096;
char *shared_memory;

int segment_id=shmget(IPC_PRIVATE, size, S_IRUSR|S_IWUSR);
shared_memory= (char *)shmat(segment_id, NULL, 0);

// command line options using getopt for -F and -S
while ((c=getopt(argc, argv, "F:S:")) != -1)
switch(c)
{
case 'F':
Fflag = 1;
//printf("test\n");
n = atoi(optarg);
break;
case 'S':
Sflag = 1;
m= atoi(optarg);
printf("\nn = %d\nm = %d\n", n, m);
break;
default:
abort();
}

//begin fibonacci sequence
for(i=0; i<=n; i+=1) {

fib = fib_seq(x);
x+=1;

// fork child to compute next Fib numbers recursively
//if((((x-1)>m)&&((x-2)>m))) {
if((x-1)>m) {
pid=fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return 1;
}
if (pid == 0) {
printf("\nChild computing next Fib number...\n");
fib = fib_seq(x);
printf("Child process complete\n");
}
else {
printf("\nParent waiting for child to finish...\n");
wait(NULL);
}
return 0;
}
// compute next fib numbers recursively
//else if((((x-1)<=m)&&((x-2)<=m))) {
else if((x-1)<=m) {
printf("\nComputing without child...");
fib = fib_seq(x-1);
}
printf("\nFibonacci sequence of %d is %d\n", x-1, fib);
}
return 0;
}

fib_seq:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int extern x;

int fib_seq(int x) {

int i, rint = (rand() % 30);
double dummy;

for (i = 0; i<rint*100; i++) {
dummy = 2.3458*i*8.7651/1.234;
}

if (x == 0)
return(0);
else if (x == 1)
return(1);
else
return fib_seq(x-1)+fib_seq(x-2);
}

输出1:

n = 6
m = 6

Computing without child...
Fibonacci sequence of 0 is 0

Computing without child...
Fibonacci sequence of 1 is 1

Computing without child...
Fibonacci sequence of 2 is 1

Computing without child...
Fibonacci sequence of 3 is 2

Computing without child...
Fibonacci sequence of 4 is 3

Computing without child...
Fibonacci sequence of 5 is 5

Computing without child...
Fibonacci sequence of 6 is 8

输出2:

n = 6
m = 3

Computing without child...
Fibonacci sequence of 0 is 0

Computing without child...
Fibonacci sequence of 1 is 1

Computing without child...
Fibonacci sequence of 2 is 1

Computing without child...
Fibonacci sequence of 3 is 2

Parent waiting for child to finish...

Child computing next Fib number...
Child process complete

最佳答案

fork() 通过复制调用进程来创建新进程。我建议您详细阅读 fork()。

回到您的程序,您只需对程序进行一些更正即可使其正常工作。

更正号1 -

在子进程中将 x-1 而不是 x 传递给 fib_seq()。

更正号2-

wait(NULL)(父进程等待子进程)之后,您已从 main() 返回父进程 -

return 0;

这将退出父进程,因此每当子进程生成时,父进程将在子进程完成后退出。

将上面的 return 语句移到 if (pid == 0) block 中,并添加一个 printf 来写入子进程中执行的 fib_seq() 函数返回的 fib 值。

    if (pid == 0) {
printf("\nChild computing next Fib number...\n");
fib = fib_seq(x-1);
printf("\nFibonacci sequence of %d is %d\n", x-1, fib);
printf("Child process complete\n");
return 0;
}

更正号3-

现在,如果子进程正在计算系列的下一个值,则父进程不需要计算系列的下一个值。因此,在 wait() 之后在父级中添加“继续”。你的程序的一部分看起来像 -

 if (pid == 0) {
printf("\nChild computing next Fib number...\n");
fib = fib_seq(x-1);
printf("\nCHILD Fibonacci sequence of %d is %d\n", x-1, fib);
printf("Child process complete\n");
return 0;
}
else {
printf("\nParent waiting for child to finish...\n");
wait(NULL);
continue;
}

关于c - C 中的递归斐波那契 : Fork(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46481233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com