day6 线程

 标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程

#include <stdio.h>    
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>

void *Time(void *arg)
{
    time_t ti;
    struct tm *t = NULL;

    while(1)
    { 
        time(&ti);
        fflush(stdout);    
        t = localtime(&ti);
        printf(" %d-%02d-%02d %02d:%02d:%02d\r",\
                t->tm_year+1900,t->tm_mon+1,t->tm_mday,\
                t->tm_hour,t->tm_min,t->tm_sec);
        sleep(1);
    }

}

int main(int argc, const char *argv[])
{
    //创建一个分支线程
    pthread_t pid;
    if(pthread_create(&pid,NULL,Time,NULL) != 0)
    {
        return -1;
    }

    char str[10] = "";
    while(1)
    {
        scanf("%s",str);
        if(0 == strcmp("quit",str))
        {
            exit(0);
        }
    }
        return 0; 
}

要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。

A线程循环打印buf字符串,

B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. 不打印!!

倒置不允许使用辅助数组。

要求A线程打印出来的结果只能为 1234567 或者 7654321 不允许出现7634521 7234567

不允许使用sleep函数

#include <stdio.h>    
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
char buf[] = "1234567";


void *Reverse(void *arg)
{
    int left = 0;
    int right = strlen(buf)-1;
    while(left < right)
    {
        buf[left] = buf[left] ^ buf[right];
        buf[right] = buf[left] ^ buf[right];
        buf[left] = buf[left] ^ buf[right];
        left++;
        right--;
    }
    return NULL;
}

void *Print(void *arg)
{
    pthread_t pid1;
    while(1)
    {
        //创建线程排序
        if(pthread_create(&pid1,NULL,Reverse,NULL) != 0)
        {
            break;
        }
        pthread_join(pid1,NULL);

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

    }
    return NULL;
}

int main(int argc, const char *argv[])
{
    //创建一个分支线程
    pthread_t pid;
    if(pthread_create(&pid,NULL,Print,NULL) != 0)
    {
        return -1;
    }
    pthread_join(pid,NULL);
    return 0; 
}

要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数

#include <stdio.h>    
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void *Cpy2(void *arg)
{
    //打开目标文件,以读的方式
    int fd_r = open("./1.png", O_RDONLY);
    if(fd_r < 0)
    {   
        perror("open");
        return NULL; 
    }   

    //以写的方式打开目标文件
    int fd_w = open("./2.png", O_WRONLY|O_CREAT|O_TRUNC, 0664);
    if(fd_w < 0)                                                                                                                       
    {   
        perror("open");
        return NULL; 
    }
    off_t size = lseek(fd_r,0,SEEK_END);
    char s[1] = "";
    int i = 0;
    lseek(fd_r,0,SEEK_SET);
    while(i < size/2)
    {
        read(fd_r,s,sizeof(s));
        write(fd_w,s,sizeof(s));
        i++;
    }
    close(fd_r);
    close(fd_w);
    return NULL;
}

void *Cpy1(void *arg)
{
    pthread_t pid1;

    //创建线程拷贝一半
    if(pthread_create(&pid1,NULL,Cpy2,NULL) != 0)
    {
        return NULL;
    }
    pthread_join(pid1,NULL);
    //打开目标文件,以读的方式
    int fd_r = open("./1.png", O_RDONLY);
    if(fd_r < 0)
    {   
        perror("open");
        return NULL; 
    }   

    //以写的方式打开目标文件
    int fd_w = open("./2.png", O_WRONLY|O_APPEND, 0664);
    if(fd_w < 0)                                                                                                                       
    {   
        perror("open");
        return NULL; 
    }   
    off_t size = lseek(fd_r,0,SEEK_END);
    char s[1] = "";
    lseek(fd_r,size/2,SEEK_SET);
    lseek(fd_w,size/2,SEEK_SET);
    while(read(fd_r,s,sizeof(s)) != 0)
    {
        write(fd_w,s,sizeof(s));
    }
    close(fd_r);
    close(fd_w);
    printf("11111111\n");
    return NULL;
}

int main(int argc, const char *argv[])
{
    //创建一个分支线程
    pthread_t pid;
    if(pthread_create(&pid,NULL,Cpy1,NULL) != 0)
    {
        return -1;
    }
    pthread_join(pid,NULL);
    return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值