免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 834 | 回复: 0
打印 上一主题 下一主题

Beginning Linux Programming (2) (signal) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-22 11:23 |只看该作者 |倒序浏览
Programming threads on Linux is not as common as suing multiple processes, because Linux process is quite lightweight. and programming multioke cooperation processes is much easier than programming threads.

★Signals
Signals are raised by some error conditions, such as memory segment violation, floating-point processor error, or illegal instructions.

signal list:
Signal Name    Description
SIGABORT       *Process abort.
SIGALRM        Alarm clock.
SIGFPE         *Floating point exception.
SIGHUP         Hangup.
SIGILL         *Illegal instruction.
SIGINT         Terminal interrupt.
SIGKILL        Kill (can't be caught or ignored).
SIGPIPE        Write on a pipe with no reader.
SIGQUIT        Terminal quit.
SIGSEGV        *Invalid memory segment access.
SIGTERM        Termination.
SIGUSR1        User−defined signal 1.
SIGUSR2        User−defined signal 2.

Note that: if a process receives one of these signals without first arranging to catch it, the process will terminate immediately. Usually, a core dump file is created. this file , called core andd placed in the current directory, is an image of the process that can be useful in debugging,

Addtional signal list:
Signal Name    Description
SIGCHLD Child process has stopped or exited.
SIGCONT Continue executing, if stopped.
SIGSTOP Stop executing. (Can't be caught or ignored.)
SIGTSTP Terminal stop signal.
SIGTTIN Background process trying to read.
SIGTTOU Background process trying to write.

Note:
typing the interrupt charater (Ctrl+c ) will result in SIGINT signal being sent to the foreground task.

Programs can handle signals using signal library function:

#include

void (*signal( int sig, void (*func)(int)))(int) ;

sig : SIG_IGN(signal ignore), SIG_DFL(restore default behavior)

sctrl.c example:
#include
#include
#include
void ouch(int sig)
{
    printf("OUCH! - I got signal %d\n", sig);
    (void) signal(SIGINT, SIG_DFL);
}

/*  The main function has to intercept the SIGINT signal generated when we type Ctrl-C .
    For the rest of the time, it just sits in an infinite loop,
    printing a message once a second.  */
int main()
{
    (void) signal(SIGINT, ouch);
    while(1) {
        printf("Hello World!\n");
        sleep(1);
    }
}

sig in ouch() is useful if the same function is used to handle more than one signal.

不建议使用 signal 介绍sigaction

Sending sigal:
using the system call "kill"
#include
#include

int kill(pid_t pid, int sig);
returns 0 ---> success -1 ---> fails
To send the signal , the sending process should have teh pemissoion to send the signal.

The alarm function call can be used by a process to schedule a SIGALRAM signal at some time in the future.
#include

unsigned int alarm( unsigned int seconds ) ;
/*a alarm clock*/
#include
#include
#include
static int alarm_fired = 0;
void ding(int sig)
{
    alarm_fired = 1;
}

/*  In main, we tell the child process to wait for five seconds
    before sending a SIGALRM signal to its parent.  */
int main()
{
    int pid;
    printf("alarm application starting\n");
    if((pid = fork()) == 0) {
        sleep(5);
       kill(getppid(), SIGALRM);
        exit(0);
    }
/*  The parent process arranges to catch SIGALRM with a call to signal
    and then waits for the inevitable.  */
    printf("waiting for alarm to go off\n");
    (void) signal(SIGALRM, ding);
    pause();
    if (alarm_fired)
        printf("Ding!\n");

    printf("done\n");
    exit(0);
}
★Signactions


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94518/showart_1935235.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP