1.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
void* timesum (void *arg)
{
time_t t;
time(&t);
struct tm *ti=NULL;
while(1)
{
t=time(NULL);
ti=localtime(&t);
if(NULL==ti)
{
perror("localtime");
return NULL ;
}
printf("%d-%02d-%02d %02d:%02d:%02d\r",\
ti->tm_year+1900,ti->tm_mon+1,ti->tm_mday,\
ti->tm_hour,ti->tm_min,ti->tm_sec);
fflush(stdout);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_t sid;
pthread_create(&sid,NULL,timesum,NULL);
char s[5]="";
scanf("%s",s);
while(1)
{
if(strcmp("quit",s)==0)
{
break;
}
}
return 0;
}
2.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
char buf[]="1234567";
void *other(void *arg)
{
char s;
int l=strlen(buf);
while(1)
{
for(int i=0;i<l/2;i++)
{
s=buf[i];
buf[i]=buf[l-i-1];
buf[l-i-1]=s;
}
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
pthread_create(&tid,NULL,other,NULL);
while(1)
{
if(buf[0]=='1'&&buf[1]=='2'&buf[2]=='3')
{
printf("主:%s\n",buf);
}
else if(buf[0]=='7'&&buf[1]=='6'&&buf[2]=='5')
{
printf("主:%s\n",buf);
}else
{
continue;
}
}
return 0;
}