Event-driven programming--"Unix/linux Programming Practice Tutorial" Reading notes (7th chapter)

Source: Internet
Author: User
Tags clear screen posix signal handler sleep function

1. Curses Library

/* Basic curses function */initscr ();            Initialize the curses library and Ttyendwin ();             Close curses and reset Ttyrefresh ();            Make the screen show your Move (R, c) as you intended;           Move the cursor to the screen (R, c) position addstr (s);            Draws a string saddch (c) at the current position;             stooped character Cclear () at the current position;              Clear screen standout ();           Start the standout mode (typically make the screen counter-color) standend ();           Turn off standout mode



2.
Mans 3 Sleep
#include <unistd.h>unsigned int sleep (unsigned int n);/* Sleep () makes the calling thread sleep until n seconds * The elapsed or a signal arrives which are not ignored.  *  Zero If the requested time has a elapsed, or the number * of seconds left to sleep, if the call is interrupted * by A signal handler. */



How Sleep () works

Each process in the system has a private alarm (alarm clock). This alarm clock is much like a timer and can be set to alarm after a certain number of seconds. When the time is up, the clock sends a signal SIGALRM to the process. Unless the process has a processing function (handler) set for SIGALRM, the signal will kill the process. The Sleep function consists of 3 steps:

/* Sleep () works */signal (SIGALRM, Handler), Alarm (num_seconds);p ause ();

A description of alarm () and pause ().



Mans 2 Alarm



#include <unistd.h>unsigned int alarm (unsigned int n_seconds); */* Alarm () arranges for a SIGALRM signal to be deliv Ered to * calling process in n seconds. * If N_seconds is zero and any pending alarm is canceled. * Envet any previously set alarm () is canceled. * * ALARM () returns the number of seconds remaining until * any previously scheduled alarm is due to be delivered, * or Z Ero If there was no previously scheduled alarm. */



Man 2 Pause



#include <unistd.h>int pause (void);/* Pause () causes the calling process (or thread) to sleep until * a signal is Delivered that either terminates the process or * causes the invocation of a signal-catching function. * * Pause () returns only if a signal was caught and the * signal-catching function returned. In this case, pause () * returns-1, and errno are set to EINTR. */



Higher Accuracy Latency:
#include <unistd.h>int usleep (useconds_t usec);



3, three kinds of timers: real, process and practical

(1) Itimer_real: This timer measures the real time. When this timer is exhausted, send the SIGALRM message.

(2) Itimer_virtual: Virtual timer Only the process is timed when the user state is running. When the virtual timer is exhausted, the SIGVTALRM message is sent.

(3) Itimer_prof: This timer is timed when the process is running in a user state or is being called by the process into a nuclear mentality. When this timer is exhausted, send the SIGPROF message.

4, SET_TICKER.C

  /* * SET_TICKER.C * Set_ticker (number_of_milliseconds) * Arranges for interval timer to issue sigalrms at regular inte Rvals * Return to error, 0 for OK * arg in milliseconds, converted to whole seconds and microseconds * Note:set_tick ER (0) turns off ticker */#include <stdio.h> #include <sys/time.h>int set_ticker (int n_msecs) {    struct Itimerval New_timeset;    Long n_sec, n_usecs;    N_sec = n_msecs/1000;    N_usecs = (n_msecs%) * 1000L;    New_timeset.it_interval.tv_sec = n_sec;    New_timeset.it_interval.tv_usec = N_usecs;    New_timeset.it_value.tv_sec = n_sec;    New_timeset.it_value.tv_usec = N_usecs;    Return Setitimer (Itimer_real, &new_timeset, NULL);}



Description of Structure Itimerval, Timeval:
struct itimerval{    struct timeval it_valuse;      /* Time to next timer expiration */    struct timeval it_interval;    /* Reload It_value with this */}struct timeval{    time_t      tv_sec;        /* seconds */    suseconds   tv_usec;       /* and microseconds */}



Description of the function Getitimer, Setitimer:
Mans 2 Getitimer



#include <sys/time.h>int getitimer (int which, struct itimerval *curr_value), int setitimer (int which, const struct I Timerval *new_value,              struct itimerval *old_value);



5. Early signal processing mechanism

Various events cause the kernel to send a signal to the process. These events include the user's keystroke, the illegal operation of the process, and the timer. A process call signal is selected among the following 3 methods of handling signals:

(1) Default action (usually terminate process), for example, signal (SIGALRM, SIG_DFL)



(2) Ignoring signals, for example, signal (SIGALRM, sig_ign)

(3) Call a function, for example, signal (SIGALRM, handler)

6, POSIX substitution signal () function sigaction ()

(1)

Mans 2 Sigaction

#include <signal.h>int sigaction (int signum, const struct sigaction *act,              struct sigaction *oldact);/* Where */STR UCT sigaction {    void (*sa_handler) (int);  /* SIG_DFL, sig_ign, or function *    /Void (*sa_sigaction) (int, siginfo_t *, void *);  /* New handler */    sigset_t sa_mask;  /* signals to block while handling */    int sa_flags;  /* Enable various behaviors */    void (*sa_restorer) (void);/* POSIX does not specify this element */};



(2) Sa_flags is to use some bits to control the processing function how to deal with the above 4



Sigaction () An overview of the problem, use a routine to understand it:

/* * SIGACTDEMO.C * purpose:shows use of sigaction () * feature:blocks ^\ while handling ^c * does not reset ^c Handler, so-kill */#include <stdio.h> #include <signal.h> #define INPUTLEN (+) int main (void) {struct SI         Gaction Newhandler;            /* New settings */sigset_t blocked;    /* Set of blocked sigs */void Inthandler (int);    Char X[inputlen];     /* Load These the first */Newhandler.sa_handler = Inthandler; /* Handler function */newhandler.sa_flags = Sa_resethand |                                            Sa_restart;                  /* Options */* Then build the list of blocked signals */Sigemptyset (&blocked);           /* Clear All bits */Sigaddset (&blocked, sigquit);           /* Add sigquit to list */Newhandler.sa_mask = blocked;    /* Store Blockmask */if (sigaction (SIGINT, &newhandler, NULL) = =-1) perror ("Sigaction");    Else    while (1) {fgets (x, Inputlen, stdin);        printf ("Input:%s\n", X); } return 0;}    void Inthandler (int s) {printf ("called with Signal%d\n", s);    Sleep (s); printf ("Done handling signal%d\n", s);}





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.