我有一個小函式,它應該為我提供有關子行程如何完成的資訊。
int debug_wait() {
int status;
wait(&status);
if (WIFSIGNALED(status)) {
int sig = WSTOPSIG(status);
printf("failed with signal %d (%s)\n", sig, strsignal(sig));
return 1;
}
else if (!WIFEXITED(status)) {
printf("ended in an unexpected way\n");
return 1;
}
return 0;
}
但我得到以下結果:
double free or corruption (out)
tests failed with signal 0 (Unknown signal 0)
我知道我應該修復我的代碼,但為什么我得到信號沒有。0? 我的功能有錯誤還是有其他含義?
既然人們問了,這里有一個示例程式:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
int debug_wait() {
int status;
wait(&status);
if (WIFSIGNALED(status)) {
int sig = WSTOPSIG(status);
printf("tests failed with signal %d (%s)\n", sig, strsignal(sig));
return 1;
}
else if (!WIFEXITED(status)) {
printf("tests ended in an unexpected way\n");
return 1;
}
return 0;
}
int main() {
void* ptr = malloc(10);
pid_t pid = fork();
if (pid == 0) {
free(ptr);
free(ptr);
}
else {
debug_wait();
}
}
uj5u.com熱心網友回復:
你正在解碼錯誤的東西。如果WIFSIGNALED是true,您可以使用WTERMSIGand WCOREDUMP(先檢查#ifdef WCOREDUMP)。推斷WSTOPSIGthenWIFSTOPPED必須是true.
例子:
int status;
pid_t pid = wait(&status);
if (pid == -1) return 1;
if (WIFEXITED(status)) {
printf("normal exit %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("termsig %d core dump=%c\n", WTERMSIG(status),
WCOREDUMP(status) ? 'Y' : 'N');
} else if (WIFSTOPPED(status)) {
printf("stop %d cont=%c\n", WSTOPSIG(status),
WIFCONTINUED(status) ? 'Y' : 'N');
}
不使用功能測驗宏:
if (WIFEXITED(status)) {
printf("normal exit %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("termsig %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stop %d\n", WSTOPSIG(status));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/525463.html
標籤:C信号叉子
