POSIX多线程程序设计(二)

版本1:最基本的同步版本

读取在终端中的输入,参数1为sleep时间,参数2为message

#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <string.h>
int main()
{
    int seconds;
    char line[128];
    char message[64];
    printf("Alarm> ");
    while (1)
    {   
        // 读取终端输入的值 
        if (fgets(line, sizeof(line), stdin) == NULL)
            exit(0);

        if (strlen(line) <= 1)
            continue;

        // 参数1为睡觉时间,参数2为打印的信息,最多64个字符   
        if (sscanf(line, "%d %64[^\n]", &seconds, message) < 2){
            fprintf(stderr, "Bad command\n");
        }   
        else {
            sleep(seconds);
            printf("(%d) %s\n", seconds, message);
        }

        printf("Alarm> ");
    }
    printf("exit main!\n");
    return 0;
}

输出结果

Alarm> 1
Bad command
Alarm> 2
Bad command
Alarm> 2 hello
(2) hello
Alarm>

问题:如果你设置了600 hello,则这600s之内你啥也做不了

进化版本2:多进程版本

#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <string.h>

#include <sys/types.h>
#include <wait.h>
int main()
{
    int seconds;
    char line[128];
    char message[64];

    int status; //新增
    pid_t pid;  //新增

    printf("Alarm> ");
    while (1)
    {       
        if (fgets(line, sizeof(line), stdin) == NULL)
            exit(0);

        if (strlen(line) <= 1)
            continue;

        if (sscanf(line, "%d %64[^\n]", &seconds, message) < 2){
            fprintf(stderr, "Bad command\n");
        }   
        else {
            pid = fork();
            if (pid == (pid_t)-1)
            {
                printf("fork error\n");
                exit(0);
            }

            if (pid == 0)
            {
                // child
                sleep(seconds);
                printf("(%d) %s\n", seconds, message);
                exit(0);
            }
            else
            {
                // parent
                do {
                    // -1 等待任何一个子进程,WNOHANG:父进程不阻塞,立即返回
                    pid = waitpid(-1, NULL, WNOHANG);
                    if (pid == -1) {
                        printf("wait for child error\n");
                        exit(0);
                    }   
                } while (pid != 0);
            }
        }

        printf("Alarm> ");
    }
    printf("exit main!\n");
    return 0;
}

结果

Alarm> 10 aaaaaa
Alarm> 7 uuuu
Alarm> 6 dddd
Alarm> (10) aaaaaa
(7) uuuu
(6) dddd

进化版本3:多线程

#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <string.h>

#include <pthread.h>

typedef struct alarm_tag {
    int seconds;
    char message[64];
} alarm_t;

void* alarm_thread(void* arg)
{
    alarm_t * alarm = (alarm_t*)arg;

    pthread_detach(pthread_self());

    sleep(alarm->seconds);
    printf("(%d) %s\n", alarm->seconds, alarm->message);
    free(alarm);
    return NULL;
}

int main()
{
    char line[128];

    int status; //新增
    pthread_t thread;
    alarm_t * alarm;

    printf("Alarm> ");
    while (1)
    {       
        if (fgets(line, sizeof(line), stdin) == NULL)
            exit(0);

        if (strlen(line) <= 1)
            continue;

        alarm = (alarm_t*)malloc(sizeof(alarm_t));
        if (alarm == NULL)
        {
            printf("Allocate alarm\n");
            exit(0);
        }

        if (sscanf(line, "%d %64[^\n]", &alarm->seconds, &alarm->message) < 2){
            fprintf(stderr, "Bad command\n");
            free(alarm);
        }   
        else {
            status = pthread_create(&thread, NULL, alarm_thread, alarm);
            if (status != 0)
            {
                printf("craete thread fail\n");
                exit(0);
            }
        }

        printf("Alarm> ");
    }
    printf("exit main!\n");
    return 0;
}

结果
Alarm> 10 sdfs
Alarm> 8 dddd
Alarm> 444
Bad command
Alarm> (10) sdfs
(8) dddd

猜你喜欢

转载自blog.csdn.net/u010921682/article/details/79791346