簡體   English   中英

C - 在進程之間來回發送數據

[英]C - Sending data back and forth between processes

我正在研究下面的程序。 它應該做的是從子進程中獲取隨機 int,並根據 item_count 變量,通過使用管道將適當的消息返回給子進程。 問題是我在執行時遇到了很多意外行為,見下文。 myCatalog 是一個結構。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#include <string.h>

struct catalog {
    char description[128];
    int price;
    int item_count;
};

int main() {
    int i, j, k, fd1[5][2], fd2[5][2];
    struct catalog myCatalog[3];
    pid_t pid;
    int itemNo;
    char message[24];
    char messageFailure[] = "Failure.", messageSuccess[] = "Success!";;        

 

    for(i = 0; i < 5; i++) {
        if(pipe(fd1[i]) == -1) {
                printf("Error opening pipe 1!\n");
                exit(1);
        }

        if(pipe(fd2[i]) == -1) {
            printf("Error opening pipe 2!\n");
            exit(1);
        }

        pid = fork();

        if(pid == 0) { //Child
            time_t t;
            srand((int)time(&t) % getpid());
            
            close(fd1[i][0]);  //Close reading end of fd1
            close(fd2[i][1]);  //Close writing end of fd2
            
            for(k = 0; k < 10; k++) {
                itemNo = rand() % 21;
                write(fd1[i][1], &itemNo, sizeof(int));
                sleep(1);
                read(fd2[i][0], message, sizeof(message));
                printf("%s\n", message);
            }
            printf("Hello from child process %d\n", getpid());
            close(fd1[i][1]);
            close(fd2[i][0]);
            exit(0);
        }
        else if(pid > 0) { //Parent
            int item;

            close(fd1[i][1]);  //Close writing end of fd1
            close(fd2[i][0]);  //Close reading end of fd2

            if(i == 0) {
                for(j = 0; j < 20; j++) {
                    snprintf(myCatalog[j].description, sizeof(myCatalog[j].description), "Item #%d", j);
                    myCatalog[j].price = (rand() % 10) + 1;
                    myCatalog[j].item_count = 2;
                }
                for(j = 0; j < 20; j++) {
                    printf("Description: %s\n", myCatalog[j].description);
                    printf("Price: %d\n", myCatalog[j].price);
                    printf("Count: %d\n", myCatalog[j].item_count);
                }   
            }
            for(j = 0; j < 10; j++) {
                read(fd1[i][0], &item, sizeof(int));
                printf("%d\n", item);
                myCatalog[item].item_count = myCatalog[item].item_count - 1;
                if(myCatalog[item].item_count <= 0) {
                    write(fd2[i][1], messageFailure, sizeof(messageFailure));
                }
                else {
                    write(fd2[i][1], messageSuccess, sizeof(messageSuccess));                
                }
                sleep(1);
            }
            printf("Hello from parent process %d\n", getpid());
            close(fd1[i][0]);
            close(fd2[i][1]);
            wait(NULL);
        }
        else {
            printf("Error forking!");
            exit(1);
        }

    }
    return 0;
}

一個子進程的示例 output 如下所示:

Hello from parent process 11868
Hello from child process 11890
6
Success!
4
Success!
3
Success!
4
3
16
Success!
14
3
6
3
18
3
20
3
18
3

它應該做的是打印數字,然后是成功或失敗等。此外,它有時會輸出 13 個數字而不是 10 個。問題是,問題出在哪里? 是我的管道嗎? 我的循環使用完全錯誤嗎? 任何幫助將不勝感激。

這看起來肯定不對。 對於 n > 2 訪問myCatalog[n]調用未定義的行為。

  struct catalog myCatalog[3];
  ...
  for(j = 0; j < 20; j++) {
          printf("Description: %s\n", myCatalog[j].description);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM