⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12-7.c

📁 提供了许多线程间通信的实例程序。用linux下的C语言实现。
💻 C
字号:
#include <stdio.h>
 #include <unistd.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
 #include <sys/wait.h>
 #include <pthread.h>
 #include "utils.h"
 #include "afxcfg.h"
 #include "db_pv.h"
 #include "web_pv.h"
 #include "log_pv.h"
 #include "http_pv.h"
   typedef struct {
         SOCKET                  sock;
           struct sockaddr_in      addr;
   } pv_arg;
  
   int g_runflag = 1;
   static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
    /* Network server main-thread */
   static void run_service();
   /* Web request sub-thread */
   static void* web_threadproc(void* arg);
   /* Database update sub-thread */
   static void* db_threadproc(void* arg);
  
   int main(void)
   {
             pthread_t db_tid;
              /* Set signal transaction */
              signal(SIGTERM, sig_action);
              // signal(SIGINT,  sig_action);
              signal(SIGPIPE, sig_action);
              signal(SIGQUIT, sig_action);
              signal(SIGHUP,  sig_action);
              signal(SIGCHLD, sig_action);
     
              /* Load local configuration file */
              if ( load_config("../etc/pvd.conf") == -1 ) {
                      fprintf(stderr, "Fail to load configuration file, exit ... \n");
                      return -1;
             }
     
              /* Initialize time */
              get_date(g_date);
              get_time(g_time);
     
              /* Initialize log */
              open_log(LOG_FILENAME, 5);
              write_log(1, "Pvd - the PV server startup ... ... \n");
     
              /* Load web id from DB */
              read_webs();
     
              /* Renew PV if exist */
              reload_allpv();

              /* Create a new thread for transact connection */
              pthread_create(&db_tid, NULL, &db_threadproc, NULL);
     
              /* Run level service */
              run_service();
     
              /* Close log */
              close_log();
     
              return 0;
      }
     
      void run_service()
      {
              SOCKET sock;
              pv_arg* arg;
             socklen_t addrlen;
             pthread_t tid;
    
            if ( (sock = create_sock(AF_INET, SOCK_STREAM, 0)) == -1 ) {
                     fprintf(stderr, "Fail to create socket \n");
                     return;
             }
    
             if ( bind_sock(sock, LIS_PORT) != 0 ) {
                     fprintf(stderr, "Fail to bind socket \n");
                    return;
             }
    
             if ( listen_sock(sock, 15) != 0 ) {
                     fprintf(stderr, "Fail to listen port \n");
                     return;
             }
    
              /* Create PID file */
             create_pidfile("../run/pvd.pid");
    
             while ( g_runflag ) {
                     arg = (pv_arg*)malloc(sizeof(pv_arg));
                if ( arg == NULL ) {
                             fprintf(stderr, "%s %d: Fail to malloc memory !\n", __FILE__, __LINE__);
                             break;
                     }
    
                     addrlen = sizeof(struct sockaddr_in);
                     if ( (arg->sock = accept(sock, (struct sockaddr*)&(arg->addr), &addrlen)) == -1 ) {
    
                             if ( errno == EINTR )
                                     continue;
                             else {
                                     fprintf(stderr, "Fail to accept socket \n");
                                     break;
                             }
                     }
    
                     /* Create a new thread for transact connection */
                     pthread_create(&tid, NULL, &web_threadproc, arg);
                     printf("Create thread id %d\n", tid);
    
             } // end while
    
             close_sock(&sock);
             return;
    }
    
    void* web_threadproc(void* arg)
     {
             int rs;
             int id;
            pv_arg pa;
             char res[128];
             http_arg http;
    
             /* Get and copy parameter */
            pa = *((pv_arg*)arg);
             free(arg);
    
             pthread_detach(pthread_self());
    
         /* Transact http Get request */
             rs = http_get_parse(pa.sock, res, &http);
             if ( rs == 0 ) { /* OK */
                     write(pa.sock, "OK", 2);
                     printf("http get %s", res);
                     for (rs; rs < http.sum; rs++)
                             printf(" %s %s", http.arg[rs].name, http.arg[rs].value);
                     printf("\n");
             }
             else { /* error */
                     write(pa.sock, "ERROR", 5);
             }
             close(pa.sock);
    
             id = http_get_check(&http);
             if ( id > 0 ) {
                     /* Lock */
                     pthread_mutex_lock(&g_mutex);
                     update_web(id, 1, (unsigned int)pa.addr.sin_addr.s_addr);
                     /* Unlock */
                     pthread_mutex_unlock(&g_mutex);
             }
    
             return (NULL);
     }
    
     void* db_threadproc(void* arg)
     {
             int i = 0;
             int sec = 0;
    
             while ( g_runflag ) {
                     usleep(100000); /* sleep 0.1 second */
                     get_date(g_date);
                     get_time(g_time);
    
                    if ( ++i >= 100 ) {
                             i = 0;
                            /* Lock */
                            pthread_mutex_lock(&g_mutex);
                         update_db();
                             backup_allpv();
                             /* Unlock */
                             pthread_mutex_unlock(&g_mutex);
                     }
                     else {
                            if ( strncmp(g_time, "23", 2) != 0 ) continue;
                            if ( strncmp(g_time + 3, "59", 2) != 0 ) continue;
                             sec = atoi(g_time + 6);
                             if ( sec >= 58 ) {
                                     /* Lock */
                                     pthread_mutex_lock(&g_mutex);
                                     update_db();
                                     write_log(1, "Last update today data of database.\n");
                                     /* Unlock */
                                     pthread_mutex_unlock(&g_mutex);
                                     i = 10000;
                                     sleep(3);
                            }
                     }
             }
    
             return (NULL);
     }
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -