在shell上运行的以下代码有错误放置()的错误,但我无法弄清楚它们有什么问题。
#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;
}
我一直试图通过使用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'
答案 0 :(得分:2)
当我编译并运行它时,这个程序运行正常。
“badly placed()”错误是shell之一,所以我认为你没有正确运行程序,而是直接将源代码提供给shell。
我做到了这一点:
- 将您的代码放在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