看板 Linux 關於我們 聯絡資訊
※ [本文轉錄自 C_and_CPP 看板 #1JOU9vFQ ] 作者: BIAO (超越自己) 看板: C_and_CPP 標題: [問題] Linux C多執行緒的疑問!? 時間: Thu May 1 13:58:15 2014 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Linux ------------------------------- 各位板友大家好, 目前小弟有個程式,內有A、B兩個thread,共用某個副程式, 其中A thread固定時間會執行該副程式, 另個B thread則隨機等候外部命令,接收外部命令後執行該副程式。 此程式在Linux內執行, 可有時候會自己中斷執行, 請問這是因為沒使用mutex, 發生衝突導致程式結束嗎!? 另想請問板友... 有什麼因素導致程式執行時,會自動結束呢!? 還請板友不吝賜教,謝謝..XD -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.124.42.70 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1398923897.A.3DA.html ※ 編輯: BIAO (140.124.42.70), 05/01/2014 14:15:52
acake:兩個thread有需要用mutex同步嗎? 05/01 20:10
RouterHsieh:沒有產生core dump嗎? 05/01 22:04
RouterHsieh:話說這問題跟本板沒啥關係..轉去LinuxDev比較恰當吧 05/01 22:05
BIAO:謝謝兩位板友的回覆,我轉到Linux.. XD 05/02 11:30
※ 發信站: 批踢踢實業坊(ptt.cc) ※ 轉錄者: BIAO (140.124.42.70), 05/02/2014 11:30:46
bitlife:你自少要知道結束原因,程式自己正常exit,一般signal 或 05/02 11:53
bitlife:SIGSEGV 05/02 11:53
BIAO:我的程式是設定...無窮迴圈 二個thread都是.. 所以才納悶XD 05/02 13:00
bitlife:所以我說要先找出原因,你可以先用交談式shell把程式丟背景 05/02 13:32
bitlife:shell不要登出,等程式結束,shell會顯示程式怎麼結束的 05/02 13:33
BIAO:謝謝b大.. 沒用過shell!不過現在明白處理方式了,感謝您~~~ 05/02 13:50
bitlife:你用linux但沒用過shell?這...? 那你怎麼跑程式的? 05/02 14:50
回b大的話... 我都是用vim編輯C code, 用gcc編譯後,直接用sudo ./執行程式.. 偶爾加個&來背景執行.. XD ※ 編輯: BIAO (140.124.42.70), 05/02/2014 15:04:57
bitlife:你sudo那行命令應該就是在交談式shell下執行的 05/02 15:10
bitlife:執行 echo $SHELL 就知道你目前是用什麼shell 05/02 15:11
BIAO:(!!!) 原來是這個樣子..謝謝b大 :D 05/02 15:16
BIAO:目前已物色到一本不錯的書..準備入手!! 05/02 15:20
BIAO:我是用bash~~~ 05/02 16:16
目前已經完全懂b大的意思.. 我的確是在interactive mode執行程式, 不過就算執行沒加上&,程序自行結束時也沒有產生程序如何結束.. 我是使用Ubuntu Linux,目前在尋找產生Core dump的方法XD C語言程序是無窮迴圈,二個thread都是.. ※ 編輯: BIAO (140.124.42.70), 05/02/2014 21:25:13 ※ 編輯: BIAO (140.124.42.70), 05/02/2014 21:26:33
kdjf:gcc -g (debug), 加上gdb試試看吧 05/02 21:51
lantw44:$SHELL 是登入的時候設定的,如果自己開其他 shell 05/02 22:46
lantw44:不會跟著變 05/02 22:46
bitlife:樓上,我是看原po連shell是什麼都不知道,所以才叫他看目前 05/02 23:21
bitlife:是什麼shell XD 05/02 23:21
bitlife:不過你這補充說明對原po還是有用就是了 05/02 23:23
感謝大家的回覆,參照k大做法, 採用gcc -o -g -DDEBUG modbus+MySQL+Control26 modbus+MySQL+Control26.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lpthread, 得到了許多資訊.. ------------------------------------- modbus+MySQL+Control26: In function `_fini': crtstuff.c:(.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crti.o:(.fini+0x0): first defined here modbus+MySQL+Control26: In function `__data_start': crtstuff.c:(.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.data+0x0): first defined here modbus+MySQL+Control26: In function `__data_start': crtstuff.c:(.data+0x4): multiple definition of `__dso_handle' /usr/lib/gcc/arm-linux-gnueabihf/4.6/crtbegin.o:(.data+0x0): first defined here modbus+MySQL+Control26:(.rodata+0x0): multiple definition of `_IO_stdin_used' /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.rodata.cst4+0x0): first defined here modbus+MySQL+Control26: In function `_start': crtstuff.c:(.text+0x0): multiple definition of `_start' /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:(.text+0x0): first defined here modbus+MySQL+Control26: In function `_init': crtstuff.c:(.init+0x0): multiple definition of `_init' /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crti.o:(.init+0x0): first defined here /tmp/ccj4NCbh.o:(.bss+0x0): multiple definition of `data_lock' modbus+MySQL+Control26:(.bss+0x10): first defined here /tmp/ccj4NCbh.o: In function `sock_server_thread': modbus+MySQL+Control26.c:(.text+0x0): multiple definition of `sock_server_thread' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:38: first defined here /tmp/ccj4NCbh.o: In function `order_dec': modbus+MySQL+Control26.c:(.text+0x3f4): multiple definition of `order_dec' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:154: first defined here /tmp/ccj4NCbh.o: In function `control': modbus+MySQL+Control26.c:(.text+0x7f4): multiple definition of `control' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:248: first defined here /tmp/ccj4NCbh.o: In function `status': modbus+MySQL+Control26.c:(.text+0x2098): multiple definition of `status' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:697: first defined here /tmp/ccj4NCbh.o: In function `info': modbus+MySQL+Control26.c:(.text+0x1204): multiple definition of `info' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:469: first defined here /tmp/ccj4NCbh.o: In function `main': modbus+MySQL+Control26.c:(.text+0x378): multiple definition of `main' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:132: first defined here /tmp/ccj4NCbh.o: In function `modbustcp': modbus+MySQL+Control26.c:(.text+0x2c20): multiple definition of `modbustcp' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:898: first defined here /tmp/ccj4NCbh.o: In function `modbusrtu': modbus+MySQL+Control26.c:(.text+0x2f1c): multiple definition of `modbusrtu' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:1004: first defined here /tmp/ccj4NCbh.o: In function `info_insert': modbus+MySQL+Control26.c:(.text+0x1c88): multiple definition of `info_insert' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:636: first defined here /tmp/ccj4NCbh.o: In function `cost_power': modbus+MySQL+Control26.c:(.text+0x1e1c): multiple definition of `cost_power' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:660: first defined here /tmp/ccj4NCbh.o: In function `status_insert': modbus+MySQL+Control26.c:(.text+0x2844): multiple definition of `status_insert' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:839: first defined here /tmp/ccj4NCbh.o: In function `crc16': modbus+MySQL+Control26.c:(.text+0x321c): multiple definition of `crc16' modbus+MySQL+Control26:/home/ubuntu/C-practice/Test/modbus+MySQL+Control26.c:1114: first defined here collect2: ld returned 1 exit status ubuntu@localhost:~/C-practice/Test$ ------------------------------------- 這些資訊是沒加上-g不會出現的..XD ※ 編輯: BIAO (140.124.42.70), 05/03/2014 13:18:36