2023年2月28日作业

该程序包含两个部分:一部分是创建一个线程持续显示系统时间,直到用户输入quit退出;另一部分是定义一个全局字符串,两个线程分别用于打印和倒置字符串,不使用辅助数组且不允许使用sleep函数。程序在运行中能正确地交替显示原始和倒置的字符串。
摘要由CSDN通过智能技术生成

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

程序代码

#include<stdio.h>
#include<time.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
void *callBack(void *arg)
{
	time_t t1=time(NULL);
	struct tm *Time=NULL;
	while(1)
	{
		time(&t1);
		Time=localtime(&t1);
		fprintf(stdout,"%4d-%02d-%02d %02d:%02d:%02d\n",\
				Time->tm_year+1900,Time->tm_mon+1,Time->tm_mday,\
				Time->tm_hour,Time->tm_min,Time->tm_sec);
		sleep(1);
	}
}


int main(int argc, const char *argv[])
{
	//定义一个pthread_t类型的数,存储返回的tid号
	pthread_t ttid;
	//创建一个子线程
	if(pthread_create(&ttid,NULL,callBack,NULL)!=0)
	{
		fprintf(stderr,"pthread_creat failed __%d__",__LINE__);
		return -1;
	}
	char str[10];
	pthread_detach(ttid);
A:	fscanf(stdin,"%s",str);
	if(strcmp("exit",str)!=0)
	{
		printf("请重新输入");
		goto A;
	}
	else
		return 0;
		

}

运行结果

ubuntu@ubuntu:作业$ gcc f_clock.c -pthread
ubuntu@ubuntu:作业$ ./a.out 
2023-02-28 20:57:55
2023-02-28 20:57:56
2023-02-28 20:57:57
2023-02-28 20:57:58
2023-02-28 20:57:59
ex2023-02-28 20:58:00
i2023-02-28 20:58:01
t
ubuntu@ubuntu:作业$ ./a.out 
2023-02-28 21:10:09
2023-02-28 21:10:10
aed2023-02-28 21:10:11
s
请重新输入2023-02-28 21:10:12
ex2023-02-28 21:10:13
it2023-02-28 21:10:14


2.要求定义一个全局变量char buf="1234567",创建两个线程,不考虑退出条件。
a.A线程循环打印buf字符串,
b.B线程循环倒置buf字符串,即buf种本来存储1234567,倒置后buf仲存储7654321.不打印!!c.倒置不允许使用辅助数组。
d.要求A线程打印出来的结果只能为1234567或者7654321e.不允许使用sleep函数
 

  1 #include<stdio.h>
  2 #include<time.h>
  3 #include<pthread.h>
  4 #include<string.h>
  5 #include<unistd.h>
  6 char buf[]="1234567";
  7 //通过更改a的值,a=1循环逆序并使a=2,a=2时循环输出使得a=1
  8 void *callBack(void *arg)                                                                                                                                       
  9 {   
 10     while(1)
 11     {   
 12         //printf("%d\t%c",*(int *)arg,buf[0]);
 13         
 14         if(*(int*)arg==2)
 15         {
 16         //  printf("%d\n",*(int*)arg);
 17             for(int i=0,j=strlen(buf)-1;i<j;i++,j--)
 18             {   
 19                 buf[i]=buf[i]+buf[j];
 20                 buf[j]=buf[i]-buf[j];
 21                 buf[i]=buf[i]-buf[j];
 22             }
 23             *(int *)arg=1;
 24         }
 25     }
 26 }
 27 int main(int argc, const char *argv[])
 28 {   
 29     int a=2;
 30     int *b=&a;
 31     pthread_t ttid;
 32     if(pthread_create(&ttid,NULL,callBack,b)!=0)
 33     {   
 34         fprintf(stderr,"pthread_creat failed __%d__",__LINE__);
 35         return -1;
 36     }
 37     while(1)
 38     {   
 39         if(1==a)
 40         {   
 41             fprintf(stdout,"%s\n",buf);
 42             a=2;
 43         }
 44      
 45     }
 46     return 0;
 47 }
~          

输出结果

1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
1234567
7654321
^Z
[37]+  已停止               ./a.out

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

malingshu404

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值