Hello Friends...

I've written a code for Simulating Paging algorithm using LRU...
The code is in the attached file.

Now on windows... it is executing perfectly, giving the output i desire...
but on Linux REGAL gnu compiler, it is giving me the error...
Error file is attached... as error2.jpg

SEGMENTATION FAULT

Can someone help me with this?
How to solve this problem?

Attachments
// Name:         Joe M. Student
// Program:      CSC 377 Program 4
// Due date:     11/20/06
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int maxpg; 				// stores max size of physical pages
int i, j, l;                            // temporary variables
char *filenm, *num[100], *ea;		// stores file name, file data
int pages[20], pages2[20];		// replica of physical pages in mem
int diststr[100], ds, dsn;		// distance string variables
int cstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};		// stores c-values from distance string
int fstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};		// stores f-values from c-str giving probability of page faults
int cstr99 = 0, fstr99 = 0;		// c-var & f-var for infinity

void disppage();			// display the page information from memory
int inspage(int a);			// insert a page from virtual to physical memory

int main(int argc, char **argv)
{
	FILE *in;
	int n[100];

	maxpg  = atoi(argv[1]);
	filenm = argv[2];

	for(i=0;i<maxpg;i++)
	{
	 if(pages[i]==0)
	   pages[i]=99;
	}

	printf("PG.NO.\tPF-DS\t");
	for(i=0; i<maxpg;i++)
	{
	 printf("P-%d\t",i);
	}
	printf("\n");
	printf("--------------------------------------------------------\n");
	if ((in = fopen(filenm, "rt")) == NULL)
	{
	   fprintf(stderr, "Cannot open input file.\n");
	   return 1;
	}
	i  = 0;
	dsn = 0;
	while (!feof(in))
	{
	   fgets(ea, 2, in);
	   if(strcmp(ea,"\n")==0)
	   {
		continue;
	   }
	   num[i] = ea;
	   n[i] = atoi(num[i]);
	   diststr[dsn] = inspage(n[i]);
	   i++;
	   dsn++;
	}
	fclose(in);

	printf("\nDistance String :-(99 represents infinity)\n\t");
	for(i=0;i<dsn;i++)
	{
		printf("%d ",diststr[i]);
		switch(diststr[i])
		{
			case 0:
				cstr[0]++;
				break;
			case 1:
				cstr[1]++;
				break;
			case 2:
				cstr[2]++;
				break;
			case 3:
				cstr[3]++;
				break;
			case 4:
				cstr[4]++;
				break;
			case 5:
				cstr[5]++;
				break;
			case 6:
				cstr[6]++;
				break;
			case 7:
				cstr[7]++;
				break;
			case 8:
				cstr[8]++;
				break;
			case 9:
				cstr[9]++;
				break;
			case 10:
				cstr[10]++;
				break;
			case 11:
				cstr[11]++;
				break;
			case 12:
				cstr[12]++;
				break;
			case 13:
				cstr[13]++;
				break;
			case 14:
				cstr[14]++;
				break;
			case 15:
				cstr[15]++;
				break;
			case 16:
				cstr[16]++;
				break;
			case 17:
				cstr[17]++;
				break;
			case 18:
				cstr[18]++;
				break;
			case 19:
				cstr[19]++;
				break;
			case 99:
				cstr99++;
				break;
		}
	}
	for(i=0;i<20;i++)
	{
		 printf("\n\tC[%2d]-->%d",i,cstr[i]);
	}
	printf("\n\tC[99]-->%d",cstr99);

	for(i=1;i<20;i++)
	{
		for(j=i+1;j<20;j++)
		{
			fstr[i]=fstr[i] + cstr[j];
		}
		fstr[i] = fstr[i] + cstr99;
	}
	fstr99 = cstr99;
	printf("\n\n");
	for(i=0;i<maxpg;i++)
	{
		 printf("\n\tF[%2d]-->%d",i,fstr[i]);
	}

	return 0;
}

int inspage(int a)
{
  int mflag=0, az;
  printf("%d\t",a);
  if(pages[0]==99)
  {
	   pages[0]=a;
	   printf("PF - I");
	   disppage();
	   return 99;
  }
  else
  {
	  for(i=0;i<maxpg;i++)
	  {
		if(a!=pages[i])
		{
			mflag = 1;
		}
		else
		{
			mflag = 2;
			break;
		}
	  }
	  if(mflag==1)
	  {
		printf("PF - I");
		for(i=0;i<maxpg-1;i++)
		  {
		   pages2[i+1]=pages[i];
		  }
		for(i=0;i<maxpg;i++)
		  {
		   pages[i]=pages2[i];
		  }
		pages[0]=a;
		disppage();
		return 99;
	  }
	  else
	  {
		for(i=0;i<maxpg;i++)
		{
			if(a==pages[i])
			{
				j=i;
				break;
			}
		}
		ds = j+1;
		printf("   - %d", ds);
		az = ds;
		for(i=0;i<j;i++)
		  {
		   pages2[i+1]=pages[i];
		  }
		for(i=0;i<maxpg;i++)
		  {
		   pages[i]=pages2[i];
		  }
		pages[0]=a;
		disppage();
		return ds;
	  }
  }
}

void disppage()
{
 int i;
 for(i=0;i<maxpg;i++)
 {
   if(pages[i]==99)
   {
    printf("\t-");
   }
   else
   {
    printf("\t%d",pages[i]);
   }
 }
 printf("\n");
}

Comment out section by section and locate which part gives that error. After that post it here. Since we don't have the input files, we can't reproduce the error here. The file is also too long for us to wade through all the code.

compile your program for debug then when the core file is created use debugger on it to see where the problem is.

Suggestion: your program makes no input validations. How does it know that argv[1] contains an integer between 0 and 20? what happens if I enter 500 as argv[1] ?

int cstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};		// stores c-values from distance string
int fstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};		// stores f-values from c-str giving probability of page faults

you can simplify the above like below. It is not necessary to specify the initialization of each element of the array because the compiler will set all unspecified initializer to 0 for you.

int cstr[20]={0};		// stores c-values from distance string
int fstr[20]={0};		// stores f-values from c-str giving probability of page faults

>> while (!feof(in))
we have said many many times on these boards that you should not rely on feof() because it does not always catch eof-of-file at the right time. Instead you should code it like this:

while(   fgets(ea, 2, in) )
{
   // blabla
}

Where is pointer ea allocated? I see where it is declared and left unitialized, but I don't see where you allocated any memory for it. That may explain the seg fault you are getting.

for(i=0;i<dsn;i++)
	{
		printf("%d ",diststr[i]);
		switch(diststr[i])
		{

why did you make this do difficult? you don't need that switch statement

for(i=0;i<dsn;i++)
	{
		printf("%d ",diststr[i]);
                if( diststr[i] < 20)
                      cstr[diststr[i]]++;
                else
                       cstr99++;
        }
This article has been dead for over six months. Start a new discussion instead.