开发者

sending same data to multiple threads over pipes?

开发者 https://www.devze.com 2023-04-05 20:03 出处:网络
I have a ressource manager that handles multiple TCP-Connections. These connections are pthreads. How can I manage it to send data from the Ressource Manager to all of these threads? Or even better: H

I have a ressource manager that handles multiple TCP-Connections. These connections are pthreads. How can I manage it to send data from the Ressource Manager to all of these threads? Or even better: How can I figure out to which thread I have to send this command?

For example: I have 2 Threads, one with pid 3333, one with pid 4444. The user sends a task to program a board (it is a ressource manager that manages FPGA-boards). The ressource manager picks a board from a list, where also this pid is saved. Then the program-command should be send to the thread with this pid or, what I was thinking in the first place, to all of the threads and the threads decide if they go on or not. Protocol looks like this: <pid>#<board-id>#<file>

I open 2 pipes (for writing to the threads and reading from the threads) in the main.c and give them as an argument to the listening-thread (forthread-struct).

main.c
// open Pipes to SSL
    int rmsslpipe[2];
    int sslrmpipe[2];
    if (pipe(rmsslpipe) == -1) {
        writelog(LOGERROR, "main: could not create RM-SSL reading pipe");
        exit(1);
    }
    if (pipe(sslrmpipe) == -1) {
        writelog(LOGERROR, "main: could not create RM-SSL reading pipe");
        exit(1);
    }
    int rmtosslserver = rmsslpipe[1];
    int sslservertorm = sslrmpipe[0];

    // start SSL-Server as a pthread
    pthread_t thread;
    forthread* ft = malloc(sizeof(forth开发者_Python百科read));
    ft->rmtosslserver = rmsslpipe[0];
    ft->sslservertorm = sslrmpipe[1];
    pthread_mutex_t ftmutex;
    pthread_mutex_init(&ftmutex, NULL);
    ft->mutex = ftmutex;

    pthread_create(&thread, NULL, startProgramserver, (void*) ft);

This thread now listens for new connections and if there is a new connection, it creates a new thread with the forthread-struct as argument. This thread is where the action happens :)

void* startProgramserver(void* ft) {

int sock, s;

forthread* f = (forthread*) ft;

// open TCP-Socket
sock = tcp_listen();

while(1){
    if((s=accept(sock,0,0))<0) {
        printf("Problem accepting");
        // try again
        sleep(60);
        continue;
    }

    writelog(LOGNOTE, "New SSL-Connection accepted");
    f->socket = s;
    pthread_t thread;
    pthread_create(&thread, NULL, serveClient, (void*) f);
}
exit(0);
}

This thread now initializes the connection, gets some information from the client and then waits for the ressource manager to get new commands.

n=read(f->rmtosslserver, bufw, BUFSIZZ);

But this fails if there is more than only one thread. So how can I manage that?


If you have one thread per board, the "pid" shouldn't be needed in the command -- you just need a way to find the right thread (or queue, or whatever) for the specified board.

You could keep a list of your forthread structures, and include the board ID in the structure. Also include a way of passing commands; this could be a pipe, but you may as well use some sort of queue or list instead. That way you use one pipe (or other mechanism) per thread instead of a single shared one, and can find the right one for each board by searching the forthread list for the one with the right board ID. Just be sure to protect any parts of the structure that may be modified while the thread runs with a mutex.

The problem with using a single pipe as you've suggested is that only one thread will get each command -- if it's the wrong one, too bad; the command is gone.


The answer is Yes. I would use a list of them.However I can open a pipe more than 1 when the the speed of the PC is very slow. 2 connections for 2 connections.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号