初めまして、こんにちは。
現在、Ubuntu-64bit/VMwareによる仮想環境のlinuxにてマルチスレッドを作成しております。
fopenにて取得したファイルポインタを構造体に格納し、構造体変数のアドレスを
スレッドに渡しました。(引数が複数あったため)
その後、pthread_createで作成したスレッドをpthread_joinで実行した際、
ファイルポインタを渡せているのですが、スレッド化した関数内のfgetsでセグメンテーションエラーが発生しました。
何故でしょうか?
Thread 1 "a.out" received signal SIGSEGV, Segmentation fault. __GI__IO_getline_info (fp=fp@entry=0x7ffff00008c0, buf=buf@entry=0x0, n=151, delim=delim@entry=10, extract_delim=extract_delim@entry=1, eof=eof@entry=0x0) at iogetline.c:77 77 iogetline.c: そのようなファイルやディレクトリはありません.
###該当のソースコード
C言語
1#define _CRT_SECURE_NO_WARNINGS 2 3#include <stdio.h> 4#include <string.h> 5#include <stdlib.h> 6#include <ctype.h> 7#include <locale.h> 8#include <pthread.h> 9#include "struct.h" 10#include "Checkfile.h" 11#include "T_Integration.h" 12 13typedef struct integration{ 14 15 int ret; 16 17 int person_count; 18 19 FILE *fp; 20 21 RESULT *start; 22 23 int l_count; 24 25 int s_count; 26 27} I_RESULT; 28 29int main(int argc, char *argv[]){ 30 int loop = 0; 31 char buff[LONG_BUF]; //1行のバッファの長さ 32 int l_count = 0; //行数カウント 33 int s_count = 0; //シャープの個数 34 int num = 0; //入力された数字 35 int person_count = 0; //人数 36 int ret = 0; //returnチェック 37 int t_ret = 0; //スレッド作成の返り値 38 RESULT *start_tmp = NULL; 39 RESULT *start = NULL; //リストの先頭ポインタ 40 OPEN_FILE open_arg; //ファイルオープンする必要な情報を渡す構造体変数 41 I_RESULT inte_arg; //整合性チェックを行う際に必要な情報を渡す構造体変数 42 ADD_LIST add_arg; //リストを作成する際に必要な情報を渡す構造体変数 43 ANALYSIS ana_arg; //解析を行う関数へ必要な情報を渡す構造体変数 44 pthread_t thread1,thread2,thread3,thread4,thread5,thread6; 45 46 while (1){ 47 48 l_count = 0; 49 s_count = 0; 50 num = 0; 51 person_count = 0; 52 t_ret = 0; 53 54 printf("Input .csv file name.\n>>"); 55 56 //CSVファイル名を入力 57 scanf("%s",open_arg.filename); 58 59 //スレッドを作成 60 t_ret = pthread_create(&thread1,NULL,CheckFile,&open_arg); 61 62 if(t_ret != 0){ 63 printf("Could not create thread.\n"); 64 } 65 66 //スレッドを実行 67 pthread_join(thread1, NULL); 68 69 if(open_arg.result != PASS){ 70 continue; 71 } 72 73 inte_arg.fp = open_arg.fp; 74 75/*-----整合性チェック・リスト作成------*/ 76 T_Integration(&inte_arg);//ここでエラーが発生します。 77 78 t_ret = pthread_create(&thread2,NULL,T_Integration,&inte_arg); 79 80 if(t_ret != 0){ 81 printf("Could not create thread.\n"); 82 printf("Input .csv file name again.\n"); 83 continue; 84 } 85 86 pthread_join(thread2,NULL); 87 if(inte_arg.ret != 0){ 88 continue; 89 } 90 91 if(t_ret != 0){ 92 printf("Input .csv file name again.\n"); 93 freeListPointer(start,ret,person_count); 94 continue; 95 } 96 97 if (inte_arg.ret != 0){ 98 printf("The file that contains an invalid value.\n"); 99 freeListPointer(start,ret,person_count); 100 continue; 101 } 102 103 if (inte_arg.l_count == 0){ 104 printf("The data does not exist in the file\n"); 105 printf("Input .csv file name again.\n"); 106 continue; 107 } 108 if (inte_arg.l_count - s_count == 0){ 109 printf("All data is comment lines.\n"); 110 printf("Input .csv file name again.\n"); 111 continue; 112 } 113 break; 114 } 115} 116 117void *T_Integration(void *inte_arg){ 118 119 int l_count = 0; 120 int s_count = 0; 121 RESULT *p = NULL; 122 RESULT *start = NULL; 123 char *dlmt = ","; 124 char *buff = NULL; 125 126 I_RESULT *ptr = (I_RESULT *)inte_arg; 127 128 while (fgets(buff, LONG_BUF, ptr->fp) != NULL){ 129 ... 130 } 131} 132 133 134 135 136
###試したこと
fgetsにいたる直前のptr->fpを確認しましたが、メインスレッドと同様の値が確認されました。
エラーコードで検索しても期待した結果が得られなかったので質問させていただきました。
###補足情報(言語/FW/ツール等のバージョンなど)
C言語 gdbでデバッグを行いました。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/31 00:08