The below codes running on the shell has the error of Badly placed ()'s but I'm not able to figure out what's wrong with them.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
void current_time() {
struct tm *current;
time_t now;
now = time(NULL);
current = localtime(&now);
printf("[%02i:%02i:%02i] ", current->tm_hour, current->tm_min, current->tm_sec);
}
void gChild(int life,int input_data){
time_t curtime;
int pid = fork(); //fork() creating process and returning pid
if (pid == 0) {
current_time();
printf(" Grand-Child process started(process %d). The process will last for %d seconds.\n", getpid(), life);
sleep(life);
current_time();
input_data++;
printf(" Grand-Child process ended(process %d). iData is %d\n", getpid(), input_data);
exit(0);
} else if (pid == -1) {
perror("Cannot start first-grandchild process.\n");
exit(0);
}
}
void Child1(int life,int input_data){
time_t curtime;
int pid = fork();
if (pid == 0) {
current_time();
printf(" ~First Child process started(process %d). The process will last for %d seconds.\n", getpid(), life);
sleep(life);
current_time();
input_data++;
printf(" ~First Child process ended(process %d). iData is %d\n", getpid(), input_data);
exit(0);
}
else if (pid == -1) {
perror("Cannot start first-child process.\n");
exit(0);
}
}
void Child2(int life, int gchildST, int ltgc, int input_data){
time_t curtime;
int pid = fork();
if (pid == 0)
{
current_time();
printf(" ~Second Child process started(process %d). The process will last for %d seconds.\n", getpid(), life);
gChild(ltgc, input_data);
current_time();
input_data++;
printf(" Second Child process ended(process %d). iData is %d\n", getpid(), input_data);
exit(0);
}
else if (pid == -1)
{
perror("Cannot start second-child process.\n");
exit(0);
}
}
void parent_code()
{
while(1)
{
pid_t wait_rv = wait(NULL);
if (wait_rv == -1)
return;
}
}
int main(int argCount, char *arg[])
{
time_t curtime;
int input_data;
int ltc1, ltc2; //life times of the two child processes after which the two child processes will terminate
int ltgc; //life time of the grandchild process
int child1ST, child2ST; //starting times of the two child processes
int gchildST; //starting times of the grandchild process
if (argCount != 8) {
printf("Invalid inputs.\n");
return 0;
}
sscanf(arg[1], "%d", &input_data); //%d for only accepting decimal values
sscanf(arg[2], "%d", &child1ST);
sscanf(arg[3], "%d", &child2ST);
sscanf(arg[4], "%d", <c1);
sscanf(arg[5], "%d", <c2);
sscanf(arg[6], "%d", &gchildST);
sscanf(arg[7], "%d", <gc);
if (ltc1<=0 || ltc2<=0 || ltgc<=0)
{
printf("Invalid life time.\n");
return 0;
}
if (child1ST<=0 || child2ST<=0 || gchildST <=0){
printf("Invalid start-time.\n");
return 0;
}
else if (ltc2 <= gchildST || ltc2 <= gchildST)
{
printf("Cannot start all grandchild processes during second process.\n");
return 0;
}
current_time();
printf("Parent process started(process %d).\n", getpid());
if(child1ST<child2ST){
sleep(child1ST);
Child1(ltc1,input_data);
sleep(child2ST-child1ST);
Child2(ltc2, gchildST, ltgc, input_data);
}
else
{
sleep(child2ST);
Child2(ltc2, gchildST, ltgc, input_data);
sleep(child1ST-child2ST);
Child1(ltc1,input_data);
}
parent_code();
current_time();
input_data++;
printf("Parent process ended(process %d). iData is %d\n", getpid(), input_data);
return 0;
}
I have been trying to find out the error by using the command of gcc -Wall
to see what happen with it shows the following:
In function 'gChild':
warning: implicit declaration of function 'fork'
warning: implicit declaration of function 'getpid'
warning: implicit declaration of function 'sleep'
warning: unused variable 'curtime'
In function 'Child1':
warning: unused variable 'curtume'
In function 'parent_code':
warning: implicit declaration of function 'wait'
In function 'main':
warning: unused variable 'curtime'
New error messages after adding libraries:
test.c: In function 'gChild':
test.c:23: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:27: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:19: warning: unused variable 'curtime'
test.c: In function 'Child1':
test.c:40: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:44: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:36: warning: unused variable 'curtime'
test.c: In function 'Child2':
test.c:59: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:63: warning: format '%d' expects type 'int', but argument 2 has
type 'pid
_t'
test.c:54: warning: unused variable 'curtime'
test.c: In function 'parent_code':
test.c:77: warning: implicit declaration of function 'wait'
test.c: In function 'main':
test.c:121: warning: format '%d' expects type 'int', but argument 2 has
type 'pi
d_t'
test.c:139: warning: format '%d' expects type 'int', but argument 2 has
type 'pi
d_t'
test.c:84: warning: unused variable 'curtime'