Title: Implement two programs mysignal, Mycontrl,mycontrl to mysignal send SIGINT signal, control mysignal whether the screen print "Hello" string.
//Capturing signals#include<stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<signal.h>intflag=0;voidCatch_sig (intSign ) { Switch(sign) { CaseSigint:flag=flag==0?1:0; Break; CaseSigalrm:exit (0); }}intMysignal (intSignvoid(*func) (int)){ structsigaction act,oact; Act.sa_handler=func; Sigemptyset (&act.sa_mask); Act.sa_flags=0; returnSigaction (sign,&act,&oact);}intMainintArgChar*args[]) { //Registration Signalmysignal (Sigint,catch_sig); Mysignal (Sigalrm,catch_sig); while(1) { if(flag==1) printf ("hello\n"); Sleep (1); } return 0;}
//Send Signal#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<signal.h>intMainintArgChar*args[]) { if(arg<2) {printf ("Please enter a parameter! \ n"); return-1; } intResid=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. OCC=Gccsrcs=mycontrl.cobjs=$ (srcs:.c=. O) EXEC=contrlstart:$ ( OBJS) -o $ (EXEC) $ (OBJS) "^_^-----OK------^_^". C.O: -wall-g-o [email protected]-C $< clean: -F $ ( Objs)F $ (EXEC)
Linux Linux Program Practice VII