标签: execv

将execvp的输出重定向到C中的文件中

我不知道我做错了什么……但这是正在执行的代码片段:

if (fork() == 0)
    {       
             // child
        int fd = open(fileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);

        dup2(fd, 1);   // make stdout go to file

        execvp("ls","ls");
        close(fd);
            exit(0);
    }
if(wait(&status) == -1)
    {
        printf("ERROR REDIRECT\n");
    }
Run Code Online (Sandbox Code Playgroud)

fileName 被创建但里面什么都没有。我做错了什么?

c redirect file execv

3
推荐指数
1
解决办法
9207
查看次数

waitpid - WIFEXITED返回0虽然孩子正常退出

我一直在编写一个生成子进程的程序,并调用waitpid等待子进程的终止.代码如下:

  // fork & exec the child
  pid_t pid = fork();
  if (pid == -1)
    // here is error handling code that is **not** triggered

  if (!pid)
    {
      // binary_invocation is an array of the child process program and its arguments
      execv(args.binary_invocation[0], (char * const*)args.binary_invocation);
      // here is some error handling code that is **not** triggered
    }
  else
    {
      int status = 0;
      pid_t res = waitpid(pid, &status, 0);

      // here I see pid_t being a positive integer …
Run Code Online (Sandbox Code Playgroud)

c fork execv waitpid

3
推荐指数
1
解决办法
2万
查看次数

创建线程后调用execv

我对线程,进程,execv等都很陌生.我研究过并发现当你执行一个execv时,它占用了调用进程的空间.我想知道当你在main中创建一个线程时会发生什么,然后在线程之后直接调用execv(不是在它完成之前,而是在创建线程之后).我知道execv会替换main但这是否意味着它也会替换线程或线程是否能够像正常一样执行和完成?

我要问的一个小例子:

  int main(){
      printf("hello from main!);
      char *buffer = "some data";

    if(pthread_creat(&mythreadpid, NULL, thread1, buffer){
        printf("ERROR!!");
     }

     execv(...) //do execv here

}

void *thread1(void *buffer){
  printf("calling from my thread!");

 //do something else

}
Run Code Online (Sandbox Code Playgroud)

我已经测试了这个并且我确实遇到了奇怪的行为,因为我的线程无法完成,因此我想知道这是否是它的原因

c pthreads process execv

2
推荐指数
1
解决办法
1641
查看次数

调用execv之后的怪异行为

我正在尝试使用execv执行命令。运行该程序后,我可以看到“将要调用execv!”语句。在标准输出上打印。

我还可以从程序“ leaky”中看到打印内容。实际上,一切都按预期工作,除了在if或else块中看不到打印语句外,即“ execv failed!error:”和“ Valgrind成功执行!” 在输出上打印。

我在这里是否缺少关于execv的明显要点?

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

int main()
{
   char *args[5] = {"/usr/bin/valgrind", "--leak-check=full", "--log-file=valgrindReport.txt", "./leaky", NULL};
   printf("Going to call execv!\n");
   if(execv("/usr/bin/valgrind", args) < 0)
   {
      printf("execv failed! error: %s\n", strerror(errno));
      return 0;
   }
   else
   {
      printf("Successfully executed valgrind!\n");
   }
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c linux execv

2
推荐指数
1
解决办法
244
查看次数

Illegal Argument Execv()Unix C++

所以我基本上有一个向量args,每个数组有一个参数,我试图传递给unix中的execv()调用.

Execv接受两个参数,如:int execv(const char*path,char*const argv []);

将我的字符串向量转换为指针数组的最佳方法是什么?现在我正在做以下事情但是当我用ps -a -f运行它时,ps对我说非法争论.任何帮助表示赞赏.

vector<string> args = tokenize(cmd);
char * arg[args.size()];
for(int j=0; j<args.size();j++)
{
        arg[j] = (char*)args[j].c_str();    
}
retval = execv(args[0].c_str(), arg);
Run Code Online (Sandbox Code Playgroud)

.

>ps

  PID TTY           TIME CMD
  635 ttys000    0:00.18 -bash
16106 ttys000    0:00.00 ./test cpp
12590 ttys001    0:00.02 -bash
>ps -a

ps: illegal argument: ?????
Run Code Online (Sandbox Code Playgroud)

c++ linux execv

1
推荐指数
1
解决办法
623
查看次数

如何获取 execv 的返回值?

我对 C++ 真的很陌生,我正在尝试从以下位置获取输出:

execv("./rdesktop",NULL);
Run Code Online (Sandbox Code Playgroud)

我正在使用 C++ 和 RHEL 6 进行编程。

像 FTP 客户端一样,我想从我的外部运行程序中获取所有状态更新。有人可以告诉我我怎么能做到这一点吗?

c++ execv

1
推荐指数
1
解决办法
5685
查看次数

如何防止execv杀死我的程序?

我的任务是编写一个简单的linux shell。我正在使用外部命令。我们需要使用execv。

for (int i = 0; i < count; i++){
  char path[1024];
  strcpy(path, PATHS[i]);         // PATHS is an array of cstrings, the paths in $PATH
  strcat(path, "/");
  strcat(path, command[0]);       // command and commands are essentially the same
  printf("%d %s",i,path);         // they are essentially argv[]
  if (!execv(path, commands))     // for ls -l $HOME
    break;                        // commands[0] = ls [1] = -l [2] = my home dir
Run Code Online (Sandbox Code Playgroud)

现在我只用ls进行测试。ls完全按照应有的方式运行,但是execv成功后,程序立即关闭。有什么办法可以让我继续使用execv检查正确的路径,并让程序在execv成功后继续运行?

c linux execv

1
推荐指数
1
解决办法
2226
查看次数

使用execv()时没有这样的文件或目录

我试着写一个基本的shell,可以解释简单的命令,比如语言c中的date,ls.

我首先获取这样的PATH变量,然后将其传递给execv()函数.

const char *name = "PATH";
char *value;
value = getenv(name)
Run Code Online (Sandbox Code Playgroud)

我打印出价值,我得到了这个:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Run Code Online (Sandbox Code Playgroud)

请注意,我使用virutalbox来运行Ubuntu.这是我用来尝试简单的ls命令的代码.在下面的代码中,变量行是用户编写的实际命令,在我们的例子中它是"ls"

  pid_t pid, wpid;
  int status;

  pid = fork();
  if (pid == 0) {
      // Child process
      if (execv(value, line) == -1) {
          perror("lsh");
      }
      exit(EXIT_FAILURE);
  }
  else if (pid < 0) {
        // Error forking
        perror("lsh");
  }
  else {
      // Parent process
      do {
          wpid = waitpid(pid, &status, WUNTRACED);
      }
      while (!WIFEXITED(status) && !WIFSIGNALED(status));
    }
Run Code Online (Sandbox Code Playgroud)

我得到的结果是这样的:

lsh: no such file or directory
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c shell execv

1
推荐指数
1
解决办法
5906
查看次数

如何在C++中查询子进程

我的c ++程序将使用fork()和execv()生成几个子进程.我该如何查询这些流程?如果我想关闭一个,我该怎么做?

c++ linux fork execv

0
推荐指数
1
解决办法
774
查看次数

sprintf() 命令不起作用

我正在尝试编写一个 C 程序,它从用户那里获取两个浮点数,然后使用 execv() 命令调用另一个程序。但我不能这样做,因为将 float 转换为 char 或者我不知道为什么。问题是 execv() 命令不起作用;输出必须是这样的

输入第一个数字:5
输入第二个数字:7
5.000000 + 7.000000 = 12.000000
父PID:9745 子PID:9746 现在可以使用

但现在就是这样

输入第一个数字:5
输入第二个数字:7
父 PID:9753 子 PID:9754 现在可以使用

我的第一个 C 程序 sum.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
  if(argc!=3)
  printf("error...\n");
  double a=atof(argv[1]);
  double b=atof(argv[2]);
  printf("%lf + %lf = %lf \n",a,b,a+b);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

和第二个程序calculate.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() 
{
  float x,y;
  pid_t pid;

  printf("Enter first num: ");
  scanf("%f",&x);
  printf("Enter …
Run Code Online (Sandbox Code Playgroud)

c arrays execv

0
推荐指数
1
解决办法
8810
查看次数

解析Shell的命令行参数

我有一个要为一个夏季项目编写的shell。例如,我正在尝试解析命令行,

如果我打电话

ls -l
Run Code Online (Sandbox Code Playgroud)

我需要解析

-l
Run Code Online (Sandbox Code Playgroud)

部分。

因此,我可以将其传递给用于的参数向量execv。我知道我正确解析了它,但是由于某种原因,找不到目录。我可能想念什么吗?下面是我的代码。

c execv argv

0
推荐指数
1
解决办法
58
查看次数

标签 统计

execv ×11

c ×8

linux ×4

c++ ×3

fork ×2

argv ×1

arrays ×1

file ×1

process ×1

pthreads ×1

redirect ×1

shell ×1

waitpid ×1