Linux信号: kill、raise、abort、alarm函数

Linux信号: kill、raise、abort、alarm函数,第1张

Linux信号: kill、raise、abort、alarm函数 kill、raise、abort函数


#include 
#include 

int main(){

    char *buf;

    strcpy(buf, "hello");


    return 0;
}
carey@ubuntu:~/Linux/lesson17_sig$ gcc core.c -g
carey@ubuntu:~/Linux/lesson17_sig$ ls
a.out  core.c
carey@ubuntu:~/Linux/lesson17_sig$ gdb a.out 
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x5fe: file core.c, line 8.
Starting program: /home/carey/Linux/lesson17_sig/a.out 

Temporary breakpoint 1, main () at core.c:8
8	    strcpy(buf, "hello");
(gdb) s

//接收到信号
Program received signal SIGSEGV, Segmentation fault.
0x0000555555554602 in main () at core.c:8
8	    strcpy(buf, "hello");
(gdb) 


#include 
#include 
#include 
#include 

int main(){

    int pid = fork();

    if(pid > 0){
        //P
        printf("Pn");
        sleep(2);
        printf("kill C processn");
        kill(pid, 9);


    }else if(pid == 0){
        //C
        for(int i=1; i<=15; i++){
            printf("C %dn",i);
            sleep(1);
        }

    }else{
        perror("fork");
        return -1;
    }


    return 0;
}
carey@ubuntu:~/Linux/lesson17_sig$ gcc kill.c -o k
carey@ubuntu:~/Linux/lesson17_sig$ ./k
P
C 1
C 2
kill C process
carey@ubuntu:~/Linux/lesson17_sig$ ./k
P
C 1
C 2
kill C process
//运气不错,第三次就看到这个了 这是正常的 因为在第二秒的时候子进程抢到了时间片又执行了一擦
carey@ubuntu:~/Linux/lesson17_sig$ ./k
P
C 1
C 2
kill C process
C 3

alarm()

定时器与进程的状态无关(自然定时法),不管进程处于什么状态,alarm都会计时。


#include 
#include 

int main(){

    int sec = alarm(10);

    printf("sec = %dn", sec);

    sleep(2);
    
    for(int i=1; i<6; i++){
        printf("j = %d n", i);
        sleep(1);
    }

    sec = alarm(10);
    printf("sec = %dn", sec);

    for(int i=1; i<100; i++){
        printf("i = %d n", i);
        sleep(1);
    }

    return 0;
}
//第一个闹钟设置5
carey@ubuntu:~/Linux/lesson17_sig$ ./ala 
sec = 0
j = 1 
j = 2 
j = 3 
Alarm clock
carey@ubuntu:~/Linux/lesson17_sig$ gcc alarm.c -o ala

//第一个闹钟设置10
carey@ubuntu:~/Linux/lesson17_sig$ gcc alarm.c -o ala
carey@ubuntu:~/Linux/lesson17_sig$ ./ala 
sec = 0
j = 1 
j = 2 
j = 3 
j = 4 
j = 5 
sec = 3
i = 1 
i = 2 
i = 3 
i = 4 
i = 5 
i = 6 
i = 7 
i = 8 
i = 9 
i = 10 
Alarm clock


小例子
#include 
#include 

//1秒钟计算机能数多少个数


int main(){
    alarm(1);
    
    int i=1;
    while(1){
        i++;
        printf("i = %d n", i);
    }
    
}
//可以用重定向查看 
gcc _.c -o a
./a >> a.txt

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5624872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存