1.标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
void *callBack(void* arg)
{
time_t t;
while(1)
{
//printf("exit q");
t = time(NULL);
struct tm* insert = localtime(&t);
printf("%d-%2d-%2d-%2d:%2d:%2d\r",\
insert->tm_year+1900,insert->tm_mon+1,insert->tm_mday,\
insert->tm_hour,insert->tm_min,insert->tm_sec);
fflush(stdout);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
char q[10] = "";
pthread_t tid;
if(pthread_create(&tid,NULL,callBack,NULL)!=0)
{
printf("pthread_create failed\n");
return -1;
}
while(1)
{
scanf("%s",q);
if(!strcmp(q,"quit"))
exit(0);
}
return 0;
}
2,
#include<stdio.h>
#include <pthread.h>
#include <unistd.h>
char buf[] = "1234567";
int flag = 1;//1打印 0逆转
void *callBack1(void* arg)
{
while(1)
{
if(1 == flag)
{
printf("%s\n",buf);
flag = 0;
}
}
pthread_exit(NULL);
}
void *callBack2(void* arg)
{
int i = 0, j = 6;
int k = 3;
while(1)
{
if(0 == flag)
{
while(i < j)
{
char c;
c = buf[i];
buf[i] = buf[j];
buf[j] = c;
i++;
j--;
}
}
flag = 1;
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,callBack1,NULL);
pthread_create(&tid2,NULL,callBack2,NULL);
while(1);
return 0;
}