1.打印时间,要求输入quit后退出,结束进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
void *printTime()
{
time_t t = time(NULL);
time_t ti;
struct tm *info = NULL;
while (1)
{
system("clear");
time(&ti);
info = localtime(&ti);
fprintf(stderr, "\t%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);
// 隔1秒打印一次
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if (pthread_create(&tid, NULL, printTime, NULL) != 0)
{
fprintf(stderr, "pthread create failed __%d__", __LINE__);
}
while (1)
{
char input[20];
scanf("%s", input);
if (strcmp(input, "quit") == 0)
{
exit(0);
}
}
return 0;
}
2.要求定义一个全局变量 char bufl ="1234567",创建两个线程,不考虑退出条件
1.A线程循环打印buf字符串
2.B线程循环倒置bu字符串,即buf中本来存储1234567,倒置后buf中存储7654321.不打印!!
3.倒置不允许使用辅助数组。
4.要求A线程打印出来的结果只能为 1234567 或者 7654321 不许出现7634521 7234567
5.不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
char buf[] = "1234567";
char flagprintf = 0;
char flaginvert = 1;
void *printstr(void *arg)
{
while (1)
{
if (flagprintf)
{
flaginvert = 0;
printf("buf=%s\n", buf);
flaginvert = 1;
}
else
{
continue;
}
}
}
void *invert(void *arg)
{
while (1)
{
if (flaginvert)
{
flaginvert = 0;
int i = 0;
int j = 0;
j = strlen(buf) - 1;
while (i < j)
{
// printf("j=%d", j);
char t = *(buf + i);
*(buf + i) = *(buf + j);
*(buf + j) = t;
i++;
j--;
}
flagprintf = 1;
}
else
{
continue;
}
}
// printf("buf=%s\n",buf);
}
int main(int argc, const char *argv[])
{
pthread_t tidA; // A进程循环打印
pthread_t tidB; // B进程循环倒置
if (pthread_create(&tidA, NULL, printstr, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tidA);
if (pthread_create(&tidB, NULL, invert, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
pthread_detach(tidB);
getchar();
return 0;
}
3.要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int len;
void *copy_t1(void *arg)
{
char buf1;
printf("前半部分正在复制..\n");
FILE *fp_r_1 = fopen("1.png", "r");
FILE *fp_w_1 = fopen("2.png", "w");
if (NULL == fp_r_1 || NULL == fp_w_1)
{
perror("fopen");
return NULL;
}
fseek(fp_r_1, 0, SEEK_END);
int len = ftell(fp_r_1);
fseek(fp_r_1, 0, SEEK_SET);
fseek(fp_w_1, 0, SEEK_SET);
// printf("len=%d", len);
int half_len = (len) / 2;
// printf("half=%d", half_len);
for (int i = 0; i < half_len; i++)
{
buf1 = fgetc(fp_r_1);
fprintf(fp_w_1, "%c", buf1);
}
fclose(fp_r_1);
fclose(fp_w_1);
printf("前半部分复制完毕\n");
pthread_exit(NULL);
}
void *copy_t2(void *arg)
{
char buf2;
printf("后半部分正在复制..\n");
FILE *fp_r_2 = fopen("1.png", "r");
FILE *fp_w_2 = fopen("2.png", "a");
if (NULL == fp_r_2 || NULL == fp_w_2)
{
perror("fopen");
return NULL;
}
fseek(fp_r_2, 0, SEEK_END);
int len = ftell(fp_r_2);
int half_len = (len) / 2;
fseek(fp_r_2, half_len, SEEK_SET);
fseek(fp_w_2, half_len, SEEK_SET);
for (int i = half_len; i < len; i++)
{
buf2 = fgetc(fp_r_2);
fprintf(fp_w_2, "%c", buf2);
}
fclose(fp_r_2);
fclose(fp_w_2);
printf("后半部分复制完毕\n");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tidA; // A进程拷贝前半部分
pthread_t tidB; // B进程拷贝前后部分
if (pthread_create(&tidA, NULL, copy_t1, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
// pthread_detach(tidA);
pthread_join(tidA, NULL);
if (pthread_create(&tidB, NULL, copy_t2, NULL) != 0)
{
fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);
return -1;
}
// pthread_detach(tidB);
pthread_join(tidB, NULL);
getchar();
return 0;
}