Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Pthreads and signals
Forum Updated to NodeBB v4.3 + New Features

Pthreads and signals

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    janaki
    wrote on 6 Jan 2014, 10:22 last edited by
    #1

    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;
    }

    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sanchir Kartiev
      wrote on 20 Jan 2014, 15:18 last edited by
      #2

      Can you use std::thread?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        janaki
        wrote on 21 Jan 2014, 05:05 last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved