How to create two different processes
-
hello sir,
i was asked to create two different process where in one process, main function should be there which should have abstract calls as follows:-
port = initialize_port(PORT _TYPE) ;
writeport = (port,buf,size,PORT_TYPE);
readport =(port,buf,size,PORT_TYPE);
closeport();
and in other process qt gui should be theremay i get any suggestion how to do this
please help me this is a bit urgent
thanking u
-
You can start "here":http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#details
-
thank u sir
but i should not use qt related calls for creating the process it should be in C++
can u please guide me
thanking u sir -
For that you need platform specific call, imo.
If you are on linux, have a look at "this pdf":http://www.advancedlinuxprogramming.com/alp-folder/alp-ch03-processes.pdf (abt fork, exec etc)
-
#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>#define MAX_DATA_SIZE 512
#define IDusing namespace std;
struct Message
{
int message_id;
}msg;void received_data(void);
int main()
{
cout<<"main program"<<endl;pthread_t thread1,thread2;
int status,status2;status= pthread_create(&thread1,NULL,received_data,NULL);
status2 = pthread_create(&thread2,NULL,process,NULL);
pthread_join(thread1,NULL);
if(status<0)
{
perror("pthread_create failed");
exit(0);
}sleep(1);
status = pthread_join(thread1,NULL);
exit (0);
}void *received_data(void *)
{
std::cerr<<"am in thread"<<std::endl;
Message msg;
printf("Hello\n");
int fd;
sigset_t set;
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("10.66.48.23"); //server ip addr
servaddr.sin_port = htons(12005); //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;}
pthread_exit(0);
}
i shud have 4 threads in my main program
- received_data - first threads receive messages
- process_data - whenever a message is received in first thread a signal should be sent to second thread for processing data - message id received from first thread should be used for processing data
- health_data - this thread sends health message for every 20 seconds
- display_data - this thread should display status in the gui.
i have written first thread which is working successfully, but am not getting idea how to send a signal when message is received and how to attach a signal handler when signal is received with the function in the second thread.
kindly suggest me for doing this