execvp

program stops after execvp( command.argv[0], command.argv)

前提是你 提交于 2019-12-02 05:25:10
I am writing a small shell program that takes a command and executes it. If the user enters a not valid command the if statement returns a -1. If the command is correct it executes the command, however once it executes the command the program ends. What am I doing wrong that is does not execute the lines of code after it? I have tested execvp( command.argv[0], command.argv) with ls and cat commands so I am pretty sure it works. Here is my code. int shell(char *cmd_str ){ int commandLength=0; cmd_t command; commandLength=make_cmd(cmd_str, command); cout<< commandLength<<endl; cout << command

Problems with using execvp on a constructed string

微笑、不失礼 提交于 2019-12-02 04:58:58
问题 I'm trying to write a shell, and part of its construction is executing code from a user-inputted string (buffer). However, when I attempt to execvp the string with additional inputs (ae. echo a), it always screws the pooch and returns -1. I'm at a loss as to why. Here's the relevant pieces: char * buffer = calloc(100, sizeof(char)); ... fgets(buffer, 100, stdin); buffer[strlen(buffer) - 1] = 0; // necessary because of a newline inserted by fgets ... cmd = strsep(&buffer, " "); char * str =

Problems with using execvp on a constructed string

爱⌒轻易说出口 提交于 2019-12-02 01:04:51
I'm trying to write a shell, and part of its construction is executing code from a user-inputted string (buffer). However, when I attempt to execvp the string with additional inputs (ae. echo a), it always screws the pooch and returns -1. I'm at a loss as to why. Here's the relevant pieces: char * buffer = calloc(100, sizeof(char)); ... fgets(buffer, 100, stdin); buffer[strlen(buffer) - 1] = 0; // necessary because of a newline inserted by fgets ... cmd = strsep(&buffer, " "); char * str = malloc(50 * sizeof(char)); strcat(str, "./"); strcat(str, cmd); strcat(str, ".out"); ... i = execvp(str,

How to pass a vector to execvp

戏子无情 提交于 2019-12-02 00:06:57
问题 I want to pass a vector in as the second argument to execvp. Is it possible? 回答1: Not directly; you'd need to represent the vector as a NULL-terminated array of string pointers somehow. If it's a vector of strings, that is straightforward to do; if it's some other kind of data you would have to figure how to encode it as strings. 回答2: Yes, it can be done pretty cleanly by taking advantage of the internal array that vectors use. This will work, since the standard guarantees its elements are

execvp - ls: fts_open: No such file or directory

a 夏天 提交于 2019-11-30 19:01:57
问题 I'm currently struggling with this error. I'm writing a shell emulator, using fork() for executing a command using execvp();. Almost every command I try to parse to my shell is working perfectly, except for ls without arguments. If I try to execute ls -lah, everything works, but a simple ls won't, receiving the following error: ls: fts_open: No such file or directory Here is a snippet of my code: (just the essential) pid = fork(); if (pid==0) { puts("CHILD"); puts(args[0]); puts(args[1]);

g++: error trying to exec 'cc1plus': execvp: No such file or directory

为君一笑 提交于 2019-11-30 07:45:20
问题 I am using ubuntu 12.04 I'm trying to "make" a project. I get this error : g++: error trying to exec 'cc1plus': execvp: No such file or directory I have g++ installed. 回答1: I tried the command @MonoThreaded gave but it failed. I tried below and it works. sudo apt-get install g++ Please don't include any version after the g++ . 回答2: I faced the same issue after upgrading gcc to 4.8 sudo apt-get install gcc-4.8 (mind the package name) Then I manually changed the g++ link to a renamed copy of

g++: error trying to exec 'cc1plus': execvp: No such file or directory

不羁的心 提交于 2019-11-29 05:25:44
I am using ubuntu 12.04 I'm trying to "make" a project. I get this error : g++: error trying to exec 'cc1plus': execvp: No such file or directory I have g++ installed. I tried the command @MonoThreaded gave but it failed. I tried below and it works. sudo apt-get install g++ Please don't include any version after the g++ . I faced the same issue after upgrading gcc to 4.8 sudo apt-get install gcc-4.8 (mind the package name) Then I manually changed the g++ link to a renamed copy of gcc-4.8 My fix was a proper g++ installation sudo apt-get install g++-4.8 For clarity you should use g++-4.8

How to use execvp()

我是研究僧i 提交于 2019-11-27 14:25:17
The user will read a line and i will retain the first word as a command for execvp. Lets say he will type "cat file.txt" ... command will be cat . But i am not sure how to use this execvp() , i read some tutorials but still didn't get it. #include <stdio.h> #include <stdlib.h> int main() { char *buf; char command[32]; char name[32]; char *pointer; char line[80]; printf(">"); while((buf = readline(""))!=NULL){ if (strcmp(buf,"exit")==0) break; if(buf[0]!=NULL) add_history(buf); pointer = strtok(buf, " "); if(pointer != NULL){ strcpy(command, pointer); } pid_t pid; int status; if ((pid = fork())

Classic C. Using pipes in execvp function, stdin and stdout redirection

浪尽此生 提交于 2019-11-27 09:08:25
I want to simulate bash in my Linux C program using pipes and execvp function. e.g ls -l | wc -l There is my program: if(pipe(des_p) == -1) {perror("Failed to create pipe");} if(fork() == 0) { //first fork close(1); //closing stdout dup(des_p[1]); //replacing stdout with pipe write close(des_p[0]); //closing pipe read close(des_p[1]); //closing pipe write if(execvp(bash_args[0], bash_args)) // contains ls -l /* error checking */ } else { if(fork() == 0) { //creating 2nd child close(0); //closing stdin dup(des_p[0]); //replacing stdin with pipe read close(des_p[1]); //closing pipe write close

How to prevent inheriting CPU affinity by child forked process?

ⅰ亾dé卋堺 提交于 2019-11-27 06:56:09
问题 I have a server process that forks many child processes. The server process has affinity to a CPU core, but I don't want that affinity to be inherited by child process (rather OS should handle where to run these processes). Is there a way to delink parent child processes with respect to cpu affinity? 回答1: You can call sched_setaffinity(2) with all bits set in CPU mask after the fork(2) and before the execve(2). 来源: https://stackoverflow.com/questions/8336191/how-to-prevent-inheriting-cpu