c - 向家长发送信号不起作用

标签 c pipe signals fork md5

我正在尝试制作一个“揭示”MD5 加密的程序。为此,我使用了 fork()。父级从文件中读取并将读取的单词发送给处理 MD5 并进行比较的子级。

问题是我想通过管道将比较结果发送回父亲,我尝试使用 signal() 来完成此操作,但它不起作用。

你能给我一些帮助吗? 我的“垃圾”代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include<sys/types.h>
#include <openssl/md5.h>
#include <unistd.h>
#include<sys/time.h>
#include"semaforo.h"
#include<sys/wait.h>
#include<signal.h>
#define LONG_MAX_LINEA  100
#define NOM_ARCHIVO  "commonPass.txt"

int boca2[2];
int flag=0;
void senal(int signumber){
    flag=1;
    printf("PAR");
    return;
}




int main()
{
    int boca1[2];
    int pid;
    char linea[LONG_MAX_LINEA];
    char aux;
    struct timeval t1;
    struct timeval t2;
    gettimeofday(&t1, 0);
    int cont=0;
    FILE* entrada;
    int tamLinea;
    int tamSalida;
    const unsigned char* prueba;
    int i;
    int andando=1;
    unsigned char result[MD5_DIGEST_LENGTH];
    int nacimiento;
    sem_bin semaforo(true);
    int x;
    char salida[2];
        const unsigned char md50[MD5_DIGEST_LENGTH] = {0xbe, 0x48, 0x44, 0x81, 0x0c, 0xf8, 0xf6, 0x4c, 0x82, 0x0d, 0x71, 0x88, 0x37, 0xc5, 0xf5, 0x3b};
        const unsigned char md51[MD5_DIGEST_LENGTH] = {0xb3, 0x40, 0x35, 0xd5, 0x66, 0x44, 0xdb, 0x4a, 0xb8, 0x4f, 0x12, 0x93, 0x63, 0x5f, 0x36, 0x64};
        const unsigned char md52[MD5_DIGEST_LENGTH] = {0xf0, 0x4b, 0x5c, 0xaf, 0x6e, 0x61, 0xd9, 0x4c, 0x97, 0x0f, 0x39, 0x2b, 0x13, 0x66, 0x11, 0x18};
        const unsigned char md53[MD5_DIGEST_LENGTH] = {0x27, 0x4f, 0x26, 0x46, 0xa2, 0x69, 0xf5, 0x74, 0x12, 0xd5, 0xfc, 0xd3, 0xbf, 0x9d, 0xfd, 0x0c};
        const unsigned char md54[MD5_DIGEST_LENGTH] = {0x08, 0x70, 0x28, 0x07, 0xb4, 0xd2, 0xf6, 0x2b, 0x65, 0xbb, 0x74, 0x4d, 0x63, 0x16, 0xd1, 0x41};
        const unsigned char md55[MD5_DIGEST_LENGTH] = {0xb8, 0x8f, 0x6a, 0x0f, 0x48, 0xcd, 0x38, 0x30, 0x85, 0x5f, 0x0a, 0xca, 0x82, 0x97, 0xa4, 0x25};




    if(pipe(boca1)== -1 || pipe(boca2)==-1){
        perror("tuberia");
        exit(-1);
    }

    entrada = fopen(NOM_ARCHIVO, "r");

    if (entrada == NULL){
        perror(NOM_ARCHIVO);
        return EXIT_FAILURE;
    }
    if(!semaforo.creado()){
            perror("No se crea el semaforo");
            return EXIT_FAILURE;
    }


    for(nacimiento=0; nacimiento<1; nacimiento++){
    pid=fork();
        if(pid==0){

            break;
        }
    }

    switch(pid){

        case -1: perror("fork");
            exit (-1);
        case 0: 


        while(1){

            semaforo.wait();
            read(boca1[0],&tamLinea, sizeof(int));
            read(boca1[0],linea, tamLinea);
            semaforo.signal();

            linea[tamLinea-1]=0;
            tamLinea-=2;

            if(strcmp(linea, "exitprimo")==0)
                exit(0);


            prueba= reinterpret_cast<const unsigned char *>(linea);

            MD5(prueba, tamLinea, result);    
            cont++;    




            if(!memcmp(result,md50,MD5_DIGEST_LENGTH) || !memcmp(result,md51,MD5_DIGEST_LENGTH)
            || !memcmp(result,md52,MD5_DIGEST_LENGTH) || !memcmp(result,md53,MD5_DIGEST_LENGTH)
            || !memcmp(result,md54,MD5_DIGEST_LENGTH) || !memcmp(result,md55,MD5_DIGEST_LENGTH)){
                signal(SIGUSR1,senal);
                sleep(1);

             }


        }






        default: 
        while(1){

            fgets(linea, LONG_MAX_LINEA, entrada);

            if(feof(entrada)){
            for(x=0; x<1; x++){            
            write(boca1[1],"exitprimo",9);
            }
            gettimeofday(&t2, NULL);

    printf("eso es todo el archivo\n\n");

    printf("Tiempo trancurrido: %f seg.\n\n", (double)(t2.tv_usec-t1.tv_usec)/1000000 +
                        (double)(t2.tv_sec - t1.tv_sec));
            exit(0);
            }
            tamLinea=strlen(linea)+1;
            write(boca1[1],&tamLinea, sizeof(int));
            write(boca1[1], linea, tamLinea);



        }


    }







}

最佳答案

您没有向USR1注册信号处理程序,因此没有理由调用该函数void senal(intsignnumber)

你可以看看我的帖子,它可能会有所帮助...... https://omerdagan.000webhostapp.com/linux_signal.html 希望这可能有所帮助...它的在线流程也有很好的记录

注意:我没有看过你的其余逻辑

关于c - 向家长发送信号不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118243/

相关文章:

c - qemu 跟踪哪些指令?

c - 试图理解c中的死锁

c - 自问自答,利用系统信号制作Linux单调定时器

linux - 如何从 GTK cairo 中的函数发出 "draw"信号

c++ - if-else 语句中的 "equal to"与 "not equal to"运算符

无法计算出此 C 程序中使用函数计算总和和平均值的输出

C - Unix 管道,close() 影响输出?

unix - 如何将 find 命令返回的文件列表通过管道传递给 cat 以查看所有文件

c++ - 通过 Linux 将字节文件通过管道传输到 C++ 程序并逐字节读取?

c - 在 sigaction 处理程序中使用 flock()