gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:44:02 25 4
gpt4 key购买 nike

<分区>

在 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'

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com