Linux signal processing-control processes by sending signals

Source: Internet
Author: User
Tags sigint signal

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

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.