1.标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
void* callback(void *arg)
{
time_t t;
struct tm* info=NULL;
while(1)
{
time(&t);
info=localtime(&t);
printf("%d/%02d/%02d %02d:%02d:%2d\r ",info->tm_year+1900,info->tm_mon+1,\
info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
fflush(stdout);
sleep(1);
}
pthread_exit(NULL);
}
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[20]={"quit"};
char arr1[20]={0};
while(1)
{
scanf("%s",arr1);
if(strcmp(arr,arr1)==0)
{
printf("已输入%s,将退出\n",arr1);
break;
}
}
printf("man function exit\n");
return 0;
}
2.要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。
A线程循环打印buf字符串,
B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. 不打印!!
倒置不允许使用辅助数组。
要求A线程打印出来的结果只能为 1234567 或者 7654321 不允许出现7634521 7234567
不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
char buf[]="1234567";
void* printf_buf(void *arg)
{
while(1)
{
if(strcmp(buf,"1234567")==0 ||strcmp(buf,"7654321")==0)
printf(" this is A: %s\n",buf);
}
return NULL;
}
void* nizhi_buf(void *arg)
{
while(1)
{
int a=strlen(buf)-1;
int i=0;
while(i<a)
{
char c=buf[i];
buf[i]=buf[a];
buf[a]=c;
i++;a--;
}
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t A;
if(pthread_create( &A, NULL, printf_buf,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_t B;
if(pthread_create( &B, NULL, nizhi_buf,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
while(1)
{
}
printf("man function exit\n");
return 0;
}
3.要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void* copy_first(void *arg)
{
int fd1=open("./01.png",O_RDONLY);
if(fd1<0)
{
perror("fd");
return NULL;
}
int fd2=open("./02.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd2<0)
{
perror("fd2");
return NULL;
}
off_t size=lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET); //光标回到开头
for(int i=1;i<size/2;i++)
{
char c;
read(fd1,&c,1);
write(fd2,&c,1);
}
return NULL;
}
void* copy_last(void *arg)
{
int fd1=open("./01.png",O_RDONLY);
if(fd1<0)
{
perror("fd");
return NULL;
}
int fd2=open("./02.png",O_RDWR);
if(fd2<0)
{
perror("fd2");
return NULL;
}
off_t size=lseek(fd1,0,SEEK_END);
lseek(fd1,size/2-1,SEEK_SET); //光标到达后半部分
lseek(fd2,size/2-1,SEEK_SET);
for(int i=size/2;i<=size;i++)
{
char c;
read(fd1,&c,1);
write(fd2,&c,1);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t A;
if(pthread_create( &A, NULL, copy_first,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(A,NULL); //阻塞主线程,等A拷贝完。
pthread_t B;
if(pthread_create( &B, NULL, copy_last,NULL)!=0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_join(B,NULL); //阻塞主线程,等B拷贝完。
printf("man function exit\n");
return 0;
}