c - Badly placed ()的错误在我的shell运行的C代码中发现,如何解决错误?

标签 c shell unix

<分区>

在 shell 上运行的以下代码有 Badly placed ()'s 的错误,但我无法弄清楚它们有什么问题。

#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;
}

我一直试图通过使用 gcc -Wall 命令找出错误,看看它发生了什么,显示如下: 在函数“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'

添加库后的新错误消息:

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'

最佳答案

这个程序在我编译和运行时工作得很好。

“badly placed ()”错误是 shell 之一,所以我认为你没有正确运行程序,而是将源代码直接输入 shell。

另请参阅:C - Badly Placed ()'s?

我已经做到了:
- 将代码放入 stacko.c
- 执行 gcc -g -Wall stacko.c
- 使用 ./a.out 10 1 2 10 10 3 7 运行程序

输出:

[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

关于c - Badly placed ()的错误在我的shell运行的C代码中发现,如何解决错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22371200/

相关文章:

c - 在服务器/客户端 tcp 实现中向每个订阅者发送通知的不同方式/方法

bash - 如何在 Makefile 目标中使用 Bash 语法?

shell - 在 shell 中打印到文本文件时如何保留新行?

bash - 将命令传递给 Bash 中的函数

java - 无法启动 Zookeeper 服务器。没有jdk目录

linux - 是否有 Linux 命令可以将其命令行参数写入文件而不使用 shell?

linux - 如何循环遍历目录中的文件,然后将它们读入 bash 中的字符串变量

在 C 中为 malloc 和 free 创建一个包装函数

c - 多个 execlp 不工作

c - 在 C 中测试值 > 127 的 ascii 字符