【发布时间】:2020-07-11 21:19:33
【问题描述】:
在这个程序 mulproc.c 中,我试图从我制作的两个程序中运行可执行文件(一个计算文件中字母字符的数量,另一个计算五个特定的特殊字符)。我正在尝试创建一个父进程(在这种情况下只是 mulproc.c),它在各自的子进程中运行这两个程序,所以基本上只需从父进程创建两个子进程。这两个程序都有自己的输出,但围绕它们各自的输出,我还想为每个输出两条消息,指示它何时开始和何时结束。但是,每次不同的尝试都会得到不正确且不同的输出(我不想在这里全部发布)。我的两个程序的输出甚至被写在彼此之间,所以我相信我可能错误地使用了 waitpid() 函数。更重要的是,我无法在父进程中打印出正确的子进程 PID。请忽略两个“子进程”编号标签,因为调试目的不合适。这里是 mulproc.c ...
#include <stdio.h>
#include "count.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <stdbool.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main( int argc, char *argv[] )
{
pid_t pid1;
pid_t pid2;
int status1;
int status2;
pid1 = fork();
if ( pid1 < 0 )
{
perror( "ERROR! Fork failed!" );
exit(1);
}
if ( pid1 == 0 )
{
// CHILD PROCESS CODE
printf( "\nChild Process 2:\npid :%d\nppid:%d\n\n", getpid(), getppid() );
printf( "CHILD <PID: %d> process is executing testspecial program!\n", getpid() );
char *specialchar[] = { "./testspecial" , NULL };
execv( specialchar[0], specialchar );
}
if ( pid1 > 0 )
{
pid2 = fork();
if ( pid2 == 0 )
{
// ANOTHER CHILD PROCESS CODE
printf( "\nChild Process 1:\npid :%d\nppid:%d\n\n", getpid(), getppid() );
printf( "CHILD <PID: %d> process is executing testalphabet program!\n", getpid() );
char *alphabetchar[] = { "./testalphabet" , NULL };
execv( alphabetchar[0], alphabetchar );
}
else if ( pid2 > 0 )
{
// PARENT PROCESS CODE
printf( "\nParent Process:\npid:%d\nppid :%d\n", getpid(), getppid() );
// if child1 terminated...
if ( waitpid( pid1, &status1, 0 ) == pid1 )
{
printf( "CHILD <PID: %d> process has done with testalphabet program! See the results above!\n", getpid() );
}
// if child2 terminated...
if ( waitpid( pid2, &status2, 0 ) == pid2 )
{
printf( "CHILD <PID: %d> process has done with testspecial program! See the results above!\n", getpid() );
}
}
}
return 0;
}
这只是其中一个不正确的输出...
Parent Process:
pid:3166
ppid :3149
Child Process 1:
pid :3168
ppid:3166
CHILD <PID: 3168> process is executing testalphabet program!
Child Process 2:
pid :3167
ppid:3166
CHILD <PID: 3167> process is executing testspecial program!
A -> 0
B -> 0
C -> 0
D -> 0
E -> 0
F -> 0
G -> 0
H -> 3
I -> 0
J -> 0
K -> 0
L -> 0
M -> 0
N -> 0
O -> 0
P -> 0
Q -> 0
, -> 1
R -> 0
S -> 0
T -> 0
. -> 1
U -> 0
: -> 1
V -> 0
; -> 1
W -> 0
! -> 1
X -> 0
Y -> 0
Z -> 3
a -> 0
b -> 0
c -> 0
d -> 0
e -> 0
f -> 0
g -> 0
h -> 3
i -> 0
j -> 0
k -> 0
CHILD <PID: 3166> process has done with testalphabet program! See the results above!
l -> 0
m -> 0
n -> 0
o -> 0
p -> 0
q -> 0
r -> 0
s -> 0
t -> 0
u -> 0
v -> 0
w -> 0
x -> 0
y -> 0
z -> 0
CHILD <PID: 3166> process has done with testspecial program! See the results above!
我希望输出看起来像这样......
CHILD <PID: 3168> process is executing testalphabet program!
A -> 0
B -> 0
C -> 0
...
...
...
x -> 0
y -> 0
z -> 0
CHILD <PID: 3168> process has done with testalphabet program! See the results above!
CHILD <PID: 3167> process is executing testspecial program!
, -> 1
. -> 1
: -> 1
; -> 1
! -> 1
CHILD <PID: 3167> process has done with testspecial program! See the results above!
如果有人能在这里纠正我的错误,我真的很感激......我对使用这样的系统调用是全新的,所以希望我在这种情况下不会搞砸太多。
【问题讨论】:
-
关于:
#include "count.h"我们无法重现该问题,因为发布的代码无法编译。请发布count.h的内容 -
OT: about:
int main( int argc, char *argv[] )参数未使用。这会导致编译器输出两条关于未使用参数的警告消息。一个简单的解决方法是使用main()的另一个有效签名。int main( void ) -
仔细检查发现
count.h头文件中的NOTHING 正在被使用。因此,不应包含该头文件。 -
pid2为 -
OT:包含那些内容未被使用的头文件是一种非常糟糕的编程习惯:建议删除:
#include <string.h>和#include <ctype.h>和#include <stdbool.h>和#include <dirent.h>和#include <errno.h>
标签: c linux fork waitpid execv