作者mingtai1 (綠豆嘉義人)
看板C_and_CPP
標題[問題] 請問pthread_create為何會產生process
時間Thu Jun 27 14:48:15 2013
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
gcc 3.4.6, RED HAT LINUX 3.4.6-11
請問一下, 之前學的觀念是所有thread應該屬於同一個process,
但我CentOS下 用pthread_create測試開2條thread, 卻多跑出了3個process
用ps aux|grep ptest看跑出了下面訊息
root 18569 0.0 0.0 24572 824 pts/2 S+ 14:42 0:00 ./ptest
root 18570 0.0 0.0 24572 824 pts/2 S+ 14:42 0:00 ./ptest
root 18571 0.0 0.0 24572 824 pts/2 S+ 14:42 0:00 ./ptest
root 18572 0.0 0.0 24572 824 pts/2 S+ 14:42 0:00 ./ptest
請問有人知道這個原理是什麼嗎?為什麼會跑出這麼多process...
有沒有大大可以解惑一下, 感謝... @@
程式碼如下:
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
void* print_message_function ( void *ptr );
typedef struct str_thdata
{
int thread_no;
char message[100];
} thdata;
int main()
{
pthread_t thread1, thread2;
thdata data1, data2;
data1.thread_no = 1;
strcpy(data1.message, "Hello!");
data2.thread_no = 2;
strcpy(data2.message, "Hi!");
pthread_create (&thread1, NULL, print_message_function, (void *)&data1);
pthread_create (&thread2, NULL, print_message_function, (void *)&data2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
}
void* print_message_function ( void *ptr )
{
thdata *data;
data = (thdata *) ptr;
while(1)
{
printf("Thread %d says %s \n", data->thread_no, data->message);
usleep(1);
}
return NULL;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.163.12.151
→ mingtai1:找到問題了,gcc版本太舊的Bug,換4.7就沒這麼多process了 06/27 15:28
→ damody:gcc 真的還是要用新版的 06/27 22:57