Program with while 1 hogs the processor [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently experimenting to find out why a process with while 1 hogs the processor. Here is the sample of my code.
int *a = NULL;
while(1)
{
a = (int*)malloc(10000);
std::cout << "Ptr to allocated memory: " << a << std::endl;
}
When I run the "top" command, it is at the top as long as it is executing. Can someone please help me understand the reason? If someone with assembly level knowledge could also provide an answer it would be really nice.

while(1) will run something forever (or until there is a break). The code inside the loop will be executed constantly, hogging much of the processor.
This is like a parent with a very needy child- your program will get all of the attention, without letting any of the other programs get what they need.

Related

C - make watchpoint with assembly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Like what gdb do with watchpoint, I want to put a watchpoint programmatically in C. I heard that is possible with assembly. In the current time I know only for breakpoints like this instruction :
int i=0;
while(1){
i++;
if(i >= 10)
asm("int");/*break*/
}
After searching, it exist a possibility to display assembly's instruction using TUI mode and reuse it in my program but it doesn't work.
gdb -q ./prog -tui
gdb: TUI mode is not supported
Thanks for help.
On x86, to make a hardware watchpoint, you'll need to program one of the debug registers. You can start here.

How could I access the memory of a running program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Presuming I have a program in C:
#include<stdio.h>
#include<time.h>
int main()
{
int a = rand() % 1000;
return 0;
}
How can I find the value of "a" without printing it on the console, but by accessing, with a linux terminal command, its address to read the value from it?
Or generally, how can I map all the written values of a binary without seeing its implementation (like a "blackbox")?
Memory is accessible via /proc/pid/mem, index is /proc/pid/maps, Python prototype is how-do-i-read-from-proc-pid-mem-under-linux.

Power efficiency in C while loops & polling [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've heard that lets say:
while(1){
i = !2;
wait(1);
}
is power efficient. Does this while loop stop at i != 2 and is therefore not polling? Let's say:
while(x == 3){
if(c == 3){
x = 4;
}
wait(1);
}
Does this follow a similar concept or is i = !2 a procedure that must be met in order to continue the while loop? Would you say that this is just as power efficient? Is the second example similar to the first in terms of power efficiency?
An example i've been shown using bad power efficient polling is:
while (x == 3) { }
The important thing from an efficiency standpoint is that the code doesn't just continually cycle. In your example, presumably the wait() function is returning control to your OS so that it can immediately dispatch another task.
In short, yes, your second example is power efficient as well, assuming wait() returns control to the the OS.

Interpreting a shellcode [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am calling a shellcode using buffer overflow to spawn a root shell. Can somebody explain what this shellcode exactly does? I have tried different shellcodes to spawn a root shell, but this was the only one which worked for me.
\x31\xdb\x89\xd8\xb0\x17\xcd\x80\x31\xdb
\x89\xd8\xb0\x2e\xcd\x80\x31\xc0\x50\x68
\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89
\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd
\x80
On first glance, the code appears to do setuid(0), then setgid(0), then call sys_execve() on some values (which include ASCII codes for "/bin//sh").
Looks like this is pure "payload" code, since I don't see anything to ensure the code is executed on the first place (buffer overflow, stack smashing, etc.).
(Thanks to #Hans Lub for the disassembler link)

Main function in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am not sure how will this program behave?
I ran this code but I am not able to figure out any reasoning behind the way it works
int main()
{
return main();
}
main() is a function by itself. The line return main() calls the function again. So in effect it should run an infinite loop. You wouldn't get any output (you said you ran it. didn't it crash?).
In reality it would be like staring into a mirror with another mirror placed behind you. You would only see endless reflections. . :)

Resources