开发者

error bad execution (operating system course) need explain

I wrote in my noteBook the code below and I write it now in the xcode and this is the output by the teacher:

child reads : 0,1
parent write: 1,2
child reads : 1,2
parent write: 3,5
child reads : 3,5
parent write: 8,13
child reads : 8,13

I can't get the output because of this 2 errors:

1-Program received signal: “EXC_BAD_ACCESS”. and mark the line a[0]

2-warning: passing argument 1 of 'wait' makes pointer 开发者_高级运维from integer without a cast

The code:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/wait.h> 
int main (int argc, const char * argv[]) {

    int shmid ,*a,*b,i;
    shmid = shmget(IPC_PRIVATE, 2*sizeof(int), 0777/IPC_CREAT);
    int pid = fork();
    if (pid == 0) {
        b=(int *)shmat(shmid, 0, 0);
        for(i = 0; i < 10;i++){

            sleep(1);
            printf("child reads: %d,%d\n",b[0],b[1]);
        }
    }

    else {
        a= (int *)shmat(shmid, 0, 0);
        a[0] = 0; //after compile this line marked in red
        a[1] = 1;
        for(i = 0; i <10;i++){

            sleep(1);
            a[0] = a[0] + a[1];
            a[1] = a[0] + a[1];
            printf("Parent write:%d,%d\n",a[0],a[1]);
        }
        wait(pid);//warning message above
    }

    return 0;
}

Can anyone explain why this is happening?


0777/IPC_CREAT should be 0777|IPC_CREAT (a logical OR, not a divide).


For the issue with wait, see man waitpid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜