Linux信号机制代码示例
1 基本功能:
本Blog创建了两个进程(父子进程):
- 父进程:
执行文本复制操作,当收到SIGUSR1信号后,打印出现在文件复制的进度; - 子进程:
每个固定时间段向父进程发送一个SIGUSR1信号。
2 代码示例:
/*
* File: Signal.c
*Description: Two process
1. Father: copy a file, when receive the SIGUSR1 signal, print the progress
2. Child: timing trigger the parent process
*Autor: Jimmy Nie
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/fcntl.h>
void handler(int sig);
void SigAlarm(int sig);
int count = 0; //has read bytes
int fileSize = 0; //the source file size
int main(int argc, char *argv[])
{
//variable define
int fd_src, fd_dst;
int tmp = 0; //how many bytes read every time
char buf[128] ; //tempature storage buffer
//0. check the argument
if(argc != 3)
{
printf("%s(%d): Check the arguments, argument=%d(3 is need)\n", argc);
exit(EXIT_FAILURE);
}
//1. open the file
if(-1 == (fd_src=open(argv[1], O_RDONLY)))
{
perror("open");
exit(EXIT_FAILURE);
}
//open the destination file, if it does not exist, creat it first
if(-1 == (fd_dst=open(argv[2], O_RDWR|O_CREAT, 0644)))
{
perror("open");
exit(EXIT_FAILURE);
}
//2. Obtain the source file size
fileSize = lseek(fd_src, 0, SEEK_END);
if(fileSize < 0)
{
perror("lseek");
exit(EXIT_FAILURE);
}
lseek(fd_src, 0, SEEK_SET);
//3. Father process install SIGUSR1 signal
if(signal(SIGUSR1, handler) == SIG_ERR)
{
perror("signal");
exit(EXIT_FAILURE);
}
//4. Creat a new process(child)
pid_t pid;
if(-1 == (pid=fork()))
{
perror("fork");
exit(EXIT_FAILURE);
}
//In child process
else if(pid == 0)
{
//Install the signal SIGALRM
if(signal(SIGALRM, SigAlarm) == SIG_ERR)
{
perror("signal");
exit(EXIT_FAILURE);
}
ualarm(200,10000); //after 20ms start trigger, and every 50ms trigger once
//alarm(1);
while(1) //execute continues
;
}
//In parent process
else
{
//3. copy source file to destination file
while(1)
{
//read the source file to buf
if(-1 == (tmp=(read(fd_src, buf, 128))))
{
perror("read");
exit(EXIT_FAILURE);
}
//check the end of file
if(0 == tmp)
{
printf("Finished copy the file, and file size:%d\n", fileSize);
kill(pid, SIGINT); //finished copy, trigger a signal to child, and terminate child process
break;
}
//write the buffer to the destination file
if(-1 == write(fd_dst, buf, tmp))
{
perror("Write");
exit(EXIT_FAILURE);
}
count += tmp;
}
wait(NULL); //wait child process exit
close(fd_src);
close(fd_dst);
}
return 0;
}
//function used to print the progree of copy file
void handler(int sig)
{
int i = 0;
i = (int)(((float)count / (float)fileSize) * 100);
printf("\nHas copyed %d%% \n",i);
int j = 0;
for(j=0; j<i; j++)
{
if(j%2)
printf("*");
}
printf("\n");
}
//used to send SIGUSR1 signal to parent process
void SigAlarm(int sig)
{
kill(getppid(), SIGUSR1);
//alarm(1);
}
编译该代码:
[root@niesh Linux]# gcc -o signal signal.c
[root@niesh Linux]# ll
总用量 36
-rwxrwxr-x. 1 niesh niesh 8659 9月 22 22:07 produce
-rw-rw-r--. 1 root niesh 467 9月 22 22:07 produce.c
-rwxr-xr-x. 1 root root 13445 9月 23 11:26 signal
-rw-rw-r--. 1 root niesh 3407 9月 22 22:45 signal.c
但是此时我们还需要一个大于1M的ASCII文件,使得CP不至于瞬间完成:
#include <stdio.h>
#include <sys/fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd;
int count = 0;
char buf[] = "Hello,world\n";
fd = open(argv[1], O_RDWR|O_CREAT, 0644);
for(count=0; count < 1024*1024; count++)
write(fd, buf, strlen(buf)); //file size = 16*256*1024*1024 = 4Mbyte
//printf("The sizeof(buf)=%d\n",sizeof(buf));
close(fd);
return 0;
}
通过以上代码我们可以产生出一个12M的文本文件:
[root@niesh Linux]# gcc -o produce produce.c
[root@niesh Linux]# ./produce test
[root@niesh Linux]# ll -h
总用量 13M
-rwxr-xr-x. 1 root root 8.5K 9月 23 11:31 produce
-rw-rw-r--. 1 root niesh 467 9月 22 22:07 produce.c
-rwxr-xr-x. 1 root root 14K 9月 23 11:26 signal
-rw-rw-r--. 1 root niesh 3.4K 9月 22 22:45 signal.c
-rw-r--r--. 1 root root 12M 9月 23 11:31 test //ASCII文件 12M
3 执行效果:
[root@niesh Linux]# ./signal test t1
Has copyed 1%
Has copyed 3%
*
Has copyed 6%
***
Has copyed 7%
***
Has copyed 9%
****
Has copyed 10%
*****
Has copyed 15%
*******
Has copyed 21%
**********
Has copyed 25%
************
Has copyed 29%
**************
Has copyed 34%
*****************
Has copyed 42%
*********************
Has copyed 44%
**********************
Has copyed 48%
************************
Has copyed 54%
***************************
Has copyed 58%
*****************************
Has copyed 65%
********************************
Has copyed 71%
***********************************
Has copyed 76%
**************************************
Has copyed 80%
****************************************
Has copyed 88%
********************************************
Has copyed 93%
**********************************************
Has copyed 97%
************************************************
Finished copy the file, and file size:12582912
查看复制后的结果:
[root@niesh Linux]# ll -h
总用量 25M
-rwxr-xr-x. 1 root root 8.5K 9月 23 11:31 produce
-rw-rw-r--. 1 root niesh 467 9月 22 22:07 produce.c
-rwxr-xr-x. 1 root root 14K 9月 23 11:26 signal
-rw-rw-r--. 1 root niesh 3.4K 9月 22 22:45 signal.c
-rw-r--r--. 1 root root 12M 9月 23 11:42 t1 //复制后生成的文件
-rw-r--r--. 1 root root 12M 9月 23 11:31 test
Linux信号机制代码示例的更多相关文章
- Linux信号机制
Linux信号(signal) 机制分析 [摘要]本文分析了Linux内核对于信号的实现机制和应用层的相关处理.首先介绍了软中断信号的本质及信号的两种不同分类方法尤其是不可靠信号的原理.接着分析了内核 ...
- 利用linux信号机制调试段错误(Segment fault)
在实际开发过程中,大家可能会遇到段错误的问题,虽然是个老问题,但是其带来的隐患是极大的,只要出现一次,程序立即崩溃中止.如果程序运行在PC中,segment fault的调试相对比较方便,因为可以通过 ...
- 利用linux信号机制调试段错误(Segment fault)【转】
转自:http://blog.csdn.net/ab198604/article/details/6164517 版权声明:本文为博主原创文章,未经博主允许不得转载. 在实际开发过程中,大家可能会遇到 ...
- linux信号机制与python信号量
1.信号本质 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程间 ...
- xenomai内核解析之信号signal(一)---Linux信号机制
版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 1. Linux信号 1.1注册信号处理函数 ...
- linux信号机制 - 用户堆栈和内核堆栈的变化【转】
转自:http://itindex.net/detail/16418-linux-%E4%BF%A1%E5%8F%B7-%E5%A0%86%E6%A0%88 此文只简单分析发送信号给用户程序后,用户堆 ...
- linux 信号机制
文章目录 1. 实时信号非实时信号 2. 信号状态: 3. 信号生命周期: 4. 信号的执行和注销 信号掩码和信号处理函数的继承 信号处理函数的继承 信号掩码的继承 sigwait 与多线程 sigw ...
- Storm入门(七)可靠性机制代码示例
一.关联代码 使用maven,代码如下. pom.xml 参考 http://www.cnblogs.com/hd3013779515/p/6970551.html MessageTopology. ...
- Linux信号(signal) 机制分析
Linux信号(signal) 机制分析 [摘要]本文分析了Linux内核对于信号的实现机制和应用层的相关处理.首先介绍了软中断信号的本质及信号的两种不同分类方法尤其是不可靠信号的原理.接着分析了内核 ...
随机推荐
- 用MVC5+EF6+WebApi 做一个考试功能(五) 前端主题
内容概述 前面絮絮叨叨没正事,到现在为止也没有开始写代码,不过在考虑下貌似这一节还是开始不了. B/S架构开发有一个特点,就是用浏览器打开,不同的用户群体可能有不同的风格,不论是管理平台还是普通的网站 ...
- uwp ListView列表滑动特效
在看过一篇文章 WPF自定义控件之列表滑动特效 PowerListBox http://www.cnblogs.com/ShenNan/p/4993374.html#3619585 实现了滑动的特效 ...
- string的函数的学习
1.string类型的构造函数和对象的定义 string s3 : 把string s2 拷贝的 s3 string s4 : 把数组首地址或者字符串首地址strArr 从0开始截取到第n个字母 st ...
- 一分钟搞懂 JavaScript this 指向问题
关于Javascript的this指向问题,网络上有很多分析文章,写的很好,比如这里和这里 我这里做一个简单的总结. 箭头函数的 this 箭头函数内的this指向外层函数定义时所在的作用域.如果没有 ...
- module.export与export的区别?
对于大多数node初学者而言, module.exports应该都是理解的, 但多出来一个exports获取就有些疑问了 疑问一: 既然有module.exports了为什么还要有exports? 疑 ...
- 【NOIP2016提高组】 Day2 T3 愤怒的小鸟
题目传送门:https://www.luogu.org/problemnew/show/P2831 说个题外话:NOIP2014也有一道题叫做愤怒的小鸟. 这题自测时算错了eps,导致被卡了精度,从1 ...
- 数据结构---平衡查找树之B树和B+树(转)
本文转载自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 前面讲解了平衡查找树中的2-3树以及其实现红 ...
- 在操作Centos系统时经常出现You have new mail in /var/spool/mail/root提示怎么回事?
例如,在命令窗口中输入date查看时间,下面会出现一行提示 实际上,该功能为Linux操作系统核对系统资源状态并汇总,默认发送到root用户的/var/spool/mail/root目录,并在标准输出 ...
- Docker学习--docker的基本认识
1.Docker 架构 Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器. Docker 容器通过 Docker 镜像来创建. 容器与镜像的关系类似于 ...
- SQLite使用入门
什么是SQLite SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准.SQLite在使用前不需要安装设置,不需要进程来启动.停止或配置,而其他大多数SQL数据库引擎是作为一个单独的 ...