1. 标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
#define ERR_MSG(msg) do\
{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
} while (0);
void print_time()
{
printf("enter print_time............\n");
time_t t;
time_t t1;
struct tm *info = NULL;
while (1)
{
system("clear");
t = time(NULL);
//fprintf(stdout,"%ld\n",t);
//time_t *t1;==>指针指向不明,容易造成野指针
time(&t1); //一级指针一般为普通变量取地址
// fprintf(stdout,"%ld\n",t1);
//将秒转换称日历格式
info = localtime(&t);
if (NULL == info)
{
ERR_MSG("localtime");
return;
}
//stderr==>无缓冲 stdout stdin 行缓冲,\n能刷新行缓冲
// 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);
// \r无法刷新缓冲区,\n能刷新
printf("%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);
fflush(stdout); //强制刷新
sleep(1);
}
printf("exit print_time............\n");
}
void * callback(void *arg)
{
print_time();
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
pthread_t tid;
if (pthread_create(&tid,NULL,callback,NULL) < 0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
char buf[32] = {0};
scanf("%s",buf);
while (1)
{
if (strcmp(buf,"quit")==0)
{
exit(0);
}
}
pthread_join(tid,NULL);
printf("主线程准备退出\n");
return 0;
}
2.
要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。
A线程循环打印buf字符串,
B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. 不打印!!
倒置不允许使用辅助数组。
要求A线程打印出来的结果只能为 1234567 或者 7654321
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
/*
void * callback(void *arg)
{
int i = 0;
while (i < 10)
{
printf("[%d] line: __%d__ \n",i,__LINE__);
sleep(1);
i++;
}
pthread_exit(NULL);
}
*/
char buf[] = "1234567";
void array_reverse(char *buf,int size)
{
int i;
int j;
for (i = 0,j = size - 1; i <=j; i++,j--)
{
char temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
void print_arry(char *buf,int size)
{
printf("数组元素为:");
for (int i = 0; i < size; i++)
{
printf("%c",buf[i]);
}
printf("\n");
}
int flags = 0;
void *callback(void *arg)
{
int size = strlen(buf);
int i =0;
while (i<1000)
{
if(flags ==0)
{
print_arry(buf,size);
}
flags = 1;
//i++;
}
}
void *callback1(void *arg)
{
int size = strlen(buf);
int i = 0;
while (i<1000)
{
if(flags == 1)
{
array_reverse(buf,size);
}
flags = 0;
//sleep(1);
//i++;
}
}
int main(int argc, char const *argv[])
{
/*
//Reverse test.......
int size = strlen(buf);
print_arry(buf,size);
array_reverse(buf,size);
print_arry(buf,size);
*/
//创建两个线程,一个用于打印,一个用于倒置
pthread_t tid,tid1;
if (pthread_create(&tid,NULL,callback,NULL) < 0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_detach(tid); //tid设置分离态,pthread_join不阻塞
if (pthread_create(&tid1,NULL,callback1,NULL) < 0)
{
fprintf(stderr,"pthread_create failed\n");
return -1;
}
pthread_join(tid1,NULL);
return 0;
}