1

I don't want to limit my process's cpu usage. Rather I want to write it in a way that it consumes less cpu. The system I'm dealing with doesn't allow making any system call.

So I can't put it to sleep, I can't do any I/O. The only thing I'm left with is to make it memory intensive, which I thought would be easy.

I made a large global array and wrote a for loop to write to it. This process ended up being cpu-intensive(showing 98-100% on top/htop).

So I thought maybe for loop calculations are using cpu, I removed the for loop. But it was still using 98-100% cpu, which doesn't make sense to me. Can anyone help me make it memory intensive? I'm attaching the code below if it helps.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ARRAY_SIZE 1024*1024*1024 // 4 GB large_array
char large_array[ARRAY_SIZE];


int main(){
   while(1){
      strncpy(large_array, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);  
      strncpy(large_array+256, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+512, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+768, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+1024, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+1280, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+1536, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+1792, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+2048, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
      strncpy(large_array+2304, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
    }
}
user3834119
  • 411
  • 9
  • 21
  • I think the time used to copy data in memory counts as CPU time. – Barmar Nov 02 '17 at 02:26
  • What's the point of this? – Barmar Nov 02 '17 at 02:26
  • I don't think so, what would be a memory-intensive process then? – user3834119 Nov 02 '17 at 02:28
  • Just to make a memory-intensive low cpu-usage process, without blocking for I/O or going to sleep. – user3834119 Nov 02 '17 at 02:29
  • A memory-intensive process uses lots of virtual memory, so it frequently has to wait for the memory to be paged in from swap space. But you're using the same memory over and over, so it doesn't get swapped out. – Barmar Nov 02 '17 at 02:29
  • Sounds like an XY problem. Why do you need a memory-intensize low cpu-usage process? – Barmar Nov 02 '17 at 02:30
  • That's a good point. Maybe I should iterate over the whole array to see if cpu usage decreases. – user3834119 Nov 02 '17 at 02:32
  • I just need a low cpu usage process. Memory intensive is just a way to make it use less cpu, which I'm not able to. – user3834119 Nov 02 '17 at 02:36
  • Well memory operations also cost CPU time.. so I doubt you will achieve anything with this. If you really have no Sleep() call available, I think the only way is to build it yourself in assembler. Send the CPU to sleep with the HALT instruction with your CPU, and wake it up in an interrupt. I doubt there's another way. Just out of curiosity: Which system does not allow system calls and IO? – user2328447 Nov 02 '17 at 02:51
  • Why do you need a process that doesn't do anything in the first place? How about just giving it really low priority, so if any other process needs to run it will be descheduled. – Barmar Nov 02 '17 at 02:59
  • @user2328447 it's a secure system which doesn't allow these for security. But I'm trying to communicate with os using cpu patterns. HALT instruction will be catched as code goes through their disassembler. – user3834119 Nov 02 '17 at 03:12
  • @Barmar For changing priority, you need a system call. If you keep it low priority in the start, there won't be any patterns. Apologies for not asking question in the correct way, I skipped this part to abstract out the problem. – user3834119 Nov 02 '17 at 03:14
  • "I'm trying to communicate with os using cpu patterns". What does that mean? Are you trying to simulate timing-based covert channels? – Barmar Nov 02 '17 at 03:19
  • Not sure if this is a timing-based covert channel, but it's a covert channel, yes. So os or another process observing cpu pattern would interpret a rise in usage as 1 and fall as 0 or otherway around. – user3834119 Nov 02 '17 at 03:23
  • To reduce cpu usage you have to give-up cpu to other processes or to the OS in the way you mentioned: wait for I/O or sleep or other syscall. Can you give that process a realtine/soft-realtime schedule? – ulix Nov 02 '17 at 05:18
  • More: in today's hosted environment after a little timeslice the OS preempt your process. If you are on a freestanding environment who cares about cpu consumption ? You are alone. – ulix Nov 02 '17 at 06:55

0 Answers0