POSIX-线程访问

我需要使用POSIX表示法在C中创建一个程序。 主程序创建第一个线程,该线程用n个值填充缓冲区。然后,它创建必须从第一个缓冲区读取的nt个线程,然后将它们放入第二个。 这是我写的:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <pthread.h>
#include <math.h>
#include <sched.h>

#define MAXTRF 30
#define N 100

void *tarefa1(void *arg);
void *tarefa2(void *arg);
void *tarefa3(void *arg);

double *vx,*vy;
int n,ny,np;
int run_x,run_y;


typedef struct {int i,nt,dimbuf,kmax,t;} trfarg_t;
pthread_mutex_t mtx1;
pthread_cond_t cond1;
pthread_mutex_t mtx2;
pthread_cond_t cond2;


int main(int argc, char **argv){

    int i,r,nt,n,dimbuf,kmax;
    srand(223);

/***** verificação de argumentos *****/

    if(argc!=5){
        printf("Numero de argumentos errado\n");
        return 0;
    }

    if(atoi(argv[1])<1){
        printf("O numero de tarefas tem que ser superior a 1\n");
        return 0;
    }

    if(atoi(argv[1])>MAXTRF){
        printf("Numero excessivo de tarefas\n");
        return 0;
    }
    nt=atoi(argv[1]);

    if(atoi(argv[2])<nt){
        printf("Numero de valores tem que ser superior ao numero de tarefas\n");
        return 0;
    }

    if(atoi(argv[2])>N){
        printf("Numero de valores superior a %d,\n",N);
        return 0;
    }
    n=atoi(argv[2]);
    ny=n;
    np=n;

    if(atoi(argv[3])<1 || atoi(argv[3]) > n){
        printf("Dimensao dos buffers invalida\n");
        return 0;
    }
    dimbuf=atoi(argv[3]);

    vx=malloc(dimbuf*sizeof(double));
    vy=malloc(dimbuf*sizeof(double));
    for (i=0;i<dimbuf;i++){
        vx[i]=-1;
        vy[i]=-1;
    }

    if(atoi(argv[4])<1 || atoi(argv[4]) > dimbuf){
        printf("Numero de valores maximo retirado do buffer invalido\n");
        return 0;
    }
    kmax=atoi(argv[4]);

    pthread_t      trfid[nt+2];    /* variavel para ID das tarefas */
    pthread_attr_t trfatr;           /* variavel para atributos das tarefas */
    trfarg_t       trfarg[nt+2];   /* variavel para argumento das tarefas */


/***** fim da verificação de argumentos e impressão inicial*****/

    printf("Calculo de %d valores de e^x com %d tarefas, dimbuf=%d e kmax=%d\n",n,nt,dimbuf,kmax);

/* inicializar variavel de atributos com valores por defeito */
    pthread_attr_init(&trfatr);
/* modificar estado de desacoplamento para "joinable" */
    pthread_attr_setdetachstate(&trfatr, PTHREAD_CREATE_JOINABLE); 
/* inicializar mutex com valores por defeito para os atributos */
    pthread_mutex_init(&mtx1, NULL);
    pthread_mutex_init(&mtx2, NULL);   

/***** Iniciar tarefa Tx e criar o vector de valores aleatorios *****/

    if (r) {

        /* erro ! */
        perror("Erro na criação da tarefa");
        exit(1);    
    }

    for(i=0; i<2; i++) {

        trfarg[i].i=i;
        trfarg[i].nt=nt;
        trfarg[i].dimbuf=dimbuf;
        trfarg[i].kmax=kmax;
        trfarg[i].t=0;

        if (i==0) {
            r= pthread_create(&trfid[i], &trfatr, tarefa1, (void*) &trfarg[i]);
        }
        else {
            r= pthread_create(&trfid[i], &trfatr, tarefa2, (void*) &trfarg[i]);
        }
        if (r) {
            /* erro ! */
            perror("Erro na criação da tarefa");
            exit(1);    
        }

    }

    /* esperar que as tarefas criadas terminem */
    for(i=0; i<3; i++)
        pthread_join( trfid[i], (void **) NULL);
   /* libertar recursos associados ao mutex */
    pthread_mutex_destroy(&mtx1);
    pthread_mutex_destroy(&mtx2);
   /* imprimir soma total */

    for (i=0;i<n;i++) {
        printf(" %f\t %f\n",vx[i],vy[i]);
    }

    return 0;
}

void *tarefa1(void *arg){

    trfarg_t *a;
    int i,kmax,dimbuf;

    srand(223);

    a= (trfarg_t *) arg;
    kmax=a->kmax;
    dimbuf=a->dimbuf;

    printf("Starting thread (%d).\n", a->i);

    while (n>0) {

        pthread_mutex_lock(&mtx1);

        for (i=0;i<dimbuf;i++) {        

            if (vx[i]==-1) {
                vx[i]=((double) rand()/(RAND_MAX));
                n--;
                printf("%.5f\n",vx[i]);
                if (n==0) {
                    return (void*) NULL;
                    printf("A terminar a sub-tarefa(%d).\n", a->i);
                }

            }

        } 

        pthread_mutex_unlock(&mtx1);
    }
    printf("Finish thread (%d).\n", a->i);
    return (void*) NULL;

}

void *tarefa2(void *arg){

    trfarg_t *a;
    int i,dimbuf,kmax;
    double valorexp;

    a= (trfarg_t *) arg;
    dimbuf=a->dimbuf;
    kmax=a->kmax;

    while (ny>0) {

        pthread_mutex_lock(&mtx1);  

        for (i=0;i<dimbuf;i++) {

            if(vx[i]!=-1){
                vy[i]=exp(vx[i]);
                kmax--;
                ny--;

                if (kmax==0 || ny==0) {
                    return (void*) NULL;
                }
            }

        }
        pthread_mutex_unlock(&mtx1);

    }

    return (void*) NULL;
}

作为测试,我只尝试了一个填充第一个缓冲区,另一个填充第二个缓冲区,我得到消息说线程0开始和结束,然后我认为程序处于循环或死锁状态。谁能帮忙