Pthreads and signals



  • i have a main thread which should have 3 different threads
    receive thread - which receives data using sockets
    process thread - process the receive data
    display thread - display it on gui

    thing is when some message is received then i shud send pthread_kill to my process thread then only it shud process the message

    i don' t have an idea of pthreads and signals in c++
    and i was told to use c++ calls only

    i have written the program like this

    and am getting segmentation fault
    can any body suggest me how to solve and continue this

    @
    #include<iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include "structures.h"
    #include <string.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <arpa/inet.h>

    #include <fcntl.h>
    #include <signal.h>
    #include <check.h>

    #define MAX_DATA_SIZE 512

    using namespace std;

    pthread_t receive_thread,process_thread;

    void sig_handler(int sig)
    {
    cout<<"caught signal"<<sig<<endl;
    }
    void *receive_data(void *)
    {

    cout<<"Receive thread is running"<<endl;
    pthread_kill(process_thread,SIGUSR1);

    return 0;
    }
    void process_data(void)
    {

    cout<<"process thread is running"<<endl;

    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = sig_handler;
    sigaction(SIGUSR1,&sa,NULL);

    pthread_exit(0);
    return 0;
    }
    int main(void)
    {
    int receive_status,process_status;

    receive_status=pthread_create(&receive_thread,NULL,receive_data,NULL); // Create Thread
    pthread_join(receive_thread,NULL); // Parent waits for

    process_status = pthread_create (&process_thread,NULL,process_data,NULL);
    //pthread_join(process_thread,NULL);

    return 0;
    }

    @



  • Can you use std::thread?



  • thank u sir
    actually am joining my first thread
    so i got that error
    my error is solved but when second time when a msg is received am not getting caught signal output
    my original code is as follows:
    @

    #include<iostream>

    #include <stdio.h>

    #include <stdlib.h>
    #include <pthread.h>
    #include "structures.h"
    #include <string.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <arpa/inet.h>
    #include <fcntl.h>
    #include <signal.h>
    #include <check.h>

    #define MAX_DATA_SIZE 512
    using namespace std;

    struct Message
    {
    int message_id;
    };

    struct Message msg;

    pthread_t p;

    void * r_data(void *);
    void * p_data(void *);

    static void usr1_handler(int signo);

    int main()
    {
    pthread_t r;
    sigset_t sigmask;
    struct sigaction action;
    sigfillset(&sigmask);
    pthread_sigmask(SIG_BLOCK, &sigmask,(sigset_t *)0);
    action.sa_flags = 0;
    action.sa_handler = usr1_handler;
    sigaction(SIGUSR1, &action, (struct sigaction *)0);
    pthread_create(&r,NULL,r_data,NULL);
    //
    pthread_create(&p,NULL,p_data,NULL);
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = usr1_handler;
    sigaction(SIGUSR1, &action, (struct sigaction *)0);
    //pthread_join(r,NULL);
    //pthread_join(p,NULL);

    cout<<"main program"<<endl;
    pthread_exit((void *)NULL);
    return 0;
    }

    void *r_data(void *)
    {
    cout<<"first thread"<<endl;
    printf("Hello\n");
    int fd;
    printf("Hello2\n");
    struct sockaddr_in servaddr, cliaddr;
    // void **status;
    fd = socket(AF_INET,SOCK_DGRAM,0);
    printf("Hello3\n");
    if(fd <0)
    cout<<"Socket creation Failed"<<endl;
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("10.66.48.11"); //server ip addr
    // servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //server ip addr
    servaddr.sin_port = htons(12007); //server poet no.
    printf("Hello4\n");
    if(bind(fd,(const struct sockaddr *)&servaddr,sizeof(servaddr))<0)
    cout<<"BIND FAILED"<<endl;

    unsigned char buffer[MAX_DATA_SIZE];
    socklen_t len=sizeof(cliaddr);
    fcntl(fd,F_SETFL,O_NONBLOCK);
    printf("Hello5\n");
    for(;;)
    {
    sleep(3);
    int n= recvfrom(fd,buffer,sizeof(buffer),0,(struct sockaddr *)&cliaddr,&len);
    memcpy(&msg,buffer,sizeof(msg));
    printf("Hello7\n");
    cout<<"Received : bytes"<<n<<" "<<msg.message_id<<endl;
    if(msg.message_id!=0)
    {
    pthread_kill(p, SIGUSR1);
    }
    }

    pthread_exit((void *)NULL);
    }

    void p_data(void)
    {
    int sig;
    sigset_t sigmask;
    sigemptyset(&sigmask);
    sigaddset(&sigmask, SIGUSR1);
    pthread_sigmask(SIG_UNBLOCK, &sigmask, (sigset_t *)0);
    sigwait(&sigmask, &sig);
    switch(sig)
    {
    case SIGUSR1:
    usr1_handler(sig);
    break;
    default:
    break;
    }
    }

    void usr1_handler(int sig)
    {
    cout<<"caught signal"<<endl;
    }

    pleas tell me how to resolve it


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.