小编Sem*_*Han的帖子

shmget不起作用

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/shm.h>
int main() {
        int i=0;
        int shmid;
        int *mem=(int*)malloc(10*sizeof(int));
        key_t key;
        key=1234;
        pid_t pid;

        shmid=shmget(1234,sizeof(*mem), IPC_CREAT|0666);
        if(shmid==-1) {
             printf("shmget error\n");
             return -1;
        }
        mem=shmat(shmid, NULL, 0);

        if(mem==(int*)-1) {
              printf("shmat error\n");
              return -1;
        }

        for(;i<10;i++) {
               *(mem+i)=0;
        }

        pid=fork();

        if(pid<0) {
               fprintf(stderr,"Fork Failed");
               printf("array : ");
        }
        else if (pid==0) {
               printf("producer is created.\n");
               printf("array : ");

               for(i=0;i<10;i++) {
                         printf("%d ", *(mem+i));
               }
               printf("\n");
               for(i=0;i<10;i++) {
                      *(mem+i)=i+1; …
Run Code Online (Sandbox Code Playgroud)

c cygwin ipc process shared-memory

5
推荐指数
1
解决办法
2463
查看次数

标签 统计

c ×1

cygwin ×1

ipc ×1

process ×1

shared-memory ×1