Introduction and use epoll

First, the Code

 

#include<assert.h>
#include<signal.h>
#include<stdio.h>
#include<sys/epoll.h>
#include<sys/time.h>
#include<sys/wait.h>
#include<unistd.h>

int  fd[2];
int* write_fd;
int* read_fd;
const char msg[] = {'m','e','s','s','a','g', ' E ' }; 

void SigHandler ( int ) { 
    size_t bytes = Write (* write_fd, MSG, the sizeof (MSG)); 
    the printf ( " Children Process MSG have have writed:% LD bytes \ n- " , bytes); 
} 

void ChildrenProcess () {
     struct itimerval the tick = { 0 }; 
    tick.it_value.tv_sec     =    . 1 ;    // 1s after starting a timer 
    tick.it_interval.tv_sec =    . 1 ;    // timer is started, every 1s to execute the respective functions 

    struct sigaction SA;
    sa.sa_flags      =    0 ; 
    sa.sa_handler    =    SigHandler; 
    the sigaction (SIGALRM, & SA, NULL); 

    // setitimer the trigger signal SIGALRM
     // used herein is ITIMER_REAL, so that the corresponding signal SIGALRM 
    assert (setitimer (ITIMER_REAL, the tick &, NULL) == 0 );
     the while ( to true ) { 
        PAUSE (); 
    } 
} 

void FatherProcess () { 
    epoll_event EV; 
    epoll_event Events [ . 1 ];
     char buf [ 1024 ] = { 0 };
     int epoll_fd epoll_create = (1);
    ev.data.fd      =   *read_fd;
    ev.events       =   EPOLLIN | EPOLLET;
    epoll_ctl(epoll_fd, EPOLL_CTL_ADD, *read_fd, &ev);

    while (true) {
        int fds = epoll_wait(epoll_fd, events, sizeof(events), -1);
        if(events[0].data.fd == *read_fd) {
            size_t bytes = read(*read_fd, buf, sizeof(buf));
            printf("father process read %ld bytes = %s\n", bytes, buf);
        }
    }

    int status;
    wait(&status);
}

int main() {
    int ret = pipe(fd);
    if (ret != 0) {
        printf("pipe failed\n");
        return -1;
    }
    write_fd = &fd[1];
    read_fd  = &fd[0];

    pid_t pid = fork();
    if (pid == 0) {//child process
        ChildrenProcess();
    } else if (pid > 0) {//father process
        FatherProcess();
    }

    return 0;
}

 

 

Function Description:

int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);

Parameters:
Which: intermittent timer type, there are three options

ITIMER_REAL // value is 0, the real-time value of the timer decrements, a signal is transmitted SIGALRM.
ITIMER_VIRTUAL // value of 1, the timer value is decremented when the execution process, the signal is transmitted SIGVTALRM.
ITIMER_PROF // timer value is decremented by 2 is performed, a process and a system value, the signal is transmitted SIGPROF.

sigaction

 

 

#include<unistd.h>

int pipe(int filedes[2]);

Return Value: success, returns 0, otherwise it returns -1. Parameter array comprising two pipe file descriptors used. fd [0]: Read the pipe, fd [1]: write pipeline.

Pipe must be called at fork () in (), otherwise the child will not inherit the file descriptors. The two processes are not shared ancestor process, you can not use the pipe. However, you can use named pipes.

The pipe is a standard input and standard output processes between two connecting mechanism, thereby providing a plurality of inter-process communication allows the method, when a process to create a pipe, each

File descriptors are required to operate to provide two conduit. One of the pipeline write operation, read operation of another pipeline. The read and write pipeline with a general system functions IO

Induced using write () function to write data, read () from the read data.

 

https://www.cnblogs.com/kunhu/p/3608109.html

Guess you like

Origin www.cnblogs.com/goya/p/11903838.html