IO进程线程DAY 8

多线程方式:

fifo1.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void *pthread_r(void *);
void *pthread_w(void *);

pthread_t tid_r;
pthread_t tid_w;

int fd_1_w = 0;
int fd_1_r = 0;

int main(int argc, char const *argv[])
{
    printf("fifo1.c ready.\n");

    // create fifo
    umask(0);
    if(mkfifo("./fifo_1to2", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 1 exist.\n");
        }
    }
    else
    {
        printf("file 1 create success.\n");
    }
    if(mkfifo("./fifo_2to1", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 2 exist.\n");
        }
    }
    else
    {
        printf("file 2 create success.\n");
    }

    // create thread
    if(pthread_create(&tid_r, NULL, pthread_r, NULL) < 0)
    {
        perror("pthread_create");
        printf("__%d__\n", __LINE__);
        return -1;
    }
    if(pthread_create(&tid_w, NULL, pthread_w, NULL) < 0)
    {
        perror("pthread_create");
        printf("__%d__\n", __LINE__);
        return -1;
    }

    // main thread block
    printf("fifo1.c mainthread blocking...\n");
    pthread_join(tid_r, NULL);
    pthread_join(tid_w, NULL);

    printf("fifo1.c exit.\n");
    return 0;
}

void *pthread_w(void *arg)
{
    fd_1_w = open("./fifo_1to2", O_WRONLY);
    printf("fifo1.c ./fifo_1to2 opened.\n");
    char buf[128] = "";
    while(1)
    {
        // printf("This is 1 writing to 2 >>> ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;// \n
        write(fd_1_w, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            pthread_cancel(tid_r);
            printf("fifo1.c : pthread_w informing pthread_r exit.\n");
            break;
        }
        bzero(buf, sizeof(buf));
    }
    printf("fifo1.c : pthread_w is about to exit.\n");
    close(fd_1_w);
    close(fd_1_r);
    pthread_exit(NULL);
}
void *pthread_r(void *arg)
{
    fd_1_r = open("./fifo_2to1", O_RDONLY);
    printf("fifo1.c ./fifo_2to1 opened.\n");
    char buf[128] = "";
    while(1)
    {
        bzero(buf, sizeof(buf));
        read(fd_1_r, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            pthread_cancel(tid_w);
            printf("fifo1.c : pthread_r informing pthread_w exit.\n");
            break;
        }
        printf("2 said >>> %s\n", buf);
    }
    printf("fifo1.c : pthread_r is about to exit.\n");
    close(fd_1_w);
    close(fd_1_r);
    pthread_exit(NULL);
}

fifo2.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void *pthread_r(void *);
void *pthread_w(void *);

pthread_t tid_r;
pthread_t tid_w;

int fd_2_w = 0;
int fd_2_r = 0;

int main(int argc, char const *argv[])
{
    printf("fifo2.c ready.\n");

    // create fifo
    umask(0);
    if(mkfifo("./fifo_1to2", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 1 exist.\n");
        }
    }
    else
    {
        printf("file 1 create success.\n");
    }
    if(mkfifo("./fifo_2to1", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 2 exist.\n");
        }
    }
    else
    {
        printf("file 2 create success.\n");
    }

    // create thread
    if(pthread_create(&tid_r, NULL, pthread_r, NULL) < 0)
    {
        perror("pthread_create");
        printf("__%d__\n", __LINE__);
        return -1;
    }
    if(pthread_create(&tid_w, NULL, pthread_w, NULL) < 0)
    {
        perror("pthread_create");
        printf("__%d__\n", __LINE__);
        return -1;
    }

    // main thread block
    printf("fifo2.c mainthread blocking...\n");
    pthread_join(tid_r, NULL);
    pthread_join(tid_w, NULL);

    printf("fifo2.c exit.\n");
    return 0;
}

void *pthread_w(void *arg)
{
    fd_2_w = open("./fifo_2to1", O_WRONLY);
    printf("fifo2.c ./fifo_2to1 opened.\n");
    char buf[128] = "";
    while(1)
    {
        // printf("This is 2 writing to 1 >>> ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;// \n
        write(fd_2_w, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            pthread_cancel(tid_r);
            printf("fifo2.c : pthread_w informing pthread_r exit.\n");
            break;
        }
        bzero(buf, sizeof(buf));
    }
    printf("fifo2.c : pthread_w is about to exit.\n");
    close(fd_2_w);
    close(fd_2_r);
    pthread_exit(NULL);
}
void *pthread_r(void *arg)
{
    fd_2_r = open("./fifo_1to2", O_RDONLY);
    printf("fifo2.c ./fifo_1to2 opened.\n");
    char buf[128] = "";
    while(1)
    {
        bzero(buf, sizeof(buf));
        read(fd_2_r, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            pthread_cancel(tid_w);
            printf("fifo2.c : pthread_r informing pthread_w exit.\n");
            break;
        }
        printf("2 said >>> %s\n", buf);
    }
    printf("fifo2.c : pthread_r is about to exit.\n");
    close(fd_2_w);
    close(fd_2_r);
    pthread_exit(NULL);
}

IO进程线程DAY 8_第1张图片

多进程方式:

fifo1.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int process_read(void);
int process_write(void);

int main(int argc, char const *argv[])
{
    // create fifo
    umask(0);
    if(mkfifo("./fifo_1to2", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 1 exist.\n");
        }
    }
    else
    {
        printf("file 1 create success.\n");
    }
    if(mkfifo("./fifo_2to1", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 2 exist.\n");
        }
    }
    else
    {
        printf("file 2 create success.\n");
    }

    pid_t pid = fork();
    if(pid < 0)
    {

    } 
    else if(pid > 0) // father
    {
        process_read();
    }
    else // child
    {
        process_write();
    }
    return 0;
}

int process_write(void)
{
    int fd_1_w = open("./fifo_1to2", O_WRONLY);
    printf("fifo1.c ./fifo_1to2 opened.\n");
    char buf[128] = "";
    while(1)
    {
        // printf("This is 2 writing to 1 >>> ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;// \n
        write(fd_1_w, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            // ask process exit
            kill(getppid(), SIGKILL);
            // s = signal(SIGCHLD, callBack_handler);

            // close fd_1_r
            printf("fifo1.c : process_write informing process_read exit.\n");
            break;
        }
        bzero(buf, sizeof(buf));
    }
    printf("fifo1.c : process_write is about to exit.\n");
    close(fd_1_w);
    return 0;
}
int process_read(void)
{
    int fd_1_r = open("./fifo_2to1", O_RDONLY);
    printf("fifo1.c ./fifo_2to1 opened.\n");
    char buf[128] = "";
    while(1)
    {
        bzero(buf, sizeof(buf));
        read(fd_1_r, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            // ask process exit
            // close fd_1_w
            printf("fifo1.c : process_read informing process_write exit.\n");
            break;
        }
        printf("2 said >>> %s\n", buf);
    }
    printf("fifo1.c : process_read is about to exit.\n");
    close(fd_1_r);
    return 0;
}

fifo2.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int process_read(void);
int process_write(void);

int main(int argc, char const *argv[])
{
    // create fifo
    umask(0);
    if(mkfifo("./fifo_1to2", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 1 exist.\n");
        }
    }
    else
    {
        printf("file 1 create success.\n");
    }
    if(mkfifo("./fifo_2to1", 0777) < 0)
    {
        if(EEXIST != errno)
        {
            perror("mkfifo");
            printf("__%d__\n", __LINE__);
            return -1;
        }
        else
        {
            printf("file 2 exist.\n");
        }
    }
    else
    {
        printf("file 2 create success.\n");
    }

    pid_t pid = fork();
    if(pid < 0)
    {

    } 
    else if(pid > 0) // father
    {
        process_read();
    }
    else // child
    {
        process_write();
    }
    return 0;
}

int process_write(void)
{
    int fd_2_w = open("./fifo_2to1", O_WRONLY);
    printf("fifo2.c ./fifo_2to1 opened.\n");
    char buf[128] = "";
    while(1)
    {
        // printf("This is 2 writing to 1 >>> ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;// \n
        write(fd_2_w, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            // ask process exit
            kill(getppid(), SIGKILL);
            // close fd_2_r
            printf("fifo2.c : process_write informing process_read exit.\n");
            break;
        }
        bzero(buf, sizeof(buf));
    }
    printf("fifo2.c : process_write is about to exit.\n");
    close(fd_2_w);
    return 0;
}
int process_read(void)
{
    int fd_2_r = open("./fifo_1to2", O_RDONLY);
    printf("fifo2.c ./fifo_1to2 opened.\n");
    char buf[128] = "";
    while(1)
    {
        bzero(buf, sizeof(buf));
        read(fd_2_r, buf, sizeof(buf));
        if(0 == strcasecmp(buf, "quit"))
        {
            // ask process exit
            // close fd_2_w
            printf("fifo2.c : process_read informing process_write exit.\n");
            break;
        }
        printf("1 said >>> %s\n", buf);
    }
    printf("fifo2.c : process_read is about to exit.\n");
    close(fd_2_r);
    return 0;
}

可优化:信号通知与fifo文件关闭

IO进程线程DAY 8_第2张图片

 测试信号:

#include 
#include 
#include 

void sig_2_handler(int sig)// ctrl+c
{
    printf("caught NO.%d sig.\n", sig);
    return ;
}
void sig_3_handler(int sig)// ctrl+
{
    printf("caught NO.%d sig.\n", sig);
    return ;
}
void sig_20_handler(int sig)// ctrl+z
{
    printf("caught NO.%d sig.\n", sig);
    return ;
}

int main(int argc, char const *argv[])
{
    // 2
    __sighandler_t s2 = signal(2, sig_2_handler);
    // 3
    __sighandler_t s3 = signal(3, sig_3_handler);
    // 20
    __sighandler_t s20 = signal(20, sig_20_handler);

    // signal error omit

    while(1)
    {
        printf("doing nothing...\n");
        sleep(1);
    }
    return 0;
}

IO进程线程DAY 8_第3张图片

你可能感兴趣的:(c语言,开发语言)