Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48764
  • 博文数量: 25
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-13 05:50
文章分类

全部博文(25)

文章存档

2011年(5)

2010年(1)

2009年(19)

我的朋友

分类: LINUX

2009-05-22 11:23:17

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
 
阅读(621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~