簡體   English   中英

兩個進程之間的FIFO通信

[英]FIFO communication between two process c

我試圖使兩個進程通過命名管道進行通信。 就像這樣,首先我的進程“ prac”派生到n個chidls中(目前僅1個),這些孩子通過信號(下面的代碼中的scanf)接收到必須執行“ execl(dadesPrac)”的時間只需通過FIFO向他的父母寫一個字符串。 問題是,它似乎卡在FIFO中。 我什至沒有通過終端看到消息:

“ printf(”我終於寫了%s \\ n“,buffer);”

編寫過程通過預先表示的execl的參數獲取fifo的“名稱”,如下所示:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX_BUF 64
#include <string.h>
#include <errno.h>
#include <signal.h>
int main(int argc, char **argv){
    int fd;
    char buffer[MAX_BUF] = "";
    char aux[MAX_BUF] = "";
    int MaxTemp;
    printf("%s , %s , %s \n",argv[0],argv[1],argv[2]);
    if(argc == 3){
        srand(time(NULL));
        MaxTemp = (rand() % 50) + 10;
        printf("I'M IN EXEC\n");
        sprintf(aux,"%i",MaxTemp);
        printf(" MAXTEMP  %s\n",aux);
        strcat(buffer,aux);

        strcat(buffer,argv[2]);
        printf("what i'm gonna write is %s\n",buffer);
        fd = open(argv[1],O_WRONLY);    
        write(fd, buffer, sizeof(MAX_BUF));
        printf("i finally wrote %s\n",buffer);
        close(fd);
        //unlink(argv[1]);

    }else{
        perror(" number of parameters incorrect");  
    }



printf("i'm gonna exit\n");
_exit(0);
}

流程prac的代碼如下:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX_BUF 64
#include <string.h>
#include <errno.h>
#include <signal.h>

int senyal = 0;

void empezar(int signum){
    senyal++;
}


void make_fifo(char * myfifo){
    /* crea l'arxiu fifo en cas de que ja no existeixi previament*/
    int m;

    if ((m = mkfifo(myfifo, 0666)) < 0){
        if (errno != EEXIST){
            perror ("mkfifo");
        }
    }
}
int main(int argc,char **argv) {

    int m;
    int pid11;
    int fd, pid1,pid2,pid3,auxint,auxint2,status;
    char * npipe1 = "/tmp/npipe1";
    char buf[MAX_BUF];
    char aux[MAX_BUF];
    pid1 = fork();
    if(pid1 == 0){

        //child1
        make_fifo(npipe1);
        pid11 = fork();

        if(pid11 == 0){
            //grandson1
            printf("inside grandson \n");
            signal(SIGUSR1,empezar);
            while(!senyal){
                printf("im in the while\n");
                sleep(1);       
            }
            printf("i'm out of the  while\n");
            sprintf(aux,"%i",getpid());
        execl("./dadesPrac","dadesPrac",npipe1,aux,NULL);


        }
        printf("almost waitpid\n"); 
        wait(NULL);
        printf("after waitpid\n");
        fd = open(npipe1,O_RDONLY);
        read(fd, buf, sizeof(MAX_BUF));
        close(fd);
        printf("%s",buf);

        //do sql stuff
    }

printf("i'm in dad \n");
scanf("%i",&auxint2);
kill(pid11,SIGUSR1);
wait(NULL);
  return 0;
}

老實說,我迷路了,為可能的錯誤感到抱歉,而不是說母語的人,因此很難清楚地說明自己。

我認為問題在於您處理信號的方式。

當您的prac叉第一次使用時,您已將孩子的pid分配給pid1 然后,父級(原始prac )調用scanf()並等待。

你的孩子的過程( prac子)()再次調用fork,並分配孫子PID號pid11prac孫子)。

然后,您嘗試使用父(原prac )將信號發送到孫子( pid11 )。 但是,父級沒有孫子代的正確pid,因為孫子是由子代代碼而非父代創建的。 父進程中的pid11仍然是其默認值,該默認值在您的代碼中未定義。

因此,當您輸入一些輸入時,父級將執行kill()函數,如果pid11的默認值為0,則意味着將信號發送到具有相同進程組ID(引用kill() )的所有進程。 如果pid11不為0,則信號被發送到錯誤的進程。

另外,您只為孫子進程安裝信號處理程序,因此父子進程均被殺死。 因此,沒人會讀取FIFO。

暫無
暫無

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

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