-
修改标准IO时候写的时钟代码,要求输入'q'后,能够退出该程序。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void* callBack(void* arg)
{
char c;
while(1)
{
scanf("%c",&c);
if('q' == c)
exit(0);
}
}
int main(int argc, const char *argv[])
{
pthread_t pid;
if(pthread_create(&pid,NULL,callBack,NULL) !=0)
{
perror("pthread_create");
return -1;
}
time_t t;
while(1)
{
system("clear");
time(&t);
struct tm* info = localtime(&t);
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);
}
return 0;
}
#include <stdio.h> #include <pthread.h> #include <string.h> char str[] = "123456"; int flag = 0; void* callBack(void *arg) { int len = strlen(str); int i,j; char tmp; while(1) { if(flag == 1) { for(i=0,j=len-1;i<j;i++,j--) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } flag = 0; } } } int main(int argc, const char *argv[]) { pthread_t pid; if(pthread_create(&pid,NULL,callBack,NULL) != 0) { perror("pthread_create"); return -1; } while(1) { if(flag == 0) { printf("%s\n",str); flag = 1; } } return 0; }
要求创建两个线程,以及一个全局变量,char str[] = "123456";要求如下:
1)一个线程专门用于打印str;
2)另外一个线程专门用于倒置str字符串,不使用辅助数组。
3)要求打印出来的结果必须是123456或者654321,不能出现乱序情况。