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);
    and in other process qt gui should be there

    may i get any suggestion how to do this

    please help me this is a bit urgent

    thanking u

  • 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": (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 ID

    using namespace std;

    struct Message
    int message_id;

    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);
    perror("pthread_create failed");


    status = pthread_join(thread1,NULL);

    exit (0);

    void *received_data(void *)
    std::cerr<<"am in thread"<<std::endl;
    Message msg;
    int fd;
    sigset_t set;
    struct sockaddr_in servaddr, cliaddr;

    void **status;

    fd = socket(AF_INET,SOCK_DGRAM,0);

    if(fd <0)
    cout<<"Socket creation Failed"<<endl;

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(""); //server ip addr
    // servaddr.sin_addr.s_addr = inet_addr(""); //server ip addr
    servaddr.sin_port = htons(12005); //server poet no.
    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);


    int n= recvfrom(fd,buffer,sizeof(buffer),0,(struct sockaddr *)&cliaddr,&len);


    cout<<"Received : bytes"<<n<<" "<<msg.message_id<<endl;




    i shud have 4 threads in my main program

    1. received_data - first threads receive messages
    2. 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
    3. health_data - this thread sends health message for every 20 seconds
    4. 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

Log in to reply

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