I have to create a program that generates 2 childs and each of them has to generate a random number. After that the child who generated the lowest number has to send a SIGUSR1 to the other child. In my case i wanna send a SIGCONT to child 1 to wake him up so that he can send SIGUSR1 to the other process but child 1 doesn't wake up.. any help? Thanks in advance.
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#define N 2
int getRand(int upper)
{
srand(time(0));
int random;
random = rand() % upper;
return random;
}
void sighandler(int signo)
{
if (signo == SIGUSR1)
{
printf("Received SIGUSR1, my PID is %d\n\n", getpid());
exit(0);
}
if (signo == SIGUSR2)
{
printf("Received SIGUSR2.. I woke up! (My PID is %d)\n\n", getpid());
}
}
int main (int argc, char **argv)
{
int i, r, m, b;
int status = 0;
int tabcpid[N], ppid, wpid;
int fd[2], fdbool[2]; //fd [0 = read] [1 = write]
sigset_t set, zeromask;
struct sigaction action;
//Gestione segnali
sigemptyset(&zeromask);
sigemptyset(&action.sa_mask);
action.sa_handler = sighandler;
action.sa_flags = 0;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigprocmask(SIG_BLOCK, &set, NULL);
if (sigaction(SIGUSR1, &action, NULL) == -1)
{
perror("Error while doing sigaction.\n\n");
}
if (pipe(fd) == -1)
{
printf("Error opening pipe fd!\n\n");
exit(1);
}
if (pipe(fdbool) == -1)
{
printf("Error opening pipe fdbool!\n\n");
exit(1);
}
printf("\nPipes opened successfully. Forking ...\n\n");
sleep(2);
for (i = 0; i < N; i++)
{
if ((tabcpid[i] = fork()) == 0) //Child code
{
int n = atoi(argv[1]);
m = getRand(n);
b = 20;
ppid = getppid();
printf("I'm the son process #%d with PID: %d\n", i + 1, getpid());
printf("Random number in interval 0 - %d: %d\n\n", n, m);
sleep(2);
if (i == 0)
{
close(fd[0]);
write(fd[1], &m, sizeof(int));
close(fd[1]);
printf("Suspending..\n\n");
sigsuspend(&zeromask);
printf("So' ripartitoo\n\n");
/*
close(fdbool[1]);
read(fdbool[0], &b, sizeof(int));
close(fdbool[0]);
printf("--- b value: %d\n\n", b);
if (b == 0)
{
printf("I'm the process %d and I got the lowest number, SIGUSR1 sent to my brother.\n\n", getpid());
kill(tabcpid[1], SIGUSR1);
}
sleep(2);
*/
}
else
{
close(fd[1]);
read(fd[0], &r, sizeof(int));
close(fd[0]);
int lower = (r < m) ? r : m;
int igotlower = (m < r) ? 1 : 0;
printf("--- igotlower value: %d\n\n", igotlower);
close(fdbool[0]);
write(fdbool[1], &igotlower, sizeof(int));
close(fdbool[1]);
//printf("Got %d from other child process, while i got %d.\nThe smallest number is %d.\nMy PID is %d and the other process' PID is %d.\n\n", r, m, lower, getpid(), tabcpid[0]);
//sleep(2);
if (igotlower == 1)
{
printf("I'm the process %d and I got the lowest number, SIGUSR1 sent to my brother..\n\n", getpid());
kill(tabcpid[0], SIGUSR1);
sigsuspend(&zeromask);
}
else
{
printf("I'm sending SIGCONT to %d\n\n", tabcpid[0]);
kill(tabcpid[0], SIGCONT);
sigsuspend(&zeromask);
}
}
sleep(2);
}
sleep(2);
}
wait(&status);
close(fdbool[1]);
read(fdbool[0], &b, sizeof(int));
close(fdbool[0]);
kill(tabcpid[b], SIGUSR1);
wait(&status);
printf("It's me the father... it's all over, we're done!\n\n");
sleep(2);
return 0;
}
Related
In this simple C code, I am trying to create 10 children. Then do some work (such as printing the time) in them individually. Then I try to kill all of them in parent process. You will easily understand the code below. The problem is that, whenever I run this, first it gives the various outputs from children then Linux shuts down. Then I start from passing user password and everything is closed. Why this happens?
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
int main() {
pid_t pids[10];
int i;
int n= 10;
for (i= 0; i<n; ++i) {
if ((pids[i] = fork()) <0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {
struct timeval tv;
time_t t;
struct tm*info;
char buffer[64];
gettimeofday(&tv,NULL);
t = tv.tv_sec;
info = localtime(&t);
sleep(1);
printf("CHILD PID: %d and the time is %s\n",getpid(),asctime(info));
}
}
int status;
pid_t pid;
sleep(5);
while (n >0) {
pid = wait(&status);
kill((long)pid, SIGTERM);
printf("CHILD %d killed.",pid);
--n;
}
}
In process created by fork() all variables have the same value as before fork. After fork in this your code
for (i= 0; i<n; ++i) {
if ((pids[i] = fork()) <0) {
perror("fork");
abort();
}
else if (pids[i] == 0) {
...
}
}
i in child process is the same as it was in parent before fork, so for loop continue to execute and spawn a lot of grand children processes.
After loop is executed all spawned processes call wait(). Processes which are spawned on last iteration (with i==9) have no children, so wait() indicates error by returning -1 (and errno==ECHILD). Then you call kill() with pid==-1, which is:
If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init)
If you want child processes to terminate by themself then you should exit explicitly, e.g. with _exit() function. In this case you don't need to send TERM signal:
int main(void)
{
pid_t pids[10];
int i;
int n = 10;
for (i = 0; i<n; ++i)
{
if ((pids[i] = fork()) < 0)
{
perror("fork");
abort();
// abort() never return, "else" is not needed
}
if (pids[i] == 0)
{
struct timeval tv;
time_t t;
struct tm*info;
gettimeofday(&tv,NULL);
t = tv.tv_sec;
info = localtime(&t);
sleep(1);
printf("CHILD PID: %d and the time is %s\n",getpid(),asctime(info));
_exit(0); // terminate process explicitly
}
}
int status;
pid_t pid;
sleep(5);
while (n > 0)
{
pid = wait(&status);
// No kill needed for already exited process
// kill((long)pid, SIGTERM);
if (pid == -1)
{
perror("wait");
abort();
} else {
printf("CHILD %d killed.\n", pid);
}
--n;
}
}
If you want to terminate child processes from parent process then children should wait until terminated, but you should send signal before waiting for child process termination:
int main(void)
{
pid_t pids[10];
int i;
int n = 10;
for (i = 0; i<n; ++i)
{
if ((pids[i] = fork()) < 0)
{
perror("fork");
abort();
// abort() never return, "else" is not needed
}
if (pids[i] == 0)
{
struct timeval tv;
time_t t;
struct tm*info;
gettimeofday(&tv,NULL);
t = tv.tv_sec;
info = localtime(&t);
sleep(1);
printf("CHILD PID: %d and the time is %s\n",getpid(),asctime(info));
// wait until terminated
for (;;)
pause();
}
}
int status;
pid_t pid;
sleep(5);
for (i = 0; i<n; ++i) {
if (kill(pids[i], SIGTERM) == -1) {
perror("kill");
}
}
while (n--)
{
pid = wait(&status);
if (pid == -1)
{
perror("wait");
abort();
} else {
printf("CHILD %d killed.\n", pid);
}
}
}
I am experimenting with signals forking but I am not sure what is going on with my code.
I should get 3 total processes. 1 parent 1 child and 1 grandchild. I decrement the level variable by 1 each time I make a fork so the program should terminate.
I don't understand why the program isn't generating any child processes. When I do ps aux| grep selfCaller I only ever see 1 process being run at a time.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
const int TEXT_LEN = 16;
const int NUM_SECS_TO_RUN = 30;
#define PROGNAME "selfCaller"
int numTimesCalled[3]
= {0,0,0};
pid_t pidToSignal = -1;
pid_t childPid = -1;
int level = +2;
int shouldRun = 1;
void sigAlarmHandler(int sig){
printf("Process %d: called level 0\n",level);
int w = rand() % 10 + 1;
alarm(w);
numTimesCalled[0]++;
if(level != 2){
pid_t pid = getppid();
kill(SIGUSR1, pid);
}
}
void sigUs1Handler(int sig){
printf("Process %d: called level 1\n",level);
numTimesCalled[1]++;
if(level != 2){
pid_t pid = getppid();
kill(SIGUSR2, pid);
}
}
void sigUs2Handler(int sig){
printf("Process %d: called level 2\n",level);
numTimesCalled[2]++;
}
void sigIntHandler(int sig){
shouldRun = 0;
}
int main (int argc,
char* argv[]
)
{
int comm;
if (argc > 1){
comm = strtol(argv[1], NULL, 0);
}
if (comm == 0 || comm == 1){
level = comm;
}
srand(getpid());
struct sigaction act;
memset(&act, '\0', sizeof(act));
act.sa_handler = sigAlarmHandler;
sigaction(SIGALRM, &act, NULL);
act.sa_handler = sigAlarmHandler;
sigaction(SIGUSR1, &act, NULL);
act.sa_handler = sigUs1Handler;
sigaction(SIGUSR2, &act, NULL);
act.sa_handler = sigIntHandler;
sigaction(SIGINT, &act, NULL);
// alarm(0);
pid_t pi;
if(level > 0){
pi = fork();
}
printf("pid is %d", pi);
if (pi ==-1){
exit(EXIT_FAILURE);
}
char text[TEXT_LEN];
if(pi == 0){
printf("This is the child");
int r;
snprintf(text,TEXT_LEN,"%d",level-1);
r =execl(PROGNAME, text, NULL);
if (r==-1){
fprintf(stderr,"Cannot find %s\n",PROGNAME);
exit(EXIT_FAILURE);
}
}
if (level == 2)
{
int i;
for (i = 0; i < NUM_SECS_TO_RUN; i++)
{
sleep(1);
}
}
else
{
pidToSignal = getppid();
while (shouldRun)
{
sleep(1);
}
}
printf("Level %d: %d %d %d\n",level,
numTimesCalled[0],numTimesCalled[1],numTimesCalled[2]
);
return(EXIT_SUCCESS);
}
I wrote a code that creates a child with a fork(). The parent of this child should sent a SIGUSR1/2 to its CHILD and the child should answer with a SIGUSR2/1.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void HDL_PSIGUSR(int sig) {
printf("Signal 0x%x received.\n\n", sig);
fflush(stdout);
}
void HDL_SSIGUSR(int sig) {
if (sig == SIGUSR1) {
printf("PID %d -> PID %d: 0x%x\n", getpid(), getppid(), SIGUSR2);
kill(getppid(), SIGUSR2);
} else if (sig == SIGUSR2) {
printf("PID %d -> PID %d: 0x%x\n", getpid(), getppid(), SIGUSR1);
kill(getppid(), SIGUSR1);
}
fflush(stdout);
}
void HDL_SSIGINT(int sig) {
kill(getppid(), SIGINT);
}
void son() {
signal(SIGUSR1, HDL_SSIGUSR);
signal(SIGUSR2, HDL_SSIGUSR);
signal(SIGINT, HDL_SSIGINT);
signal(SIGALRM, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
while (true) {
pause();
}
}
int main() {
int* _buf, n, i, t, timer = 0;
pid_t pid;
char buff[1000];
printf("Number of signals to send: ");
scanf("%d", &n);
printf("Interval time: ");
scanf("%d", &t);
printf("Signals to send: ");
_buf = malloc(n * sizeof *_buf);
for (i = 0; i < n; i++) {
scanf("%d", &_buf[i]);
}
fflush(stdout);
if (pid = fork()) {
signal(SIGUSR1, HDL_PSIGUSR);
signal(SIGUSR2, HDL_PSIGUSR);
i = 0;
while (true) {
i %= n;
//sprintf(buff, "kill -USR1 %d", pid);
//system(buff);
kill(pid, SIGUSR1);
sleep(t);
}
} else {
son();
exit(0);
}
waitpid(pid, (int*)0, 0);
return 0;
}
The problem is that if I use the kill() system call, the child process become a zombie process. Instead, if I use the system() system call and calling from there the command kill on CHILD PID, it works! Why?
Thank you guys! The problem was as said by #Jonathan Leffler and #Shawn. It can be solved with a sleep(1); in the parent process before starting to send signals. I modified my code as shown here:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
bool startProg = false;
void HDL_PSIGUSR(int sig) {
if (!startProg) {
startProg = true;
return;
}
printf("Received signal 0x%x\n", sig);
fflush(stdout);
}
void HDL_SSIGUSR(int sig) {
kill(getppid(), (sig == SIGUSR1) ? SIGUSR2 : SIGUSR1);
}
void HDL_SSIGINT(int sig) {
kill(getppid(), SIGINT);
}
void son() {
signal(SIGUSR1, HDL_SSIGUSR);
signal(SIGUSR2, HDL_SSIGUSR);
signal(SIGINT, HDL_SSIGINT);
signal(SIGALRM, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
kill(getppid(), SIGUSR1);
while (true) {
pause();
}
}
int main() {
int* _buf, n, i, t;
pid_t pid;
printf("Number of signals to send: ");
scanf("%d", &n);
printf("Interval time: ");
scanf("%d", &t);
printf("Signals to send: ");
_buf = malloc(n * sizeof *_buf);
for (i = 0; i < n; i++) {
scanf("%d", &_buf[i]);
}
fflush(stdout);
if (pid = fork()) {
signal(SIGUSR1, HDL_PSIGUSR);
signal(SIGUSR2, HDL_PSIGUSR);
while (!startProg) {
pause();
}
i = 0;
while (true) {
i %= n;
kill(pid, _buf[i++]);
pause();
sleep(t);
}
} else {
son();
exit(0);
}
waitpid(pid, (int*)0, 0);
return 0;
}
I was wondering if someone can help me modify my current code....
Currently it creates my process using fork() and takes a pointer to a function which executes that childs code block.
I wanted to play around with pipes and attempt to now have Process Y send its pid to Process X and then i want to send it back to the Main...
Heres what i have currently
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h> // exit
#include <fcntl.h>
#include <sys/wait.h>
void processX();
void processY();
pid_t addChild(void (*childPtr) (), int fileDes[2]) {
pid_t cpid;
if((cpid=fork()) == 0) {
pipe(fileDes);
childPtr(fileDes);
wait(NULL);
exit(0);
} else if (cpid < 0) {
printf("failed to fork");
exit(1);
} else {
}
return cpid;
}
void processY(int fileDes[2]) {
printf("Child Y[%d] Created of Parent X[%d]\n", getpid(), getppid());
printf("We are now going to write Y PID to process X\n");
pid_t a = getpid();
char buf[1024]; // child reads from pipe() to buffer
close(fileDes[0]); // close reading end of the pipe
write(fileDes[1], &a, sizeof(buf) / sizeof(int));
}
void processX(int fileDes[2]) {
printf("Child X[%d] Created of parent Main[%d]\n", getpid(), getppid());
int status;
pid_t Y = addChild(processY, fileDes);
wait(&status);
pid_t new_val = 5;
close(fileDes[1]); // closing the writing end of the pipe.
read(fileDes[0], &new_val, sizeof(new_val));
printf("Message read with number %d: \n", new_val);
}
int main() {
int status;
int fd[2];
printf("Main process[%d]\n", getpid());
pid_t root = addChild(processX, fd);
wait(&status);
printf("We are going to read from X to Main and then return the Value we got from Y\n");
return 0;
}
I dont know to create a pipe from Y - X and then X - Main....
Y---->send pid ----> X received Y pid ----- send new info to main --->Main print received data...
My answer i came up with
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h> // exit
#include <fcntl.h>
#include <sys/wait.h>
void processX();
void processY();
pid_t addChild(void (*childPtr) (), int fileDes[2], int backToMainFd[2]) {
pid_t cpid;
if(childPtr != *processX //prevents the the pipe from main to x from recreating
pipe(fileDes);
if((cpid=fork()) == 0) {
if(childPtr == *processX) {
childPtr(fileDes, backToMainFd);
} else {
childPtr(fileDes);
}
wait(NULL);
exit(0);
} else if (cpid < 0) {
printf("failed to fork");
exit(1);
} else {
}
return cpid;
}
void processY(int fileDes[2]) {
printf("[PROCESS Y]: Child Y[%d] Created of Parent X[%d]\n", getpid(), getppid());
pid_t a = getpid();
char buf[1024]; // child reads from pipe() to buffer
close(fileDes[0]); // close reading end of the pipe
write(fileDes[1], &a, sizeof(buf) / sizeof(int));
}
void processX(int fileDes[2], int BackToMainFd[2]) {
printf("[PROCESS X]: Child X[%d] Created of parent Main[%d]\n", getpid(), getppid());
int status;
pid_t Y = addChild(processY, fileDes, NULL);
wait(&status);
pid_t new_val = 5;
close(fileDes[1]); // closing the writing end of the pipe.
read(fileDes[0], &new_val, sizeof(new_val));
printf("[PROCESS X]: We got Ys' PID as:%d from [PROCESS Y]\n", new_val);
close(BackToMainFd[0]); // close reading end of the pipe
char buf[1024]; // child reads from pipe() to buffer
write(BackToMainFd[1], &new_val, sizeof(buf) / sizeof(pid_t));
}
int main() {
int status;
int fd[2];
int backToMainFD[2];
printf("Main process[%d]\n", getpid());
pipe(backToMainFD);
pid_t root = addChild(processX, fd, backToMainFD);
wait(&status);
pid_t new_val = 5;
close(backToMainFD[1]); // closing the writing end of the pipe.
read(backToMainFD[0], &new_val, sizeof(new_val));
printf("[MAIN]: We got Ys' PID as:%d from [PROCESS X]\n", new_val);
printf("Send sig kills too Y and root\n");
kill(new_val, SIGKILL);
kill(root, SIGKILL);
printf("Terminate program.\n");
return 0;
}
In the C language create a program that creates two processes and connects them via pipe.
The first descendant redirects its' stdout into the pipe and writes (space separated) pairs of random numbers into it (function rand). Delay the output of the numbers by 1 second.
The second descendant redirects the pipe output to it's stdin, redirects it's stdout into a file called out.txt in the current directory.
The parent process waits 5 seconds and then sends SIGUSR1 to the first process (number generator). This should perform a correct termination of both processes. It waits for the sub-processes to terminate (wait function) and terminates itself.
I really need help with:
The first descendant has to treat the SIGUSR1 signal (sigaction function) and in case of receiving such signal it prints a string “TERMINATED” to it's stderr and terminates.
FILE *file;
file = fopen(NAZEV, "a+");
int pipefd[2];
pipe(pipefd);
pid_t pid1;
int retcode;
pid1=fork();
if(pid1 == 0) // child 1
{
close(roura[0]);
printf("child1...\n");
dup2(roura[1], STDOUT_FILENO);
int i = 0;
while(i < 6)
{
i++;
int a = rand();
int b = rand();
sleep(1);
printf("%d %d\n", a, b);
}
close(roura[1]);
exit(45);
}
else if (pid1 < 0)
{
printf("Fork selhal\n");
exit(2);
}
else
{
pid_t pid2;
pid2 = fork();
if (pid2 == 0) //child 2
{
close(roura[1]);
dup2(roura[0], STDIN_FILENO);
printf("child2...\n");
int i = 0;
while(i < 5)
{
i++;
int c;
int d;
scanf("%d %d", &c, &d);
printf("%d %d\n", c, d);
fprintf(file,"%d %d\n", c, d);
}
printf("child2 end\n");
exit(0);
}
else if (pid2 < 0)
{
printf("Fork error\n");
exit(2);
}else
{
sleep(5);
kill(pid1, SIGUSR1);
wait(&pid1); //wait for child 1
wait(&pid2); //wait for child 2
printf("parent end\n");
exit(0);
}
}
exit(0);
}
Adda signal handler to sigusr1 that prints to stderr and exits.
Try this, adapted to compile in cygwin:
#include <stdio.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif
void sig_handler(){
fprintf(stderr,"TERMINATED");
exit(0);
}
void main(int argc, char ** argv){
FILE *file;
file = fopen("NAZEV", "a+");
int pipefd[2];
int roura[2] ;
pipe(pipefd);
pid_t pid1;
int retcode;
pid1=fork();
if(pid1 == 0) // child 1
{
close(roura[0]);
printf("child1...\n");
dup2(roura[1], STDOUT_FILENO);
if (signal(SIGUSR1, sig_handler) == SIG_ERR){
printf("\ncan't catch SIGUSR1\n");
exit(13);
}
int i = 0;
while(i < 6)
{
i++;
int a = rand();
int b = rand();
sleep(1);
printf("%d %d\n", a, b);
}
close(roura[1]);
exit(45);
}
else if (pid1 < 0)
{
printf("Fork selhal\n");
exit(2);
}
else
{
pid_t pid2;
pid2 = fork();
if (pid2 == 0) //child 2
{
close(roura[1]);
dup2(roura[0], STDIN_FILENO);
printf("child2...\n");
int i = 0;
while(i < 5)
{
i++;
int c;
int d;
scanf("%d %d", &c, &d);
printf("%d %d\n", c, d);
fprintf(file,"%d %d\n", c, d);
}
printf("child2 end\n");
exit(0);
}
else if (pid2 < 0)
{
printf("Fork error\n");
exit(2);
}else
{
sleep(5);
kill(pid1, SIGUSR1);
wait(&pid1); //wait for child 1
wait(&pid2); //wait for child 2
printf("parent end\n");
exit(0);
}
}
exit(0);
}
You need to register a signal handler using sigaction if you want to override the default action. For SIGUSR1, the default action is to terminate the process.