线程进给其他多线程

发布于 2024-08-30 16:23:39 字数 1453 浏览 3 评论 0原文

我发现使用 fork 在两个进程之间打开管道很容易,但是我们如何将打开的管道传递给线程。 假设我们需要“可能通过多个线程”从程序 A 传递到程序 B, 程序 B 将其输出发送到程序 C

编辑: 我修改代码之后又来了,变得更容易阅读。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>

void *thread1(void *arg) {

    int status, fd[2];
    pid_t pid;

    pipe(fd);
    pid = fork();

    if (pid == 0) {
        int fd2 = *((int *) (arg));
        dup2(STDIN_FILENO, fd2);

        close(fd[0]);
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);

        execvp("PROGRAM B", NULL);
        exit(1);
    } else {
        close(fd[1]);
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);

        execl("PROGRAM C", NULL);
        wait(&status);

        return NULL;
    }
}

int main(void) {


    FILE *fpipe;
    char *command = "PROGRAM A";
    char buffer[1024];

    if (!(fpipe = (FILE*) popen(command, "r"))) {
        perror("Problems with pipe");
        exit(1);
    }

    char* outfile = "out.dat";
    //FILE* f = fopen (outfile, "wb");
    //int fd = fileno( f );

    int fd[2];
    fd[0] = open(outfile, O_WRONLY);

    pthread_t thid;
    if (pthread_create(&thid, NULL, thread1, fd) != 0) {
        perror("pthread_create() error");
        exit(1);
    }

    int len;
    while (read(fpipe, buffer, sizeof (buffer)) != 0) {
        len = strlen(buffer);
        write(fd[0], buffer, len);
    }

    pclose(fpipe);

    return (0);
}

I see it's easy to open pipe between two process using fork, but how we can passing open pipe to threads.
Assume we need to pass out of PROGRAM A to PROGRAM B "may by more than one thread",
PROGRAM B send his output to PROGRAM C

EDIT:
I come again after modifying the code to become more easy for reading.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>

void *thread1(void *arg) {

    int status, fd[2];
    pid_t pid;

    pipe(fd);
    pid = fork();

    if (pid == 0) {
        int fd2 = *((int *) (arg));
        dup2(STDIN_FILENO, fd2);

        close(fd[0]);
        dup2(fd[1], STDOUT_FILENO);
        close(fd[1]);

        execvp("PROGRAM B", NULL);
        exit(1);
    } else {
        close(fd[1]);
        dup2(fd[0], STDIN_FILENO);
        close(fd[0]);

        execl("PROGRAM C", NULL);
        wait(&status);

        return NULL;
    }
}

int main(void) {


    FILE *fpipe;
    char *command = "PROGRAM A";
    char buffer[1024];

    if (!(fpipe = (FILE*) popen(command, "r"))) {
        perror("Problems with pipe");
        exit(1);
    }

    char* outfile = "out.dat";
    //FILE* f = fopen (outfile, "wb");
    //int fd = fileno( f );

    int fd[2];
    fd[0] = open(outfile, O_WRONLY);

    pthread_t thid;
    if (pthread_create(&thid, NULL, thread1, fd) != 0) {
        perror("pthread_create() error");
        exit(1);
    }

    int len;
    while (read(fpipe, buffer, sizeof (buffer)) != 0) {
        len = strlen(buffer);
        write(fd[0], buffer, len);
    }

    pclose(fpipe);

    return (0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-09-06 16:23:39

对于进程内消息传递,POSIX 队列可能比管道更适合您的需求。查看man mq_overview(或在线)。

For intra-process messaging, POSIX queues will probably suit your needs better than pipes. Check out man mq_overview (or online).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文