• 《POSIX多线程程序设计》笔记(1)开头的几个闹钟示例


    /*功能:设置一个一次性的定时器。*/
    #include <stdio.h> #include <string.h> //strlen #include <unistd.h> //sleep #include <stdlib.h> //sscanf int main(int argc ,char *argv[]) { int seconds; char line[128]; char message[64]; while (1) { printf("Alarm> "); /*读到文件尾或出错返回NULL*/ if (fgets(line, sizeof(line), stdin) == NULL) exit(0); if (strlen(line) <= 1) continue; /*sscanf: 匹配 之前最多64个字符*/ if (sscanf(line, "%d %64[^ ]", &seconds, message) < 2) { fprintf(stderr, "Bad command "); } else { sleep(seconds); printf("(%d) %s ", seconds, message); } } return 0; }
    #include <stdio.h>
    #include <string.h> //strlen
    #include <unistd.h> //sleep, fork
    #include <stdlib.h> //sscanf
    #include <sys/types.h> //waitpid
    #include <sys/wait.h>  //waitpid
    
    
    int main(int argc ,char *argv[])
    {
        char line[128];
        int seconds;
        pid_t pid;
        char message[64];
    
        while (1) 
        {   
            printf("Alarm> ");
            if (fgets(line, sizeof(line), stdin) == NULL) exit(0);
            if (strlen(line) <= 1) continue;
    
            if (sscanf (line, "%d %64[^
    ]",
                        &seconds, message) < 2)
            {   
                fprintf(stderr, "Bad command
    ");
            }   
            else
            {   
                pid = fork();
                if (pid == -1) exit(0);
                if (pid == 0)
                {   
                    sleep(seconds);
                    printf("(%d) %s
    ", seconds, message);
                    exit(0);
                }   
                else                                                                                                                      
                {   
                    do  
                    {   
                        //waitpid: -1,回收任意子进程, NULL 不保存子进程状态, WNOHANG 不阻塞(挂起)
                        pid = waitpid(-1, NULL, WNOHANG);
                        if (pid == -1) exit(0);
                    }   
                    while (pid != 0); 
                }   
            }   
        }   
        return 0;
    }
    #include <stdio.h>
    #include <string.h> //strlen
    #include <unistd.h> //sleep, fork
    #include <stdlib.h> //sscanf
    //#include <sys/types.h> //waitpid
    //#include <sys/wait.h>  //waitpid
    #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;
        int status;
    
        status = pthread_detach(pthread_self());
        if (status != 0) exit(1);
    
        sleep(alarm->seconds);//YouComplete:
        printf("(%d) %s
    ", alarm->seconds,alarm->message);
        free(alarm);
        return NULL;
    
    }
    int main(int argc ,char *argv[])
    {
        int status;
        char line[128];
        //int seconds;
        //pid_t pid;
        //char message[64];
        pthread_t thread;
        alarm_t *alarm;
    
        while (1) 
        {   
            printf("Alarm> ");
            if (fgets(line, sizeof(line), stdin) == NULL) exit(0);
            if (strlen(line) <= 1) continue;
    
            alarm = (alarm_t*)malloc(sizeof(alarm_t));
            if (alarm == NULL) exit(1);
    
            memset(alarm, 0 ,sizeof(alarm_t));
            if (sscanf (line, "%d %64[^
    ]",
                        &alarm->seconds, alarm->message) < 2)
            {   
                fprintf(stderr, "Bad command
    ");
                free(alarm);
            }   
            else
            {   
                //pthread_create: thread,线程ID; NULL,线程属性; alarm_thread,线程回调; alarm,用户数据
                status = pthread_create(&thread, NULL, alarm_thread, alarm);
                if (status != 0) exit(1);
            }                                                                                                                             
        }   
        return 0;
    }
  • 相关阅读:
    element-ui分页组件修改当前页current-page后current-change事件不触发
    使用element-ui 的table 组件 表格线条不对齐
    vue 禁止浏览器自带的后退功能
    echart 柱图的(层叠柱图和柱图)label只显示总数 ,折线图的legent上显示总数
    element-ui table组件,分别设置表头和表格内容的居中,自定义渲染
    vue-cli-3+element-ui 按需引入element-ui的配置
    今天使用ElementUI与vue-quill-editor的时候发现,富文本编辑框选项对齐出现了点问题。
    vue-router 切换页面的时候,页面没有请求,
    msvcp140.dll缺失解决办法
    CTF中RSA题目的pem文件处理
  • 原文地址:https://www.cnblogs.com/orejia/p/12555581.html
Copyright © 2020-2023  润新知