--------------------------------------------
Snippet:
char *value="12345";
int size;
size = sizeof(value);
printf("1.value[%s], size[%d]\n", value, size);
strcpy(value, "67890");
printf("2.value[%s], size[%d]\n", value, size);
--------------------------------------------
Result:
1.value[12345], size[4]
Segmentation fault
--------------------------------------------
Snippet:
char value[]="12345";
int size;
size = sizeof(value);
printf("1.value[%s], size[%d]\n", value, size);
strcpy(value, "67890");
printf("2.value[%s], size[%d]\n", value, size);
--------------------------------------------
Result:
1.value[12345], size[6]
2.value[67890], size[6]
2011年6月30日
Array and Pointers
標籤: Programming
2011年5月30日
Get external defined string from Makefile
gcc -D 'HEADER_PREFIX="MYIMAGE"' ...
#define IMAGE_HEADER HEADER_PREFIX"_PKG"
printf("My image full header [%s]\n", IMAGE_HEADER);
標籤: Programming
2011年4月12日
system() return (10): No child processes
The code includes "signal(SIGCHLD, SIG_IGN); " to prevent process from zombie.
So the wait() is unable to get the signal and system() will return -1.
Solution:
Change from SIG_IGN to SIG_DFL.
In my case, I have to retain signal(SIGCHLD, SIG_IGN), so how to handle well with its return value?
標籤: Programming
2011年4月7日
popen hangs!?
The suspicion is waitpid()?
After comment it out, the code works well and doesn't hang on popen().
Ref: http://pubs.opengroup.org/onlinepubs/009695399/functions/pclose.html
The issue may be related to pclose().
The return value of pclose() is always "-1", and the next popen() call hangs then.
From functional description, it says:
The pclose() function shall close a stream that was opened by popen(), wait for the command to terminate, and return the termination status of the process that was running the command language interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() shall return -1 with errno set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:
- wait()
- waitpid() with a pid argument less than or equal to 0 or equal to the process ID of the command line interpreter
- Any other function not defined in this volume of IEEE Std 1003.1-2001 that could do one of the above.
In any case, pclose() shall not return before the child process created by popen() has terminated.
And in my case, the signal handler routine is using waitpid(-1, &stat, WNOHANG). After change it to waitpid(forked_pid, &stat, WNOHANG) to ignore SIGCHLD triggered by popen(), the application doesn't hang anymore.
標籤: Programming