[10/12]

  1. 修改标准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;
}
  1. #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,不能出现乱序情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值