Behavior of read function when child receive a sigstop signal





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Explanation of the program



The programs spawns n child process that are paused right after being created. After that, those child processes take turn to talk to the parent process via pipes.



They talk for a fixed amount of time (ex: should talk for a total of 20 times but stop when they've talked 7 times consecutively and pass to the next process)



Flow of the program




  1. Forking all the childs

  2. Pausing them right after their creation

  3. Waking them up by sending a signal

  4. Child write (for a fixed amount of time) to parent via pipe and then stop itself right after sending a signal to parent / Parent read content of pipe and write to STDOUT

  5. When parent receive signal change pipe to read other child and signal wake up the following child


The problem



The problem i'm facing is that when it is time to transition to the next process in order to listen to it, I don't get anything from the pipe of the following child even though I do write on the write-end of the pipe in the child process. Is there something I'm missing about SIGSTOP and SIGCONT when reading.



NB: I did read the docs



Program



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

#define LOOP_SIZE 20;

/* SIG HANDLERS DEFINITION */
void parentsighandler(int);

/* GLOBAL VARIABLE DEFINITION */
pid_t n, ppid, wpid;
int nb_args, read_return;

// array of pid_t to send signals to child
pid_t *child_pids;

volatile sig_atomic_t status = 0;
volatile sig_atomic_t compteur = 0;

int main(int argc, char *argv){
write(STDERR_FILENO, "** Begining the programn", 24); // debug
nb_args = argc - 1;
if (nb_args >= 1 ){
// Variable initialisation
child_pids= malloc(nb_args * sizeof(pid_t));

// String for sprintf
char sstr[40];
char rstr[40];
char pidstr[23];
char strichild[16];
char strloopchild[10];

// Initializing all the pipes for the processes
int p[2 * nb_args];

// getting the pid of the parent process
ppid = getpid();
sprintf(pidstr, "Pid of parent is : %dn", ppid);
write(STDERR_FILENO, pidstr, 23); // debug

int i, j, stop_val;

//Initializing all pipes
write(STDERR_FILENO, "**Init pipesn", 13); // debug
for (i = 0; i < nb_args; i++){
if (pipe(&p[2*i]) == -1){
perror("Error : pipe failed");
_exit(EXIT_FAILURE);
}
}
write(STDERR_FILENO, "**end init pipesn", 17); // debug
write(STDERR_FILENO, "**Begin forkn", 15); // debug
for (i = 0; i < nb_args; i ++){
switch( n = fork()){
case -1:
perror("Error : fork failed");
_exit(EXIT_FAILURE);
case 0:
/* CHILD */
sprintf(strichild, "**In child n%dn", i); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
if (0 == (stop_val = atoi(argv[i+1]))){
perror("atoi failed");
_exit(EXIT_FAILURE);
}
sprintf(strichild, "stop_val is : %dn", stop_val); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
close(p[i*2]);
for (j = 1; j <= 20 ; j++){
sprintf(strloopchild, "loop : %dn", j); // debug
write(STDERR_FILENO, strloopchild , strlen(strloopchild)); // debug
if (j % stop_val == 0){
write(STDERR_FILENO, "I am pausedn", 15); // debug
if ( kill(ppid, SIGUSR1) == -1){
write(STDERR_FILENO, "Could not killn",15); // debug
}
write(STDERR_FILENO, "Sent a sigusr1n",15); // debug
kill(getpid(), SIGSTOP);

}
write(STDERR_FILENO, "In child : ",11);
sprintf(sstr, "J ecris sur l'entree %d du pipen", i*2+1);
write(STDERR_FILENO, sstr, strlen(sstr));
sprintf(sstr, "Je suis le proc %d : message %dn", getpid(), j);
write(STDERR_FILENO, sstr, 40); // debug
write(p[i * 2 + 1], sstr, 40);
sleep(1);

}
close(p[i*2+1]);
write(STDERR_FILENO, "**Exiting the childn", 20); // debug
_exit(EXIT_SUCCESS);

default:
/* PARENT - process scheduling */
write(STDERR_FILENO, "**Parent - puting to sleep childsn", 34); // debug
child_pids[i] = n;
kill(n, SIGSTOP);
// Setting up the signal
signal(SIGUSR1, parentsighandler);
}
}
write(STDOUT_FILENO, "Debut de l'ordonnancementn",26);
char strpids[20];
for (i = 0; i < nb_args; i++){
sprintf(strpids, "id processus : %dn", child_pids[i]);
write(STDOUT_FILENO, strpids, 20);
}
char mystr[17];
sprintf(mystr, "compteur : %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
kill(child_pids[compteur], SIGCONT);
while(1){
if (read(p[2 * compteur], rstr, 40) > 0) {
write(STDOUT_FILENO,"P : ",5);
write(STDOUT_FILENO, rstr, 40);
sprintf(mystr, "compt value: %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
} else {
write(STDERR_FILENO, "Erreur lecturen", 15);
}
sleep(1);
}


} else {
perror("Error : Not enough argsn");
_exit(EXIT_FAILURE);
}
return 0;
}

void parentsighandler(int sig){
if (sig == SIGUSR1){
write(STDOUT_FILENO, "*************n", 14);//debug
write(STDOUT_FILENO, "P : going into sig handlern", 28); // debug
if (compteur == (nb_args - 1)){
compteur = 0;
} else {
compteur ++;
}
kill(child_pids[compteur], SIGCONT);

char strst[19];
sprintf(strst, "P : compteur : %dn", compteur); // debug
write(STDOUT_FILENO, strst, 19); // debug
write(STDOUT_FILENO, "*************n", 14);//debug
}
}









share|improve this question




















  • 2





    If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

    – Barmar
    Jan 3 at 21:02






  • 2





    Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

    – Barmar
    Jan 3 at 21:04






  • 2





    You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

    – Barmar
    Jan 3 at 21:08






  • 1





    The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

    – John Bollinger
    Jan 3 at 21:24








  • 1





    read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

    – John Bollinger
    Jan 3 at 21:26




















0















Explanation of the program



The programs spawns n child process that are paused right after being created. After that, those child processes take turn to talk to the parent process via pipes.



They talk for a fixed amount of time (ex: should talk for a total of 20 times but stop when they've talked 7 times consecutively and pass to the next process)



Flow of the program




  1. Forking all the childs

  2. Pausing them right after their creation

  3. Waking them up by sending a signal

  4. Child write (for a fixed amount of time) to parent via pipe and then stop itself right after sending a signal to parent / Parent read content of pipe and write to STDOUT

  5. When parent receive signal change pipe to read other child and signal wake up the following child


The problem



The problem i'm facing is that when it is time to transition to the next process in order to listen to it, I don't get anything from the pipe of the following child even though I do write on the write-end of the pipe in the child process. Is there something I'm missing about SIGSTOP and SIGCONT when reading.



NB: I did read the docs



Program



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

#define LOOP_SIZE 20;

/* SIG HANDLERS DEFINITION */
void parentsighandler(int);

/* GLOBAL VARIABLE DEFINITION */
pid_t n, ppid, wpid;
int nb_args, read_return;

// array of pid_t to send signals to child
pid_t *child_pids;

volatile sig_atomic_t status = 0;
volatile sig_atomic_t compteur = 0;

int main(int argc, char *argv){
write(STDERR_FILENO, "** Begining the programn", 24); // debug
nb_args = argc - 1;
if (nb_args >= 1 ){
// Variable initialisation
child_pids= malloc(nb_args * sizeof(pid_t));

// String for sprintf
char sstr[40];
char rstr[40];
char pidstr[23];
char strichild[16];
char strloopchild[10];

// Initializing all the pipes for the processes
int p[2 * nb_args];

// getting the pid of the parent process
ppid = getpid();
sprintf(pidstr, "Pid of parent is : %dn", ppid);
write(STDERR_FILENO, pidstr, 23); // debug

int i, j, stop_val;

//Initializing all pipes
write(STDERR_FILENO, "**Init pipesn", 13); // debug
for (i = 0; i < nb_args; i++){
if (pipe(&p[2*i]) == -1){
perror("Error : pipe failed");
_exit(EXIT_FAILURE);
}
}
write(STDERR_FILENO, "**end init pipesn", 17); // debug
write(STDERR_FILENO, "**Begin forkn", 15); // debug
for (i = 0; i < nb_args; i ++){
switch( n = fork()){
case -1:
perror("Error : fork failed");
_exit(EXIT_FAILURE);
case 0:
/* CHILD */
sprintf(strichild, "**In child n%dn", i); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
if (0 == (stop_val = atoi(argv[i+1]))){
perror("atoi failed");
_exit(EXIT_FAILURE);
}
sprintf(strichild, "stop_val is : %dn", stop_val); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
close(p[i*2]);
for (j = 1; j <= 20 ; j++){
sprintf(strloopchild, "loop : %dn", j); // debug
write(STDERR_FILENO, strloopchild , strlen(strloopchild)); // debug
if (j % stop_val == 0){
write(STDERR_FILENO, "I am pausedn", 15); // debug
if ( kill(ppid, SIGUSR1) == -1){
write(STDERR_FILENO, "Could not killn",15); // debug
}
write(STDERR_FILENO, "Sent a sigusr1n",15); // debug
kill(getpid(), SIGSTOP);

}
write(STDERR_FILENO, "In child : ",11);
sprintf(sstr, "J ecris sur l'entree %d du pipen", i*2+1);
write(STDERR_FILENO, sstr, strlen(sstr));
sprintf(sstr, "Je suis le proc %d : message %dn", getpid(), j);
write(STDERR_FILENO, sstr, 40); // debug
write(p[i * 2 + 1], sstr, 40);
sleep(1);

}
close(p[i*2+1]);
write(STDERR_FILENO, "**Exiting the childn", 20); // debug
_exit(EXIT_SUCCESS);

default:
/* PARENT - process scheduling */
write(STDERR_FILENO, "**Parent - puting to sleep childsn", 34); // debug
child_pids[i] = n;
kill(n, SIGSTOP);
// Setting up the signal
signal(SIGUSR1, parentsighandler);
}
}
write(STDOUT_FILENO, "Debut de l'ordonnancementn",26);
char strpids[20];
for (i = 0; i < nb_args; i++){
sprintf(strpids, "id processus : %dn", child_pids[i]);
write(STDOUT_FILENO, strpids, 20);
}
char mystr[17];
sprintf(mystr, "compteur : %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
kill(child_pids[compteur], SIGCONT);
while(1){
if (read(p[2 * compteur], rstr, 40) > 0) {
write(STDOUT_FILENO,"P : ",5);
write(STDOUT_FILENO, rstr, 40);
sprintf(mystr, "compt value: %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
} else {
write(STDERR_FILENO, "Erreur lecturen", 15);
}
sleep(1);
}


} else {
perror("Error : Not enough argsn");
_exit(EXIT_FAILURE);
}
return 0;
}

void parentsighandler(int sig){
if (sig == SIGUSR1){
write(STDOUT_FILENO, "*************n", 14);//debug
write(STDOUT_FILENO, "P : going into sig handlern", 28); // debug
if (compteur == (nb_args - 1)){
compteur = 0;
} else {
compteur ++;
}
kill(child_pids[compteur], SIGCONT);

char strst[19];
sprintf(strst, "P : compteur : %dn", compteur); // debug
write(STDOUT_FILENO, strst, 19); // debug
write(STDOUT_FILENO, "*************n", 14);//debug
}
}









share|improve this question




















  • 2





    If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

    – Barmar
    Jan 3 at 21:02






  • 2





    Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

    – Barmar
    Jan 3 at 21:04






  • 2





    You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

    – Barmar
    Jan 3 at 21:08






  • 1





    The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

    – John Bollinger
    Jan 3 at 21:24








  • 1





    read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

    – John Bollinger
    Jan 3 at 21:26
















0












0








0








Explanation of the program



The programs spawns n child process that are paused right after being created. After that, those child processes take turn to talk to the parent process via pipes.



They talk for a fixed amount of time (ex: should talk for a total of 20 times but stop when they've talked 7 times consecutively and pass to the next process)



Flow of the program




  1. Forking all the childs

  2. Pausing them right after their creation

  3. Waking them up by sending a signal

  4. Child write (for a fixed amount of time) to parent via pipe and then stop itself right after sending a signal to parent / Parent read content of pipe and write to STDOUT

  5. When parent receive signal change pipe to read other child and signal wake up the following child


The problem



The problem i'm facing is that when it is time to transition to the next process in order to listen to it, I don't get anything from the pipe of the following child even though I do write on the write-end of the pipe in the child process. Is there something I'm missing about SIGSTOP and SIGCONT when reading.



NB: I did read the docs



Program



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

#define LOOP_SIZE 20;

/* SIG HANDLERS DEFINITION */
void parentsighandler(int);

/* GLOBAL VARIABLE DEFINITION */
pid_t n, ppid, wpid;
int nb_args, read_return;

// array of pid_t to send signals to child
pid_t *child_pids;

volatile sig_atomic_t status = 0;
volatile sig_atomic_t compteur = 0;

int main(int argc, char *argv){
write(STDERR_FILENO, "** Begining the programn", 24); // debug
nb_args = argc - 1;
if (nb_args >= 1 ){
// Variable initialisation
child_pids= malloc(nb_args * sizeof(pid_t));

// String for sprintf
char sstr[40];
char rstr[40];
char pidstr[23];
char strichild[16];
char strloopchild[10];

// Initializing all the pipes for the processes
int p[2 * nb_args];

// getting the pid of the parent process
ppid = getpid();
sprintf(pidstr, "Pid of parent is : %dn", ppid);
write(STDERR_FILENO, pidstr, 23); // debug

int i, j, stop_val;

//Initializing all pipes
write(STDERR_FILENO, "**Init pipesn", 13); // debug
for (i = 0; i < nb_args; i++){
if (pipe(&p[2*i]) == -1){
perror("Error : pipe failed");
_exit(EXIT_FAILURE);
}
}
write(STDERR_FILENO, "**end init pipesn", 17); // debug
write(STDERR_FILENO, "**Begin forkn", 15); // debug
for (i = 0; i < nb_args; i ++){
switch( n = fork()){
case -1:
perror("Error : fork failed");
_exit(EXIT_FAILURE);
case 0:
/* CHILD */
sprintf(strichild, "**In child n%dn", i); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
if (0 == (stop_val = atoi(argv[i+1]))){
perror("atoi failed");
_exit(EXIT_FAILURE);
}
sprintf(strichild, "stop_val is : %dn", stop_val); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
close(p[i*2]);
for (j = 1; j <= 20 ; j++){
sprintf(strloopchild, "loop : %dn", j); // debug
write(STDERR_FILENO, strloopchild , strlen(strloopchild)); // debug
if (j % stop_val == 0){
write(STDERR_FILENO, "I am pausedn", 15); // debug
if ( kill(ppid, SIGUSR1) == -1){
write(STDERR_FILENO, "Could not killn",15); // debug
}
write(STDERR_FILENO, "Sent a sigusr1n",15); // debug
kill(getpid(), SIGSTOP);

}
write(STDERR_FILENO, "In child : ",11);
sprintf(sstr, "J ecris sur l'entree %d du pipen", i*2+1);
write(STDERR_FILENO, sstr, strlen(sstr));
sprintf(sstr, "Je suis le proc %d : message %dn", getpid(), j);
write(STDERR_FILENO, sstr, 40); // debug
write(p[i * 2 + 1], sstr, 40);
sleep(1);

}
close(p[i*2+1]);
write(STDERR_FILENO, "**Exiting the childn", 20); // debug
_exit(EXIT_SUCCESS);

default:
/* PARENT - process scheduling */
write(STDERR_FILENO, "**Parent - puting to sleep childsn", 34); // debug
child_pids[i] = n;
kill(n, SIGSTOP);
// Setting up the signal
signal(SIGUSR1, parentsighandler);
}
}
write(STDOUT_FILENO, "Debut de l'ordonnancementn",26);
char strpids[20];
for (i = 0; i < nb_args; i++){
sprintf(strpids, "id processus : %dn", child_pids[i]);
write(STDOUT_FILENO, strpids, 20);
}
char mystr[17];
sprintf(mystr, "compteur : %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
kill(child_pids[compteur], SIGCONT);
while(1){
if (read(p[2 * compteur], rstr, 40) > 0) {
write(STDOUT_FILENO,"P : ",5);
write(STDOUT_FILENO, rstr, 40);
sprintf(mystr, "compt value: %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
} else {
write(STDERR_FILENO, "Erreur lecturen", 15);
}
sleep(1);
}


} else {
perror("Error : Not enough argsn");
_exit(EXIT_FAILURE);
}
return 0;
}

void parentsighandler(int sig){
if (sig == SIGUSR1){
write(STDOUT_FILENO, "*************n", 14);//debug
write(STDOUT_FILENO, "P : going into sig handlern", 28); // debug
if (compteur == (nb_args - 1)){
compteur = 0;
} else {
compteur ++;
}
kill(child_pids[compteur], SIGCONT);

char strst[19];
sprintf(strst, "P : compteur : %dn", compteur); // debug
write(STDOUT_FILENO, strst, 19); // debug
write(STDOUT_FILENO, "*************n", 14);//debug
}
}









share|improve this question
















Explanation of the program



The programs spawns n child process that are paused right after being created. After that, those child processes take turn to talk to the parent process via pipes.



They talk for a fixed amount of time (ex: should talk for a total of 20 times but stop when they've talked 7 times consecutively and pass to the next process)



Flow of the program




  1. Forking all the childs

  2. Pausing them right after their creation

  3. Waking them up by sending a signal

  4. Child write (for a fixed amount of time) to parent via pipe and then stop itself right after sending a signal to parent / Parent read content of pipe and write to STDOUT

  5. When parent receive signal change pipe to read other child and signal wake up the following child


The problem



The problem i'm facing is that when it is time to transition to the next process in order to listen to it, I don't get anything from the pipe of the following child even though I do write on the write-end of the pipe in the child process. Is there something I'm missing about SIGSTOP and SIGCONT when reading.



NB: I did read the docs



Program



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>

#define LOOP_SIZE 20;

/* SIG HANDLERS DEFINITION */
void parentsighandler(int);

/* GLOBAL VARIABLE DEFINITION */
pid_t n, ppid, wpid;
int nb_args, read_return;

// array of pid_t to send signals to child
pid_t *child_pids;

volatile sig_atomic_t status = 0;
volatile sig_atomic_t compteur = 0;

int main(int argc, char *argv){
write(STDERR_FILENO, "** Begining the programn", 24); // debug
nb_args = argc - 1;
if (nb_args >= 1 ){
// Variable initialisation
child_pids= malloc(nb_args * sizeof(pid_t));

// String for sprintf
char sstr[40];
char rstr[40];
char pidstr[23];
char strichild[16];
char strloopchild[10];

// Initializing all the pipes for the processes
int p[2 * nb_args];

// getting the pid of the parent process
ppid = getpid();
sprintf(pidstr, "Pid of parent is : %dn", ppid);
write(STDERR_FILENO, pidstr, 23); // debug

int i, j, stop_val;

//Initializing all pipes
write(STDERR_FILENO, "**Init pipesn", 13); // debug
for (i = 0; i < nb_args; i++){
if (pipe(&p[2*i]) == -1){
perror("Error : pipe failed");
_exit(EXIT_FAILURE);
}
}
write(STDERR_FILENO, "**end init pipesn", 17); // debug
write(STDERR_FILENO, "**Begin forkn", 15); // debug
for (i = 0; i < nb_args; i ++){
switch( n = fork()){
case -1:
perror("Error : fork failed");
_exit(EXIT_FAILURE);
case 0:
/* CHILD */
sprintf(strichild, "**In child n%dn", i); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
if (0 == (stop_val = atoi(argv[i+1]))){
perror("atoi failed");
_exit(EXIT_FAILURE);
}
sprintf(strichild, "stop_val is : %dn", stop_val); // debug
write(STDERR_FILENO, strichild, strlen(strichild)); // debug
close(p[i*2]);
for (j = 1; j <= 20 ; j++){
sprintf(strloopchild, "loop : %dn", j); // debug
write(STDERR_FILENO, strloopchild , strlen(strloopchild)); // debug
if (j % stop_val == 0){
write(STDERR_FILENO, "I am pausedn", 15); // debug
if ( kill(ppid, SIGUSR1) == -1){
write(STDERR_FILENO, "Could not killn",15); // debug
}
write(STDERR_FILENO, "Sent a sigusr1n",15); // debug
kill(getpid(), SIGSTOP);

}
write(STDERR_FILENO, "In child : ",11);
sprintf(sstr, "J ecris sur l'entree %d du pipen", i*2+1);
write(STDERR_FILENO, sstr, strlen(sstr));
sprintf(sstr, "Je suis le proc %d : message %dn", getpid(), j);
write(STDERR_FILENO, sstr, 40); // debug
write(p[i * 2 + 1], sstr, 40);
sleep(1);

}
close(p[i*2+1]);
write(STDERR_FILENO, "**Exiting the childn", 20); // debug
_exit(EXIT_SUCCESS);

default:
/* PARENT - process scheduling */
write(STDERR_FILENO, "**Parent - puting to sleep childsn", 34); // debug
child_pids[i] = n;
kill(n, SIGSTOP);
// Setting up the signal
signal(SIGUSR1, parentsighandler);
}
}
write(STDOUT_FILENO, "Debut de l'ordonnancementn",26);
char strpids[20];
for (i = 0; i < nb_args; i++){
sprintf(strpids, "id processus : %dn", child_pids[i]);
write(STDOUT_FILENO, strpids, 20);
}
char mystr[17];
sprintf(mystr, "compteur : %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
kill(child_pids[compteur], SIGCONT);
while(1){
if (read(p[2 * compteur], rstr, 40) > 0) {
write(STDOUT_FILENO,"P : ",5);
write(STDOUT_FILENO, rstr, 40);
sprintf(mystr, "compt value: %dn", compteur); // debug
write(STDOUT_FILENO, mystr, 17); // debug
} else {
write(STDERR_FILENO, "Erreur lecturen", 15);
}
sleep(1);
}


} else {
perror("Error : Not enough argsn");
_exit(EXIT_FAILURE);
}
return 0;
}

void parentsighandler(int sig){
if (sig == SIGUSR1){
write(STDOUT_FILENO, "*************n", 14);//debug
write(STDOUT_FILENO, "P : going into sig handlern", 28); // debug
if (compteur == (nb_args - 1)){
compteur = 0;
} else {
compteur ++;
}
kill(child_pids[compteur], SIGCONT);

char strst[19];
sprintf(strst, "P : compteur : %dn", compteur); // debug
write(STDOUT_FILENO, strst, 19); // debug
write(STDOUT_FILENO, "*************n", 14);//debug
}
}






c unix process fork system






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 21:01









Barmar

435k36260363




435k36260363










asked Jan 3 at 20:15









Alexandre ManetaAlexandre Maneta

2719




2719








  • 2





    If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

    – Barmar
    Jan 3 at 21:02






  • 2





    Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

    – Barmar
    Jan 3 at 21:04






  • 2





    You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

    – Barmar
    Jan 3 at 21:08






  • 1





    The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

    – John Bollinger
    Jan 3 at 21:24








  • 1





    read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

    – John Bollinger
    Jan 3 at 21:26
















  • 2





    If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

    – Barmar
    Jan 3 at 21:02






  • 2





    Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

    – Barmar
    Jan 3 at 21:04






  • 2





    You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

    – Barmar
    Jan 3 at 21:08






  • 1





    The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

    – John Bollinger
    Jan 3 at 21:24








  • 1





    read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

    – John Bollinger
    Jan 3 at 21:26










2




2





If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

– Barmar
Jan 3 at 21:02





If you don't close them in the other children, you won't read EOF from the pipe until all children exit.

– Barmar
Jan 3 at 21:02




2




2





Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

– Barmar
Jan 3 at 21:04





Instead of pipe[2*nb_args], use pipe[nb_args][2], then you don't have to keep multiplying by 2 everywhere.

– Barmar
Jan 3 at 21:04




2




2





You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

– Barmar
Jan 3 at 21:08





You're making lots of assumptions about the order that all the child processes will run. Also, signals aren't queued. If multiple children send SIGUSR1 before the parent's handler runs, it will only see one of them.

– Barmar
Jan 3 at 21:08




1




1





The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

– John Bollinger
Jan 3 at 21:24







The parent should close its copies of the write ends of the pipes, too, since it's the children that write. And if it does so, then just a little bit of care will ensure that the children don't need to worry about that.

– John Bollinger
Jan 3 at 21:24






1




1





read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

– John Bollinger
Jan 3 at 21:26







read() does not normally wait for EOF unless there are no data at all available to return. But it also does not necessarily recapitulate the writer's writes on a 1:1 basis. Data from multiple write()s may be read by the same read(), and under some circumstances, data from one write() may be spread across multiple read()s.

– John Bollinger
Jan 3 at 21:26














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029184%2fbehavior-of-read-function-when-child-receive-a-sigstop-signal%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029184%2fbehavior-of-read-function-when-child-receive-a-sigstop-signal%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas