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

C语言中使用共享内存的fibonacci序列,c,posix,fork,shared-memory,fibonacci,C,Posix,Fork,Shared Memory,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:

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

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 <stdio.h>
# include <sys/shm.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
# 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 < 0)
        printf("Please enter a non-negative integer\n");
else 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;i<shared_data->sequence_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;i<= shared_data->sequence_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;
}
父进程将通过以下步骤进行: A.接受在命令行上传递的参数并执行错误检查以确保 参数是≤ MAX_序列。 B创建大小为shared_数据的共享内存段。 C将共享内存段连接到其地址空间。 D将sequence_size的值设置为命令行上的参数。 E分叉子进程并调用wait()系统调用以等待子进程启动 结束。 F输出共享内存段中斐波那契序列的值。 G分离并卸下共享内存段

共享内存段将连接到孩子的地址空间以及 父级的地址空间。子进程然后将斐波那契序列写入

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

专家们请帮忙。

第一个问题:

int a,b,m,n,i,j;

sequence.fib_sequence[0] = a;
sequence.fib_sequence[1] = b;
您从不初始化
a
b
,因此您会得到垃圾(和未定义的行为)。初始化

a = 0;
b = 1;
更深层次的问题:您设置了一个共享内存段,但从未使用它。您使用全局

shared_data sequence;
在子级中写入,在父级中读取。由于该全局与您设置的共享内存无关,因此子级的操作不会修改父级的
序列

您应该使用
shared\u memory
,即指向要写入和读取的共享内存的指针。因此,与其使用
char*
,不如使用它

shared_data *shared_memory = shmat(...);
然后使用
共享内存->序列[i]

在修复了
共享数据/共享内存
的混淆并添加了一点错误检查之后,程序可能看起来像

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

// 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//因此,我们可以在不编辑源代码的情况下使用其他尺寸。
#ifndef MAX_序列
#定义最大值序列10
#恩迪夫
//检查MAX_序列是否足够大!
#如果最大_序列<2
#错误最大\u序列必须至少为2
#恩迪夫
类型定义结构{
长fib_序列[MAX_序列];
int序列的大小;
}共享数据;
int main()
{
int a,b,m,n,i;
a=0;b=1;
printf(“输入斐波那契序列的编号:\n”);
//始终检查输入转换是否工作
如果(scanf(“%d”,&m)!=1){
printf(“无效输入,无法转换。\n”);
返回退出失败;
}
if(m MAX_序列){
printf(“请输入一个小于%d\n”的整数,最大顺序);
返回EXIT_FAILURE;//如果输入无效,则退出
}
/*共享内存段的标识符*/
int段_id;
/*共享内存段的大小(字节)*/
大小\u t段\u大小=大小of(共享\u数据);
/*分配共享内存段*/
段id=shmget(IPC_专用,段大小,S_IRUSR | S_IWUSR);
//shmget的检查结果
如果(段id==-1){
perror(“shmget失败”);
返回退出失败;
}
/*连接共享内存段*/
共享数据*共享内存=shmat(段id,NULL,0);
//检查连接是否成功
如果((void*)共享内存==(void*)-1){
perror(“shmat失败”);
转到销毁;//清理
}
printf(“\n附加在地址%p\n的共享内存段%d”,段id,(void*)共享内存);
共享内存->序列大小=m;
pid_t pid;
pid=fork();
如果(pid==0){
printf(“Child正在生成斐波那契序列…\n”);
共享内存->fib\U序列[0]=a;
共享内存->fib\U序列[1]=b;
对于(i=2;isequence\u size;i++){
n=a+b;
共享内存->fib\u序列[i]=n;
a=b;
b=n;
}
printf(“\n文档结束\n”);
}
否则{
printf(“父项正在等待子项完成…\n”);
等待(空);
printf(“父端\n”);
对于(i=0;isequence\u size;i++){
printf(“%ld”,共享内存->fib\u序列[i]);
}
printf(“\n”);
}
/*现在分离共享内存段*/
if(shmdt(共享内存)=-1){
fprintf(stderr,“无法分离\n”);
}
销毁:
/*现在移除共享内存段*/
shmctl(段id,IPC\U RMID,空);
返回0;
}
第一个问题:

int a,b,m,n,i,j;

sequence.fib_sequence[0] = a;
sequence.fib_sequence[1] = b;
您从不初始化
a
b
,因此您会得到垃圾(和未定义的行为)。初始化

a = 0;
b = 1;
更深层次的问题:您设置了一个共享内存段,但从未使用它。您使用全局

shared_data sequence;
在子级中写入,在父级中读取。由于该全局与您设置的共享内存无关,因此子级的操作不会修改父级的
序列

您应该使用
shared\u memory
,即指向要写入和读取的共享内存的指针。因此,与其使用
char*
,不如使用它

shared_data *shared_memory = shmat(...);
然后使用
共享内存->序列[i]

在修复了
共享数据/共享内存
的混淆并添加了一点错误检查之后,程序可能看起来像

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

// 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//因此,我们可以在不编辑源代码的情况下使用其他尺寸。
#ifndef MAX_序列
#定义最大值序列10
#恩迪夫
//检查MAX_序列是否足够大!
#如果最大_序列<2
#错误最大\u序列必须至少为2
#恩迪夫
类型定义结构{
长fib_序列[MAX_序列];
int序列的大小;
}共享数据;
int main()
{
int a,b,m