首页编程sigpipe,signal的函数名: signal

sigpipe,signal的函数名: signal

编程之家2023-11-0291次浏览

大家好,今天来为大家解答sigpipe这个问题的一些问题点,包括signal的函数名: signal也一样很多人还不知道,因此呢,今天就来为大家分析分析,现在让我们一起来看看吧!如果解决了您的问题,还望您关注下本站哦,谢谢~

sigpipe,signal的函数名: signal

signal的函数名: signal

表头文件#include<signal.h>

功能:设置某一信号的对应动作

函数原型:void(*signal(int signum,void(* handler)(int)))(int);

或者:typedef void(*sig_t)( int);

sig_t signal(int signum,sig_t handler);

参数说明:

sigpipe,signal的函数名: signal

第一个参数signum指明了所要处理的信号类型,它可以取除了SIGKILL和SIGSTOP外的任何一种信号。

第二个参数handler描述了与信号关联的动作,它可以取以下三种值:

(1)一个无返回值的函数地址

此函数必须在signal()被调用前申明,handler中为这个函数的名字。当接收到一个类型为signum的信号时,就执行handler所指定的函数。这个函数应有如下形式的定义:

void func(int sig);

(2)SIG_IGN

sigpipe,signal的函数名: signal

这个符号表示忽略该信号,执行了相应的signal()调用后,进程会忽略类型为sig的信号。

(3)SIG_DFL

这个符号表示恢复系统对信号的默认处理。

函数说明:

signal()会依参数signum指定的信号编号来设置该信号的处理函数。当指定的信号到达时就会跳转到参数handler指定的函数执行。当一个信号的信号处理函数执行时,如果进程又接收到了该信号,该信号会自动被储存而不会中断信号处理函数的执行,直到信号处理函数执行完毕再重新调用相应的处理函数。但是如果在信号处理函数执行时进程收到了其它类型的信号,该函数的执行就会被中断。

返回值:返回先前的信号处理函数指针,如果有错误则返回SIG_ERR(-1)。

下面的情况可以产生Signal:按下CTRL+C产生SIGINT硬件中断,如除0,非法内存访问(SIGSEV)等等 Kill函数可以对进程发送Signal Kill命令。实际上是对Kill函数的一个包装软件中断。如当Alarm Clock超时(SIGURG),当Reader中止之后又向管道写数据(SIGPIPE),等等程序例:

/* This example installs a signal handler routine for SIGFPE,

catches an integer overflow condition, makes an adjustment

to AX register, and returns. This example program MAY cause

your computer to crash, and will produce runtime errors

depending on which memory model is used.

*/

#pragma inline

#include<stdio.h>

#include<signal.h>

void Catcher(int sig, int type, int*reglist)

{

printf(Caught it!\n);

*(reglist+ 8)= 3;/* make return AX= 3*/

}

int main(void)

{

signal(SIGFPE, Catcher);

asm mov ax,07FFFH/* AX= 32767*/

asm inc ax/* cause overflow*/

asm into/* activate handler*/

/* The handler set AX to 3 on return. If that hadn't happened,

there would have been another exception when the next'into'

was executed after the'dec' instruction.*/

asm dec ax/* no overflow now*/

asm into/* doesn't activate*/

return 0;

}

2 Signals: Signal Description SIGABRT由调用abort函数产生,进程非正常退出 SIGALRM用alarm函数设置的timer超时或setitimer函数设置的interval timer超时 SIGBUS某种特定的硬件异常,通常由内存访问引起 SIGCANCEL由Solaris Thread Library内部使用,通常不会使用 SIGCHLD进程Terminate或Stop的时候,SIGCHLD会发送给它的父进程。缺省情况下该Signal会被忽略 SIGCONT当被stop的进程恢复运行的时候,自动发送 SIGEMT和实现相关的硬件异常 SIGFPE数学相关的异常,如被0除,浮点溢出,等等 SIGFREEZE Solaris专用,Hiberate或者Suspended时候发送 SIGHUP发送给具有Terminal的Controlling Process,当terminal被disconnect时候发送 SIGILL非法指令异常 SIGINFO BSD signal。由Status Key产生,通常是CTRL+T。发送给所有Foreground Group的进程 SIGINT由Interrupt Key产生,通常是CTRL+C或者DELETE。发送给所有ForeGround Group的进程 SIGIO异步IO事件 SIGIOT实现相关的硬件异常,一般对应SIGABRT SIGKILL无法处理和忽略。中止某个进程 SIGLWP由Solaris Thread Libray内部使用 SIGPIPE在reader中止之后写Pipe的时候发送 SIGPOLL当某个事件发送给Pollable Device的时候发送 SIGPROF Setitimer指定的Profiling Interval Timer所产生 SIGPWR和系统相关。和UPS相关。 SIGQUIT输入Quit Key的时候(CTRL+\)发送给所有Foreground Group的进程 SIGSEGV非法内存访问 SIGSTKFLT Linux专用,数学协处理器的栈异常 SIGSTOP中止进程。无法处理和忽略。 SIGSYS非法系统调用 SIGTERM请求中止进程,kill命令缺省发送 SIGTHAW Solaris专用,从Suspend恢复时候发送 SIGTRAP实现相关的硬件异常。一般是调试异常 SIGTSTP Suspend Key,一般是Ctrl+Z。发送给所有Foreground Group的进程 SIGTTIN当Background Group的进程尝试读取Terminal的时候发送 SIGTTOU当Background Group的进程尝试写Terminal的时候发送 SIGURG当out-of-band data接收的时候可能发送 SIGUSR1用户自定义signal 1 SIGUSR2用户自定义signal 2 SIGVTALRM setitimer函数设置的Virtual Interval Timer超时的时候 SIGWAITING Solaris Thread Library内部实现专用 SIGWINCH当Terminal的窗口大小改变的时候,发送给Foreground Group的所有进程 SIGXCPU当CPU时间限制超时的时候 SIGXFSZ进程超过文件大小限制 SIGXRES Solaris专用,进程超过资源限制的时候发送

ios sigsegv怎么解决

iOS SDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler

用来做异常处理,但功能非常有限,而引起崩溃的大多数原因如:内存访问错误,重复释放等错误就无能为力了,因为这种错误它抛出的是Signal,所以必须

要专门做Signal处理。首先定义一个UncaughtExceptionHandler类,.h头文件的代码如下:

#import<UIKit/UIKit.h>@interface UncaughtExceptionHandler: NSObject{ BOOL dismissed;}@end void InstallUncaughtExceptionHandler();然后在.mm文件实现InstallUncaughtExceptionHandler(),如下: void InstallUncaughtExceptionHandler(){ signal(SIGABRT, MySignalHandler); signal(SIGILL, MySignalHandler); signal(SIGSEGV, MySignalHandler); signal(SIGFPE, MySignalHandler); signal(SIGBUS, MySignalHandler); signal(SIGPIPE, MySignalHandler);}

这样,当应用发生错误而产生上述Signal后,就将会进入自己自定义的回调函数MySignalHandler。为了得到崩溃时的现场信息,还可以加入一些获取CallTrace及设备信息的代码,.mm文件的完整代码如下:

#import"UncaughtExceptionHandler.h"#include<libkern/OSAtomic.h>#include<execinfo.h> NSString* const UncaughtExceptionHandlerSignalExceptionName=@"UncaughtExceptionHandlerSignalExceptionName"; NSString* const UncaughtExceptionHandlerSignalKey=@"UncaughtExceptionHandlerSignalKey"; NSString* const UncaughtExceptionHandlerAddressesKey=@"UncaughtExceptionHandlerAddressesKey"; volatile int32_t UncaughtExceptionCount= 0; const int32_t UncaughtExceptionMaximum= 10; const NSInteger UncaughtExceptionHandlerSkipAddressCount= 4; const NSInteger UncaughtExceptionHandlerReportAddressCount= 5;@implementation UncaughtExceptionHandler+(NSArray*)backtrace{ void* callstack[128]; int frames= backtrace(callstack, 128); char**strs= backtrace_symbols(callstack, frames); int i; NSMutableArray*backtrace= [NSMutableArray arrayWithCapacity:frames]; for( i= UncaughtExceptionHandlerSkipAddressCount; i< UncaughtExceptionHandlerSkipAddressCount+ UncaughtExceptionHandlerReportAddressCount; i++){ [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];} free(strs); return backtrace;}-(void)alertView:(UIAlertView*)anAlertView clickedButtonAtIndex:(NSInteger)anIndex{ if(anIndex== 0){ dismissed= YES;}}-(void)handleException:(NSException*)exception{ UIAlertView*alert= [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Unhandled exception", nil) message:[NSString stringWithFormat:NSLocalizedString(@"You can try to continue but the application may be unstable.\n"@"%@\n%@", nil), [exception reason], [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]] delegate:self cancelButtonTitle:NSLocalizedString(@"Quit", nil) otherButtonTitles:NSLocalizedString(@"Continue", nil), nil] autorelease]; [alert show]; CFRunLoopRef runLoop= CFRunLoopGetCurrent(); CFArrayRef allModes= CFRunLoopCopyAllModes(runLoop); while(!dismissed){ for(NSString*mode in(NSArray*)allModes){ CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);}} CFRelease(allModes); NSSetUncaughtExceptionHandler(NULL); signal(SIGABRT, SIG_DFL); signal(SIGILL, SIG_DFL); signal(SIGSEGV, SIG_DFL); signal(SIGFPE, SIG_DFL); signal(SIGBUS, SIG_DFL); signal(SIGPIPE, SIG_DFL); if([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName]){ kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]);} else{ [exception raise];}}@end NSString* getAppInfo(){ NSString*appInfo= [NSString stringWithFormat:@"App:%@%@(%@)\nDevice:%@\nOS Version:%@%@\nUDID:%@\n", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"], [UIDevice currentDevice].model, [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion, [UIDevice currentDevice].uniqueIdentifier]; NSLog(@"Crash!!!!%@", appInfo); return appInfo;} void MySignalHandler(int signal){ int32_t exceptionCount= OSAtomicIncrement32(&UncaughtExceptionCount); if(exceptionCount> UncaughtExceptionMaximum){ return;} NSMutableDictionary*userInfo= [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey]; NSArray*callStack= [UncaughtExceptionHandler backtrace]; [userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey]; [[[[UncaughtExceptionHandler alloc] init] autorelease] performSelectorOnMainThread:@selector(handleException:) withObject: [NSException exceptionWithName:UncaughtExceptionHandlerSignalExceptionName reason: [NSString stringWithFormat: NSLocalizedString(@"Signal%d was raised.\n"@"%@", nil), signal, getAppInfo()] userInfo: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey]] waitUntilDone:YES];} void InstallUncaughtExceptionHandler(){ signal(SIGABRT, MySignalHandler); signal(SIGILL, MySignalHandler); signal(SIGSEGV, MySignalHandler); signal(SIGFPE, MySignalHandler); signal(SIGBUS, MySignalHandler); signal(SIGPIPE, MySignalHandler);}

在应用自身的 didFinishLaunchingWithOptions前,加入一个函数:

-(void)installUncaughtExceptionHandler{ InstallUncaughtExceptionHandler();}

最后,在

didFinishLaunchingWithOptions中加入这一句代码就行了:

[self InstallUncaughtExceptionHandler];

现在,基本上所有崩溃都能Hold住了。

关于sigpipe和signal的函数名: signal的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

中国建设部网站 建设部资格证书查询官网站长网(网站站长的职责是什么)