2011-10-24 11 views
3

私は以下のコードを持っており、SEGV信号によって殺されています。デバッガを使用すると、main()の最初のsem_init()によってkillされていることが示されます。最初のsem_init()をコメントアウトすると、同じ問題が2番目に発生します。私はこのsys呼び出しがSEGVを引き起こす原因を突き止めることを試みた。 elseは実行されていないので、エラーが起きてから値を返すことができます。 ご協力いただけると幸いです。 ありがとうございます。SEGVを引き起こすsem_init()

この問題が発生する前に実行されていない残りのコードを削除しました。

#define PORTNUM 7000 
#define NUM_OF_THREADS 5 
#define oops(msg) { perror(msg); exit(1);} 
#define FCFS 0 
#define SJF 1; 

void bindAndListen(); 
void acceptConnection(int socket_file_descriptor); 
void* dispatchJobs(void*); 
void* replyToClient(void* pos); 

//holds ids of worker threads 
pthread_t threads[NUM_OF_THREADS]; 

//mutex variable for sleep_signal_cond 
pthread_mutex_t sleep_signal_mutex[NUM_OF_THREADS]; 
//holds the condition variables to signal when the thread should be unblocked 
pthread_cond_t sleep_signal_cond[NUM_OF_THREADS]; 

//mutex for accessing sleeping_thread_list 
pthread_mutex_t sleeping_threads_mutex = PTHREAD_MUTEX_INITIALIZER; 
//list of which threads are sleeping so they can be signaled and given a job 
std::vector<bool> *sleeping_threads_list = new std::vector<bool>(); 

//number of threads ready for jobs 
sem_t* available_threads; 
sem_t* waiting_jobs; 


//holds requests waiting to be given to one of the threads for execution 
std::vector<std::vector<int> >* jobs = new std::vector<std::vector<int> >(); 

pthread_mutex_t jobs_mutex = PTHREAD_MUTEX_INITIALIZER; 




int main (int argc, char * const argv[]) { 

//holds id for thread responsible for removing jobs from ready queue and assigning them to worker thread 
pthread_t dispatcher_thread; 


//initializes semaphores 
    if(sem_init(available_threads, 0, NUM_OF_THREADS) != 0){   //this is the line causing the SEGV 
     oops("Error Initializing Semaphore"); 
    } 

    if(sem_init(waiting_jobs, 0, 0) !=0){ 
     oops("Error Initializing Semaphore"); 
    } 


//initializes condition variables and guarding mutexes 
for(int i=0; i<NUM_OF_THREADS; i++){ 
    pthread_cond_init(&sleep_signal_cond[i], NULL); 
    pthread_mutex_init(&sleep_signal_mutex[i], NULL); 
} 




if(pthread_create(&dispatcher_thread, NULL, dispatchJobs, (void*)NULL) !=0){ 
    oops("Error Creating Distributer Thread"); 
+0

'}' sがありません。インデントが貧弱です。 –

+0

私はすべてのコードを投稿しなかったので、}はありません。エラーのポイントを過ぎたコードは投稿する必要がなく、このページを不必要に長くしてしまいます。私の貧弱なインデントは、私のコードをウィンドウに貼り付けることです。私の書式の多くは失われました。それは私のファイルに適切にインデントされています。 – jln646v

+0

あなたがここにいるのは、コードを書くときに間違いをしたからです。質問を投稿する際にコードを任意に削除しました。実際のミスと貼り付け中のミスはどうやって分かりますか?あなたが尋ねている問題を_only_出す完全な、最小限のテストケースを作成し、投稿する前に_これがケースであるかどうかをテストしてください。 –

答えて

5

はあなたのセマフォへのポインタを宣言します。

sem_t* available_threads; 
sem_t* waiting_jobs; 

が、メモリを初期化することはありません。 sem_init関数は、メモリの割り当てを期待していません。メモリの既存のBLOBを初期化するだけです。メモリを割り当ててポインタを代入するか、セマフォをsem_tと宣言してアドレスをsem_initに渡します。

+0

この回答は、6年後に私が学校に派遣されたときに助けてくれました。ありがとう! – Driice

関連する問題