I'm getting the following error:
program.c:24:3: error: case label does not reduce to an integer constant
case proc_id[0]: //child process 1 pid
for all case statements in the following code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
int proc_id[4];
int main(int argc, char *argv[]){
int i;
int proc=1;
for (i = 0; i < 3; ++i)
{
if(proc != 0){
proc = fork();
}
if (proc != 0)
{
proc_id[i]=proc;
}
}
switch(getpid()){
case proc_id[0]: //child process 1 pid
for (i = 0; i < l; ++i)
{
alarm(3);
kill(proc_id[1], SIGUSR1);
}
break;
case proc_id[1]: //child process 2 pid
for (i = 0; i < l; ++i)
{
alarm(6);
kill(proc_id[2], SIGUSR2);
}
break;
case proc_id[2]: //child process 3 pid
for (i = 0; i < l; ++i)
{
alarm(9);
kill(proc_id[0], SIGUSR1);
}
break;
case proc_id[3]: //parent pid
printf("parent\n");
break;
}
return(0);
}
void custom_handler(int signum){
kill(SIGINT, getppid());
printf("PID:%d \t Signal Number: %d\n", getpid(), signum);
}
void parent_handler(int signum){
++sigcount;
switch(sigcount){
case l:
kill(proc_id[0], SIGTERM);
break;
case l+3:
kill(proc_id[1], SIGTERM);
break;
case l+6:
kill(proc_id[2], SIGTERM);
kill(proc_id[3], SIGTERM);
break;
}
}
void sigterm_handler(int signum){
exit(0);
}
I have two questions:
Why is this error occurring
and
Can we put an expression that evaluates to an integer as a case label?
Environment details:
Compiled program using gcc on ubuntu terminal.
switch labels are evaluated at compiling time, so it must be constant expressions. You cannot put an expression that evaluates to anything.
If you want an equivalent algorithm, you have to use an if statement
In
case proc_id[0]:
proc_id[0] is not a constant, it is a variable. The case label can only use a const such as:
case 10:
Similarly for the other case statements.
When you have a variable, your only option is to use if-else blocks.
int pid = getpid();
if ( pid == proc_id[0] )
{
for (i = 0; i < l; ++i)
{
alarm(3);
kill(proc_id[1], SIGUSR1);
}
}
else if ( pid == proc_id[1] )
{
for (i = 0; i < l; ++i)
{
alarm(6);
kill(proc_id[2], SIGUSR2);
}
}
else if ( pid == proc_id[2] )
{
for (i = 0; i < l; ++i)
{
alarm(9);
kill(proc_id[0], SIGUSR1);
}
}
else if ( pid == proc_id[3] )
{
printf("parent\n");
}
Related
I am trying to fork() 10 child processes in one loop and then in another loop wait() for them to terminate and print their PID along with their exit status code. It cannot be done any other way or using any other function. Two loops/waves and the function wait();
This is what I have tried:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t wait_p, p[10], p_child;
int status;
for (int i = 0; i < 10; i++)
{
p[i] = fork();
}
for (int i = 0; i < 10; i++)
{
switch (p[i])
{
case 0:
p_child = getpid();
exit(p_child % 10);
break;
case -1:
puts("ERROR");
break;
default:
wait_p = wait(&status);
printf("Child with PID: %d", wait_p);
if (WIFEXITED(status))
printf(" terminated with STATUS: %d\n", WEXITSTATUS(status));
break;
}
}
return (EXIT_FAILURE);
}
This code will execute an endless count of child processes. It must print only the first original(issued by THE one parent) 10. What am I doing wrong?
You have to handle the child processes directly in your first loop:
for (int i = 0; i < 10; i++)
{
p[i] = fork();
if (p[i] == 0) {
p_child = getpid();
exit(p_child % 10);
} else if (p[i] == -1) {
perror("fork");
}
}
and then wait for them in the second loop
for (int i = 0; i < 10; i++)
{
wait_p = wait(&status);
printf("Child with PID: %d", wait_p);
if (WIFEXITED(status))
printf(" terminated with STATUS: %d\n", WEXITSTATUS(status));
}
You cannot handle the case, that fork() returns in the child process (yielding 0 as return value), in your second loop, otherwise each child process in the first loop keeps forking more child processes.
I've been programming this over and over again and I've had trouble learning how to work with fork() in order to generate child processes recursively. I started programming some pretty complicated stuff and I decided to start over again with something way more simple.
I just started learning about processes and I've had trouble understanding them.
The program is designed to fork a tree of processes, however , I must fork from the root two processes which will fork , on the left side 3 children processes, and on the right side 4 children processes. These processes must fork 3 and 4, respectively, processes of their own.
My issue with this is that the program can fork the processes, however only one process on each side is fathering all of the children on their respective side.
Any help you could give me would be great, please tell me if I'm not being clear enough.
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
/* Prototypes */
void three_children();
void four_children();
int main()
{
pid_t process;
int status,n;
printf("Number of levels: \n");
scanf("%d", &n);
for (int i = 0; i < n ; i++) {
process = fork();
switch (process) {
case -1:
printf("Error\n");
break;
case 0:
if (i == 0) {
printf("Left\n");
three_children(process, status);
}
if (i == 1) {
printf("Right\n");
three_children(process, status);
}
printf("Hi I'm a child PID: %d, my father is PPID: %d\n", getpid(), getppid());
exit(0);
default:
printf("I'm a father PPID: %d\n", getppid());
break;
}
}
}
void four_children(pid_t process, int status)
{
for (int j = 0; j < 4; j++) {
process = fork();
switch (process) {
case -1:
printf("Error\n");
break;
case 0:
printf("I'm child: %d, and my father is: %d\n(four children)\n", getpid(), getppid());
exit(0);
break;
default:
printf("I'm a father process: %d\n", getpid());
four_children(process, status);
for (int j = 0; j < 3; j++) {
wait(&status);
}
break;
}
}
}
void three_children(pid_t process, int status)
{
for (int k = 0; k < 3; k++) {
process = fork();
switch (process) {
case -1:
printf("Error\n");
break;
case 0:
printf("I'm a child: %d, and my father is: %d\n(three children )\n", getpid(), getppid());
exit(0);
break;
default:
printf("I'm father %d\n", getpid());
three_child(process, status);
for (int j = 0; j < 3; j++) {
wait(&status);
}
break;
}
}
}
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
typedef struct Node
{
int childrenCount;
struct Node* child[0];
} Node;
Node* makeNode(int children)
{
Node* result = (Node*)malloc(sizeof(Node) + sizeof(Node*) * children);
if (!result) {
exit(1);
}
result->childrenCount = children;
for(int loop = 0;loop < children; ++loop) {
result->child[loop] = NULL;
}
return result;
}
void buildTree(int indent, pid_t parent, Node* node);
void createChild(int indent, pid_t parent, Node* node, int childIndex)
{
pid_t pid = fork();
if (pid == -1) {
exit(1);
}
if (pid == 0) {
buildTree(indent + 1, getpid(), node->child[childIndex]);
srand(parent * 10 + childIndex);
exit(rand());
}
for(int ind = 0; ind < indent; ++ind) {
fprintf(stderr, " ");
}
fprintf(stderr, "Parent: %d Spawned %d\n", parent, pid);
}
void buildTree(int indent, pid_t parent, Node* node)
{
for(int ind = 0; ind < indent; ++ind) {
fprintf(stderr, " ");
}
fprintf(stderr, "Parent %d Has %d Children\n", parent, node->childrenCount);
for(int loop = 0; loop < node->childrenCount; ++loop) {
createChild(indent, parent, node, loop);
}
for(int loop = 0; loop < node->childrenCount; ++loop) {
int status;
int child = wait(&status);
for(int ind = 0; ind < indent; ++ind) {
fprintf(stderr, " ");
}
fprintf(stderr, "Parent: %d Reaped %d with status %d\n", parent, child, status);
}
}
int main()
{
Node* root = makeNode(2);
root->child[0] = makeNode(3);
for(int loopChild = 0;loopChild < 3; ++loopChild) {
root->child[0]->child[loopChild] = makeNode(3);
for(int loopGrandChild = 0;loopGrandChild < 3; ++loopGrandChild) {
root->child[0]->child[loopChild]->child[loopGrandChild] = makeNode(0);
}
}
root->child[1] = makeNode(4);
for(int loopChild = 0;loopChild < 4; ++loopChild) {
root->child[1]->child[loopChild] = makeNode(4);
for(int loopGrandChild = 0;loopGrandChild < 4; ++loopGrandChild) {
root->child[1]->child[loopChild]->child[loopGrandChild] = makeNode(0);
}
}
fprintf(stderr, "Start\n");
buildTree(0, getpid(), root);
fprintf(stderr, "Stop\n");
}
I was given this particular task:
Using SIGCONT, SIGSTOP, and SIGCHLD, write a program that accepts
through argv[] a list of commands (none of them has arguments) and
runs the commands in a round-robin style, alternating commands in
1-second intervals.
To achieve it, I tried this:
int dead_children = 0;
int nr_processes;
void inc_dead () {
printf("I, %d, died\n", getpid());
dead_children++;
if (dead_children == nr_processes)
_exit(0);
}
int main (int argc, char *argv[]) {
int pids[argc - 1];
nr_processes = argc - 1;
signal(SIGCHLD, inc_dead);
for (int i = 1; i < argc; i++) {
pid_t pid;
if ( (pid = fork()) == -1 ) {
perror("fork");
return EXIT_FAILURE;
}
if ( !pid ) {
pause();
execlp(argv[i], argv[i], (char *) NULL);
perror("exec");
_exit(1);
}
pids[i - 1] = pid;
}
while (dead_children < nr_processes)
for (int j = 0; j < argc - 1; j++) {
kill(pids[j], SIGCONT);
sleep(1);
kill(pids[j], SIGSTOP);
}
return EXIT_SUCCESS;
}
I tried to run this code with very simple program that would allow me to verify task's correctness:
int main (void) {
int i = 5;
while (i-- > 0) {
printf("This is %d saying HI!\n", getpid());
sleep(1);
}
return EXIT_SUCCESS;
}
However, this does not work. I noticed that after creation, child processes never left pause() instruction, even though parent is sending a SIGCONT. Any thoughts on how to achieve the goal?
Let's say i have a main C program that has to wait for sigchld of two children, and that these two sons have to do two separate task, for example one should write "1", and the other
one should write "2" ,wait 2 seconds and then terminate, now how should I write the code so that the father write his children's pid only after the two sons ends with sigchld? It's obvious that i'm missing some theory, if you look at my code you will understand what my issue is.
After that i'll have to force the execution of the second son before the first son, suggestion?
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char **argv)
{
int pids[2], cpid, i, status;
char buff[200];
for(i=0; i < 2; i++)
{
if ((pids[i] = fork()) < 0)
perror("errno");
else
{
//child
if (pids[i] == 0)
{
if(i == 0)
write(1,"1\n", 2);
else
{
sleep(2);
write(1,"2\n", 2);
}
return 0;
}
}
}
for(i = 0; i < 2; i++)
{
cpid = waitpid(pids[i], &status, 0);
if (WTERMSIG(status))
printf("status:%d , pid terminated:\n", status,cpid);
else
printf("error: not exited with a signal\n");
}
return 0;
}
If the last for loop is changed as:
for(i = 0; i < 2; i++)
{
cpid = waitpid(pids[i], &status, 0);
if (WIFEXITED(status))
printf("status:%d , pid %d terminated normally :\n", status,cpid);
else if (WTERMSIG(status))
printf("status:%d , pid %d terminated by signal:\n", status,cpid);
else
printf("error: not exited with a signal\n");
}
Then the thing works better, as there is no signal to terminate the childs.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<errno.h>
int main(int argc, char **argv){
int n = atoi(argv[1]);
int superdaddy = getpid();
int p[n+1][2];
int i=0;
int cpid,output;
int result = 0;
if(pipe(p[0])<0){
perror("1");
return 1;
}
if(pipe(p[n])<0){
perror("2");
return 1;
}
output = p[0][1];
if(getpid()==superdaddy){
if(write(p[0][1],&result,sizeof(result))<0){
perror("3");
return 1;
}
if(close(p[0][1])<0){
perror("4");
return 1;
}
}
while(1){
if(i==n){
if(read(p[n-1][0],&result,sizeof(result)<0)){
perror("5");
return 1;
}
result++;
output = p[n][1];
if(write(output,&result,sizeof(result))<0){
perror("6");
return 1;
}
if(close(p[n-1][0])<0){
perror("7");
return 1;
}
if(close(p[n][1])<0){
perror("8");
return 1;
}
break;
}
i++;
cpid = fork();
if(cpid==0){
if(i==n)
continue;
if(pipe(p[i])<0){
perror("9");
return 1;
}
if(read(p[i-1][0],&result,sizeof(result))<0){
perror("10");
return 1;
}
result++;
output = p[i][1];
if(write(output,&result,sizeof(result))<0){
perror("11");
return 1;
}
if(close(p[i-1][0])<0){
perror("12");
return 1;
}
if(close(p[i][1]<0)){
perror("13");
return 1;
}
continue;
}
else if(cpid<0){
perror("14");
return 1;
}
break;
}
if(getpid()==superdaddy){
wait(NULL);
if(read(p[n][0],&result,sizeof(result))<0){
perror("15");
return 1;
}
printf("Result: %d\n",result);
if(close(p[n][0])<0){
perror("16");
return 1;
}
}
return 0;
}
The Program aims to read a number n from command line and then forks n child process and create n pipes. process p0 will be parent of process p1, p1 will be parent of p2, so and so on. One variable (named result here) will be passed through pipes, every time it is passed it will be added by 1. So the output should be n as well. Pipe Fi connects Pi and P(i+1). Attached is my code.
When n=1 or n=2, the program can output correctly, which is 1 and 2 correspondingly. However, when n=3, it gives me a bad file error at error 5. I have hand-tracked the code for the whole afternoon but got no idea what is wrong with it. Anyone could help? Appreciate it first!
when n=3, it gives me a bad file error at error 5.
This could be fixed by removing that if(close(p[i][1]<0)){ in your code, because you need to read from p[i][0] in your last iteration, i.e.
if (i == n) {
if(read(p[n-1][0],&result,sizeof(result)<0)){
...
}
}
This is an implementation of your idea, I hope it may be helpful:
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s N\n", argv[0]);
exit(EXIT_FAILURE);
}
int n = atoi(argv[1]);
int pipes[n][2];
int i, val;
pid_t pid;
val = 0;
for (i = 0; i < n; i++) {
if (pipe(pipes[i]) < 0) {
perror("pipe");
exit(EXIT_FAILURE);
}
if ((pid = fork()) < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
close(pipes[i][1]);
if (read(pipes[i][0], &val, sizeof(val)) != sizeof(val)) {
perror("read");
exit(EXIT_FAILURE);
}
printf("C %d read %d\n", getpid(), val);
val++;
}
else {
close(pipes[i][0]);
printf("P %d writes %d\n", getpid(), val);
if (write(pipes[i][1], &val, sizeof(val)) != sizeof(val)) {
perror("write");
exit(EXIT_FAILURE);
}
if (waitpid(pid, NULL, 0) != pid) {
perror("waitpid");
exit(EXIT_FAILURE);
}
printf("%d is going to leave.\n", getpid());
exit(EXIT_SUCCESS);
}
}
printf("%d is going to leave.\n", getpid());
exit(EXIT_SUCCESS);
}
Testing run:
$ ./a.out 3
P 2005 writes 0
C 2006 read 0
P 2006 writes 1
C 2007 read 1
P 2007 writes 2
C 2008 read 2
2008 is going to leave.
2007 is going to leave.
2006 is going to leave.
2005 is going to leave.
Explanation:
The frame of that code is for (i = 0; i < n; i++) { pipe(); fork(); }, which means it will create n pipes, and n new processes. In each iteration, the parent will write to pipes[i][1] and child will read from pipes[i][0]. Eventually, it will create a process chain connected by a series of pipes, and a value is passed down from the first process to the last through that series of pipes.