Reputation: 323
I'm triying to make two process communicate through a named pipe. It goes like this, first my process "prac" forks into n chidls ( just 1 for the moment) these childs receive via signal(a scanf in the code below) the time when they have to lauch an "execl(dadesPrac)" who just have to write through the FIFO a string to his parent. The problem is, it seems like it gets stuck writting in the FIFO. I don't even see via terminal the message :
"printf("i finally wrote %s\n",buffer);"
The process who writtes gets the "name" of the fifo via the parameters of the execl prevoiusly said, it goes like this:
#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);
}
The code of the process prac is the following:
#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;
}
I'm honestly lost, sorry for the possible mistakes, not a native speaker so it's kinda hard to explain myself clearly.
Upvotes: 0
Views: 1010
Reputation: 1331
I think the issue is the way you do fork and handle signal.
When your prac
fork for the 1st time, you have assigned the child's pid to pid1
.
The parent (original prac
) then calls scanf() and wait.
Your child process (prac
child) calls fork() again, and you assign the grandchild's pid to pid11
(prac
grandchild).
Then you are trying to use the parent (original prac
) to send the signal to the grandchild (pid11
). However, the parent does not have the correct pid of the grandchild, because grandchild is create by the child code not the parent. The pid11
in the parent process is still its default value which is undefined in your code.
So when you enter some input, the parent executes kill()
function and if the default value to pid11
is 0, then it means sending the signal to all the process with same process group id (reference of kill()
). If pid11
is not 0, the signal are send to the wrong process.
Also, you code only install signal handler for the grandchild process, so both parent and son processes are killed. Hence no one is going to read the FIFO.
Upvotes: 1