Partage
  • Partager sur Facebook
  • Partager sur Twitter

using pthread_self() with multiple threads

    14 janvier 2021 à 18:25:41

    hello guys, i need your help with the following C-program below. I am trying to create a number of threads which ids are always the same. The program should return for instance : Thread4 receives from Thread "thread_id" Message 3 and thank youu

    #include <stdio.h>
    #include <sched.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <errno.h>
    #include <process.h>
    #include <sys/neutrino.h>
    #include <sys/netmgr.h>
    #include <semaphore.h>
    #include<string.h>
    #include <unistd.h>
    int Received_ID_3,Received_ID_5,Connect_ID_3,Connect_ID_5,Channel_ID;
    pthread_t Th3,Th4,Th5;
    void *Emission_Th3()
    {
    char Msg_S_3[30]= "Message 3";
    char Msg_R_3[30];
    Connect_ID_3 = ConnectAttach(0,Th3,Channel_ID,0,0);
    MsgSend(Connect_ID_3,Msg_S_3,sizeof(Msg_S_3),Msg_R_3,sizeof(Msg_R_3));
    return(0);
    }
    void *Emission_Th5()
    {
    char Msg_S_5[30]= "Message 5";
    char Msg_R_5[30];
    Connect_ID_5 = ConnectAttach(0,Th5,Channel_ID,0,0);
    MsgSend(Connect_ID_5,Msg_S_5,sizeof(Msg_S_5),Msg_R_5,sizeof(Msg_R_5));
    return(0);
    }
    void *Reception_Th4()
    {
    char Received_Msg_3[200],Received_Msg_5[200];
    char Rep_Msg_3[200],Rep_Msg_5[200];
    struct _msg_info i3,i5;
    Received_ID_3 = MsgReceive(Channel_ID,Received_Msg_3,sizeof(Received_Msg_3),&i3);
    Received_ID_5 =MsgReceive(Channel_ID,Received_Msg_5,sizeof(Received_Msg_5),&i5);
    MsgReply(Received_ID_3,0,Rep_Msg_3,sizeof(Rep_Msg_3));
    MsgReply(Received_ID_5,0,Rep_Msg_5,sizeof(Rep_Msg_5));
    printf("Le thread4 receives from Thread  %d  %s \n",pthread_self(),Received_Msg_3);
    printf("Le thread4 receives from Thread  %d  %s \n",pthread_self(),Received_Msg_5);
    return (0);
    }
    int main()
    {
    Channel_ID=ChannelCreate(0);
    pthread_create(&Th3,NULL,&Emission_Th3,NULL);
    pthread_create(&Th5,NULL,&Emission_Th5,NULL);
    pthread_create(&Th4,NULL,&Reception_Th4,NULL);
    sleep(2);
    return EXIT_SUCCESS;
    }



    • Partager sur Facebook
    • Partager sur Twitter

    using pthread_self() with multiple threads

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown