1、图片拷贝
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct xccpy
{
int num;
off_t size;
int fd_r;
int fd_w;
}cpy;
void* hello(void*arg)
{
char c;
for(((cpy*)arg)->num=0;((cpy*)arg)->num<(((cpy*)arg)->size)/2;(((cpy*)arg)->num)++)
{
read(((cpy*)arg)->fd_r,&c,1);
write(((cpy*)arg)->fd_w,&c,1);
}
}
int main( int argc, const char *argv[])
{
struct xccpy xc;
xc.num=0;
xc.size=lseek(xc.fd_r,0,SEEK_END);
xc.fd_r=open("./1.jpg",O_RDONLY);
if(xc.fd_r<0)
{
perror("open");
return -1;
}
xc.fd_w=open("./2.jpg",O_WRONLY|O_CREAT,0664);
if(xc.fd_w<0)
{
perror("open");
return -1;
}
pthread_t tid;
pthread_create(&tid,NULL,hello,(void*)&xc);
int i=0;
char c;
while(xc.num>=(xc.size)/2)
{
for(i=(xc.size)/2;i<xc.size;i++)
{
read(xc.fd_r,&c,1);
write(xc.fd_w,&c,1);
(xc.num)++;
}
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
char buf[]="1234567";
void *fz(void*arg)
{
int i=0;
int j=6;
for(;i<j;i++,j--)
{
((char*)arg)[i]=((char*)arg)[i]^((char*)arg)[j];
((char*)arg)[j]=((char*)arg)[i]^((char*)arg)[j];
((char*)arg)[i]=((char*)arg)[i]^((char*)arg)[j];
}
}
int main( int argc, const char *argv[])
{
pthread_t tid;
pthread_create(&tid,NULL,fz,(void*)&buf);
int i=0;
while(1)
{
printf("%s\n",buf);
}
return 0;
}