LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: zdh

一个关于随机数的程序问题,问问大家啊

[复制链接]
发表于 2004-5-25 20:53:12 | 显示全部楼层
你修改之后的代码呢?
 楼主| 发表于 2004-5-25 21:12:55 | 显示全部楼层
我改完的我原来程序的一个子程序,现在随机数产生没有问题,但在调用我原来需要的函数时,还是会出现段错误.
程序如下:
[php]
#include<stdlib.h>
#define TRUE 1

#define FALSE 0

#define INVALID -1

#define null 0



#define total_instruction 320

#define total_vp 32

#define clear_period 50





typedef struct{

    int pn,pfn,counter,time;

    }pl_type;

pl_type pl[total_vp];

struct pfc_struct{

  int pn,pfn;

  struct pfc_struct *next;

};

typedef struct pfc_struct pfc_type;

pfc_type pfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;



int diseffect,a[total_instruction];

int page[total_instruction],offset[total_instruction];

void initialize();

void FIFO();


main()
{
int S,i,j;

srand(10*getpid());


S=(int) (319.0*rand()/(RAND_MAX+1.0));
printf("%d\n",S);
for(i=0;i<total_instruction;i+=4)

  {

  a=S;

  a[i+1]=S+1;

  a[i+2]=(int)(a*rand()/RAND_MAX);

  a[i+3]=a[i+2]+1;

  S=(int) ((318.0-a[i+2])*rand()/RAND_MAX)+a[i+2]+2;

  }
for(i=0;i<32;i++)

  {printf("a[%d]=%d\n",i,a);}

for(i=0;i<total_instruction;i++)

{

page=a/10;

offset=a%10;

}
void initialize();
for(i=0;i<32;i++){printf("page[%d]=%d\n",i,page);}
for(i=0;i<32;i++){printf("offest[%d]=%d\n",i,offset);}
for(i=4;i<=32;i++)

{

printf("%2d page frames",i);

FIFO(i);

printf("\n");



}


}

void FIFO(total_pf)

int total_pf;

{ int i,j;

  pfc_type *p,*t;

  initialize(total_pf);

  busypf_head=busypf_tail=null;

  for(i=0;i<total_instruction;i++)

  {

  if(pl[page].pfn==INVALID)

  {
  diseffect+=1;

  if(freepf_head==null)

  {
   p=busypf_head->next;

   freepf_head=busypf_head;

   freepf_head->next=null;

   busypf_head=p;

  }

p=freepf_head->next;

freepf_head->next=null;

freepf_head->pn=page;

pl[page].pfn=freepf_head->pfn;

if(busypf_tail->next==null)

      busypf_head=busypf_tail=freepf_head;

else

{
  busypf_tail->next=freepf_head;

  busypf_tail=freepf_head;

}

freepf_head=p;



  }

  }

printf("FIFO:%6.4f",1-(float)diseffect/320);

}



void initialize(total_pf)

   int total_pf;

  {

  int i;

  diseffect=0;

  for(i=0;i<total_vp;i++)

  {

  pl.pn=i;
  pl.pfn=INVALID;

  pl.counter=0;
  pl.time=-1;

  }

  for(i=1;i<total_pf;i++)

  {pfc[i-1].next=&pfc;pfc[i-1].pfn=i-1;}

  pfc[total_pf-1].next=null;pfc[total_pf-1].pfn=total_pf-1;

  freepf_head=&pfc[0];

  }
[/php]
 楼主| 发表于 2004-5-25 21:13:54 | 显示全部楼层
运行的结果如下:
[zdh@localhost zdh]$ gcc -o 07 07.c
[zdh@localhost zdh]$ ./07
265
a[0]=265
a[1]=266
a[2]=0
a[3]=1
a[4]=69
a[5]=70
a[6]=0
a[7]=1
a[8]=298
a[9]=299
a[10]=0
a[11]=1
a[12]=124
a[13]=125
a[14]=0
a[15]=1
a[16]=22
a[17]=23
a[18]=0
a[19]=1
a[20]=64
a[21]=65
a[22]=0
a[23]=1
a[24]=208
a[25]=209
a[26]=0
a[27]=1
a[28]=57
a[29]=58
a[30]=0
a[31]=1
page[0]=26
page[1]=26
page[2]=0
page[3]=0
page[4]=6
page[5]=7
page[6]=0
page[7]=0
page[8]=29
page[9]=29
page[10]=0
page[11]=0
page[12]=12
page[13]=12
page[14]=0
page[15]=0
page[16]=2
page[17]=2
page[18]=0
page[19]=0
page[20]=6
page[21]=6
page[22]=0
page[23]=0
page[24]=20
page[25]=20
page[26]=0
page[27]=0
page[28]=5
page[29]=5
page[30]=0
page[31]=0
offest[0]=5
offest[1]=6
offest[2]=0
offest[3]=1
offest[4]=9
offest[5]=0
offest[6]=0
offest[7]=1
offest[8]=8
offest[9]=9
offest[10]=0
offest[11]=1
offest[12]=4
offest[13]=5
offest[14]=0
offest[15]=1
offest[16]=2
offest[17]=3
offest[18]=0
offest[19]=1
offest[20]=4
offest[21]=5
offest[22]=0
offest[23]=1
offest[24]=8
offest[25]=9
offest[26]=0
offest[27]=1
offest[28]=7
offest[29]=8
offest[30]=0
offest[31]=1
段错误
[zdh@localhost zdh]$


最后还是段错误,不明白
 楼主| 发表于 2004-5-27 17:08:44 | 显示全部楼层
不想让我帖子沉下去:)
发表于 2004-5-27 19:24:23 | 显示全部楼层
自己先想点办法调试呀,不学会调试程序,将来怎么当程序员?
以后请用[code]整理一下你的代码,不然看起来太乱。
 楼主| 发表于 2004-6-2 00:00:18 | 显示全部楼层
最上面我发的哪个程序,我在linux底下运行总有"段错误",后来我换到window底下运行就没有错误,结果也显示出来了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表