用线程完成图片拷贝,要求一个线程拷贝一半,另一个线程拷贝另一半。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
struct msg{
int fp;
int fw;
off_t size;
pthread_mutex_t *mutex;
};
void* A(void* arg)
{
{
int fp =((struct msg*)arg)->fp;
int fw =((struct msg*)arg)->fw;
off_t size =((struct msg*)arg)->size;
pthread_mutex_t *mutex=((struct msg*)arg)->mutex;
pthread_mutex_lock(mutex);
char c;
lseek(fp,0,SEEK_SET);
lseek(fw,0,SEEK_SET);
for(int i=0;i<size/2;i++)
{
read(fp,&c,1);
write(fw,&c,1);
}
printf("前半部分拷贝完毕\n");
// close(fp);
// close(fw);
pthread_mutex_unlock(mutex);
}
// pthread_exit(NULL);
return NULL;
}
void* B(void* arg)
{
{
int fp =((struct msg*)arg)->fp;
int fw =((struct msg*)arg)->fw;
off_t size =((struct msg*)arg)->size;
pthread_mutex_t *mutex=((struct msg*)arg)->mutex;
pthread_mutex_lock(mutex);
char c;
lseek(fp,size/2,SEEK_SET);
lseek(fw,size/2,SEEK_SET);
for(int i=size/2;i<size;i++)
{
read(fp,&c,1);
write(fw,&c,1);
}
printf("后半部分拷贝完毕\n");
pthread_mutex_unlock(mutex);
}
// pthread_exit(NULL);
return NULL;
}
int main(int argc, const char *argv[])
{
int fp=open("./1.png",O_RDONLY);
if(fp<0)
{
perror("open");
return -1;
}
int fw=open("./3.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
if(fw<0)
{
perror("open");
return -1;
}
off_t size=lseek(fp,0,SEEK_END);
pthread_t tid,pid;
pthread_mutex_t mutex;
pthread_mutex_init(&mutex,NULL);
struct msg file;
file.fp=fp;
file.fw=fw;
file.size=size;
file.mutex= &mutex;
if(pthread_create(&tid,NULL,A,(void*)&file)!=0)
{
printf("failed %d\n",__LINE__);
return -1;
}
if(pthread_create(&pid,NULL,B,(void*)&file)!=0)
{
printf("failed %d\n",__LINE__);
return -1;
}
pthread_join(tid,NULL);
pthread_join(pid,NULL);
pthread_mutex_destroy(&mutex);
close(fp);
close(fw);
return 0;
}
在循环打印倒置那题的条件基础上用信号量实现,打印一次,倒置一次。
提示:用两个信号量,一个初始化为1,另外一个初始化为0
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <semaphore.h>
char buf[]="1234567";
void* A(void* arg)
{
sem_t *sem=(sem_t*)arg;
while(1)
{
if(sem_wait(&sem[0])<0)
{
perror("sem_wait");
pthread_exit(NULL);
}
printf("%s\n",buf);
sleep(1);
if(sem_post(&sem[1])<0)
{
perror("sem_post");
pthread_exit(NULL);
}
}
return NULL;
}
void* B(void* arg)
{
sem_t *sem=(sem_t*)arg;
while(1)
{
if(sem_wait(&sem[1])<0)
{
perror("sem_wait");
pthread_exit(NULL);
}
int i,j;
char c;
for(i=0,j=strlen(buf)-1;i<j;i++,j--)
{
c=buf[i];
buf[i]=buf[j];
buf[j]=c;
}
sleep(1);
if(sem_post(&sem[0])<0)
{
perror("sem_post");
pthread_exit(NULL);
}
}
return NULL;
}
int main(int argc, const char *argv[])
{
sem_t sem[2];
if(sem_init(&sem[1],0,1)<0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem[0],0,0)<0)
{
perror("sem_init");
return -1;
}
pthread_t tid;
if(pthread_create(&tid,NULL,A,sem)!=0)
{
printf("pthread_create failed %d\n",__LINE__);
return -1;
}
pthread_t pid;
if(pthread_create(&pid,NULL,B,sem)!=0)
{
printf("pthread_create failed %d\n",__LINE__);
return -1;
}
pthread_join(tid,NULL);
pthread_join(pid,NULL);
sem_destroy(&sem[0]);
sem_destroy(&sem[1]);
return 0;
}