I have a problems to communicate a signal from parent process to child, cause its like the child didnt open correctly cause when i do if(oct_pid == 0) i cant enter in.
calculator.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
void octal(int fd){
int valor;
while (1)
{
pause();
read(fd,&valor,sizeof(int));
printf("%o",valor);
}
}
void hexa(int fd){
int valor;
while (1)
{
pause();
read(fd,&valor,sizeof(int));
printf("%X",valor);
}
}
int main(){
int canal,pipe_fd[2],octal_pid,hexa_pid;
unlink("canal");
if(mkfifo("./canal", 00660)!=0){
printf("Error al crear la pipe");
exit(EXIT_FAILURE);
}
canal = open("./canal",O_RDONLY);
if(canal < 0){
printf("Error al obrir la pipe.");
exit(EXIT_FAILURE);
}
pipe(pipe_fd);
octal_pid = fork();
if(octal_pid == -1){
printf("Error al crear el proceso octal.\n");
exit(EXIT_FAILURE);
} else if(octal_pid == 0){
close(pipe_fd[1]);
octal(pipe_fd[0]);
signal(SIGUSR1,octal);
} else {
hexa_pid = fork();
if(hexa_pid == -1){
printf("Error al crear el proceso hexadecimal.\n");
kill(octal_pid, SIGTERM);
exit(EXIT_FAILURE);
} else if(hexa_pid == 0){
close(pipe_fd[1]);
hexa(pipe_fd[0]);
signal(SIGUSR1,hexa);
} else {
close(pipe_fd[0]);
while(1){
char c;
int valor;
read(canal,&c,sizeof(char));
read(canal,&valor,sizeof(int));
if(c == 'o'){
printf("Calculant en octal ... \n");
write(pipe_fd[1], &valor, sizeof(int));
kill(octal_pid,SIGUSR1);
}
else if(c == 'h'){
printf("Calculant en hexadecimal ... \n");
write(pipe_fd[1], &valor, sizeof(int));
kill(hexa_pid,SIGUSR1);
}
else if( c == 'x'){
printf("Sortint...\n");
kill(octal_pid, SIGTERM);
kill(hexa_pid, SIGTERM);
break;
}
}
close(canal);
close(pipe_fd[1]);
waitpid(octal_pid, NULL, 0);
waitpid(hexa_pid, NULL, 0);
}
}
return 0;
}
client.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
int fd, valor;
char operacio;
int llegir_numero()
{
char buff [10];
int llegits;
llegits=read(0, buff, 10); //llegir del teclat un màxim de 10 chars
buff[llegits-1] = (char)0; //canvi del \n per un \0
return(atoi(buff)); //es converteix a enter
}
int main(){
fd = open("./canal",O_WRONLY);
if(fd < 0){
printf("La pipe no s'ha obert correctament.");
exit(EXIT_FAILURE);
}
do{
printf("Introdueix 'o' o 'h', x per acabar\n");
scanf(" %c",&operacio);
if(operacio == 'o' || operacio == 'h'){
printf("Introdueix un valor enter positiu:\n");
valor = llegir_numero();
write(fd,&operacio,sizeof(char));
write(fd,&valor,sizeof(int));
}
else if(operacio != 'x'){
printf("Lletra incorrecte.");
}
}
while(operacio != 'x');
close(fd);
return 0;
}
I have this code where I have a problem with kill(octal_pid,SIGUSR1), when, in theory, I send the signal to the child process octal_pid, it didnt work cause I cant enter in this pipe and idk why. Thanks for the help