1.标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
void *Time(void *agr)
{
time_t ts, ots;
struct tm *tm = NULL;
FILE *fp;
if ((fp = fopen("./time.txt", "a+")) == NULL)
{
perror("fopen");
return NULL;
}
ts = ots = 0;
while (1)
{
if ((ts = time(NULL)) == -1)
{
perror("time");
return NULL;
}
if (ts != ots)
{
ots = ts;
if ((tm = localtime(&ts)) == NULL)
{
perror("localtime");
return NULL;
}
fprintf(fp, "%d-%02d-%02d %02d:%02d:%02d\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
fflush(fp);
}
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;// 创建时间线程,成功返回tid号
if (pthread_create(&tid, NULL, Time, NULL) != 0)
{
fprintf(stderr,"函数运行失败\n");
return -1;
}
char buf[8] = "";
while (1)
{
fscanf(stdin, "%s", buf);
if (strcmp("quit",buf) == 0)
{
if (pthread_cancel(tid) == 0)
{
printf("子线程退出成功\n");
break;
}
}
}
printf("主线程退出成功\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 <pthread.h>
#include <time.h>
#include <unistd.h>
char buf[]="1234567";
void *B(void *arg)
{
int i=0,j=strlen(buf)-1;
while(i<j)
{
buf[i]=buf[i]^buf[j];
buf[j]=buf[i]^buf[j];
buf[i]=buf[i]^buf[j];
i++;
j--;
}
pthread_exit(NULL);
}
void *A(void *arg)
{
while(1)
{
pthread_t tidB;
if(pthread_create(&tidB,NULL,B,NULL)!=0)
{
fprintf(stderr,"线程B创建失败\n");
pthread_exit(NULL);
}
fprintf(stderr,"%s\n",buf);
pthread_join(tidB,NULL);
}
}
int main(int argc,const char *argv[])
{
pthread_t tidA;
if(pthread_create(&tidA,NULL,A,NULL)!=0)
{
fprintf(stderr,"线程A创建失败\n");
return -1;
}
pthread_join(tidA,NULL);
return 0;
}
3.要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int openfd1()
{
int fd1 = open("./pic01.png", O_RDONLY);
if (fd1 < 0)
{
perror("open1");
return -1;
}
return fd1;
}
int openfd2()
{
int fd2;
fd2 = open("./copy_pic01.png", O_WRONLY | O_CREAT, 0664);
if (fd2 < 0)
{
perror("open2");
return -1;
}
return fd2;
}
void *A(void *arg)
{
pthread_mutex_lock(&mutex);
int fd1 = openfd1();
int fd2 = openfd2();
off_t size = lseek(fd1, 0, SEEK_END);
lseek(fd1, 0, SEEK_SET);
lseek(fd2, 0, SEEK_SET);
char buf = 0;
for (int i = 0; i < size / 2; i++)
{
if (read(fd1, &buf, 1) < 0)
{
perror("read");
return NULL;
}
if (write(fd2, &buf, 1) < 0)
{
perror("write");
return NULL;
}
}
printf("前半部分拷贝完毕\n");
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
close(fd1);
close(fd2);
return NULL;
}
void *B(void *arg)
{
pthread_mutex_lock(&mutex);
int fd1 = openfd1();
int fd2 = openfd2();
off_t size = lseek(fd1, 0, SEEK_END);
lseek(fd1, size / 2, SEEK_SET);
lseek(fd2, size / 2, SEEK_SET);
char buf = 0;
for (int i = size / 2; i < size; i++)
{
if (read(fd1, &buf, 1) < 0)
{
perror("read");
return NULL;
}
if (write(fd2, &buf, 1) < 0)
{
perror("write");
return NULL;
}
}
printf("后半部分拷贝完毕\n");
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
close(fd1);
close(fd2);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tidA;
if (0 != pthread_create(&tidA, NULL, A, NULL))
{
fprintf(stderr, "A线程创建失败\n");
return -1;
}
pthread_join(tidA, NULL);
pthread_t tidB;
if (0 != pthread_create(&tidB, NULL, B, NULL))
{
fprintf(stderr, "B线程创建失败\n");
return -1;
}
pthread_join(tidB, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}