Linux Linux Program Practice VII

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.