thank u sir
i have tried it and it is starting my second thread which is process_thread
but sir my caught signal output is coming for only once when my first message is received
when am receiving second message my second thread is not giving caught signal output
can u tell me where am wrong and how to correct it
my code is 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;
}