#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid == 0)
{
printf("I am Child\n");
exit(0);
}
printf("I am Parent\n");
while(1);
}
Here is what happens on my Linux when i run this code: zombie process shows up in htop for a second and then just disappears.
I tried setting signal handler in parent:
void callback(int signum)
{
return;
}
int main()
{
int pid = fork();
if (pid == 0)
{
printf("I am Child\n");
exit(0);
}
printf("I am Parent\n");
signal(SIGCHLD, callback);
while(1);
}
But nothing changed. Why my zombie process disappears from htop?
P.S.: compiled with no optimization
Update: it doesn't show up in htop but does show up in top. Seems like a bug to me. I vote for deletion of this question.
The process disappears from htop because by default it sorts by CPU usage, so a zombie process will be so far down the list that it's off the screen.
It happens after one second because that's the default htop refresh interval.
Related
I am blinking LED using TIM2 General timer.
Code in main.c
HAL_TIM_Base_Start_IT(&htim2);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
if(htim->Instance==TIM2){
HAL_GPIO_TogglePin(GPIOB, LED_Pin);
}
}
When the button is clicked EXTI interrupt should be called to blink the led faster 20 times. However, LED stays on and stops blinking at all.
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
if(GPIO_Pin == BT_Pin){
for(volatile int i=20; i>0; i--){
HAL_GPIO_TogglePin(GPIOB, LED_Pin);
HAL_Delay(100);
}
}
}
Could you please advice how can i adjust interrupt ISR for EXTI so it will use TIM2 as well with faster blinking.
HAL_GPIO_EXTI_Callback() runs in the interrupt context - it is not appropriate to flash the indicator there much less include a delay. No other code while run while in the interrupt, and HAL_Delay() is probably not interrupt safe in any case.
Instead, in the button handler, set a down-counter to be decremented in the timer handler, and in the timer handler set the reload depending on the down-counter being zero or not. Something like this:
[I have not included the HAL calls to do the TODO's above, because I'd have to look them up, but that is the outline.]
volatile unsigned fast_flash_count = 0 ;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM2)
{
HAL_GPIO_TogglePin(GPIOB, LED_Pin);
if( fast_flash_count > 0 )
{
fast_flash_count-- ;
// Set TIM2 reload to fast-flash period
TODO
}
else
{
// Set TIM2 reload to slow-flash period
TODO
}
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == BT_Pin)
{
fast_flash_count = 20 ;
// Set TIM2 counter to current reload value
// to force immediate interrupt
TODO
}
}
I have never get this before; i'm doing a simple program: a father process which creates child processes, and after it terminates; this is the code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int child(pid_t pid)
{
printf("process %d is terminating\n",pid);
exit(EXIT_SUCCESS);
}
int main()
{
int i;
for(i = 0; i < 2; i++){
if(fork() == 0)
child(getpid());
}
printf("father, pid=%d terminated\n",getpid());
/*if(wait(NULL) == -1)
perror("wait");*/
exit(EXIT_SUCCESS);
}
when i run, all processes printed, but one process, i think father but i'm not sure, doesn't terminate because terminal is active(doesn't appear $);
insted, if i insert commented line with wait, program runs correctly; why this?
terminal output
If you look at the last run in the screen capture, you'll see "process 8429 is terminating" after the terminal prompt has been printed to the screen.
What appears to be happening is your "father" process happens to exit first, and your shell then outputs the prompt. Then, in the last case, the child process (PID 8429) printed its output and then exited.
So it is terminating, but sometimes the child process doesn't terminate until after the parent process exited and your shell process has already printed the prompt.
Just hit "enter" again when it does that, and you should see a proper shell prompt. Or even enter a command, and it should run normally.
How to avoid zombie processes? and what exactly init process does in this situation?
I've seen this program,but not able to get it:
How does this program creates a zombie process:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t p = fork();
if (p != 0)
{
waitpid(p, NULL, 0); /* See if the child already had ended. */
sleep(1); /* Wait 1 seconds for the child to end. And eat away the SIGCHLD in case if arrived. */
pause(); /* Suspend main task. */
}
else
{
sleep(3); /* Just let the child live for some tme before becoming a zombie. */
}
return 0;
}
A child process turns into a zombie process when it did exit but the parent process did not yet run waitpid, wait or waitid on it. In a normal situation a parent would want to know the exit status on the child process it spawned and therefore woud run waitpid on the pid it got from fork.
What happens in your code above:
the child is spawned and exits (leaves the else clause and returns 0)
the parent runs into the endless pause loop until you press ctrl-c (the sleep and waitpid are superflous)
If you start the program and leave it running (./a.out &) and then run ps -fx then you see something like this:
6940 pts/1 SN 0:00 ./a.out
6943 pts/1 ZN 0:00 \_ [a.out] <defunct>
Now, if you kill the parent process (kill 6940) then the child becomes an orphan and the init process automatically becomes the new parent. Since the init process runs waitpid (aka "reaps" the child) on all processes it inherits the zombie process finally is deleted from the process table and does not show up any more via ps -f
Good morning !
I have recently read articles quite interesting about hooking functions, I have followed one or two tutorials but it never seems to work, I am using Detoured and here is the full code which seems to me perfectly normal :(
#include <stdio.h>
#include <windows.h>
#include "stdafx.h"
#include "detours.h"
#pragma comment(lib, "detours.lib")
int(__stdcall* realFunc)(int) = (int(__stdcall*)(int))(0x004157B0);
void hookedFunc(int num)
{
printf("Test : %d\n", num + 100);
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
break;
case DLL_THREAD_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
DetourTransactionCommit();
hookedFunc(100);
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DetourDetach((PVOID*)0x004157B0, hookedFunc);
break;
}
return TRUE;
}
When using RemoteDLL and a simple console application as dummy to hook the function, all steps are completed successfully (running as administrator), the memory address to the function I want to be hooked matches, however the code line "printf("Test : %d\n", num + 100);" is not executed, the result does not appears at screen...
If anyone would have an idea about what's going on I would be really happy to hear it !
Thanks in advance !
First, hookedFunc must have the same signature: int __stdcall hookedFunc(int x).
I suppose the following effect of your code: hookedFunc is called each time somebody calls the function at address 0x004157B0. Is it what you expect?
For testing, you call this address. Let me change the code a little to clarify:
extern int __stdcall FunctionIWantToHook(int);
int(__stdcall* realFunc)(int) = FunctionIWantToHook;
...
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
FunctionIWantToHook(100); // hookedFunc will be called here
I'd like to call a function before processing the stop/kill signal.
Is there an easy way to do this?
You can handle a SIGTERM and SIGINT signal by setting-up a signal handler (see signal(3)), however you cannot handle SIGKILL, which is why it should be the last resort to use against a program.
If you always want to do something before the process exits, then see atexit(3).
$ cat sig.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
static void closedown() {
printf("running closedown\n");
}
static void sighandler(int signal) {
exit(1);
}
int main(int argc, const char **argv) {
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
atexit(closedown);
while (1) {
sleep(1);
printf("tick\n");
}
return 0;
}
$ clang -o sig sig.c
$ ./sig
tick
tick
^Crunning closedown