IO进程线程DAY 6

作业1:

#include 
#include 
#include 
#include 
#include 

void *Clock(void *arg)
{
    time_t sec = 0;
    struct tm *pst = NULL;
    while(1)
    {
        sec = time(NULL);
        pst = localtime(&sec);
        printf("%d/%d/%d %d:%d:%d\r", 
        pst->tm_year+1900, pst->tm_mon+1, pst->tm_mday,
        pst->tm_hour, pst->tm_min, pst->tm_sec);
        fflush(stdout);
        sleep(1);
    }
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t tid = 0;
    if(0 != pthread_create(&tid, NULL, Clock, NULL))
    {
        fprintf(stderr, "func1");
    }
    char buf[32] = "";
    while(1)
    {
        scanf("%s", buf);
        if(0 == strcmp(buf, "quit"))
        {
            break;
        }
    }
    pthread_cancel(tid);
    pthread_join(tid, NULL);
    return 0;
}

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

作业2:(mutex版本)

#include 
#include // sleep
#include 
#include 
#include 

// 临界资源
char str[] = "1234567";
// 互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *pthread_func1(void *);
void *pthread_func2(void *);

int main(int argc, char const *argv[])
{
	// pthread_mutex_init(&mutex, NULL);

    // job 1
    pthread_t tid_func1 = 0;
    if(0 != pthread_create(&tid_func1, NULL, pthread_func1, NULL))
    {
        return -1;
    }
    else
    {
        printf("creat %lu\n", tid_func1);
        pthread_detach(tid_func1);
    }
    // job 2
    pthread_t tid_func2 = 0;
    if(0 != pthread_create(&tid_func2, NULL, pthread_func2, NULL))
    {
        return -1;
    }
    else
    {
        printf("creat %lu\n", tid_func2);
        pthread_detach(tid_func2);
    }
    // keep all threads alive
    while (1)
    {
        sleep(1);
    }
    return 0;
}

void *pthread_func1(void *arg)
{
    // entry
    printf("pthread_func1 start running...\n");
    while(1)
    {
        pthread_mutex_lock(&mutex);

        printf("%s\n", str);

        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
void *pthread_func2(void *arg)
{
    // entry
    printf("pthread_func2 start running...\n");
    while(1)
    {
        pthread_mutex_lock(&mutex);

        for(int i = 0; i < strlen(str)/2; i++)
        {
            str[i] = str[i] + str[strlen(str)-1-i];
            str[strlen(str)-1-i] = str[i] - str[strlen(str)-1-i];
            str[i] = str[i] - str[strlen(str)-1-i];
        }

        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

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

作业3:

#include 
#include 
#include 
#include 


void *PrevHalf(void *arg)
{
    printf("first half started by %lu\n", pthread_self());
    FILE *stream_r = fopen("./src.jpg", "r");
    FILE *stream_w = fopen("./des.jpg", "w");
    fseek(stream_r, 0, SEEK_END);
    long size = ftell(stream_r);
    rewind(stream_r);
    char buf = '\0';
    for(int i = 0; i < size/2; i++)
    {
        fread(&buf, 1, 1, stream_r);
        fwrite(&buf, 1, 1, stream_w);
        // fflush
    }
    fclose(stream_r);
    fclose(stream_w);
    printf("PrevHalf exit\n");
    pthread_exit(NULL);
}

void *PostHalf(void *arg)
{
    pthread_join(*(pthread_t *)arg, NULL);
    printf("last half started after %lu\n", *(pthread_t *)arg);
    int fd_src = open("./src.jpg", O_RDONLY);
    int fd_des = open("./des.jpg", O_WRONLY);
    int size = lseek(fd_src, 0, SEEK_END);
    lseek(fd_src, size/2, SEEK_SET);
    lseek(fd_des, size/2, SEEK_SET);
    char cBuf = '\0';
    for(int i = size/2; i < size; i++)
    {
        read(fd_src, &cBuf, 1);
        write(fd_des, &cBuf, 1);
        // fflush
    }
    close(fd_src);
    close(fd_des);
    printf("PostHalf exit\n");
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pthread_t tid_1 = 0;
    if(0 != pthread_create(&tid_1, NULL, PrevHalf, NULL))
    {
        fprintf(stderr, "...");
    }
    else
    {
        fprintf(stderr, "PrevHalf created.\n");
    }
    pthread_t tid_2 = 0;
    if(0 != pthread_create(&tid_2, NULL, PostHalf, &tid_1))
    {
        fprintf(stderr, "...");
    }
    else
    {
        fprintf(stderr, "PostHalf created.\n");
    }

    // pthread_join(tid_1, NULL);
    pthread_join(tid_2, NULL);

    return 0;
}

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

(mutex版本): 

#include 
#include 
#include // off_t
#include 

typedef struct msg
{
    int fd_r;
    int fd_w;
    off_t size;
    int flag;
}Msg;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *callback(void *arg)// void *arg = &info
{
    pthread_mutex_lock(&mutex);

    int fd_r = ((Msg *)arg)->fd_r;
    int fd_w = ((Msg *)arg)->fd_w;
    off_t size = ((Msg *)arg)->size;
    int flag = ((Msg *)arg)->flag;
    char buf = 0;
    for(int i = 0; i < size/2+flag; i++)
    {
        read(fd_r, &buf, 1);
        write(fd_w, &buf, 1);
        // fflush?
    }
    if(0 == ((Msg *)arg)->flag)
    {
        flag = size%2;
    }
    pthread_mutex_unlock(&mutex);

    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    int fd_r = open("./src.jpg", O_RDONLY);umask(0);
    int fd_w = open("./des.jpg", O_WRONLY|O_TRUNC|O_CREAT, 0766);
    off_t size = lseek(fd_r, 0, SEEK_END);lseek(fd_r, 0, SEEK_SET);
    int flag = 0;
    Msg info = {fd_r, fd_w, size, flag};

    pthread_t tid_1 = 0;
    if(0 != pthread_create(&tid_1, NULL, callback, &info))
    {
        // omit
        return -1;
    }
    pthread_t tid_2 = 0;
    if(0 != pthread_create(&tid_2, NULL, callback, &info))
    {
        // omit
        return -1;
    }

    pthread_join(tid_1, NULL);
    pthread_join(tid_2, NULL);
    
    close(fd_r);
    close(fd_w);

    pthread_mutex_destroy(&mutex);
    return 0;
}

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