#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
char str[20]="";
ssize_t a;
ssize_t b;
off_t c;
off_t p;
off_t j;
int fd;
void *ptr(void *fz){
//sleep(3);
fd=open("./copy5.jpg",O_RDWR|O_CREAT|O_APPEND,0777);
if(0==fd){
perror("open");
return 0;
}
c=lseek(*(int*)fz,0,SEEK_END);
lseek(*(int*)fz,0,SEEK_SET);
j=c/2;
while(1){
p=j/sizeof(str)==0?j:sizeof(str);
if((a=read(*(int*)fz,str,sizeof(str)))== -1){
perror("read");
return 0;
}
if(a==0){
break;
}
if((b=write(fd,str,p))== -1){
perror("write");
return 0;
}
if((j -= sizeof(str))<0){
break;
}
}
}
void *sar(void *dy){
sleep(3);
fd=open("./copy5.jpg",O_RDWR|O_CREAT|O_APPEND,0777);
if(0==fd){
perror("open");
return 0;
}
c=lseek(*(int*)dy,0,SEEK_END);
int falg=0;
if(c%2!=0){
falg=1;
}
j=c/2+falg;
printf("%ld\n",j);
lseek(*(int*)dy,j,SEEK_SET);
while(1){
p=j/sizeof(str)==0?j:sizeof(str);
if((a=read(*(int*)dy,str,p))== -1){
perror("read");
return 0;
}
if(a==0){
break;
}
if((b=write(fd,str,p))== -1){
perror("write");
return 0;
}
if((j -= sizeof(str))<0){
break;
}
}
}
int main(int argc, const char *argv[])
{
if(argc<2){
printf("请输入要拷贝的图片名\n");
return 0;
}
int fg=open(argv[1],O_RDONLY);
if(0==fg){
perror("open");
return 0;
}
pthread_t tid;
if(pthread_create(&tid,NULL,ptr,(void*)&fg)!=0){
perror("pthread_create");
return 0;
}
pthread_t aid;
if(pthread_create(&aid,NULL,sar,(void*)&fg)!=0){
perror("pthread_create");
return 0;
}
while(1){
sleep(1);
}
return 0;
}
`作业1 逆置``
在这里插#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
int c=10;
char str[]=“123456”;
int falg=0;
void *ptr(void *fz){
while(1){
sleep(1);
char *start=str;
int len=strlen(str);
char *end=(str+len-1);
char temp=0;
while(start < end){
temp = *start;
*start = *end;
*end=temp;
start++;
end–;
falg=1;
}
}
}
void *sar(void *dy){
while(1){
sleep(1);
if(falg==1)
printf(“%s\n” ,str);
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if(pthread_create(&tid,NULL,ptr,NULL)!=0){
perror(“pthread_create”);
return 0;
}
pthread_t aid;
if(pthread_create(&aid,NULL,sar,NULL)!=0){
perror("pthread_create");
return 0;
}else{
while(1){
sleep(1);
}
}
return 0;
}
入代码片