开发者

UBUNTU on VMWARE Segmentation fault (core dumped)

sorry if my english is horrible: I'm realy stresed :( I'm using Ubuntu 11.04 that work as virtual machine. Using VMWARE 开发者_StackOverflowas virtual machine operating system is windows 7. I'm trying to write a program on linux with C language: that using shared memory. when I try to compile there are no errors but when I execute it I see an error like this: Segmentation fault (core dumped) I'm not sure but as far as I learned VMWARE is causing this :S here are the codes:

#include<stdio.h>
#include<sys/wait.h>    //Process wait
#include <fcntl.h>  //File
//#include <cstdlib>
//#include<fstream.h>

int main(){

  printf("\n Here we go...!");
  int *Numbers;
  Numbers=(int*)getmem(327); // shared memory

  int i,ProcID;
  ProcID=fork(); //depart processor

  if(ProcID==0){          // child processor
    for(i=0;i<50;i++){
      Numbers[i]=random()%50;   
    }
  }else if(ProcID<0){
    printf("\n Hmm... There is an error!");
  }

  int Waiting;
  wait(&Waiting);   

  if(ProcID>0){          // parent processor

    int fileeven,fileodd;
    fileeven=open("EK_even.txt",O_RDWR|O_CREAT,0600);
    fileodd=open("EK_odd.txt",O_RDWR|O_CREAT,0600);

    for(i=0;i<50;i++){
      if(Numbers[i]%2==0){
    write(fileeven,&Numbers[i],sizeof(Numbers[i]));
      }else{
    write(fileodd,&Numbers[i],sizeof(Numbers[i]));
      }
    }

    close(fileeven);
    close(fileodd);

  }else if(ProcID<0){
    printf("\n Hmm... There is an error!");
  }
  return 1;
}

I'm using this to compile on terminal:gcc -o ./RUN ./EK.c -shared to Run :./RUN as result :Segmentation fault (core dumped)

Thanks for your time and reponds I'm realy in need...


Assuming getmem takes a number of bytes as a parameter, you allocate 327 bytes for your array of numbers:

Numbers=(int*)getmem(327);

If you are on a 64 bit system with 8-byte int, this is enough space for 40 integers.

You then proceed to put 50 numbers into that array, more than you allocated space for. This might very well cause a segmentation fault.

Generally, start your program in a debugger to see where exactly the segmentation fault occurs. This way you can more easily locate the error in your program.


The problem is VMWare as Marc B said. I tryed it on a real operating system and it worked. getmem() function is not my own function. To use it you have to add "-shared" at the end of compile line. Thanks for replies...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜