Code that send signals, one of the terminals closes - c

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?

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().

Related

Trying to randomly send signal from parent to child process in a loop

I'm trying to have it execute in a loop where the parent randomly picks between SIGUSR1 and SIGUSR2 and send it to the child process to receive and write to a file
My problem is the signal will only send in the first loop and after that it stops
int main(int argc, char* argv[], char *envp[]){
time_t start, finish; //for example purposes, to save the time
struct sigaction sact; //signal action structure
sact.sa_handler = &handler;
sact.sa_handler = &handler2;
sigset_t new_set, old_set; //signal mask data-types
FILE *file = fopen("received_signal.txt", "w");
fprintf(file,"%s\t %s\t %s\n", "Signal Type",
"Signal Time", "thread ID");
fclose(file);
int pid;
int cpid;
pid = fork();
if(pid == 0){//recieves
//sigaction(SIGUSR1, &sact, NULL);
while(1){
signal(SIGUSR1, handler);
signal(SIGUSR2, handler2);
sleep(1);
}
} else{ //generates
while(1){
sleep(1); // give child time to spawn
printf("hello\n");
parent_func(0);
//wait(NULL);
usleep(((rand() % 5) + 1) * 10000);
}
}
return 0;
}
void parent_func(int child_pid){
srand(time(NULL));
int rnd = rand();
int result = (rnd & 1) ? 2 : 1;
struct timeval t;
gettimeofday(&t, NULL);
unsigned long time = 1000000 * t.tv_sec + t.tv_usec;
printf("result: %d\n", result);
printf("time: %ld\n", time);
if(result == 1){
//sigaction(SIGUSR1, &sact, NULL);
kill(child_pid, SIGUSR1);
log(SIGUSR1);
} else{
//sigaction(SIGUSR2, &sact, NULL);
kill(child_pid, SIGUSR2);
log(SIGUSR2);
}
}
void handler(int sig){
if (sig == SIGUSR1){
puts("child received SIGUSR1");
}
}
void handler2(int sig){
if (sig == SIGUSR2){
puts("child received SIGUSR2");
}
}
Tried throwing the child in a while loop to get it to repeat but no such luck
man signal(2) tells you that the handler is reset to SIG_DFL once a signal is delivered:
If the disposition is set to a function, then first either the disposition is reset to SIG_DFL, or the signal is blocked (see Portability below), and then handler is called with argument signum. If invocation of the handler caused the signal to be blocked, then the signal is unblocked upon return from the handler.
I suggest you use sigaction instead of signal:
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 199309L
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
void handler(int sig) {
char s[] = "child received signal SIGUSR?\n";
char *s2 = strchr(s, '?');
*s2 = sig == SIGUSR1 ? '1' : '2';
write(STDOUT_FILENO, s, strlen(s));
}
int main(int argc, char* argv[], char *envp[]){
pid_t child_pid = fork();
if(!child_pid) {
struct sigaction sa = {
.sa_handler = &handler
};
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
for(;;) {
sleep(1);
}
return 0;
}
for(;;) {
sleep(1);
int s = (int []){SIGUSR1, SIGUSR2}[rand() % 2];
printf("parent sending signal %d to %d\n", s, child_pid);
kill(child_pid, s);
}
}
and sample output:
parent sending signal 12 to 521586
child received signal SIGUSR2
parent sending signal 10 to 521586
child received signal SIGUSR1
parent sending signal 12 to 521586
child received signal SIGUSR2
parent sending signal 12 to 521586
child received signal SIGUSR2

System Calls in C signals and fork

Hi I've this problem to solve with a functional program in C.
"Write a C program where a process F create a childprocess C.
The childprocess C waits the user to type the password, if is correct sends a signal SIGUSR1 to the father, if after 3 attempts the password is still incorrect it will send a SIGUSR2 signal to the father and terminate; if it receives from the father SIGUSR1 signal must stop viewing the "timeout" message.
His father after 30 seconds (if it has not received any signal from the child) must send the signal SIGUSR1 to the child and end with exit(1); if it receives the SIGUSR1 signal must end with exit(0); if it receives the signal SIGUSR2 must end with exit (2)."
I'm trying to solve it but I'm stuck. This is what I've done:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
void fatherprocess(int mysignal){
if (mysignal == SIGUSR1) {
printf("ACCESS GRANTED!\n");
exit(0);
}
if (mysignal == SIGUSR2){
printf("ACCESS DISCARDED! More than 3 tentatives!\n");
exit(2);
}
}
void childprocess(int mysignal){
if (mysignal == SIGUSR1) {
printf("TIMEOUT\n");
exit(1);
}
}
int main(int argc, char *argcv[]){
int fatherpid, childpid;
char enteredpassword[], password[] = "test";
int i =0;
unsigned int time_to_sleep = 30;
fatherpid = getpid();
childpid = fork();
if (childpid == 0) {
printf("Child Process waiting for a password\n");
while (1){
if (i < 3) {
printf("Enter Password: ");
scanf("%s", enteredpassword);
if (enteredpassword == password)
signal(SIGUSR1, fatherprocess);
} else {
signal(SIGUSR2, fatherprocess);
exit(1);
}
i++;
}
} else {
printf("Father Process\n");
while(time_to_sleep){
time_to_sleep = sleep(time_to_sleep);
signal(SIGUSR1, childprocess);
}
}
return 0;
}
I've edited my program in this way:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
void fatherprocess(int mysignal, int fatherpid){
if (mysignal == SIGUSR1) {
printf("ACCESS GRANTED!\n");
kill(fatherpid, SIGUSR1);
exit(0);
}
if (mysignal == SIGUSR2){
printf("ACCESS DISCARDED! More than 3 tentatives!\n");
kill(fatherpid, SIGUSR2);
exit(2);
}
}
void childprocess(int mysignal, int childpid){
if (mysignal == SIGUSR1) {
printf("TIMEOUT\n");
kill(childpid, SIGUSR1);
exit(1);
}
}
int main(int argc, char *argcv[]){
int fatherpid, childpid;
char enteredpassword[] = "test", password[] = "test";
int i =0;
unsigned int time_to_sleep = 30;
fatherpid = getpid();
childpid = fork();
if (childpid == 0) {
printf("Child Process waiting for a password\n");
while (1){
if (i < 3) {
printf("Enter Password: ");
scanf("%s", enteredpassword);
if (strcmp(enteredpassword, password) == 0)
fatherprocess(SIGUSR1, fatherpid);
} else {
fatherprocess(SIGUSR2, fatherpid);
exit(1);
}
i++;
}
} else {
printf("Father Process\n");
while(time_to_sleep){
time_to_sleep = sleep(time_to_sleep);
childprocess(SIGUSR1, childpid);
}
}
return 0;
}
Now it works perfectly but I don't know if I've respected the exercise text.
As was mentioned in the comments (by Jonathan Leffler), you need to use the kill() system call (to send the signals) and register a signal handler using a call like sigaction(). I have linked these two calls to online manual pages that provide additional information about them.
Here's some code that demonstrates how these can be used towards achieving your stated goal. You will still need to add/modify the code for things like the prompts you want and the acceptable input string. Please note that I'm not claiming this to be the best way to do it, only that it's an example of how it could be done (it compiled and worked for me):
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
static void get_password(char* buf, int maxbuf)
{
fgets(buf, maxbuf, stdin);
}
static int is_password_correct(char* buf)
{
return buf[0] == 'a';
}
volatile int got_signal = 0;
volatile int child_signal = 0;
static void parent_sig_handler(int signum)
{
if (!got_signal)
{
got_signal = signum;
printf("parent_sig_handler: got sig %d\n", signum);
}
}
static void child_sig_handler(int signum)
{
if (!child_signal)
{
child_signal = signum;
printf("child_sig_handler: got sig %d\n", signum);
}
}
int main()
{
struct sigaction act;
sigfillset(&act.sa_mask);
act.sa_handler = parent_sig_handler;
sigaction(SIGALRM, &act, NULL);
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &act, NULL);
pid_t child_pid = fork();
if (child_pid == -1)
{
perror("error forking");
exit(3);
}
if (child_pid == 0)
{
printf("child running\n");
act.sa_handler = child_sig_handler;
sigaction(SIGUSR1, &act, NULL);
pid_t parent_pid = getppid();
for (int i = 0; i < 3; ++i)
{
char passwd[64];
passwd[0] = '\0';
get_password(passwd, sizeof(passwd));
if (is_password_correct(passwd))
{
kill(parent_pid, SIGUSR1);
exit(0);
}
}
kill(parent_pid, SIGUSR2);
exit(2);
}
printf("parent running\n");
alarm(30); /* sets parent up to receive a SIGALRM signal in 30 seconds */
sigset_t sigmask;
sigemptyset(&sigmask);
while (!got_signal)
{
sigsuspend(&sigmask);
}
switch (got_signal)
{
case SIGALRM:
kill(child_pid, SIGUSR1);
exit(1);
case SIGUSR1:
exit(0);
case SIGUSR2:
exit(2);
default:
exit(3);
}
exit(3);
}

C synchronize processes using signal

Okay so I am trying to teach myself on how to do signalling, and I came across a hiccup and I can't figure out what I'm doing wrong. What is going on right now is: it is executing the parent then goes to child and then back to parent.. It's not doing what I want it to do which is execute the parent (which the user defines the amount of time it runs) then kills it then go to child and run itself at the same amount of time.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h> // for wait
#include <sys/wait.h> // for wait
void action(int);
void action(int dummy){
sleep(1);
printf("Switching\n");
}
int main(int argc, char *argv[]){
pid_t pid;
int m = atoi(argv[1]), i = 0, x = 0;
if((pid=fork())>0){//parent
sleep(1);
while(i < m){
printf("hello %d\n", x);
x++;
kill(pid, SIGUSR1);
signal(SIGUSR1, action);
pause();
i++;
}
}
else
while(i < m){//child
//waitpid(getppid(), &status, 0); // wait for parent
signal(SIGUSR1, action);
pause();
printf("hi%d\n", x);
x++;
kill(getppid(), SIGUSR1);
i++;
}
}
What I want it to do is:
hello 0
hello 1
hello 2
hello 3
hello 4
Switching
hi 0
hi 1
hi 2
hi 3
hi 4
Any help is much appreciated!
You've got most of the pieces, they just need to be reordered a little bit.
install the signal handler in both processes before using kill
the parent should finish printing before signaling the child
the child can signal back after its done printing
void action(int dummy)
{
sleep(1);
printf("Switching\n");
}
int main(int argc, char *argv[])
{
int m = 3;
if (argc == 2)
m = atoi(argv[1]);
pid_t pid = fork(); // create the child process
signal(SIGUSR1, action); // set up the signal handler for both parent and child
if ( pid > 0 ) // the parent
{
for ( int i = 0; i < m; i++ )
{
sleep(1);
printf("hello %d\n", i);
}
kill( pid, SIGUSR1 ); // signal the child
pause(); // wait for the child to signal back
printf("All done\n");
}
else // the child
{
pause(); // wait for the signal from the parent
for ( int i = 0; i < m; i++ )
{
sleep(1);
printf("hi %d\n", i);
}
kill(getppid(), SIGUSR1); // signal the parent
}
}

Two signals in the same main

I want to use two signals in the same main. So I made two handlers etc. That's my code:
volatile sig_atomic_t go_on = 0;
volatile sig_atomic_t execute = 0;
void sig_syn(int sig_no)
{
go_on = 1;
}
void exe_handler(int sig_no)
{
execute = 1;
}
struct sigaction action;
sigset_t mask;
struct sigaction e_action;
sigset_t e_mask;
sigfillset (&mask);
action.sa_handler = sig_syn;
action.sa_mask = mask;
action.sa_flags = 0;
sigaction (SIGRTMIN, &action, NULL);
sigfillset (&e_mask);
e_action.sa_handler = exe_handler;
e_action.sa_mask = e_mask;
e_action.sa_flags = 0;
sigaction (SIGRTMIN, &e_action, NULL);
while(go_on == 0){}
go_on = 0;
.
.
.
while(execute == 0){}
execute = 0;
.
.
.
Is it correct that i use all these two times? The reason I ask is because my program doesn't run but no errors appear... Any help? Thanks in advance!
First of all, if your program doesn't run try out putting some debugging, gdb would be better, but printfs can do the job.
A Unix program can receive a lot of signals, checkout "man signal" to the usage and "man 7 signal" to all signals.
I'written and tested the following code.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void
termination_handler (int signum)
{
printf("Signal %d\n",signum);
exit(0);
}
int signal1 = 0;
void
usr_signal1(int signum)
{
printf("Signal 1 received\n");
signal1 = 1;
}
int signal2 = 0;
void
usr_signal2(int signum)
{
printf("Signal 2 received\n");
signal2 = 1;
}
int
main (void)
{
printf("My pid is : %d\n",getpid());
if (signal (SIGTERM, termination_handler) == SIG_IGN)
signal (SIGTERM, SIG_IGN);
if (signal (SIGUSR1, usr_signal1) == SIG_IGN)
signal(SIGUSR1, SIG_IGN);
if (signal (SIGUSR2, usr_signal2) == SIG_IGN)
signal(SIGUSR2, SIG_IGN);
printf("Main has started\n");
while(0 == signal1) { sleep(1); };
printf("Main moved to stade 1 \n");
while(0 == signal2) { sleep(1); };
printf("Main moved to stade 2 \n");
printf("Main is done ! \n");
return 0;
}
After compiling and running, it will print it's pid and keep waiting signals SIGUSR1 and SIGUSR2.
$ ./main
My pid is : 6365
Main has started
Signal 1 received
Main moved to stade 1
Signal 2 received
Main moved to stade 2
Main is done !
Sending the kills with
kill -10 6365
kill -12 6365
works.

C: Signal sending to children causes infinity loop

I've got following problem with my code. It(children) should start counting from 0 when child process receives SIGHUP (works fine), but when parent process gets signal hup it should make all children count from 0 again, but when signal is sent then something like infinity loop happens and on_hup function for parent is called all time and doesn''t stop :( Thanks in advance.
#include <signal.h>
#include <stdio.h>
#include <wait.h>
#include <unistd.h>
#include <sys/types.h>
//#include "err.h"
#define NR_PROC 3
pid_t pid;
int a;
int pids[3];
void on_hup(int sig){
int i;
if(pid!=0)
kill(0,SIGHUP);
else
a=0;
}
void pierwsze(){
for (;a<10000;a++){
printf("%d: %d PID: %d \n",getpid(),a,pid);
fflush(stdout);
sleep(2);
}
}
struct sigaction init(){
struct sigaction sa;
sigset_t block_mask;
sigemptyset (&block_mask);
sa.sa_mask = block_mask;
sa.sa_flags=0;
sa.sa_handler=on_hup;
return sa;
}
int main(){
int i;
a=0;
struct sigaction setup_action = init();
sigaction(SIGHUP,&setup_action,0);
for(i=0;i<NR_PROC;i++){
switch(pid = fork()){
case -1:
fprintf(stderr,"Error w fork \n");
case 0:
pids[i]=getpid();
pierwsze();
return 0;
default:
printf("I am parent My PID = %d\n",getpid());
printf("fork = %d\n",pid);
}
}
while(1){}
wait(0);
return 0;
}
the array pids cannot be shared between the parent and child.
So instead of trying to put the pid into the array from the context of the child, you can insert it from the parent's context:
Code will look like:
case 0:
pierwsze();
return 0;
default:
pids[i] = pid;
printf("I am parent My PID = %d\n",getpid());
printf("fork = %d\n",pid);

Resources