Requirements:
implement two programs mysignal, Mycontrl, Mycontrl to mysignal send SIGINT signal, control mysiganl whether the screen print "Hello,world" string.
Makefile
. Suffixes:. C. occ=gccsrcs1=mysignal.csrcs2=mycontrol.c objs1=$ (SRCS1:.C=.O) objs2=$ (SRCS2:.C=.O) EXEC1= Mysignalexec2=mycontrolall: $ (OBJS1) $ (OBJS2) $ (cc)- o $ (EXEC1) $ (OBJS1) $ (cc)- o $ (EXEC2) $ (OBJS2) @echo '-------------ok--------------'. C.O: $ (CC)-G- o [email protected]-C $< Clean: rm-f $ (OBJS1 ) $ (OBJS2) rm-f core*
//mysignal.c
#include <stdio.h> #include <stdlib.h> #include <signal.h>int status = 0;//Global, 1-print, 0-do not print void Catch_ Signal (int sign) { switch [sign] {case SIGINT: if (status = = 0) { status = 1; } else { status = 0;}} } int signal1 (int signo, void (*func) (int)) { struct sigaction Act, oact; Act.sa_handler = func; Sigemptyset (&act.sa_mask); act.sa_flags = 0; Return Sigaction (Signo, &act, &oact);} int main (int arg, char *args[]) { signal1 (SIGINT, catch_signal);//Capture SIGINT signal while (1) { if ( Status)//If status is 1, print the string "Hello" { puts ("Hello, World"); } Sleep (1); } return 0;}
//MYCONTROL.C
#include <stdio.h> #include <stdlib.h> #include <signal.h>int main (int arg, char *args[]) { if (arg < 2)//If there is no parameter, main returns { return-1; } else { int pid = atoi (Args[1]); Kill (PID, SIGINT);//Send SIGINT signal to the specified PID } return 0;}
Operating effect:
Linux signal processing-control processes by sending signals