线程的使用

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

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

char quit[5] = "";

void *Callback(void *arg){
	time_t t;
	struct tm* now_t = NULL;	
	while(1){
		time(&t);
		now_t = localtime(&t);
		fprintf(stderr,"%d-%02d-%02d  %02d:%02d:%02d\r",\
				now_t->tm_year+1900,now_t->tm_mon+1,now_t->tm_mday,\
				now_t->tm_hour,now_t->tm_min,now_t->tm_sec);
		if(strcmp(quit,"quit") == 0){
			pthread_exit(NULL);
		}
	}
}

int main(int argc,const char *argv[])
{
	pthread_t tid;
	if( pthread_create(&tid,NULL,Callback,NULL) != 0){
		printf("pthread_create error    __%d__\n",__LINE__);
		return -1;
	}

	scanf("%s",quit);
	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 7234567.不允许使用sleep函数
 

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

char buff[] = "1234567";
int count = 1;
int num = 1;

void *Callback(void *arg){   //A
	while(1){
		while(count == 1){
			num = 0;
			printf("%s\n",buff);
			num = 1;
		}
	}
	pthread_exit(NULL);
}


int main(int argc,const char *argv[])
{
	pthread_t tid;
	if(pthread_create(&tid,NULL,Callback,NULL) != 0){
		printf("pthread_create failed!    __%d__\n",__LINE__);
		return -1;
	}
	//B
	while(1){
		while(num == 1){
            count = 0;
			for(int i = 0 ; i < (strlen(buff)/2) ; i++){
				char temp = buff[i];
				buff[i] = buff[strlen(buff)-i-1];
				buff[strlen(buff)-i-1]=temp;
			}
            count = 1;
		}
	}
	pthread_join(tid,NULL);
	return 0;
}

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int fd_r,fd_w;
off_t size; 

void *Callback(void *arg){
	lseek(fd_r,0,SEEK_SET);
	lseek(fd_w,0,SEEK_SET);
	char c;
	for(int i=0;i<(size/2);i++){
		read(fd_r,&c,1);
		write(fd_w,&c,1);
	}
	pthread_exit(NULL);
}

int main(int argc,const char *argv[])
{
    if(argc < 2){
    printf("请输入图片名称\n");
    return -1;
    }
	//open a picture
 	fd_r = open(argv[1],O_RDONLY);
	if(fd_r == -1){
		perror("open");
		printf("line : %d\n",__LINE__);
		return -1;
	}
	//create a new picture
 	fd_w = open("cp_picture.bmp",O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(fd_w == -1){
		perror("open");
		printf("line : %d\n",__LINE__);
		return -1;
	}
	//get picture's size
	size =  lseek(fd_r,0,SEEK_END);

	//create a pthread
	pthread_t tid;
	if(pthread_create(&tid,NULL,Callback,NULL) != 0){
		printf("pthread_create error    __%d__\n",__LINE__);
		return -1;
	}
	//branch pthread has exited
	pthread_join(tid,NULL);
	lseek(fd_r,(size/2),SEEK_SET);
	lseek(fd_w,(size/2),SEEK_SET);
	char c;
	for(int i=(size/2);i<size;i++){
		read(fd_r,&c,1);
		write(fd_w,&c,1);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值