- #1
freshcoast
- 185
- 1
Hey everybody, I am having trouble with working with child processes. I am to implement a parent process creating 2 child processes to each run different code. e.g child 1 is a wallclock and child 2 is a countdown program. I also have to implement a pipe for when child 2, the countdown program, upon reaching 0 it will use the pipe to tell the other child process to terminate.
So far I seem to have written everything correct but for some reason, my child processes are not exiting like they are supposed to and it is causing my parent process to be in a forever loop waiting until the kids end. I swear before a recent modification the code was working fine, until I added another child process then I started having this trouble. I tried back tracking as best as I can to where I remember it was working correctly, but for some reason I am still getting this error. So now I am asking if someone can take a look at my code and see where the problem lies because I for one have been looking at my source code for hours but can not notice anything that would make it behave this way.
Don't mind the runFile() function as I will be implementing that in the future.
I have been racking my brain and am frustrated because I feel that my logic is correct and it literally worked hours ago but I must've changed something and now I can't find what is causing it to behave this way.
So thanks for any/all input or advice
So far I seem to have written everything correct but for some reason, my child processes are not exiting like they are supposed to and it is causing my parent process to be in a forever loop waiting until the kids end. I swear before a recent modification the code was working fine, until I added another child process then I started having this trouble. I tried back tracking as best as I can to where I remember it was working correctly, but for some reason I am still getting this error. So now I am asking if someone can take a look at my code and see where the problem lies because I for one have been looking at my source code for hours but can not notice anything that would make it behave this way.
Don't mind the runFile() function as I will be implementing that in the future.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
#define DEFAULT 3
#define pipeSize 2
#define NUMBEROFCHILD 2
#define STOP 1
#define RUN 0
int fd[pipeSize];
void wallClock(){
int status = RUN; /*signals*/
int newStatus = RUN;
time_t now;
struct tm *lcltime;
if(close(fd[1]) == -1){
printf("Error closing writing end of pipe\n");
_exit(1);
}
while(status != STOP){ /*loop until signal occurs*/
now = time(NULL);
lcltime = localtime(&now);
printf("the time is %d:%d:%d\n", lcltime->tm_hour,lcltime->tm_min,lcltime->tm_sec);
sleep(1);
read(fd[0], &newStatus, sizeof(newStatus)); /*check for status*/
status = newStatus;
}
if(close(fd[0]) == -1){
printf("Error closing reading end of pipe\n");
_exit(1);
}
printf("Message has been received--Terminating now\n");
exit(1);
}
void runFile(){
int status = RUN;
int newStatus = RUN;
if(close(fd[1]) == -1){
printf("Error closing writing end of pipe\n");
_exit(1);
}
while(status != STOP){
/*run file in here*/
}
}
void countDown(int start){
int stopNow = STOP; /*signals*/
int cont = RUN;
if(close(fd[0]) == -1){
printf("Error closing reading end of pipe\n");
_exit(1);
}
while(start > 0){ /*while loop until start reaches 0*/
if(start >= 10){
printf("Count Down: 00:%2d\n", start);
}
else{
printf("Count Down: 00:0%d\n", start);
}
write(fd[1], &cont, sizeof(cont)); /*write in pipe to continue still*/
start--;
sleep(1);
}
printf("Count Down: 00:00 -- sending message to terminate other processess\n");
write(fd[1], &stopNow, sizeof(stopNow)); /*Tell others to terminate*/
if(close(fd[1]) == -1){
printf("Error closing writing end of pipe\n");
_exit(1);
}
exit(1);
}
int main(int argc, char *argv[]){
int time , i, pid = 0;
pid_t child[NUMBEROFCHILD];
if(argc <= 2){ /*check arguments*/
if(argv[1] != '\0'){
time = atoi(argv[1]);
}
else
time = DEFAULT;
}
else{
printf("Too many parameters -- exiting program\n");
return -1;
}
if(pipe(fd) == -1){ /*open pipes*/
printf("Error opening pipe\n");
exit(1);
}
for(i = 0; i < NUMBEROFCHILD; i++){ /* create children */
if((child[i] = fork()) == -1){
printf("Error in creating child\n");
exit(1);
}
if(child[i] == 0){
switch(i){
case 0: wallClock();
case 1: countDown(time);
}
}
}
if(close(fd[0]) == -1){
printf("Error closing parent reading pipe\n");
exit(1);
}
if(close(fd[1]) == -1){
printf("Error closing parent writing pipe\n");
exit(1);
}
while(pid = wait(NULL)){ /*Wait for all children to finish*/
if(pid == -1){
printf("Error exiting child\n");
exit(EXIT_FAILURE);
}
}
printf("All processes has ended, exiting program --\n");
return 0;
}
So thanks for any/all input or advice