• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            colorful

            zc qq:1337220912

             

            libcurl 多線程使用注意事項(xiàng)(補(bǔ)充)——HTTPS,openssl多線程使用加鎖

            問題

            多線程libcurl運(yùn)行一段時(shí)間后出現(xiàn)崩掉,沒有確定的點(diǎn),沒有確定的URL。一直查看源代碼沒有問題,最后通過debug跟蹤發(fā)現(xiàn)是在訪問SSL的時(shí)候出現(xiàn)的crash。

            才想起來openssl是不支持多線程的,要自己做加鎖處理。而且libcurl中并沒有支持相關(guān)的加鎖操作。


            解決辦法:

            在初始化libcurl的時(shí)候?yàn)閛penssl創(chuàng)建一個(gè)互斥鎖函數(shù),一個(gè)回調(diào)函數(shù)傳給openss

            openssl鎖l函數(shù)原形 :void (* func )(int ,int , const char * ,int)

            設(shè)置方式:CRYPTO_set_locking_callback(void (* func )(int ,int , const char * ,int));

            設(shè)置這樣一個(gè)函數(shù)還不夠,另外還要配置一個(gè)鎖id回調(diào)函數(shù),這個(gè)可以參考o(jì)penssl多線程下的使用相關(guān)。

            id函數(shù)原形:unsigned int (*func)(void)

            設(shè)置方式:CRYPTO_set_id_callback(unsigned int (*func)(void));

            通過這兩個(gè)設(shè)置就可以解決HTTPS多線程請求出現(xiàn)crash的問題。


            代碼示例:

            下面是引用了libcurl示例的一個(gè)代碼

            http://curl.haxx.se/libcurl/c/opensslthreadlock.html

            最關(guān)鍵就是,兩個(gè)callback的實(shí)現(xiàn),還有初始化鎖(init_locks)和釋放鎖(kill_locks)的位置

            1. #define USE_OPENSSL    
            2.    
            3. #include <stdio.h>  
            4. #include <pthread.h>  
            5. #include <curl/curl.h>  
            6.    
            7. #define NUMT 4  
            8.    
            9. /* we have this global to let the callback get easy access to it */   
            10. static pthread_mutex_t *lockarray;  
            11.    
            12. #ifdef USE_OPENSSL  
            13. #include <openssl/crypto.h>  
            14. static void lock_callback(int mode, int type, char *file, int line)  
            15. {  
            16.   (void)file;  
            17.   (void)line;  
            18.   if (mode & CRYPTO_LOCK) {  
            19.     pthread_mutex_lock(&(lockarray[type]));  
            20.   }  
            21.   else {  
            22.     pthread_mutex_unlock(&(lockarray[type]));  
            23.   }  
            24. }  
            25.    
            26. static unsigned long thread_id(void)  
            27. {  
            28.   unsigned long ret;  
            29.    
            30.   ret=(unsigned long)pthread_self();  
            31.   return(ret);  
            32. }  
            33.    
            34. static void init_locks(void)  
            35. {  
            36.   int i;  
            37.    
            38.   lockarray=(pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *  
            39.                                             sizeof(pthread_mutex_t));  
            40.   for (i=0; i<CRYPTO_num_locks(); i++) {  
            41.     pthread_mutex_init(&(lockarray[i]),NULL);  
            42.   }  
            43.    
            44.   CRYPTO_set_id_callback((unsigned long (*)())thread_id);  
            45.   CRYPTO_set_locking_callback((void (*)())lock_callback);  
            46. }  
            47.    
            48. static void kill_locks(void)  
            49. {  
            50.   int i;  
            51.    
            52.   CRYPTO_set_locking_callback(NULL);  
            53.   for (i=0; i<CRYPTO_num_locks(); i++)  
            54.     pthread_mutex_destroy(&(lockarray[i]));  
            55.    
            56.   OPENSSL_free(lockarray);  
            57. }  
            58. #endif  
            59.    
            60. #ifdef USE_GNUTLS  
            61. #include <gcrypt.h>  
            62. #include <errno.h>  
            63.    
            64. GCRY_THREAD_OPTION_PTHREAD_IMPL;  
            65.    
            66. void init_locks(void)  
            67. {  
            68.   gcry_control(GCRYCTL_SET_THREAD_CBS);  
            69. }  
            70.    
            71. #define kill_locks()  
            72. #endif  
            73.    
            74. /* List of URLs to fetch.*/   
            75. const char * const urls[]= {  
            76.   "https://www.example.com/",  
            77.   "https://www2.example.com/",  
            78.   "https://www3.example.com/",  
            79.   "https://www4.example.com/",  
            80. };  
            81.    
            82. static void *pull_one_url(void *url)  
            83. {  
            84.   CURL *curl;  
            85.    
            86.   curl = curl_easy_init();  
            87.   curl_easy_setopt(curl, CURLOPT_URL, url);  
            88.   /* this example doesn't verify the server's certificate, which means we 
            89.      might be downloading stuff from an impostor */   
            90.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);  
            91.   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);  
            92.   curl_easy_perform(curl); /* ignores error */   
            93.   curl_easy_cleanup(curl);  
            94.    
            95.   return NULL;  
            96. }  
            97.    
            98. int main(int argc, char **argv)  
            99. {  
            100.   pthread_t tid[NUMT];  
            101.   int i;  
            102.   int error;  
            103.   (void)argc; /* we don't use any arguments in this example */   
            104.   (void)argv;  
            105.    
            106.   /* Must initialize libcurl before any threads are started */   
            107.   curl_global_init(CURL_GLOBAL_ALL);  
            108.    
            109.   init_locks();  
            110.    
            111.   for(i=0; i< NUMT; i++) {  
            112.     error = pthread_create(&tid[i],  
            113.                            NULL, /* default attributes please */   
            114.                            pull_one_url,  
            115.                            (void *)urls[i]);  
            116.     if(0 != error)  
            117.       fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);  
            118.     else  
            119.       fprintf(stderr, "Thread %d, gets %s\n", i, urls[i]);  
            120.   }  
            121.    
            122.   /* now wait for all threads to terminate */   
            123.   for(i=0; i< NUMT; i++) {  
            124.     error = pthread_join(tid[i], NULL);  
            125.     fprintf(stderr, "Thread %d terminated\n", i);  
            126.   }  
            127.    
            128.   kill_locks();  
            129.    
            130.   return 0;  

            posted @ 2013-12-02 17:13 多彩人生 閱讀(2658) | 評論 (0)編輯 收藏

            BIOS 啟動過程

            BIOS 即basic input output system,在計(jì)算機(jī)的開機(jī)過程中伴有很重要的角色。它的啟動順序如下:

            第一步: 當(dāng)我們按下電源開關(guān)時(shí),電源就開始向主板和其它設(shè)備供電,此時(shí)電壓還不太穩(wěn)定,主板上的控制芯片組會向CPU發(fā)出并保持一個(gè)RESET(重置)信號,讓 CPU內(nèi)部自動恢復(fù)到初始狀態(tài),但CPU在此刻不會馬上執(zhí)行指令。當(dāng)芯片組檢測到電源已經(jīng)開始穩(wěn)定供電了(當(dāng)然從不穩(wěn)定到穩(wěn)定的過程只是一瞬間的事情), 它便撤去RESET信號(如果是手工按下計(jì)算機(jī)面板上的Reset按鈕來重啟機(jī)器,那么松開該按鈕時(shí)芯片組就會撤去RESET信號),CPU馬上就從地址 FFFF0H處開始執(zhí)行指令,從前面的介紹可知,這個(gè)地址實(shí)際上在系統(tǒng)BIOS的地址范圍內(nèi),無論是Award BIOS還是AMI BIOS,放在這里的只是一條跳轉(zhuǎn)指令,跳到系統(tǒng)BIOS中真正的啟動代碼處。

            第二步: 系統(tǒng)BIOS的啟動代碼首先要做的事情就是進(jìn)行POST(Power-On Self Test,加電后自檢),POST的主要任務(wù)是檢測系統(tǒng)中一些關(guān)鍵設(shè)備是否存在和能否正常工作,例如內(nèi)存和顯卡等設(shè)備。由 于POST是最早進(jìn)行的檢測過 程,此時(shí)顯卡還沒有初始化,如果系統(tǒng)BIOS在進(jìn)行POST的過程中發(fā)現(xiàn)了一些致命錯(cuò)誤,例如沒有找到內(nèi)存或者內(nèi)存有問題(此時(shí)只會檢查640K常規(guī)內(nèi) 存),那么系統(tǒng)BIOS就會直接控制喇叭發(fā)聲來報(bào)告錯(cuò)誤,聲音的長短和次數(shù)代表了錯(cuò)誤的類型。在正常情況下,POST過程進(jìn)行得非常快,我們幾乎無法感覺 到它的存在,POST結(jié)束之后就會調(diào)用其它代碼來進(jìn)行更完整的硬件檢測。

            第三步: 接下來系統(tǒng)BIOS將查找顯卡的BIOS,前面說過,存放顯卡BIOS的ROM芯片的起始地址通常設(shè)在C0000H處,系統(tǒng)BIOS在這個(gè)地方找到顯卡 BIOS之后就調(diào)用它的初始化代 碼,由顯卡BIOS來初始化顯卡,此時(shí)多數(shù)顯卡都會在屏幕上顯示出一些初始化信息,介紹生產(chǎn)廠商、圖形芯片類型等內(nèi)容,不 過這個(gè)畫面幾乎是一閃而過。系統(tǒng)BIOS接著會查找其它設(shè)備的BIOS程序,找到之后同樣要調(diào)用這些BIOS內(nèi)部的初始化代碼來初始化相關(guān)的設(shè)備。

            第四步: 查找完所有其它設(shè)備的BIOS之后,系統(tǒng)BIOS將顯示出它自己的啟動畫面,其中包括有系統(tǒng)BIOS的類型、序列號和版本號等內(nèi)容。

            第五步: 接著系統(tǒng)BIOS將檢測和顯示CPU的類型和工作頻率,然后開始測試所有的RAM,并同時(shí)在屏幕上顯示內(nèi)存測試的進(jìn)度,我們可以在CMOS設(shè)置中自行決定 使用簡單耗時(shí)少或者詳細(xì)耗時(shí)多的測試方式。

            第六步: 內(nèi)存測試通過之后,系統(tǒng)BIOS將開始檢測系統(tǒng)中安裝的一些標(biāo)準(zhǔn)硬件設(shè)備,包括硬盤、CD-ROM、串口、并口、軟驅(qū)等設(shè)備,另外絕大多數(shù)較新版本的系統(tǒng)BIOS在這 一過程中還要自動檢測和設(shè)置內(nèi)存的定時(shí)參數(shù)、硬盤參數(shù)和訪問模式等。

            第七步: 標(biāo)準(zhǔn)設(shè)備檢測完畢后,系統(tǒng)BIOS內(nèi)部的支持即插即用的代碼將開始檢測和配置系統(tǒng)中安裝的即插即用設(shè)備,每找到一個(gè)設(shè)備之后,系統(tǒng)BIOS都會在屏幕上顯 示出設(shè)備的名稱和型號等信息,同時(shí)為該設(shè)備分配中斷、DMA通道和I/O端口等資源。

            第八步: 到這一步為止,所有硬件都已經(jīng)檢測配置完畢了,多數(shù)系統(tǒng)BIOS會重新清屏并在屏幕上方顯示出一個(gè)表格,其中概略地列出了系統(tǒng)中安裝的各種標(biāo)準(zhǔn)硬件設(shè)備, 以及它們使用的資源和一些相關(guān)工作參數(shù)。

            第九步: 接下來系統(tǒng)BIOS更新ESCD(Extended System Configuration Data,擴(kuò)展系統(tǒng)配置數(shù)據(jù))。ESCD是系統(tǒng)BIOS用來與操作系統(tǒng)交換硬件配置信息的一種手段,這些數(shù)據(jù)被存放在CMOS(一小塊特殊的RAM,由主 板上的電池來供電)之中。通常ESCD數(shù)據(jù)只在系統(tǒng)硬件配置發(fā)生改變后才會更新,所以不是每次啟動機(jī)器時(shí)我們都能夠看到“Update ESCD… Success”這樣的信息,不過,某些主板的系統(tǒng)BIOS在保存ESCD數(shù)據(jù)時(shí)使用了與Windows 9x不相同的數(shù)據(jù)格式,于是Windows 9x在它自己的啟動過程中會把ESCD數(shù)據(jù)修改成自己的格式,但在下一次啟動機(jī)器時(shí),即使硬件配置沒有發(fā)生改變,系統(tǒng)BIOS也會把ESCD的數(shù)據(jù)格式改 回來,如此循環(huán),將會導(dǎo)致在每次啟動機(jī)器時(shí),系統(tǒng)BIOS都要更新一遍ESCD,這就是為什么有些機(jī)器在每次啟動時(shí)都會顯示出相關(guān)信息的原因。

            第十步: ESCD更新完畢后,系統(tǒng)BIOS的啟動代碼將進(jìn)行它的最后一項(xiàng)工作,即根據(jù)用戶指定的啟動順序從軟盤、硬盤或光驅(qū)啟動。 以從C盤啟動為例,系統(tǒng)BIOS 將讀取并執(zhí)行硬盤上的主引導(dǎo)記錄,主引導(dǎo)記錄接著從分區(qū)表中找到第一個(gè)活動分區(qū),然后讀取并執(zhí)行這個(gè)活動分區(qū)的分區(qū)引導(dǎo)記錄,而分區(qū)引導(dǎo)記錄將負(fù)責(zé)讀取并 執(zhí)行IO.SYS,這是DOS和Windows 9x最基本的系統(tǒng)文件。Windows 9x的IO.SYS首先要初始化一些重要的系統(tǒng)數(shù)據(jù),然后就顯示出我們熟悉的藍(lán)天白云,在這幅畫面之下,Windows將繼續(xù)進(jìn)行DOS部分和GUI(圖 形用戶界面)部分的引導(dǎo)和初始化工作。


            posted @ 2013-11-28 16:39 多彩人生 閱讀(358) | 評論 (0)編輯 收藏

            關(guān)于騰迅c++sdk

            1/ 多線程下程序蹦的問題
            查找 crul_easy_getinfo
            將上面的 int http_code = 0; 改為 long http_code = 0;


            2/ 多線程下超時(shí)退出 signal 11問題
            在 curl_easy_perform(curl);前加上 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);

            3/ 多線程環(huán)境下,程序啟動時(shí)調(diào)用一次 curl_global_init(CURL_GLOBAL_ALL);
            程序退出時(shí)調(diào)用curl_global_cleanup();

            libcurl 多線程使用注意事項(xiàng)(補(bǔ)充)——HTTPS,openssl多線程使用加鎖

            http://blog.csdn.net/jaylong35/article/details/6988690

            posted @ 2013-11-20 17:46 多彩人生 閱讀(446) | 評論 (1)編輯 收藏

            正則表達(dá)式

            ----------------------------------------------------------------------------------------------
            包含tb_player和79669的行
            tb_player.*?79669

            ----------------------------------------------------------------------------------------------

            posted @ 2013-11-18 11:28 多彩人生 閱讀(254) | 評論 (0)編輯 收藏

            pg熱備

            數(shù)據(jù)庫的PITR是一般數(shù)據(jù)庫都必須滿足的技術(shù)。其原理是依據(jù)之前的物理備份文件加上wal的預(yù)寫日志模式備份做的恢復(fù)。該技術(shù)支持8.*及以上版本。下面主要概述PITR的準(zhǔn)備和恢復(fù)過程。 測試環(huán)境

            OS 環(huán)境:CentOS 6.2
            數(shù)據(jù)庫 :PostgreSQL 9.1.9


            一、前期工作既要恢復(fù),肯定是需要一個(gè)備份基礎(chǔ)的,否則再怎么的巧婦也難為無米之炊。
            1.修改數(shù)據(jù)庫參數(shù),修改postgresql.conf:

            archive_mode = on
            archive_timeout = 300   --單位是秒,此處以5分鐘為限強(qiáng)制歸檔,僅作測試
            archive_command = 'cp %p /data/pgbackup/archive/%f'  -- 注意/data/pgbackup/archive/目錄權(quán)限, chmod -R 777 /data/pgbackup/archive/
            wal_level = archive

            修改完重啟下reload,DB

            2.基于文件級別的持續(xù)備份,
            a.基礎(chǔ)備份
            postgres=# select pg_start_backup('backup_2012_05_20_14:22:10');

            b.打包備份pg_data
            # cd /data
            # tar -cvzf pgdata.tar ./postgres
            mv pgdata.tar /data/pgbackup/base/

            c.結(jié)束基礎(chǔ)備份并切換歸檔

            postgres=# select pg_stop_backup();

            postgres=# select pg_switch_xlog();
             pg_switch_xlog
            ----------------
             0/C000020
            (1 row)

            postgres=# select pg_current_xlog_location();
             pg_current_xlog_location
            --------------------------
             0/C000020
            (1 row)

            postgres=# create table test_1(id int,name varchar(50));
            postgres=# insert into test_1 values (1,'kenyon');
            INSERT 0 1

            此時(shí)在pg_data路徑下會產(chǎn)生一個(gè)label,可以查看內(nèi)容有checkpoint時(shí)間,基礎(chǔ)備份的開始和結(jié)束時(shí)間,以及標(biāo)簽名稱等。因?yàn)橹耙呀?jīng)設(shè)置了archive的三個(gè)參數(shù),可以在archive的備份路徑pg_home/archive下看到歸檔的文件會定時(shí)傳過來。

            二、恢復(fù)過程
            停數(shù)據(jù)庫
            # pg_stop

            假定數(shù)據(jù)庫的崩潰場景,將pgdata數(shù)據(jù)刪除
            # rm -rf /database/pgdata

            恢復(fù)之前備份的tar文件
            # tar xvf pgdata.tar

            刪除pg_xlog文件夾并重建
            # rm -rf pg_xlog
            # mkdir -p pg_xlog/archive_status

            新建recovery.conf文件并修改
            # vi /data/postgres/recovery.conf
            --新增內(nèi)容,指定恢復(fù)文件和路徑,%f,%p見上面說明
            restore_command = 'cp /data/pgbackup/archive/%f "%p"'

            啟動數(shù)據(jù)庫

            # pg_start
            [postgres@localhost archive]$ psql
            spsql (9.1.3)
            Type "help" for help.

            postgres=# select * from test_1;
             id |  name  
            ----+--------
              1 | kenyon
            (1 rows)

            --恢復(fù)成功,會恢復(fù)到之前接收到的最后一個(gè)歸檔文件。另外recovery.conf會改名變成recovery.done

            日志內(nèi)容:

            LOG:  shutting down
            LOG:  database system is shut down
            LOG:  database system was interrupted; last known up at 2012-05-20 22:23:15 CST
            LOG:  starting archive recovery
            LOG:  restored log file "000000010000000000000002" from archive
            LOG:  redo starts at 0/8000078
            LOG:  consistent recovery state reached at 0/C000000
            LOG:  restored log file "000000010000000000000003" from archive
            LOG:  restored log file "000000010000000000000004" from archive
            LOG:  restored log file "000000010000000000000005" from archive
            LOG:  restored log file "000000010000000000000006" from archive
            LOG:  restored log file "000000010000000000000007" from archive
            cp: cannot stat `/home/postgres/archive/000000010000000000000008': No such file or directory
            LOG:  could not open file "pg_xlog/000000010000000000000008" (log file 0, segment 8): No such file or directory
            LOG:  redo done at 0/1C000078
            LOG:  last completed transaction was at log time 2012-05-20 23:01:22.960591+08
            LOG:  restored log file "000000010000000000000007" from archive
            cp: cannot stat `/home/postgres/archive/00000002.history': No such file or directory
            LOG:  selected new timeline ID: 2
            cp: cannot stat `/home/postgres/archive/00000001.history': No such file or directory
            LOG:  archive recovery complete
            LOG:  database system is ready to accept connections
            LOG:  autovacuum launcher started

            PS:若要恢復(fù)到指定時(shí)間,還需要再recovery.conf中設(shè)置recovrey_target_time,recovery_target_timeline等參數(shù)

            總結(jié):pitr技術(shù)對于7*24小時(shí)支撐是至關(guān)重要的,但是如果數(shù)據(jù)庫非常小,增大pg_dump備份的頻率可能更方便,但對于大數(shù)據(jù)庫就需要了。

            posted @ 2013-11-06 17:12 多彩人生 閱讀(661) | 評論 (0)編輯 收藏

            crontab

            由于Cron 是Linux的內(nèi)置服務(wù),可以用以下的方法啟動、關(guān)閉這個(gè)服務(wù):

            /sbin/service crond start //啟動服務(wù)
            /sbin/service crond stop //關(guān)閉服務(wù)
            /sbin/service crond restart //重啟服務(wù)
            /sbin/service crond reload //重新載入配置


            Crontab命令的格式為:crontab –l|-r|-e|-i [username],其參數(shù)含義如表一
            crontab -e   //編輯crontab文件

            crontab 格式

            第1列分鐘1~59
            第2列小時(shí)1~23(0表示子夜)
            第3列日1~31
            第4列月1~12
            第5列星期0~6(0表示星期天)
            第6列要運(yùn)行的命令

            下面是crontab的格式:
            分 時(shí) 日 月 星期 要運(yùn)行的命令

            這里有crontab文件條目的一些例子:

            30 21 * * * /usr/local/apache/bin/apachectl restart
            上面的例子表示每晚的21:30重啟apache。

            45 4 1,10,22 * * /usr/local/apache/bin/apachectl restart
            上面的例子表示每月1、10、22日的4 : 45重啟apache。

            10 1 * * 6,0 /usr/local/apache/bin/apachectl restart
            上面的例子表示每周六、周日的1 : 10重啟apache。

            0,30 18-23 * * * /usr/local/apache/bin/apachectl restart
            上面的例子表示在每天18 : 00至23 : 00之間每隔30分鐘重啟apache。

            0 23 * * 6 /usr/local/apache/bin/apachectl restart
            上面的例子表示每星期六的11 : 00 pm重啟apache。

            * */1 * * * /usr/local/apache/bin/apachectl restart
            每一小時(shí)重啟apache

            * 23-7/1 * * * /usr/local/apache/bin/apachectl restart
            晚上11點(diǎn)到早上7點(diǎn)之間,每隔一小時(shí)重啟apache

            0 11 4 * mon-wed /usr/local/apache/bin/apachectl restart
            每月的4號與每周一到周三的11點(diǎn)重啟apache

            0 4 1 jan * /usr/local/apache/bin/apachectl restart
            一月一號的4點(diǎn)重啟apache

            posted @ 2013-11-05 15:30 多彩人生 閱讀(353) | 評論 (0)編輯 收藏

            linux shell 小盒子

            -----------------------------------------------------------------------------
            grep 一次排除多個(gè)
            grep -v 'aaaa\|bbbb'  // 排除aaaa和bbbb

            -----------------------------------------------------------------------------
            查看文件數(shù)
            ls |wc -l

            -----------------------------------------------------------------------------
            查看文件夾大小
            du -sh *
            du -sh /home
            du -sh

            ------------------------------------------------------------------------------
            查找最新的文件

            (1)將文件按從新到舊排列,取第一個(gè)。

            ls -t *.cpp | head -1

            (2)將文件按從舊到新排列,取最后一個(gè)。

            ls -rt *.cpp | tail -1


            ------------------------------------------------------------------------------
            linux下遞歸刪除某個(gè)文件夾或文件
            今天給學(xué)校部署校慶網(wǎng)站,做網(wǎng)站的同學(xué)傳給我的網(wǎng)站文件夾里,有很多exe文件,而且這些exe的文件名都是原來的目錄名,看起來是他的機(jī)器中了病毒。雖然exe文件在linux下無法運(yùn)行,但是還是要?jiǎng)h除這些exe文件。在網(wǎng)上找了一下,《linux下遞歸刪除某個(gè)文件夾或文件》給了我滿意的方法,讓我可以一次性刪除某目錄及其子目錄下所有的exe文件。

            find . -name '*.exe' -type f -print -exec rm -rf {} \;

            (1) "."    表示從當(dāng)前目錄開始遞歸查找
            (2) “ -name '*.exe' "根據(jù)名稱來查找,要查找所有以.exe結(jié)尾的文件夾或者文件
            (3) " -type f "查找的類型為文件
            (4) "-print" 輸出查找的文件目錄名
            (5) 最主要的是是-exec了,-exec選項(xiàng)后邊跟著一個(gè)所要執(zhí)行的命令,表示將find出來的文件或目錄執(zhí)行該命令。exec選項(xiàng)后面跟隨著所要執(zhí)行的命令或腳本,然后是一對兒{},一個(gè)空格和一個(gè)\,最后是一個(gè)分號

            -----------------------------------------------------------------------------

            posted @ 2013-11-04 11:06 多彩人生 閱讀(467) | 評論 (0)編輯 收藏

            postgresql 安裝

            yum install readline-devel

            ./configure -without-zlib
            make
            su
            make install


            mkdir -p /data/postgres
            ln -s /data/postgres/ /home/
            groupadd postgres
            useradd -d /home/postgres -g postgres postgres
            chown -R postgres:postgres /data/postgres/
            su - postgres
            /usr/local/pgsql/bin/initdb -A md5 --locale=en_US.utf8 --lc-ctype=en_US.utf8 -E UTF-8 -W /data/postgres
            ln -s /usr/local/pgsql/bin/ ./

            mkdir /data/postgres/pg_log
            chown -R postgres:postgres /data/postgres/pg_log

            //開啟pg
            bin/postgres -D /data/postgres >pg_log/logfile 2>&1 &

            // 設(shè)為開機(jī)啟動
            cp /data/zc/postgresql-9.1.9/contrib/start-scripts/linux /etc/init.d/postgresql
            chmod a+x /etc/init.d/postgresql
            // 修改/etc/init.d/postgresql
            prefix 設(shè)為postgres安裝路徑
            PGDATA 設(shè)為數(shù)據(jù)庫路徑(/data/postgres)

            chkconfig --add postgresql

            posted @ 2013-11-01 16:16 多彩人生 閱讀(473) | 評論 (0)編輯 收藏

            變量在shell腳本間的傳遞

            前幾篇文章中已經(jīng)介紹過局部變量和環(huán)境變量的含義,接下來我們來拓展下,看看變量怎樣實(shí)現(xiàn)在shell腳本
            中的傳遞shell腳本其實(shí)是用當(dāng)前shell的子shell去執(zhí)行的,所以在shell腳本中定義的普通變量只適用于當(dāng)前
            shell的子shell環(huán)境,也就是說在當(dāng)前shell環(huán)境中不適用,也不適用于這個(gè)shell腳本的子shell。
            在shell腳本中定義的環(huán)境變量可以傳承給它的子shell,但是也不能傳遞給當(dāng)前shell(不能逆向傳遞)
            如果在一個(gè)腳本中需要執(zhí)行另一個(gè)腳本,并且運(yùn)用其中的變量,改如何申明變量呢,我們來看一個(gè)例子:
            /root/test1.sh內(nèi)容如下:
            #!/bin/bash
            aaa=yuanfaxiang
            echo "test1:$aaa"

            /root/test2.sh內(nèi)容如下:
            #!/bin/bash
            /root/test1.sh
            echo "test2:$aaa"
            執(zhí)行test2.sh結(jié)果如下:
            [root@centos ~]# sh test2.sh
            test1:yuanfaxiang
            test2:
            從結(jié)果可以看出test1.sh沒有把變量aaa的值傳遞給test2.sh

            我們把test2.sh改成:
            #!/bin/bash
            source /root/test1.sh
            echo "test2:$aaa"
            執(zhí)行test2.sh結(jié)果如下:
            [root@centos ~]# sh test2.sh
            test1:yuanfaxiang
            test2:yuanfaxiang
            結(jié)果顯示test2.sh繼承了test1.sh中定義的變量aaa。
            原因分析:在第一次執(zhí)行test2.sh時(shí),test1.sh被作為了test2.sh的子shell來執(zhí)行,其中定義的變量只
            在test1.sh中起效,不能逆向傳遞到test2.sh中;而在第二次執(zhí)行中,采用source來執(zhí)行test1.sh,意思
            是直接把test1.sh在當(dāng)前的test2.sh中執(zhí)行,沒有作為子shell去執(zhí)行,test1.sh中定義的變量,就影響
            到了test2.sh。

            如果我們再建一個(gè)test3.sh
            #!/bin/bash
            echo "test3:$aaa"

            把test2.sh改成:
            #!/bin/bash
            source /root/test1.sh
            echo "test2:$aaa"
            /root/test3.sh
            執(zhí)行test2.sh:
            [root@shenji ~]# sh test2.sh
            test1:yuanfaxiang
            test2:yuanfaxiang
            test3:
            結(jié)果顯示test3.sh沒有繼承test1.sh中申明的變量,因?yàn)閟ource /root/test1.sh只是讓test1.sh
            中的變量在test2.sh中生效,aaa畢竟還是個(gè)普通局部變量,并不能被test3.sh這個(gè)子shell所繼承,
            所以我們可以想到環(huán)境變量,把a(bǔ)aa變成test2.sh這個(gè)腳本的環(huán)境變量,讓test2.sh的子進(jìn)程也能繼承。

            將test1.sh改成:
            #!/bin/bash
            export aaa=yuanfaxiang
            echo "test1:$aaa"
            執(zhí)行test2.sh后有如下結(jié)果:
            [root@shenji ~]# sh test2.sh
            test1:yuanfaxiang
            test2:yuanfaxiang
            test3:yuanfaxiang
            在test1.sh中聲明了環(huán)境變量也就是全局變量,在test2.sh中用source執(zhí)行test1.sh,將變量帶到了
            test2.sh中,并使之成為test2.sh執(zhí)行過程中的環(huán)境變量,可以被test2.sh的子進(jìn)程繼承,起到了順向
            傳遞效果。

            posted @ 2013-09-11 13:37 多彩人生 閱讀(670) | 評論 (0)編輯 收藏

            libcurl

            最近項(xiàng)目要上QQ平臺, 用到騰迅開放平臺的SDK,  SDK使用的是libcurl. 發(fā)現(xiàn)很多bug,
            其中最大的問題是, sdk在單線程下正常, 在多線程下就蹦, 后來研究發(fā)現(xiàn) 是curl_easy_getinfo第三個(gè)參數(shù)需要的是long, 而SDK傳給它的是int, 其它地方還有類似的錯(cuò)誤.
            哎,騰迅也是坑爹啊.

            -------------------------------------------------------------

            1.libcurl中常用的API

            #include <curl/curl.h>

             

            CURL *curl_easy_init()

            必須第一個(gè)調(diào)用,返回一個(gè)CURL指針,用于表示當(dāng)前的curl會話。

            如果之前沒有執(zhí)行curl_global_init(long flags),則它會自動先執(zhí)行。

            但是由于curl_global_init(long flags)和其調(diào)用的函數(shù)在多線程中是不安全的,所以多線程程序中必須在其它線程啟動之前執(zhí)行curl_global_init(long flags)。


            void curl_easy_cleanup(CURL * handle )

            會話結(jié)束后調(diào)用,以清除一個(gè)CURL * handle


            CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter)

            設(shè)置libcurl執(zhí)行參數(shù)


            CURLcode curl_easy_perform(CURL * handle)

            根據(jù)設(shè)置的參數(shù)執(zhí)行網(wǎng)絡(luò)數(shù)據(jù)傳輸,在初始化和參數(shù)設(shè)置后調(diào)用


            CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... )

            獲取所指向的libcurl會話的執(zhí)行信息

             

            2.libcurl最簡單使用流程

             

            #include <stdio.h>
            #include <curl/curl.h>
             
            int main(void)
            {
              CURL *curl;
              CURLcode res;

              int response;
             

              res = curl_global_init(CURL_GLOBAL_ALL);

              curl = curl_easy_init();
              if(curl) {
                curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
                res = curl_easy_perform(curl);
                res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); 


               
                curl_easy_cleanup(curl);
              }

              curl_global_cleanup();
              return 0;
            }

             

            3.libcurl使用實(shí)例

            更多的使用示例可以參考官方網(wǎng)站上的以下網(wǎng)頁

            http://curl.haxx.se/libcurl/c/example.html

             

             

            參考資料:

            http://curl.haxx.se


             

            posted @ 2013-08-27 16:12 多彩人生 閱讀(569) | 評論 (0)編輯 收藏

            僅列出標(biāo)題
            共25頁: 1 2 3 4 5 6 7 8 9 Last 

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            一本久久a久久精品vr综合| 久久高清一级毛片| 久久精品亚洲福利| 久久91综合国产91久久精品 | 久久综合鬼色88久久精品综合自在自线噜噜| 久久国产精品99精品国产| 人妻无码久久一区二区三区免费 | 国产精品久久久久久久久久影院 | 欧美久久综合性欧美| AV无码久久久久不卡网站下载| 久久乐国产综合亚洲精品| 久久精品国产精品亚洲精品 | 色婷婷狠狠久久综合五月| 亚洲国产精品嫩草影院久久| 亚洲精品美女久久久久99小说| 色综合久久久久综合99| 思思久久99热只有频精品66| 久久中文字幕人妻丝袜| 久久精品a亚洲国产v高清不卡| 久久99国产精品一区二区| 久久一本综合| 久久精品人人做人人妻人人玩 | 久久久久久久综合日本| 亚洲Av无码国产情品久久| 久久人人爽人人爽人人爽| 国产精品国色综合久久| 久久精品无码av| 色综合久久久久久久久五月| 99久久国产综合精品成人影院| 久久久久久亚洲精品不卡 | 久久综合亚洲欧美成人| 国产精品无码久久久久久| 久久成人永久免费播放| 亚洲AV无码久久| 国产综合成人久久大片91| 色综合久久综合中文综合网 | 久久精品无码一区二区三区免费| 欧美一区二区三区久久综| 久久久这里有精品中文字幕| 亚洲AV日韩精品久久久久| 久久男人中文字幕资源站|