#Disableing CPU cores, fopen() resolves to a Segmentation fault

33 messages ยท Page 1 of 1 (latest)

uneven hornet
#

Hello everyone,

so i got the Idea of disabling some cores on my laptop for increasing the battery lifetime. Which it does, when disabling the cores manually.
So i tried to write a code in C for disabling the cores automatically, but the code fails at the point where i try to open the "online" file with fopen(path,"w") under the "/sys/devices/system/cpu/cpuN/" path with a segmentation fault. Where N is a digit from 1-7 (Since the 0th core does not contain the online file).

Is this behaviour normal? Because i can open it with the "r" parameter..
Or have i did something wrong with opening and closing the file (Which I think I have).
I'll post in following my code.

solemn kelpBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

uneven hornet
#

So in the first for loop im just checking how many cores are available for the system.

#

In the second I'm checking which cores are offline or online (just with print outs).
Then the next section I'm just opening the file again, but instead "r" I'm using the "w" parameter.
But this gives me a segmentation fault.. (and yes i have closed the last pointer before i have posted here, i just left the closing out for debugging purposes this time).

#

If there is someone who could give me a hint on that problem, i'd appreciate it a lot.

#

Okay, update. I only get the segmentation fault when I'm only trying to close the opened file.

mortal tulip
#

Check return values and errno

severe needle
#

So many possible reasons why something could fail

  • opening with w truncates the file, which might lead to unexpected data loss.
  • You have more than 10 cores => i + '0' = ':' for i = 10.
  • You don't have permissions to read and/or write to the files you're trying to access.
#

Also this snippet doesn't look quiet right:

#

!f

solemn kelpBOT
#

for (unsigned char i = 0; i < core_count; i++) {
  mod_path[27] = i + '0';
  mod_path[28] = '/';
  mod_path[29] = '\0';
  printf("%s\n", mod_path);
  if ((fp = fopen(mod_path, "r")) == NULL) {
    fclose(fp);
    i = i + 1;
    core_count = i;
    fprintf(stdout, "Your system has %d cores.", i);
    break;
  } else {
    fclose(fp);
  }
}

Monke
severe needle
#

So I guess you want to count the cores here, which is why it doesn't make sense to hardcode the value 8 for core_count immediately before but whatever.

#

However this only counts the cores and sets core_count accordingly if it can't find the file for the current core, which doesn't really make that much sense, does it?

#

Also why the break;?

#

I'm not familiar at all with the files that are there, so I might be utterly mistaken here, but from just looking at this it looks kinda wrong

uneven hornet
#

Thank you for the fast replys! I've checked the return values, for some reason it shows me that fp is NULL. I'll write you in a bit when that is fixed. Of course closing a pointer which does point to NULL does not make any sense..

solemn kelpBOT
#

@uneven hornet Has your question been resolved? If so, run !solved :)

uneven hornet
# severe needle So many possible reasons why something could fail - opening with `w` truncates ...
  1. Point, the file gets truncated I know, the idea is just to overwrite the only character in the file to 1 or to 0, depending on if it contains a 0 or a 1.
    Unless no process is using this file during the overwrite process it should be fine it guess.

  2. I only have 8 cores. The idea is, if this code runs on a system with less cores, it will detect that. This is just the first stage, i wanted to to that also for more cores but then i run into the problem of that i cannot iterate over my path properly. Since 8+'0' is a character which is not a number anymore, but another character represented in the ASCII table.

  3. I know i don't have full permissions to the file, i need sudo rights for it. But i just want to have the code working to the point where i just need to alter its permissions ๐Ÿ˜„

uneven hornet
#

Currently running on one core lol

severe needle
uneven hornet
#

aaah okay

severe needle
#

I'll do it later if the error persists, but rn I'm kinda playing League of Legends

uneven hornet
#

Have some fun ๐Ÿ˜‰ I'll give some updates

uneven hornet
# severe needle Also why the `break;`?

If the break is not there, the iteration will just go on without stopping. But i want it to stop it there are less cores in the system. I mean, i cold also write i=8 or something, but yea. It just works :DD

uneven hornet
#

So i have rewritten the structure of the code, it looks like this now:

#

Now im on changing the value in the "online" file

uneven hornet
#

Holy moly it works!! So the problem was that i accidentely typed in the wrong path, the cpu_online_path instead of the mod_path into the fopen() function. That caused the program to crash all the time

#

now i can finally manage the online and offline state of my CPUs ๐Ÿ˜„

#

!solved