io 线程相关练习

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

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
char buf[32] = "";
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);
        if(0 == strcmp(buf, "quit"))
        {
			printf("__%d__",__LINE__);
            pthread_exit(NULL);
		}
        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");
    }
        scanf("%s", buf);
	printf("__%d__",__LINE__);
    pthread_join(tid, NULL);
    return 0;
}

输出结果:

 

2.要求定义一个全局变量char buf="1234567",创建两个线程,不考虑退出条件。
a.A线程循环打印buf字符串,
b.B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321.不打印! !c.倒   置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为1234567或者7654321不允许出现7634521 7234567e.不允许 使用sleep函数

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
char buf[] = "1234567";
pthread_mutex_t mutex;
void *task1(void *arg)
{
    while(1){
    pthread_mutex_lock(&mutex);
    for(int i=0;i<strlen(buf);i++){
        printf("%c",buf[i]);
    }
    printf("\n");
    pthread_mutex_unlock(&mutex);
    }
}
void *task2(void *arg)
{
    while(1){
    pthread_mutex_lock(&mutex);
    for(int i=strlen(buf);i>=0;i--){
        printf("%c",buf[i]);
    }
    printf("\n");
    pthread_mutex_unlock(&mutex);
    }
}
 
int main(int argc, char const *argv[])
{
	
    pthread_t tid1,tid2;
    if(0!=pthread_mutex_init(&mutex,NULL)){
        perror("mutex");
    }
    if(0 != pthread_create(&tid1, NULL, task1, NULL))
    {
        fprintf(stderr, "func1");
    }
    if(0 != pthread_create(&tid2, NULL, task2, NULL))
    {
        fprintf(stderr, "func1");
    }
       
    pthread_join(tid1, NULL);
    pthread_join(tid1, NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}

输出结果:

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

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


// 1.定义无名信号量
sem_t sem1, sem2;
int size=471798;

//图片前半部分线程
void* task1(void* arg)
{
        // 1.p操作
        sem_wait(&sem1);
        int fd=open("./xuanzi.jpg",O_RDONLY);
        if(fd<0){
            perror("open");
            return 0;
        }
        int fd1=open("./copy.jpg",O_WRONLY|O_CREAT|O_APPEND);
        if(fd1<0){
            perror("open");
            return 0;
        }
        char buf='\0';
        for(int i=0;i<size/2;i++){
            read(fd,&buf,1);
            write(fd1,&buf,1);
        }
        printf("图片前半部分线程拷贝完成\n");

        // 2.v操作
        sem_post(&sem2);
}

//图片后半部分线程
void* task2(void* arg)
{
    
        // 1.p操作
        sem_wait(&sem2);
        int fd=open("./xuanzi.jpg",O_RDONLY);
        if(fd<0){
            perror("open");
            return 0;
        }
        int fd1=open("./copy.jpg",O_WRONLY|O_CREAT|O_APPEND);
        if(fd1<0){
            perror("open");
            return 0;
        }
        lseek(fd,size/2,SEEK_SET);
         lseek(fd1,size/2,SEEK_SET);
        char buf='\0';
        for(int i=size/2;i<size;i++){
            read(fd,&buf,1);
            write(fd1,&buf,1);
        }
        printf("图片后半部分线程拷贝完成\n");
        // 2.v操作
        sem_post(&sem1);
    }
int main(int argc, const char* argv[])
{
    pthread_t tid1, tid2;

    // 2.初始化无名信号量
    sem_init(&sem1, 0, 1);
    sem_init(&sem2, 0, 0);

    if ((errno = pthread_create(&tid1, NULL, task1, NULL)) != 0)
        printf("create thread1 error");
    if ((errno = pthread_create(&tid2, NULL, task2, NULL)) != 0)
        printf("create thread1 error");

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    // 3.销毁无名信号量
    sem_destroy(&sem1);
    sem_destroy(&sem2);

    return 0;
}

输出结果为:

前后图片对比:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值