VMWARE 上的 UBUNTU 分段错误(核心转储)

发布于 2024-11-10 06:27:17 字数 1396 浏览 1 评论 0原文

抱歉,如果我的英语很糟糕:我真的很紧张:( 我正在使用 Ubuntu 11.04 作为虚拟机。使用VMWARE作为虚拟机操作系统是windows 7。 我正在尝试用C语言在linux上编写一个程序:使用共享内存。当我尝试编译时没有错误,但当我执行它时,我看到如下错误: 分段错误(核心转储) 我不确定,但据我所知,VMWARE 是导致此问题的原因:S 这是代码:

#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;
}

我使用它在终端上编译:gcc -o ./RUN ./EK.c -shared 运行:./RUN 结果:分段错误(核心已转储)

感谢您的时间并回复我真的需要......

sorry if my english is horrible: I'm realy stresed :(
I'm using Ubuntu 11.04 that work as virtual machine. Using VMWARE as 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-11-17 06:27:17

假设 getmem 将多个字节作为参数,则为数字数组分配 327 个字节:

Numbers=(int*)getmem(327);

如果您使用的是 8 字节 int 的 64 位系统,则此有足够的空间容纳 40 个整数。

然后,您继续将 50 个数字放入该数组中,这超出了您分配的空间。这很可能会导致分段错误。

通常,在调试器中启动程序以查看分段错误到底发生在哪里。这样您就可以更轻松地定位程序中的错误。

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.

逆流 2024-11-17 06:27:17

正如 Marc B 所说,问题出在 VMWare 上。我在真实的操作系统上尝试过它并且有效。 getmem() 函数不是我自己的函数。要使用它,您必须在编译行末尾添加“-shared”。谢谢各位的回复...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文