0

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", &ltc1);
    sscanf(arg[5], "%d", &ltc2);
    sscanf(arg[6], "%d", &gchildST);
    sscanf(arg[7], "%d", &ltgc);

    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'
Network study
  • 83
  • 1
  • 2
  • 6

1 Answers1

2

This program works just fine when I compile and run it.

The "badly placed ()" error is one of the shell, so I think you're not running the program correctly, but feeding the source code directly into the shell.

Also look at: C - Badly Placed ()'s?

I've done this:
- Put your code in stacko.c
- Execute gcc -g -Wall stacko.c
- Run program with ./a.out 10 1 2 10 10 3 7

Output:

[09:24:34]  Parent process started(process 11277).
[09:24:35]   ~First Child process started(process 11278). The process will last for 10 seconds.
[09:24:36]   ~Second Child process started(process 11279). The process will last for 10 seconds.
[09:24:36]   Second Child process ended(process 11279). iData is 11
[09:24:36]    Grand-Child process started(process 11280). The process will last for 7 seconds.
[09:24:43]    Grand-Child process ended(process 11280). iData is 11
[09:24:45]   ~First Child process ended(process 11278). iData is 11
[09:24:45]  Parent process ended(process 11277). iData is 11
Community
  • 1
  • 1
neeohw
  • 555
  • 4
  • 12
  • Yes, I can compile it now. Thanks for your answer. But, can I ask do I need to type in ./a.out for all my projects on this kind of system? I just want to find out the real problem causing this. – Network study Mar 13 '14 at 08:35
  • Well, of course, if every project is a separate application, you will need to do that. It all depends on what you're exactly trying to accomplish. You could however start by adding a "-o" flag with the name of the program, but "gcc --help" will tell you that. A good place to start is here: http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html Also the -g flag is for debugging symbols. If you don't need those, remove the flag – neeohw Mar 14 '14 at 18:30