arjunhm / cache-lab

0 stars 0 forks source link

Segmentation fault avoided if using sleep #1

Open arjunhm opened 1 month ago

arjunhm commented 1 month ago

Getting segmentation fault, but works fine if sleep(1) is used.
7295cef

arjunhm commented 1 month ago

Issue is in evict(). Returns -1 if not using sleep()

hindsight

should have associated sleep() with the fact that I am using timestamps for LRU

arjunhm commented 1 month ago

if (c->sets[set_index].lines[i].timestamp <= min_timestamp). Changing < to <= fixes the issue. Because the time was the same?

Turns out time(NULL) returns the seconds. naturally would run into conflicts here

arjunhm commented 1 month ago

switched to counter.

but it would fail if I used it like this

for (int i = 0; i < c->line_count; i++) {
    Line l = c->sets[set_index].lines[i]; // storing in variable was the issue
    if (l.valid == 1 && l.tag == tag) {
      l.lru = lru_counter++;
      c->stats->hits++;
      return;
    }
  }

but works if I use this

for (int i = 0; i < c->line_count; i++) {
    if (c->sets[set_index].lines[i].valid == 1 &&
        c->sets[set_index].lines[i].tag == tag) {
      c->sets[set_index].lines[i].lru = lru_counter++;
      c->stats->hits++;
      return;
    }
  }
arjunhm commented 1 month ago

Line l = c->sets[set_index].lines[i] creates local copy. instead use Line *l = &(...)