IO函数与进程线程的练习

文章展示了三个多线程编程的示例。第一个程序创建一个线程持续打印当前时间,直到用户输入exit。第二个程序定义了一个全局字符串,两个线程分别执行打印和倒置操作,要求打印结果只能是原始或倒置后的字符串。第三个程序使用两个线程分别拷贝图片的前半部分和后半部分,不使用sleep函数来同步。
摘要由CSDN通过智能技术生成

1.标准IO函数得到当前时间并在终端循环打印当前时间,在终端输入exit后,结束进程

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

#define ERR_MSG(msg) {\
	fprintf(stderr,"line: %d\n",__LINE__);\
	perror(msg);\
}

void *timefun(void *type){
	time_t timedata;
	struct tm *timelist;
	while(1){
		time(&timedata);
		timelist = localtime(&timedata);
		fprintf(stdout,"%4d-%02d-%02d %02d:%02d:%02d\n",\
				timelist->tm_year+1900,timelist->tm_mon+1,\
				timelist->tm_mday,timelist->tm_hour,\
				timelist->tm_min,timelist->tm_sec);
		sleep(1);
	}
	return NULL;
}

int main(int argc, const char *argv[])
{
	pthread_t pressid;
	if(pthread_create(&pressid, NULL, timefun, NULL) != 0){
		fprintf(stderr,"pthread_create is failed line: %d \n",__LINE__);
		return -1;
	}
	char str[10] = "\0";
	while(1){
		scanf("%s",str);
		getchar();
		if(strcmp(str,"exit") == 0){
			break;
		}
	}

	return 0;
}

结果如下:       

 

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

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

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

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

        >要求A线程打印出来的结果只能为1234567 或 7654321

        >不允许使用sleep函数

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

#define ERR_MSG(msg) {\
	fprintf(stderr,"line: %d\n",__LINE__);\
	perror(msg);\
}

char buf[] = "1234567";

void *pthreadBfun(void *dataB){
	int len = strlen(buf);
	int left = 0;
	int right = len - 1;
	while(left < right){
		char mid = buf[left];
		buf[left] = buf[right];
		buf[right] = mid;
		left ++;
		right --;
	}
	pthread_exit(NULL);
}

void *pthreadAfun(void *dataA){ 
	while(1){
		pthread_t pthreadidB;
		if(pthread_create(&pthreadidB, NULL, pthreadBfun, NULL) != 0){
			fprintf(stderr,"pthreadB is failed line: %d \n",__LINE__);
			pthread_exit(NULL);	
		}
		printf("A : %s\n",buf);
		pthread_join(pthreadidB,NULL);
	}
}

int main(int argc, const char *argv[])
{
	pthread_t pthreadidA;
	if(pthread_create(&pthreadidA, NULL, pthreadAfun, NULL) != 0){
		fprintf(stderr,"pthreadA is failed line: %d \n",__LINE__);
		return -1;
	}
	pthread_join(pthreadidA,NULL);
	return 0;
}

结果如下:

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

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

#define ERR_MSG(msg) {\
	fprintf(stderr,"line: %d\n",__LINE__);\
	perror(msg);\
}

void *pthreadBfun(void *dataB){
	int picidB = open((char *)dataB,O_RDONLY);
	if(picidB < 0){
		ERR_MSG("openB");
		pthread_exit(NULL);
	}
	int picidcpyB = open("./4.png",O_WRONLY|O_CREAT|O_APPEND,0664);
	if(picidcpyB < 0){
		ERR_MSG("opencpyB");
		pthread_exit(NULL);
	}
	off_t size = lseek(picidB,0,SEEK_END);
	off_t i = size/2;
	lseek(picidB,i,SEEK_SET);
	char datapicB[2] = "\0";
	while(i < size){
		bzero(datapicB,sizeof(datapicB));
		ssize_t len = read(picidB,datapicB,1);
		if(len == 0){
			break;
		}
		else if(len < 0){
			ERR_MSG("read");
			pthread_exit(NULL);
		}
		write(picidcpyB,datapicB,len);
		i++;
	}
	pthread_exit(NULL);
}

void *pthreadAfun(void *dataA){
	int picid = open((char *)dataA,O_RDONLY);
	if(picid < 0){
		ERR_MSG("open");
		pthread_exit(NULL);
	}
	int picidcpy = open("./4.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(picidcpy < 0){
		ERR_MSG("opencpy");
		pthread_exit(NULL);
	}
	off_t size = lseek(picid,0,SEEK_END);
	off_t i = 0;
	char datapic[2] = "\0";
	lseek(picidcpy,0,SEEK_SET);
	lseek(picid,0,SEEK_SET);
	while(i < size/2){
		bzero(datapic,sizeof(datapic));
		ssize_t len = read(picid,datapic,1);
		if(len == 0){
			break;
		}
		else if(len < 0){
			ERR_MSG("read");
			pthread_exit(NULL);
		}
		write(picidcpy,datapic,len);
		i++;
	}

	pthread_t pthreadidB;
	if(pthread_create(&pthreadidB,NULL,pthreadBfun,(char *)dataA) != 0){
		fprintf(stderr,"pthreadBfun is failed line: %d\n",__LINE__);
		pthread_exit(NULL);
	}
	pthread_join(pthreadidB,NULL);

	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	pthread_t pthreadidA;
	char lujin[20];
	strcpy(lujin,argv[1]);
	if(pthread_create(&pthreadidA,NULL,pthreadAfun,lujin) != 0){
		fprintf(stderr,"pthreadAfun is failed line: %d\n",__LINE__);
		return -1;
	}
	pthread_join(pthreadidA,NULL);
	return 0;
}

结果如下:

        我在终端自己输入想要拷贝的路径,然后拷贝到当前路径,重命名为4.png

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值