• 《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;
    }
  • 相关阅读:
    eclipse 64和32位切换
    把web项目部署到阿里云linux服务器上
    CDN工作过程(第二种版本)
    CDN的基本工作过程
    第五课 JAVA反射获取对象属性和方法(通过配置文件)
    第五课 JAVA反射获取对象属性和方法
    第四课:通过配置文件获取对象(Spring框架中的IOC和DI的底层就是基于这样的机制)
    第三课:JAVA反射机制
    爬虫基本操作、requests和BeautifulSoup
    孤荷凌寒自学python第十八天python变量的作用范围
  • 原文地址:https://www.cnblogs.com/orejia/p/12555581.html
Copyright © 2020-2023  润新知