Linux Linux程序练习七
生活随笔
收集整理的這篇文章主要介紹了
Linux Linux程序练习七
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:實現兩個程序mysignal、mycontrl,mycontrl給mysignal發送SIGINT信號,控制mysignal是否在屏幕打印“hello”字符串。
?
//捕捉信號 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>#include <unistd.h> #include <signal.h>int flag=0;void catch_sig(int sign) {switch(sign){case SIGINT:flag=flag==0?1:0;break;case SIGALRM:exit(0);} }int mysignal(int sign,void (*func)(int)) {struct sigaction act,oact;act.sa_handler=func;sigemptyset(&act.sa_mask);act.sa_flags=0;return sigaction(sign,&act,&oact); }int main(int arg,char *args[]) {//注冊信號 mysignal(SIGINT,catch_sig);mysignal(SIGALRM,catch_sig);while(1){if(flag==1)printf("hello\n");sleep(1);}return 0; } //發送信號 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h>#include <unistd.h> #include <sys/types.h> #include <signal.h>int main(int arg,char * args[]) {if(arg<2){printf("請輸入一個參數!\n");return -1;}int resid=0;pid_t pid=atoi(args[1]);resid=kill(pid,SIGALRM);if(resid!=0){printf("error message:%s\n",strerror(errno));return -1;}return 0; } .SUFFIXES:.c .o CC=gcc SRCS=mycontrl.c OBJS=$(SRCS:.c=.o) EXEC=contrlstart:$(OBJS)$(CC) -o $(EXEC) $(OBJS)@echo "^_^-----OK------^_^" .c.o:$(CC) -Wall -g -o $@ -c $< clean:rm -f $(OBJS)rm -f $(EXEC)?
轉載于:https://www.cnblogs.com/zhanggaofeng/p/5851289.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Linux Linux程序练习七的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: u-boot裁减
- 下一篇: linux设置套接字缓冲区的大小