为什么方案没有在这​​个C编程或UNIX编程(execvp()系统调用)执行一些句子? [英] Why the program didn't execute some sentences in this C programming or unix programming(execvp() System calls)?

查看:168
本文介绍了为什么方案没有在这​​个C编程或UNIX编程(execvp()系统调用)执行一些句子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的程序,当我运行该程序,我觉得真的很困惑就是为什么我的程序没有EXCUTE

I have the following program, when I run the program, I feel really confused that why my program didn't excute

   int num=i;
       printf("it is No.%d !",num);
       printf("hello , I will excute execvp!");

我的程序基本上是创建6子进程EXCUTE executionbode()函数,然后使用execvp超载原来的程序。不过,每次当我运行该程序,字符串你好,我将执行execvp一直没有出现!此外,我认为那些三句以上也是在正在运行的程序没有执行?有人可以告诉我为什么?下面是我的程序

My program basically create 6 child processes to excute executionbode() function, and then use execvp to overload original program. However, everytime when I run the program, the string "hello, I will execute execvp" never shows up! Also I think those three sentences above also didn't execute in the running program? can someone tell me why? Here is my program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include "makeargv.h"
#include "redirection.h"
#include <sys/wait.h>



int executionnode(int i);

int main(){
pid_t childpid;
     int i;
     int row=6;
     for(i=0;i<row;i++)
     {   childpid=fork();
         if(childpid==0)
            continue;
         else if (childpid>0)
            executionnode(i);

         else {
           perror("something wrong");
            exit(1);
          }
      }


}


int executionnode(int i){
   sleep(i);
   printf("hello, I am process:%ld\n",(long)getpid());
   wait(NULL);
   char *execArgs[] = { "echo", "Hello, World!", NULL };
   int num=i;
   printf("it is No.%d !",num);
   printf("hello , I will excute execvp!");
   execvp("echo", execArgs);

}

有人能告诉我为什么吗?以及如何解决它?我觉得这是真的很奇怪?是因为execvp()函数呢?我刚开始学习的操作系统,所以我真搞不清楚了!谢谢你对我的帮助!

Can someone tell me why? and how to fix it? I feel it is really strange? Is it because of execvp() functions? I just began to learn operating system,so I am really confused about it! Thank you for helping me!

推荐答案

您所面临的问题是2个不同的东西组合的结果是:

The issue you are facing is the result of the combination of 2 different things:

1 - 随着的 tonysdg 的注释中,execvp overwites当前的进程映像,并引用的这个

1 - As tonysdg mentioned in the comment, execvp overwites the current process image, and to quote this:

的execve()不会成功返回,文本,数据,BSS和
  调用进程的堆栈是由程序的改写
  加载。

execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded.

execvp()是一个前端的execve() 的)

(execvp() is a front-end for execve())

2 - 该stdout是一个缓冲流,这意味着当一个换行符发现或缓冲区将刷新实际打印发生。

2 - The stdout is a buffered stream, which means that the actual printing takes place when a newline character is found or the buffer is flushed.

(看到更多关于这个在的陆克文Zwolinski的的答案这里

(see more on this at Rudd Zwolinski's answer here )

所以,现在,让我们来看看如何将这些东西互动,并产生您的问题:
你叫就在 execvp()您的输出缓冲已经包含了2 previous内容的的printf的的,但是因为没有换行符,没有什么是印在屏幕上。

So, now, let's see how these things interact and produce your issue: Just before you call execvp() your output buffer already contains the contents of the 2 previous printf's, however since there is no newline character, nothing is printed on the screen.

然后, execvp()被执行,在当前的过程中,当然这意味着你的'previous输出缓冲区的内容会丢失所有信息被覆盖

Then, execvp() is executed and overwrites everything on your current process, which of course means that the contents of your 'previous' output buffer are lost.

<一个href=\"http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin\">Here你可以找到很多方法来解决这个问题,并且只是为了完整性,我建议你添加 \\ n 最终的printf

Here you can find many ways to address this issue and, just for completeness, I suggest you add \n to the final printf:

...
printf("hello , I will excute execvp!\n");
...

,然后你去好:

$ ./soc
hello, I am process:4701
hello, I am process:4702
hello, I am process:4703
hello, I am process:4704
hello, I am process:4705
hello, I am process:4706
it is No.5 !hello , I will excute execvp!
Hello, World!
it is No.4 !hello , I will excute execvp!
Hello, World!
it is No.3 !hello , I will excute execvp!
Hello, World!
it is No.2 !hello , I will excute execvp!
Hello, World!
it is No.1 !hello , I will excute execvp!
Hello, World!
it is No.0 !hello , I will excute execvp!
Hello, World!

这篇关于为什么方案没有在这​​个C编程或UNIX编程(execvp()系统调用)执行一些句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆