3.2.......


 
char buf[] = "1234567";

void* test_a(void* arg)
{
	int i = 0;
	for(i = 0; buf[i] != '\0'; i++)
	{
		printf("buf = %s\n", buf);
	}
}
 

void* test_b(void* arg)
{
	int i;
	int j = strlen(buf)-1;
	while(1)
	{
		for(i=0;i<=j;i++,j--)
		{
			int temp = ((char*) arg)[i];
			((char*)arg)[i] = ((char*)arg)[j];
			((char*)arg)[j] = temp;
		}
		pthread_exit(NULL);
	}
	return arg;
}
 
int main(int argc, const char *argv[])
{
	
	pthread_t a;
	pthread_create(&a, NULL, test_a, NULL);
	printf("abuf = %s\n", buf);
 
	pthread_t b;
	pthread_create(&bb, NULL, test_b, (void*)buf);
	pthread_join(bb,NULL);
	printf("bbuf = %s\n", buf);
 
	return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
typedef struct open{
    int fd;
    int fd2;
    off_t size;
}FUN;
 
void* func(void* arg){
    FUN* mode=(FUN*)arg;
    lseek(mode->fd,0,SEEK_SET);
    lseek(mode->fd2,0,SEEK_SET);
    char c;
    for(int i=0;i<(mode->size)/2;i++){
        read(mode->fd,&c,1);
        write(mode->fd2,&c,1);
    }
    printf("前半部分拷贝完成\n");
 
    pthread_exit(NULL);
    return NULL;
}
 
int main(int argc, const char *argv[])
{
    FUN mode;
    mode.fd=open("./1.png",O_RDONLY);
    if(mode.fd<0){
        perror("open");
        return -1;
    }
    mode.fd2=open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
    if(mode.fd2<0){
        perror("open_fd2");
        return -1;
    }
    mode.size=lseek(mode.fd,0,SEEK_END);
    pthread_t tid;
    pthread_create(&tid,NULL,func,(void*)&mode);
 
    pthread_join(tid,NULL);
    char c;
                                           
    for(int i=(mode.size)/2;i<mode.size;i++){
        read(mode.fd,&c,1);
        write(mode.fd2,&c,1);
    }
    printf("全部拷贝完成\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值