向父母发送信号不起作用

问题描述 投票:-1回答:1

我试图制作一个“揭示”MD5加密的程序。为此,我使用了fork()。Parent从文件中读取并将readed一词发送给处理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);



        }


    }







}
c pipe signals fork md5
1个回答
0
投票

tou没有向USR1注册一个信号处理程序,因此没有理由将该函数称为void senal(int signumber)

你可以查看我的帖子,它可能会有所帮助... https://omerdagan.000webhostapp.com/linux_signal.html希望可能有所帮助...这也是非常好的文档处理在线

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

© www.soinside.com 2019 - 2024. All rights reserved.