在C中使用共享内存的fibonacci序列

我有一个问题要解决,但它给了我错误:

2009-EE-182-Part2.c: In function 'main': 2009-EE-182-Part2.c:35:13: error: expected identifier or '(' before '->' token 2009-EE-182-Part2.c:40:22: error: expected identifier or '(' before '->' token 2009-EE-182-Part2.c:41:14: error: expected identifier or '(' before '->' token 2009-EE-182-Part2.c:42:22: error: expected expression before 'shared_data' 2009-EE-182-Part2.c:44:15: error: expected identifier or '(' before '->' token 2009-EE-182-Part2.c:54:15: error: expected expression before 'shared_data' 2009-EE-182-Part2.c:55:19: error: expected expression before 'shared_data' 

代码是:

 # include  # include  # include  # include  # include  # define MAX_SEQUENCE 10 typedef struct{ long fib_sequence[MAX_SEQUENCE]; int sequence_size; } shared_data; char* shared_memory; /* a pointer to the shared memory segment */ int main() { int a,b,m,n,i,j; a=0; b=1; printf("Enter the number of a Fibonacci Sequence:\n"); scanf("%d", &m); if (m  MAX_SEQUENCE) printf("Please enter an integer less than 10\n"); int segment_id; /* the identifier for the shared memory segment */ int segment_size = sizeof(shared_data); /* the size (in bytes) of the shared memory segment */ segment_id = shmget(IPC_PRIVATE, segment_size, S_IRUSR | S_IWUSR); /** allocate a shared memory segment */ shared_data *shared_memory = shmat(segment_id, NULL, 0); /** attach the shared memory segment */ printf("\nshared memory segment %d attached at address %p\n", segment_id, shared_memory); shared_data->sequence_size = m; pid_t pid; pid = fork(); if (pid == 0){ printf("Child is producing the Fibonacci Sequence...\n"); shared_data->fib_sequence[0] = a; shared_data->fib_sequence[1] = b; for (i=2;isequence_size;i++){ n=a+b; shared_data->fib_sequence[i] = n; a=b; b=n; } printf("\nChild ends\n"); } else{ printf("Parent is waiting for child to complete...\n"); wait(NULL); printf("Parent ends\n"); for(i=0;isequence_size;i++) printf("%ld ", shared_data->fib_sequence[i]); } /**printf("%s \n", shared_memory); now print out the string from shared memory */ /** now detach the shared memory segment */ if ( shmdt(shared_memory) == -1) { fprintf(stderr, "Unable to detach\n"); } /** now remove the shared memory segment */ shmctl(segment_id, IPC_RMID, NULL); return 0; } 

声明是“设计fibonacci程序的方法是在父进程和子进程之间建立共享内存段。这种技术允许子进程将Fibonacci序列的内容写入共享内存段并让父输出序列当孩子完成时。由于内存是共享的,孩子所做的任何更改也会反映在父进程中。这个程序将使用POSIX共享内存构建,如http://graphics.im.ntu.edu.tw所述。 /~robin/courses/os07/code/03proc/shm-posix.c这个程序首先要求为共享内存段创建数据结构。这最容易使用结构来完成。这个数据结构将包含两个项目:1一个大小为MAX_SEQUENCE的固定大小的数组,它将保存Fibonacci值和2.子进程要生成的序列的大小,即sequence_size,其中sequence_size≤MAX_SEQUENCE。

这些项可以在结构中表示如下:

 # define MAX_SEQUENCE 10 typedef struct{ long fib_sequence[MAX_SEQUENCE]; int sequence_size; } shared_data; 

父进程将通过以下步骤进行: 接受在命令行上传递的参数并执行错误检查以确保参数≤MAX_SEQUENCE。 湾 创建大小为shared_data的共享内存段。 C。 将共享内存段附加到其地址空间。 d。 将sequence_size的值设置为命令行上的参数。 即 fork子进程并调用wait()系统调用以等待子进程完成。 F。 输出共享内存段中Fibonacci序列的值。 G。 分离并删除共享内存段。

共享内存段将附加到子地址空间以及父地址空间。 然后子进程将Fibonacci序列写入

共享内存段。 必须同步父进程和子进程,以便父进程不会输出Fibonacci序列,直到子进程生成序列。 注意:在控制台上显示足够的消息,以便用户知道何时执行某个操作,例如创建和终止子进程等。“

专家介意请帮忙。

第一个问题:

 int a,b,m,n,i,j; sequence.fib_sequence[0] = a; sequence.fib_sequence[1] = b; 

你永远不会初始化ab ,因此你得到垃圾(和未定义的行为)。 初始化

 a = 0; b = 1; 

更深层次的问题:您设置了共享内存段,但从不使用它。 你使用全局

 shared_data sequence; 

写入孩子,并从父母那里读。 由于该全局与您设置的共享内存无关,因此子操作不会修改父级的sequence

您应该使用shared_memory ,指向共享内存的指针来写入和读取。 所以不应该使用char* ,而应该是

 shared_data *shared_memory = shmat(...); 

然后使用shared_memory->sequence[i]等。

修复shared_data/shared_memory mixup并添加一些错误检查后,程序可能看起来像

 # include  # include  # include  # include  # include  # include  # include  # include  // So we could use other sizes without editing the source. #ifndef MAX_SEQUENCE # define MAX_SEQUENCE 10 #endif // Check that MAX_SEQUENCE is large enough! #if MAX_SEQUENCE < 2 #error MAX_SEQUENCE must be at least 2 #endif typedef struct{ long fib_sequence[MAX_SEQUENCE]; int sequence_size; } shared_data; int main() { int a, b, m, n, i; a = 0; b = 1; printf("Enter the number of a Fibonacci Sequence:\n"); // Always check whether input conversion worked if (scanf("%d", &m) != 1) { printf("Invalid input, couldn't be converted.\n"); return EXIT_FAILURE; } if (m <= 0) { printf("Please enter a positive integer\n"); return EXIT_FAILURE; // exit if input is invalid } else if (m > MAX_SEQUENCE) { printf("Please enter an integer less than %d\n", MAX_SEQUENCE); return EXIT_FAILURE; // exit if input is invalid } /* the identifier for the shared memory segment */ int segment_id; /* the size (in bytes) of the shared memory segment */ size_t segment_size = sizeof(shared_data); /* allocate a shared memory segment */ segment_id = shmget(IPC_PRIVATE, segment_size, S_IRUSR | S_IWUSR); // Check result of shmget if (segment_id == -1) { perror("shmget failed"); return EXIT_FAILURE; } /* attach the shared memory segment */ shared_data *shared_memory = shmat(segment_id, NULL, 0); // Check whether attaching succeeded if ((void*)shared_memory == (void*)-1) { perror("shmat failed"); goto destroy; // clean up } printf("\nshared memory segment %d attached at address %p\n", segment_id, (void*)shared_memory); shared_memory->sequence_size = m; pid_t pid; pid = fork(); if (pid == 0){ printf("Child is producing the Fibonacci Sequence...\n"); shared_memory->fib_sequence[0] = a; shared_memory->fib_sequence[1] = b; for (i = 2; i < shared_memory->sequence_size; i++){ n = a+b; shared_memory->fib_sequence[i] = n; a = b; b = n; } printf("\nChild ends\n"); } else{ printf("Parent is waiting for child to complete...\n"); wait(NULL); printf("Parent ends\n"); for(i = 0; i < shared_memory->sequence_size; i++) { printf("%ld ", shared_memory->fib_sequence[i]); } printf("\n"); } /* now detach the shared memory segment */ if (shmdt(shared_memory) == -1) { fprintf(stderr, "Unable to detach\n"); } destroy: /* now remove the shared memory segment */ shmctl(segment_id, IPC_RMID, NULL); return 0; }