1. 程式人生 > >C語言多執行緒

C語言多執行緒

將按鍵 和LED燈的控制放在兩個執行緒,因為通過按鍵檢測是阻塞

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "read_line_conf.h"
#include <pthread.h>

#define LINE 2
#define FILENAME "PingNetwork.txt"

void * Key(void *);
void * LedNet(void *);

typedef struct {  
    char **str;     //the PChar of string array  
    size_t num;     //the number of string  
}IString;  

int msg_split(char *src, char *delim, IString *istr)//split buf  
{  
    int i;  
    char *str = NULL, *p = NULL;  
  
    (*istr).num = 1;  
    str = (char*)calloc(strlen(src)+1,sizeof(char));  
    if (str == NULL) return 0;  
    (*istr).str = (char**)calloc(1,sizeof(char *));  
    if ((*istr).str == NULL) return 0;  
    strcpy(str,src);  
  
    p = strtok(str, delim);  
    (*istr).str[0] = (char*)calloc(strlen(p)+1,sizeof(char));  
    if ((*istr).str[0] == NULL) return 0;  
    strcpy((*istr).str[0],p);  
    for(i=1; p = strtok(NULL, delim); i++)  
    {  
        (*istr).num++;  
        (*istr).str = (char**)realloc((*istr).str,(i+1)*sizeof(char *));  
        if ((*istr).str == NULL) return 0;  
        (*istr).str[i] = (char*)calloc(strlen(p)+1,sizeof(char));  
        if ((*istr).str[0] == NULL) return 0;  
        strcpy((*istr).str[i],p);  
    }  
    free(str);  
    str = p = NULL;  
  
    return 1;  
}  

void LEDNetwork(){

        //C system ping -->PingNetwork.txt
//      system("killall ping &"); 
//      system("./pingNet.sh");
//      sleep(5);
//        system("killall ping"); 
        //C system ping -->PingNetwork.txt 
        char str[200];
        char str1[20];
        IString istr;
        IString istr1;
        memset(&istr, 0, sizeof(IString));
        memset(&istr1, 0, sizeof(IString));
        
        int flag=0;
        int i=1;
        flag = WhetherIsNull(FILENAME);
        int line = 0;
        while(i<=5&&line<2){
                system("killall ping &"); 
                system("./pingNet.sh");
                sleep(5);
                system("killall ping"); 
                flag = WhetherIsNull(FILENAME);
                line = read_line(FILENAME);
                i++;
        }
//      line = read_line(FILENAME);
        printf("line ----------------:%d\n",line);
        if(i == 6&&line <=1){
                system("./LedNetworkNo.sh"); 
                return;
                }


        read_line_date(LINE,str,FILENAME);
        printf("s--tr :%s",str);
        msg_split(str, " ", &istr);
        printf("istr.num  --:%d",istr.num);
        printf("istr.6  --:%s\n",istr.str[6]);
        msg_split(istr.str[6], "=", &istr1);
        printf("istr1  --:%s\n",istr1.str[1]);

        float netSp=0.0;
        netSp=atof(istr1.str[1]);
        printf("netSp--%f\n",netSp);
        
        if(netSp<50&&netSp>0.0) {
        printf("network is good\n");
        system("./LedNetworkGood.sh"); 
        }else if(netSp>=50&&netSp<=200){
        printf("network is yi ban\n");
        system("./LedNetworkYiban.sh"); 
        }else if(netSp>200){
        printf("network is yi bad\n");
        system("./LedNetworkBad.sh"); 
        }
        return;
}
int main(){
        pthread_t t0;
        pthread_t t1;

        // 建立執行緒key  reboot update file
        if(pthread_create(&t0, NULL, Key, NULL) == -1){
                puts("fail to create pthread t0");
                exit(1);
        }
          
        if(pthread_create(&t1, NULL, LedNet, NULL) == -1){
        puts("fail to create pthread t1");
        exit(1);
        }

        //等待執行緒結束
    void * result;
    if(pthread_join(t0, &result) == -1){
        puts("fail to recollect t0");
        exit(1);
    }

    if(pthread_join(t1, &result) == -1){
        puts("fail to recollect t1");
        exit(1);
    }
        return 1;
}

// 執行緒key方法
void * Key(void *a){
        printf("-----------------Key");
        KeyReboot();
        return NULL;

}

// 執行緒LED net方法
void * LedNet(void *b){
        printf("-----------------LedNet");
        while(1){
        LEDNetwork();
        };
    return NULL;
}