《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;
}
原文地址:https://www.cnblogs.com/orejia/p/12555581.html