我是
靠谱客的博主
坚定哈密瓜,最近开发中收集的这篇文章主要介绍
linux应用编程:signal(信号量) 实例11:示例代码2:执行结果,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1:示例代码
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>
#include<stdlib.h>
#include <sys/wait.h>
//the copy fie size must>M
int count=0;
//current copy number
int file_size;
//the file size
pid_t pid;
void sig_alarm(int arg)
{
kill(pid,SIGUSR1);
}
void sig_int(int arg)
{
printf("file size:%d totalreceive size:%dn",file_size,count);
exit(EXIT_SUCCESS);
}
char bar[10000];
void sig_usr(int sig)
{
float i;
strcat(bar,"#");
fflush(stdout);
i=(float)count/(float)file_size;
//printf("curent over :%0.0f%%n",i*100);
printf("curent over :%s %0.0f%%n",bar,i*100);
}
int main(int argc,char *argv[])
{
int i=0,stat_val;;
int fd_src,fd_des;
char buf[4096];
//in order to infirm the problem, buf can set small
if(argc!=3)
{
printf("check the format:comm src_file des_filen");
return -1;
}
if((fd_src=open(argv[1],O_RDONLY) )==-1 )
{
perror("open file src");
exit(EXIT_FAILURE);
}
file_size=lseek(fd_src,0,SEEK_END);
lseek(fd_src,0,SEEK_SET);
if( (fd_des=open(argv[2],O_RDWR|O_CREAT,0644) )==-1 )
{
perror("open fd_fdes");
exit(EXIT_FAILURE);
}
if( (pid=fork())==-1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{ printf("1pid();%d ppid%dn",getpid(),getppid());
signal(SIGUSR1,sig_usr);
do
{
memset(buf,'