【问题标题】:create 6 childs with fork() systemcall使用 fork() 系统调用创建 6 个孩子
【发布时间】:2020-10-23 12:19:36
【问题描述】:

我正在尝试编写一个程序来创建 6 个具有 fork 系统调用的孩子,每个孩子都运行 /bin/ls 具有不同类型的 exec() 函数的命令,当所有孩子完成他们的工作时,父母应该打印出来PID。 这段代码仍然给出错误的输出:

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

int main(int argc, char *argv[])
{
    printf("hello world (pid:%d)\n", (int) getpid());
    for (int i =0 ; i<6;i++)
    {
        char *myargs_path = "/bin/ls";
        char *myargs_file[2] = {"ls",NULL};
        char *myargs_temp[2] = {"done",NULL};
        if (fork() == 0)
        {
            if (i == 0)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execl(myargs_path,myargs_path,NULL); 
            }
            if (i == 1)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execlp(myargs_file[0],myargs_file[0],NULL);
            }
            if (i == 2)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execle(myargs_path,myargs_path,NULL,myargs_temp);
            }
            if (i == 3)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execv(myargs_path,myargs_path);
            }
            if (i == 4)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execvp(myargs_file[0],myargs_file[0]);
            }
            if (i == 5)
            {
                printf("hello, I am child (pid:%d)\n", (int) getpid());
                execvpe(myargs_file[0],myargs_file,myargs_temp);
            }
        }
    }
    
    
         
    for(int i=0;i<6;i++)
    {
        int wc = wait(NULL);
        printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",wc, wc, (int) getpid());
    }
    

    return 0;
}

【问题讨论】:

  • 尝试获取由 fork() 返回的 pid 值并改用 waitpid()。
  • 或者使用线程来代替这个旧的、缓慢的垃圾 API...
  • myargs_temp 数组未使用可行的环境字符串进行初始化。除最后一个之外的成员应指向variable=value 形式的字符串。可以想象,如果不太可能,您的 ls 需要一个您没有提供的环境变量。
  • 此外,您也不会检查或处理 exec 调用或 forkwait 调用的失败。如果这些函数失败,这些函数将返回-1,如果在这种情况下程序继续运行,就好像它们成功一样,您肯定会得到与预期不同的输出。特别是 Exec 调用仅在它们失败时才返回,并且通常会在该事件中终止(子)进程。
  • 我收到很多不兼容的指针类型警告。在某些情况下,当您应该传递数组时,您正在传递字符串。注意编译器警告。

标签: c fork exec


【解决方案1】:

你有一些不正确的论点。你想要:

                switch(i) {
                case 0: execl(myargs_path, myargs_path, NULL);
                        break;
                case 1: execlp(myargs_file[0], myargs_file[0], NULL);
                        break;
                case 2: execle(myargs_path, myargs_path, NULL, myargs_temp);
                        break;
                case 3: execv(myargs_path, myargs_file);
                        break;
                case 4: execvp(myargs_file[0], myargs_file);
                        break;
                case 5: execvpe(myargs_file[0], myargs_file, myargs_temp);
                        break;
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2011-06-10
    • 1970-01-01
    • 2021-04-28
    • 2011-07-13
    相关资源
    最近更新 更多