用线程完成图片拷贝,要求一个线程拷贝一半,另一个线程拷贝另一半。
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
pthread_t fz1,fz2;
int d;
int x;
off_t dx;
char cc;
ssize_t ds;
pthread_mutex_t hcs = PTHREAD_MUTEX_INITIALIZER;
void* aaa(void* ab){
pthread_mutex_lock(&hcs);
lseek(d,dx/2,SEEK_SET);
lseek(x,dx/2,SEEK_SET);
for(int i=0;i<dx/2;i++){
read(d,&cc,1);
write(x,&cc,1);
}
printf("后半段完毕\n");
pthread_mutex_unlock(&hcs);
pthread_exit(NULL);
}
void* bbb(void* ab){
pthread_mutex_lock(&hcs);
lseek(d,0,SEEK_SET);
lseek(x,0,SEEK_SET);
for(int i=0;i<dx/2;i++){
read(d,&cc,1);
write(x,&cc,1);
}
printf("前半段完毕\n");
pthread_mutex_unlock(&hcs);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
struct stat buf;
stat("./1.png",&buf);
dx=buf.st_size;
d=open("./1.png",O_RDONLY);
x=open("./cp.png",O_WRONLY | O_CREAT | O_TRUNC,0777);
pthread_create(&fz1,NULL,aaa,NULL);
pthread_create(&fz2,NULL,bbb,NULL);
pthread_join(fz1,NULL);
pthread_join(fz2,NULL);
pthread_cancel(fz1);
pthread_cancel(fz2);
close(d);
close(x);
return 0;
}
创建编号为ABC三个线程,三个线程循环打印自己的编号,要求打印出来的结果必须是ABC;
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
char buf[]="1234567";
pthread_cond_t tjbl1,tjbl2,tjbl3;
pthread_mutex_t hcs;
int tj=0;
void* a(void* arg){
while(1){
pthread_mutex_lock((pthread_mutex_t*)arg);
if(tj!=0){
pthread_cond_wait(&tjbl1,&hcs);
}
printf("A");
tj=1;//修改访问时机
pthread_cond_signal(&tjbl2);
pthread_mutex_unlock((pthread_mutex_t*)arg);
}
pthread_exit(NULL);
return NULL;
}
void* b(void* arg){
while(1){
pthread_mutex_lock((pthread_mutex_t*)arg);
if(tj!=1){
pthread_cond_wait(&tjbl2,&hcs);
}
printf("B");
tj=2;
pthread_cond_signal(&tjbl3);
pthread_mutex_unlock((pthread_mutex_t*)arg);
}
pthread_exit(NULL);
}
void* c(void* arg){
while(1){
pthread_mutex_lock((pthread_mutex_t*)arg);
if(tj!=2){
pthread_cond_wait(&tjbl3,&hcs);
}
printf("C\n");
tj=0;
pthread_cond_signal(&tjbl1);
pthread_mutex_unlock((pthread_mutex_t*)arg);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建一个线程
pthread_t tid,tid2;
//创建互斥锁
pthread_mutex_init(&hcs,NULL);
//创建条件变量
pthread_cond_init(&tjbl1,NULL);
pthread_cond_init(&tjbl2,NULL);
pthread_cond_init(&tjbl3,NULL);
if(pthread_create(&tid,NULL,a,(void*)&hcs)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid,NULL,b,(void*)&hcs)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,c,(void*)&hcs)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid,NULL);
return 0;
}
无名管道
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
int pfd[2];
if(pipe(pfd)<0){
perror("pipe");
return -1;
}
pid_t pid=fork();
if(pid>0){//父进程为真
char buf[128];
while(1){
scanf("%s",buf);
getchar();
if(write(pfd[1],buf,sizeof(buf))<0){
perror("write");
return-1;
}
printf("写入\n");
break;
}
wait(NULL);
}else if(0==pid){
char buf[128]="";
ssize_t res;
while(1){
res=read(pfd[0],buf,sizeof(buf));
if(res<0){
perror("read");
return-1;
}
printf("res=%ld buf=%s\n",res,buf);
if(strcmp(buf,"quit")==0){
break;
}
}
}else{
perror("fork");
return-1;
}
close(pfd[0]);
close(pfd[1]);
return 0;
}