斐波那契数列
#define MAX_SEQUENCE 10 // Max values to store in shared memory
#define MIN_SEQUENCE 2 // Min value the user can enter
//shared memory:
// 1) holds an array of numbers
// 2) holds how many numbers are in the array
typedef struct {
int fib_seq[MAX_SEQUENCE];
int sequence_size;
} shared_data;
//MAIN function
int main(int argc, char *argv[]) {
pid_t pid; //process ID
int segment_id; //Shared Memory ID
shared_data *mem; //Shared Memory Pointer
//check to validate atleast two arguments
if(argc != 2) {
printf("USAGE ERROR: [0-9]\n");
exit(0);
}
//validate the input is not larger then the MAX
if(atoi(argv[1]) > MAX_SEQUENCE) {
printf("Max Input Size: %d\n", MAX_SEQUENCE);
exit(0);
}
//validate the input is not smaller then the MIN
if(atoi(argv[1]) < MIN_SEQUENCE) {
printf("Min Input Size: %d\n", MIN_SEQUENCE);
exit(0);
}
// 1) create a new shared memory location 'IPC_PRIVATE'
// 2) the size of our shared memory structure 'sizeof(shared_data)'
// 3) Set Modes S_IRUSR and S_IWUSR so the owner can read and write to the shared memory 'S_IRUSR|S_IWUSR'
segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR|S_IWUSR);
//attach the shared memory and get the pointer to the beginning location in memory
mem = (shared_data *) shmat(segment_id,NULL,0);
//set the size of the sequence to the argument that was passed in via command line
mem->sequence_size = atoi(argv[1]);
// fork a child process
pid = fork();
if(pid < 0) { /* error occured */
fprintf(stderr, "Fork Failed\n");
return 1;
}
else if(pid == 0) { /* child process */
int counter = 0;
printf("Child Fibonacci Sequence: ");
while(counter < mem->sequence_size) {
if(counter == 0){
//FIB of zero is always zero
mem->fib_seq[counter] = 0;
}
else if(counter == 1){
//FIB of one is always one
mem->fib_seq[counter] = 1;
}
else {
//The Fibonacci Sequence formula 'R = fib(n-1) + fib(n-2)'
//The first two numbers in the sequence are always 0 and 1.
//To get a value in the sequence you will want to take the previous
//two numbers and add them together. For example:
// b + a = c
// [fib(d-1) = c] + [fib(d-2) = b] = R
// fib(0) = 0
// fib(1) = 1
// fib(2): 1 + 0 = 1
// fib(3): 1 + 1 = 2
// fib(4): 2 + 1 = 3
// fib(5): 3 + 2 = 5
// The next Fibonacci number in the sequence will be '8'
mem->fib_seq[counter] = mem->fib_seq[counter - 1] + mem->fib_seq[counter - 2];
}
printf("%d ", mem->fib_seq[(counter)]);
counter++;
}
}
else { /* parent process */
/* parent will wait for the child process to complete */
wait(NULL);
//Print out shared memory
int count = 0;
printf("\nParent Fibonacci Sequence: ");
while(count < mem->sequence_size){
printf("%d ", mem->fib_seq[count]);
count++;
}
//detach shared memory
shmdt(mem);
//remove shared memory segment
shmctl(segment_id,IPC_RMID,NULL);
printf("\nComplete\n");
}
return 0;
}
好吧,我有这个程序,我已经研究了一段时间了,问题是数字序列偏离了 1,我似乎找不到它在哪里。它不会为 fib(0) 打印 0。所以当我做 Fib(2) 时它给我 0 1 而不是 0 1 1,有人有什么建议吗?
#define MAX_SEQUENCE 10 // Max values to store in shared memory
#define MIN_SEQUENCE 2 // Min value the user can enter
//shared memory:
// 1) holds an array of numbers
// 2) holds how many numbers are in the array
typedef struct {
int fib_seq[MAX_SEQUENCE];
int sequence_size;
} shared_data;
//MAIN function
int main(int argc, char *argv[]) {
pid_t pid; //process ID
int segment_id; //Shared Memory ID
shared_data *mem; //Shared Memory Pointer
//check to validate atleast two arguments
if(argc != 2) {
printf("USAGE ERROR: [0-9]\n");
exit(0);
}
//validate the input is not larger then the MAX
if(atoi(argv[1]) > MAX_SEQUENCE) {
printf("Max Input Size: %d\n", MAX_SEQUENCE);
exit(0);
}
//validate the input is not smaller then the MIN
if(atoi(argv[1]) < MIN_SEQUENCE) {
printf("Min Input Size: %d\n", MIN_SEQUENCE);
exit(0);
}
// 1) create a new shared memory location 'IPC_PRIVATE'
// 2) the size of our shared memory structure 'sizeof(shared_data)'
// 3) Set Modes S_IRUSR and S_IWUSR so the owner can read and write to the shared memory 'S_IRUSR|S_IWUSR'
segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR|S_IWUSR);
//attach the shared memory and get the pointer to the beginning location in memory
mem = (shared_data *) shmat(segment_id,NULL,0);
//set the size of the sequence to the argument that was passed in via command line
mem->sequence_size = atoi(argv[1]);
// fork a child process
pid = fork();
if(pid < 0) { /* error occured */
fprintf(stderr, "Fork Failed\n");
return 1;
}
else if(pid == 0) { /* child process */
int counter = 0;
printf("Child Fibonacci Sequence: ");
while(counter < mem->sequence_size) {
if(counter == 0){
//FIB of zero is always zero
mem->fib_seq[counter] = 0;
}
else if(counter == 1){
//FIB of one is always one
mem->fib_seq[counter] = 1;
}
else {
//The Fibonacci Sequence formula 'R = fib(n-1) + fib(n-2)'
//The first two numbers in the sequence are always 0 and 1.
//To get a value in the sequence you will want to take the previous
//two numbers and add them together. For example:
// b + a = c
// [fib(d-1) = c] + [fib(d-2) = b] = R
// fib(0) = 0
// fib(1) = 1
// fib(2): 1 + 0 = 1
// fib(3): 1 + 1 = 2
// fib(4): 2 + 1 = 3
// fib(5): 3 + 2 = 5
// The next Fibonacci number in the sequence will be '8'
mem->fib_seq[counter] = mem->fib_seq[counter - 1] + mem->fib_seq[counter - 2];
}
printf("%d ", mem->fib_seq[(counter)]);
counter++;
}
}
else { /* parent process */
/* parent will wait for the child process to complete */
wait(NULL);
//Print out shared memory
int count = 0;
printf("\nParent Fibonacci Sequence: ");
while(count < mem->sequence_size){
printf("%d ", mem->fib_seq[count]);
count++;
}
//detach shared memory
shmdt(mem);
//remove shared memory segment
shmctl(segment_id,IPC_RMID,NULL);
printf("\nComplete\n");
}
return 0;
}
Ok, I have this program that I've been working on for a while now, the problem is that the sequence of numbers are off by 1 and I can not seem to find where it is. It's not printing the 0 for fib(0). So when I do Fib(2) it gives me 0 1 instead of 0 1 1, anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
典型的欧比万错误(差一)。您需要执行以下操作:
(已编辑,之前的发布导致数组访问越界)
A classical Obi Wan error (off-by-one). You need to do:
(Edited, the previous posting was causing out-of-bounds array access)
您的代码似乎可以工作...
如果您希望 Fib(2) 打印出三个数字,您可能需要查看这一行:
但是,如果您这样做,则需要注意您将需要 11 块内存计算 Fib(10)。你目前只会给自己10段记忆。
如果这没有意义,请将
MIN_SEQUENCE
设置为 0,并问问自己在计算Fib(0)
时代码会执行什么操作。Your code seems to work...
If you want Fib(2) to print out three numbers, you'll probably want to look at this line:
If you do that however, you need to note that you will need 11 pieces of memory to calculate Fib(10). You currently would only give yourself 10 pieces of memory.
If this doesn't make sense, then set
MIN_SEQUENCE
to 0, and ask yourself what your code does when you calculateFib(0)
.根据您的代码,序列大小设置为您传递的参数
mem->sequence_size = atoi(argv[1]);
并且在 while 循环中您正确检查为:
while(countersequence_size)
.因此,
Fib 2
应该只打印斐波那契序列中的 2 个元素0 1
。如果您需要0 1 1
的输出,那么您是否会将程序作为fib 3
运行?As per your code, the sequence size is set to the parameter you are passing
mem->sequence_size = atoi(argv[1]);
And in the while loop you are checking correctly as:
while(counter < mem->sequence_size)
.So
Fib 2
should be printing only 2 elements0 1
from the fibonacci sequence. If you need the output of0 1 1
then wouldn't you be running the program asfib 3
?您正在打印
counter
数量的项目。即,当 counter == 2 时,您将打印两个数字,而当 counter == 0 时,您将打印零个数字。You are printing
counter
number of items. ie, when counter == 2, you are printing two numbers, and when counter == 0 you are printing zero numbers.