C 当使用信号量时,程序进入无限循环(死锁)

C 当使用信号量时,程序进入无限循环(死锁),c,multithreading,posix,deadlock,semaphore,C,Multithreading,Posix,Deadlock,Semaphore,程序在没有信号量的情况下正确执行,但当我使用信号量进行同步时,程序会陷入无限循环死锁!。我不知道为什么会这样。在执行后,程序从用户处获得输入,并进入无限循环*这是找家教的问题 以下是我使用信号量的函数-> void* CoOrdinator(void* arg) { sem_wait(&co); // sorting std temp; for (int i = 0; i < chair-1; i++) {

程序在没有信号量的情况下正确执行,但当我使用信号量进行同步时,程序会陷入无限循环死锁!。我不知道为什么会这样。在执行后,程序从用户处获得输入,并进入无限循环*这是找家教的问题

以下是我使用信号量的函数->

void* CoOrdinator(void* arg)
{   
    
     sem_wait(&co);
    // sorting
    std temp;
    for (int i = 0; i < chair-1; i++)
    {
        for (int j = 0; j < chair-i-1; j++)
        {
            if (buffer[j].priority == buffer[j+1].priority)
            {
                continue;
            }
            else if(buffer[j].priority>buffer[j+1].priority)
            {
                temp = buffer[j];
                buffer[j] = buffer[j+1];
                buffer[j+1] = temp;
            }
            
        }
        
    }

    

    // End Sorting

    printf("I'm a CoOrdinator\n");
    for (int i = 0; i < chair; i++)
    {
        printf("Student - %d -",buffer[i].id);
        printf("Priority - %d\n",buffer[i].priority);
    }
    
     sem_post(&tc);
    
}

// Teacher

void* Teacher(void* ox)
{

    for (int i = 0; i < teachers; i++)
    {
         sem_wait(&tc);
    
         pthread_mutex_lock(&mutex);

        
        tcObj[i] = buffer[out];
        
        out = (out+1)%chair;

        printf("Student - %d is in classroom. Priority: %d\n",tcObj[i].id,tcObj[i].priority);

         pthread_mutex_unlock(&mutex);
         sem_post(&st);
    }
    
    
    
    
    // // Studing with Teacher

    // for (int i = 0; i < teachers; i++)
    // {
    //  printf("Stuying with Teacher - %d", i);
    //  printf("Student - %d -",tcObj[i].id);
    //  printf("Priority - %d\n",tcObj[i].priority);
    // }

    
    
}

// Student
void* Student(void *px)
{
    
        // we have to use semaphore

        
        for (int i = 0; i < students; i++)
        {
             sem_wait(&st);
            
             pthread_mutex_lock(&mutex);

             buffer[in] = stObj[i];
             in = (in+1)%chair;

             pthread_mutex_unlock(&mutex);
             sem_post(&co);
            
    
            // if((int*)px == students)
            // {
            //  break;
            // }
        }

}
节目的其余部分是

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include<semaphore.h>

// Global Variables

int teachers, students, chair, help;

// Student Structure
typedef struct Student
{
    int id;
    int priority;

}std;

// Objects Array
std *stObj, *tcObj, *buffer;

// Buffer

int in = 0, out = 0;

// Semaphores

sem_t st, tc, co;

pthread_mutex_t mutex;
主要功能

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

    // Getting Inputs
    printf("Enter The Number Of Students: ");
    scanf("%d",&students);

    printf("Enter The Number Of Teachers: ");
    scanf("%d",&teachers);

    printf("Enter The Number Of Chair: ");
    scanf("%d",&chair);

    // Buffer Array

    buffer = (std*)malloc(chair * sizeof(std));
    stObj = (std*)malloc(students * sizeof(std));
    tcObj = (std*)malloc(students * sizeof(std));

    //Declaring Thread Variables
    pthread_t tc_thread[teachers], st_thread[students], co_thread;

    //Getting Priorities

    for(int i=0; i<students; i++)
    {
            stObj[i].id = i+1;
        printf("Enter The Priority of Student: ");
        scanf("%d",&stObj[i].priority);
    }


    // Mutex & Semaphores

    pthread_mutex_init(&mutex, NULL);
        // sem_init(&empty,0,chair);
        // sem_init(&full,0,0);

    sem_init(&st, 0, 1);
    sem_init(&tc, 0, 1);
    sem_init(&co, 0, 1);
 
    // Creating Threads

    for (int i = 0; i < students; ++i)
    {
        pthread_create(&st_thread[i],NULL,Student,(void*)&stObj[i].id);
        pthread_join(st_thread[i],NULL);
    }

    for (int i = 0; i < teachers; ++i)
    {
        pthread_create(&tc_thread[i],NULL,Teacher,(void*)i);
        pthread_join(tc_thread[i],NULL);
    }

    pthread_create(&co_thread,NULL,CoOrdinator,NULL);
    pthread_join(co_thread,NULL);


    // Destroying Semaphores & Mutex

    pthread_mutex_destroy(&mutex);
        sem_destroy(&st);
        sem_destroy(&tc);
    sem_destroy(&co);

    return 0;
}

有什么错误吗?

pthread\u连接是您的问题。一旦student的第一个循环运行,它就消耗了st信号量,并且没有任何东西可以补充它

for (int i = 0; i < students; ++i)
    {
        pthread_create(&st_thread[i],NULL,Student,(void*)&stObj[i].id);
        pthread_join(st_thread[i],NULL);
    }
从这些循环中删除pthread_连接;创建完所有线程后,添加循环以连接它们。因此,您的主屏幕看起来更像:

// Creating Threads
for (int i = 0; i < students; ++i)
{
    pthread_create(&st_thread[i],NULL,Student,(void*)&stObj[i].id);
}
for (int i = 0; i < teachers; ++i)
{
    pthread_create(&tc_thread[i],NULL,Teacher,(void*)i);
}
pthread_create(&co_thread,NULL,CoOrdinator,NULL);
pthread_join(co_thread,NULL);
for (int i = 0; i < students; ++i)
{
    pthread_join(st_thread[i],NULL);
}
for (int i = 0; i < teachers; ++i)
{
    pthread_join(tc_thread[i],NULL);
}
此外,与您当前的问题无关,此分配:tcObj=std*mallocstudents*sizeofstd;可能应该是tcObj=mallocteachers*sizeof*tcObj;注意你打错电话了;但是您也不应该强制转换malloc,通常最好使用sizeof*var\u,因为它不那么脆弱,并且可以自我记录


进行这些更改,您的程序在挂起之前会运行更长的时间…

看到信号量与互斥量一起使用是可疑的。不一定是错误的,但通常会使用信号量或互斥量和条件变量的组合。提供的信息太多或太少。对于调试问题,我们通常的期望是提供一个。然而,实际呈现的代码包含了大量可能不相关的内容,而忽略了关于信号量和互斥量如何初始化以及线程如何启动的关键细节,我的猜测是,这个问题与主线程的初始化和信号量的操作有关,而这些都没有被呈现出来;我已经编辑了我的问题。我没有要求你的程序的其余部分。我要了一份能代表你问题的报告。按照链接获取关于这是什么以及如何构建的信息。