Linux C multi-thread practice

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

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
#define ERR_MSG(msg) do\
{\
    fprintf(stderr,"line:%d\n",__LINE__);\
    perror(msg);\
} while (0);

void print_time()
{
    printf("enter print_time............\n");
    time_t t;
    time_t t1; 
    struct tm *info = NULL;
    while (1)
    {
        system("clear");
        t = time(NULL);
        //fprintf(stdout,"%ld\n",t);

        //time_t *t1;==>指针指向不明,容易造成野指针
        time(&t1); //一级指针一般为普通变量取地址
       // fprintf(stdout,"%ld\n",t1);

        //将秒转换称日历格式
        info = localtime(&t);
        if (NULL == info)
        {
            ERR_MSG("localtime");
            return;
        }
        //stderr==>无缓冲   stdout stdin 行缓冲,\n能刷新行缓冲
        // fprintf(stderr,"%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);
        // sleep(1);

        // \r无法刷新缓冲区,\n能刷新
        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);
    }
    printf("exit print_time............\n");
}

void * callback(void *arg)
{
    print_time();
    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    pthread_t tid;
    if (pthread_create(&tid,NULL,callback,NULL) < 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }

    char buf[32] = {0};
    scanf("%s",buf);
    while (1)
    {
        if (strcmp(buf,"quit")==0)
        {
            exit(0);
        }
    }

    pthread_join(tid,NULL);
    printf("主线程准备退出\n");
    return 0;
}

2. 

要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。

A线程循环打印buf字符串,

B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. 不打印!!

倒置不允许使用辅助数组。

要求A线程打印出来的结果只能为 1234567 或者 7654321

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>

/*
void * callback(void *arg)
{
    int i = 0;
    while (i < 10)
    {
        printf("[%d] line: __%d__ \n",i,__LINE__);
        sleep(1);
        i++;
    }
    
    pthread_exit(NULL);
}
*/
char buf[] = "1234567";

void array_reverse(char *buf,int size)
{
    int i;
    int j;
    for (i = 0,j = size - 1; i <=j; i++,j--)
    {
        char temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
    }
}

void print_arry(char *buf,int size)
{
    printf("数组元素为:");
    for (int i = 0; i < size; i++)
    {
        printf("%c",buf[i]);
    }
    printf("\n");
}
int flags = 0;
void *callback(void *arg)
{
    
    int size = strlen(buf);
    int i =0;
    while (i<1000)
    {
        if(flags ==0)
        {
            print_arry(buf,size);
        }
        flags = 1;
        //i++;
    }
    
}

void *callback1(void *arg)
{
    
    int size = strlen(buf);
    int i = 0;
    while (i<1000)
    {
        if(flags == 1)
        {
            array_reverse(buf,size);
            
        }
        flags = 0;
        //sleep(1);
        //i++;
    }
    
}

int main(int argc, char const *argv[])
{
    /*
      //Reverse test.......
    int size = strlen(buf);
    print_arry(buf,size);
    array_reverse(buf,size);
    print_arry(buf,size);
    */

    //创建两个线程,一个用于打印,一个用于倒置
    pthread_t tid,tid1;
    if (pthread_create(&tid,NULL,callback,NULL) < 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }

    pthread_detach(tid); //tid设置分离态,pthread_join不阻塞

    if (pthread_create(&tid1,NULL,callback1,NULL) < 0)
    {
        fprintf(stderr,"pthread_create failed\n");
        return -1;
    }

    pthread_join(tid1,NULL);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值