练习
1.标准lO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
代码段:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
void* callback(void *arg)
{
while(1)
{
time_t t1;
struct tm *info = NULL;
time(&t1);
info = localtime(&t1);
printf("%4d-%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);
fflush(stdout);
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if(pthread_create(&tid,NULL,callback,NULL) != 0)
{
fprintf(stderr,"pthread_creat failed __%d__\n",__LINE__);
return -1;
}
char str[20]="";
scanf("%s",str);
while(1)
{
if(0 == strcmp(str,"quit"))
{
break;
}
}
return 0;
}
输出结果:
输入quit直接结束
2.要求定义一个全局变量char buf="1234567",创建两个线程,不考虑退出条件。
a.A线程循环打印buf字符串,
b.B线程循环倒置buf字符串,即buf种本来存储1234567,倒置后buf仲存储7654321.不打印!!c.倒置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为1234567或者7654321e.不允许使用sleep函数
代码段
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
char buf[]="1234567";
void* callback1(void *arg)
{
char t = 0;
int n = strlen(buf)-1;
for(int i = 0;i<n/2;i++)
{
t = buf[i];
buf[i] = buf[n-i];
buf[n-i] = t;
}
pthread_exit(NULL);
}
void* callback(void *arg)
{
while(1)
{
pthread_t tid1;
if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return NULL;
}
pthread_join(tid1,NULL);
printf("buf = %s\n",buf);
}
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;
}
pthread_join(tid,NULL);
return 0;
}
输出结果
3.要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
代码段
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
void* callback1(void* arg)
{
int fd = open("./1.png",O_RDONLY);
struct stat buf;
if(stat("./1.png",&buf)<0)
{
perror("stat");
return NULL;
}
long int a=buf.st_size;
int fb = open("./2.png",O_WRONLY|O_CREAT,0777);
if(fd < 0)
{
perror("open");
return NULL;
}
if(fb < 0)
{
perror("open");
return NULL;
}
lseek(fd,a/2,SEEK_SET);
lseek(fb,a/2,SEEK_SET);
char c = 0;
if(a%2==1)
{
for(int i=0;i<a/2+1;i++)
{
read(fd,&c,1);
write(fb,&c,1);
}
}
else
{
for(int i=0;i<a/2;i++)
{
read(fd,&c,1);
write(fb,&c,1);
}
}
pthread_exit(NULL);
}
void* callback(void* arg)
{
int fd = open("./1.png",O_RDONLY);
struct stat buf;
if(stat("./1.png",&buf)<0)
{
perror("stat");
return NULL;
}
long int a=buf.st_size;
int fb = open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
if(fd < 0)
{
perror("open");
return NULL;
}
if(fb < 0)
{
perror("open");
return NULL;
}
lseek(fd,0,SEEK_SET);
lseek(fb,0,SEEK_SET);
char c = 0;
for(int i=0;i<a/2;i++)
{
read(fd,&c,1);
write(fb,&c,1);
}
pthread_t tid1;
if(pthread_create(&tid1,NULL,callback1,NULL) != 0)
{
fprintf(stderr,"pthread_create failed __%d__",__LINE__);
return NULL;
}
pthread_join(tid1,NULL);
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__",__LINE__);
return -1;
}
pthread_join(tid,NULL);
return 0;
}
输出结果: