IO进程线程day5

 IO进程线程day5_第1张图片

#include 
void *sthread(void *arg);
int main(int argc, const char *argv[])
{
	int fp = open("cpy.jpg",O_WRONLY | O_TRUNC | O_CREAT,0664);
	if(EOF == fp){
		ERR_MSG("fp");
		return -1;
	}
	close(fp);
	int fp1 = open("./xiao.jpg",O_RDONLY);
	if(EOF == fp1){
		ERR_MSG("fp1");
		return -1;
	}
	int cpy = open("cpy.jpg",O_WRONLY);
	if(EOF == cpy){
		ERR_MSG("cpy");
		return -1;
	}
	char s;
	off_t size = lseek(fp1, 0, SEEK_END);
	off_t mid = size/2;
	lseek(fp1, 0, SEEK_SET);
	int i = 0;
	pthread_t pd = 0;
	if(pthread_create(&pd, NULL, sthread, (void *)&mid) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	while(1){
		read(fp1, &s, 1);
		i++;
		write(cpy, &s, 1);
		if(i == mid)
			break;
	}
	close(fp1);
	close(cpy);
    pthread_join(pd, NULL);
	return 0;
}
void *sthread(void *arg)
{
	int fp = open("./xiao.jpg",O_RDONLY);
	if(EOF == fp){
		ERR_MSG("fp");
		pthread_exit(NULL);
	}
	int cpy = open("cpy.jpg",O_WRONLY);
	if(EOF == cpy){
		ERR_MSG("cpy");
		pthread_exit(NULL);
	}
	off_t mid = *(off_t *)arg;
	lseek(fp, mid, SEEK_SET);
	lseek(cpy, mid, SEEK_SET);
	char s[64];
	while(1){
		int res = read(fp, s, sizeof(s));
		if(0 == res)
			break;
		write(cpy, s, res);
	}
	close(fp);
	close(cpy);
	pthread_exit(NULL);
}

IO进程线程day5_第2张图片

#include 
void *spthr(void *arg);
char buf[] = "1234567";
int main(int argc, const char *argv[])
{
	pthread_t pd;
	if(pthread_create(&pd, NULL, spthr, NULL) != 0){
		printf("pthread_create failed\n");
		return -1;
	}
	while(1){
		if(strcmp(buf,"7654321") == 0 || strcmp(buf,"1234567") == 0)
		printf("%s\n",buf);
	}
	return 0;
}
void *spthr(void *arg)
{
	char *p = buf;
	int size = strlen(buf);
	while(1){
		int j = size-1;
		for(int i = 0; i < j; i++){
			char temp = *(p+i);
			*(p+i) = *(p+j);
			*(p+j) = temp;
			j--;
		}
	}
}

你可能感兴趣的:(java,前端,开发语言)