Reputation: 47
I have a problem, and it is because I execute these two codes and the second one closes the terminal just when I execute it, and the first one gets blocked because of it.
First code:
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#define N 10 // Numero de plazas disponibles en total
int POcupadas;
int main(){
POcupadas = 0;
int sig;
union sigval user_sigval;
sigset_t sigset;
siginfo_t siginfo;
sigemptyset(&sigset);
sigaddset(&sigset, SIGRTMIN);
pthread_sigmask(SIG_BLOCK, &sigset, NULL);
while(1){
sig=sigwaitinfo(&sigset, &siginfo);
int pid = siginfo.si_value.sival_int;
if (sig!=-1){
if (POcupadas != N){
++POcupadas;
user_sigval.sival_int = 0;
sigqueue(pid, SIGRTMIN+1, user_sigval);
}else{
user_sigval.sival_int = 1;
sigqueue(pid, SIGRTMIN+1, user_sigval);
break;
}
}else{
printf("Error");
}
}
return 0;
}
Second Code:
#include <signal.h>
#include <stdio.h>
#include <pthread.h>
int main () {
int sig;
srand(time(NULL));
sigset_t set;
siginfo_t siginfo;
union sigval user_sigval;
int i, num;
sigemptyset(&set);
sigaddset(&set,SIGRTMIN+1);
pthread_sigmask(SIG_BLOCK, &set, NULL);
// PID
int pid = 5845;
// PID
for(i=0; i<30; i++) {
user_sigval.sival_int = getppid();
sigqueue(pid, SIGRTMIN, user_sigval);
sig=sigwaitinfo(&set, &siginfo);
if (siginfo.si_value.sival_int == 0){
printf ("Continue executing the code.\n");
}else{ // No hay sitio 1
printf ("Finish executing the code.\n");
break;
}
sleep(1);
}
return 0;
}
Why is it? What am I doing wrong?
Upvotes: 0
Views: 116
Reputation: 180048
It looks like you are doing it to yourself. Consider this excerpt of the second code:
user_sigval.sival_int = getppid();
sigqueue(pid, SIGRTMIN, user_sigval);
Now look at the central part of the first code:
sig=sigwaitinfo(&sigset, &siginfo);
int pid = siginfo.si_value.sival_int;
if (sig!=-1){
if (POcupadas != N){
++POcupadas;
user_sigval.sival_int = 0;
sigqueue(pid, SIGRTMIN+1, user_sigval);
}else{
user_sigval.sival_int = 1;
sigqueue(pid, SIGRTMIN+1, user_sigval);
break;
}
}else{
printf("Error");
}
Supposing that a process running the second code (process 2) successfully directs its signal to a process running the first code (process 1), process 1 responds by signaling the process whose PID is delivered with the signal. That is process 2's parent (refer to getppid()
in the excerpt from the second code). The default disposition for a real-time signal is process termination.
You probably want process 2 to send its own PID with the signal; that is spelled getpid()
.
Upvotes: 1