c - 使用系统调用时的 Printf()

标签 c linux system-calls

我正在处理的家庭作业问题非常简单,但我是 C 的新手,所以我可能会遇到语法错误。我想做的是从另一个程序调用一个程序,使用 fork() 分配一个新进程,然后等待 child 完成使用 waitpid() .我正在尝试 A. 让 waitpid() 工作,B. 在 child 完成后显示一条简单的测试消息。

./countprimes 正在通过 ./countmaster 远程运行。 ./countprimes 工作正常,它只是将所有从 x 到 y 的素数打印到屏幕上。 ./countmaster 调用 ./countprimes。

#include <unistd.h>
#include <stdio.h>

int main(int argc, char* argv[]){

         int processid = fork();
         int status;
         char* arguments[4];
         arguments[0]="./countprimes";
         arguments[1]="10";
         arguments[2]="30";
         arguments[3]=NULL;

         if(processid == 0)
                  execvp("./countprimes",arguments);
         else
                  waitpid(processid, &status, 0);
                  printf("test1");
}

我希望看到: 计算从 10 开始到 29 结束的素数实例 计数 10 到 29 的素数实例是 6 测试1

我看到的不是 test1 消息。我还收到 gcc 警告:函数“waitpid”的隐式声明;您指的是 'getpid' 吗?

最佳答案

我可以看到一些语法错误和缺少的 header :

    #include <unistd.h>
    #include <stdio.h>
    #include <sys/wait.h>

    int main(int argc, char* argv[]){

             int processid = fork();
             int status;
             char* arguments[4];
             arguments[0]="./countprimes";
             arguments[1]="10";
             arguments[2]="30";
             arguments[3]=NULL;

             if(processid == 0) {
                      execvp("./countprimes",arguments);
            } else {
                      waitpid(processid, &status, 0);
                      printf("test1");
            } 
    }

关于c - 使用系统调用时的 Printf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58102113/

相关文章:

c# - SignalR Owin Self-Host on Linux/Mono SocketException 当客户端失去连接时

linux - syscall() 调用 tkill()

在两种情况下,Python 系统调用 (os.system) 无法按预期使用相同的字符串

c - 如何格式化输出文本,使不同长度的可变字符串排列在多行中?

java - 地址已被使用(绑定(bind)失败)

c - 如何使用 libqmi-glib 检测设备

linux - 自定义 SPI 驱动程序来实现 lseek

c - 在 docker 容器内使用 membarrier

c - 为什么编译器不发出诊断来获取寄存器操作数的地址?

c - 多个返回语句在 C 中如何工作?