Greetings everyone, I've run into a bit of problem with creating and killing processes in linux(debian to be more precise, compiled with gcc). I'm trying to do the following: I need to ask a user his/her name, and then offer him a timed test, if the user answers correctly to all the questions, or if the timer expires, a new user should take the test. The code for this is supposed to make use of multiple processes, and I'm not allowed to use pipes.
This is the code that I've been working on till now:
The problem I'm getting is that the kill(pid, SIGTERM) acts in a way that i don't understand, for example if I replace
with
thus making sure that I kill both child processes, on the next program run the children seem unable to run the test() and timer() functions for some reason, also, if I remove the exit(1) calls, even though I terminate both children in the parent, they seem to keep running -- as a test for this, if you complete the questions, the first while run ends, but my timer doesn't stop (first child).
Been testing multiple variations of this, but they all do something wrong, or better said, I'm doing something wrong in writing the code.
Any input on this would be much appreciated.
This is the code that I've been working on till now:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <stdlib.h>
void user_details (); // user enters personal information
int timer (); // prints elapsed time
void test (); // prints questions and receives answers
void main () {
while (1) {
int status;
pid_t pid1 = 0, pid2 = 0, done_pid;
user_details ();
pid1 = fork ();
if (pid1 < 0) {
printf("error in first fork\n");
}
if (pid1 == 0) {
printf("first child = %d\n", done_pid = getpid());
printf("fist child parent = %d\n", done_pid = getppid() );
timer ();
exit(1);
}
if (pid1 > 0) {
pid2 = fork ();
if (pid2 < 0) {
printf("error in second fork\n");
}
if (pid2 == 0) {
printf("second child %d\n", done_pid = getpid());
printf("second child parent %d\n", done_pid = getppid() );
test();
exit(1);
}
if (pid2 > 0) {
printf("pid2 = %d\n", pid2);
printf("pid1 = %d\n", pid1);
printf("parent pid = %d\n", done_pid = getpid());
done_pid = wait(&status);
if (done_pid == pid1) {
kill(pid2, SIGTERM);
}
else {
kill(pid1, SIGTERM);
}
}
}
}
}
void user_details () {
char first_name [20], last_name [20];
printf ("Enter your first name: ");
scanf ("%s", first_name);
printf ("Enter your last name: ");
scanf ("%s", last_name);
}
int timer () {
time_t t_start = time(NULL);
int dif = 0, ok ;
while (dif < 20){
time_t t_end = time(NULL);
dif = t_end - t_start;
if (dif % 10 == 0 || dif == 0){
printf ("Time left: %d \n", 20-dif);
}
sleep(1);
}
printf("Time is up\n");
ok = 1;
return ok;
}
void test () {
int a=0,i=0;
while (i <= 3) {
int ok = 0;
while (ok == 0) {
printf ("Question %d \nYour answer:\n", i+1);
scanf ("%d", &a);
if (a == i+1)
ok = 1;
}
i++;
}
}
The problem I'm getting is that the kill(pid, SIGTERM) acts in a way that i don't understand, for example if I replace
Code:
if (done_pid == pid1) {
kill(pid2, SIGTERM);
}
else {
kill(pid1, SIGTERM);
}
Code:
kill(pid1, SIGTERM);
kill(pid2, SIGTERM);
thus making sure that I kill both child processes, on the next program run the children seem unable to run the test() and timer() functions for some reason, also, if I remove the exit(1) calls, even though I terminate both children in the parent, they seem to keep running -- as a test for this, if you complete the questions, the first while run ends, but my timer doesn't stop (first child).
Been testing multiple variations of this, but they all do something wrong, or better said, I'm doing something wrong in writing the code.
Any input on this would be much appreciated.