1.时钟代码运行后,要求输入quit字符后,结束进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
void* callback(void* arg) //void* arg=NULL
{
time_t ti;
struct tm* info=NULL;
while(1){
time(&ti);
info=localtime(&ti);
fprintf(stderr,"%d-%02d-%02d %02d:%02d:%02d\r",\
info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
info->tm_hour,info->tm_min,info->tm_sec);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
//创建一个分支线程用于循环
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
char arr[5]="quit";
char quit[10]="";
while(1){
scanf("%s",quit);
if(0==strcmp(quit,arr)){
break;
}
}
pthread_join(tid, NULL);
return 0;
}
2.要求定义一个全局变量char buf[] = "1234567",创建两个线程,不考虑退出条件
a. A线程循环打印buf字符串
b. B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321 不打印!
c.倒置不允许使用辅助数组
d.要求A线程打印出来的结果只能为1234567或者7654321,不允许出现7634521 7234567
e.不允许使用sleep函数
方法一:打印结束逆置 逆置结束打印
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
char buf[]="1234567";
int reverse=0;//用于表示逆置的开始和结束 置0
int print=1;//用于表示打印的开始和结束 置1
//逆置分支线程
void* callback(void* arg)
{
while(1){
while(1==print){
int i=0,j=strlen(buf)-1;
reverse = 0;
while(i<j){
char t=buf[i];
buf[i]=buf[j];
buf[j]=t;
i++;
j--;
}
reverse = 1;
}
}
}
int main(int argc, const char *argv[])
{
//创建一个分支线程用于逆置
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
while(1){
while(1==reverse){
print=0;
printf("%s\n",buf);
print=1;
}
}
return 0;
}
方法二:互斥锁
#include <stdio.h>
#include <string.h>
#include <pthread.h>
char buf[]="1234567";
//互斥锁初始化
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
//打印分支线程
void* callback_print(void* arg)
{
while(1){
pthread_mutex_lock(&mutex);//上锁
printf("%s\n",buf);
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
//逆置分支线程
void* callback_reverse(void* arg)
{
while(1){
pthread_mutex_lock(&mutex);//上锁
int i=0,j=strlen(buf)-1;
while(i<j){
char t=buf[i];
buf[i]=buf[j];
buf[j]=t;
i++;
j--;
}
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建分支线程用于打印和逆置
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback_print,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callback_reverse,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);//销毁互斥锁
return 0;
}
3.要求用两个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int fd1;//原图
int fd2;//复制图
ssize_t offset;//偏移量
ssize_t res=0;//read返回值
char buf='0';//存储读取的字符
//互斥锁初始化
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
//A线程
void* callback_copyA(void* arg)
{
pthread_mutex_lock(&mutex);//上锁
lseek(fd1,0,SEEK_SET);
lseek(fd2,0,SEEK_SET);
while(offset/2!=lseek(fd1,0,SEEK_CUR)){
res=read(fd1,&buf,sizeof(buf));
write(fd2,&buf,res);
memset(&buf,0,sizeof(buf));
}
pthread_mutex_unlock(&mutex);//解锁
pthread_exit(NULL);
}
//B线程
void* callback_copyB(void* arg)
{
pthread_mutex_lock(&mutex);//上锁
lseek(fd1,offset/2,SEEK_SET);
lseek(fd2,offset/2,SEEK_SET);
while(1){
res=read(fd1,&buf,sizeof(buf));
if(0==res){
printf("copy success\n");
break;
}
write(fd2,&buf,res);
memset(&buf,0,sizeof(buf));
}
pthread_mutex_unlock(&mutex);//解锁
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//打开图片
fd1=open("./02_picture.png",O_RDONLY);
fd2=open("./fcopy.png",O_RDWR|O_CREAT|O_TRUNC,0777);
offset=lseek(fd1,0,SEEK_END);
//创建分支线程A和B
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,callback_copyA,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
if(pthread_create(&tid2,NULL,callback_copyB,NULL)!=0){
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(tid1,NULL);//回收
pthread_join(tid2,NULL);
pthread_mutex_destroy(&mutex);//销毁互斥锁
close(fd1);
close(fd2);
return 0;
}