IO线程进程(线程)

1、标准I0函数时候讲解的时钟代码,要求输入quit字符串后,结束进程

#include 
#include 
#include 
#include 
#include 
#include 
#include 
void* callBack(void * arg)
{
	time_t t;
	time(&t);
	printf("%ld\n", t);
	struct tm *info = NULL;
	char str[10]="";
	system("clear"); 	//让c代码调用shell指令
	while(1)
	{
		t = time(NULL);
		info = localtime(&t);
		if(NULL == info)
		{
			perror("localtime");
			return NULL;
		}

		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 NULL;
}
int main(int argc, const char *argv[])
{
	
	char str[10]="";
		pthread_t tid;
	if(pthread_create(&tid,NULL,callBack,NULL)!=0){
		fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
		return -1;
	}
	sleep(1);
	while(1){
	
		scanf("%s",str);
		if(!strcmp(str,"quit")){
			break;
		}
	}

	return 0;
}

IO线程进程(线程)_第1张图片

 

2、IO线程进程(线程)_第2张图片

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
char buf[]="1234567";
void* callBack(void * arg)
{
	int len=strlen(buf);
	char temp;
	while(1){
	char *p=buf;
	char *q=buf+len-1;

		while(q>p){
			temp=*p;
			*p=*q;
			*q=temp;
			p++;
			q--;
		}
		pthread_exit(NULL);
	}
	return NULL;
}
int main(int argc, const char *argv[])
{

	while(1){
		pthread_t tid;
		if(pthread_create(&tid,NULL,callBack,NULL)!=0){
			fprintf(stderr,"pthread_create failed line:%d\n",__LINE__);
			return -1;
		}
		pthread_join(tid,NULL);
	
		printf("%s\n",buf);

	}


	return 0;
}

IO线程进程(线程)_第3张图片

 

你可能感兴趣的:(c++,算法,java)