Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Please, need help with C code in Linux please! Process control. Fairly simple code has been provided, need a few changes regarding exec please. Please

Please, need help with C code in Linux please! Process control. Fairly simple code has been provided, need a few changes regarding "exec" please. Please show output as well. Thank you!

There are many variations of the exec system call that can be used to execute an executable le (program). Calling exec does not create a new process, it replaces the current process with the new process. If the exec command fails, then it returns -1. But if it succeeds it does not return because execution proceeds at the beginning of the program that was executed. We will consider some useful forms of exec commands.

1) execl: takes the path name of an executable as its rst argument, the rest of the arguments are the command line arguments ending with a NULL. execl ("/bin/ ls " , " ls " , "a" , "l " , NULL)

2) execv: takes the path name of a binary executable as its rst argument, and a NULL terminated array of arguments as its second argument. The rst element can be the command or " ". static char args [ ] = {" ls " , "a" , "l " , NULL}; execv ("/bin/ ls " , args ) ;

3) execlp: same as execl except that we dont have to give the full path name of the command. execlp (" ls " , " ls " , "a" , "l " , NULL) ;

4) execvp: Same as exexv except that we dont have to give the full path name of the command. static char args [ ] = {" ls " , "a" , "l " , NULL}; execvp (" ls " , args ) ; shell.c below is a stripped down shell program. It will run a Unix command without any arguments. Copy and save the program as shell.c. Modify the program so that it can execute any shell command. It should be able to take at least 2 arguments. Usually, you need to parse the command line you enter. Using execvp instead of execlp will probably help. It should work on any UNIX command consisting of 0-2 arguments after the command entered by the user, and be error free.

The following are a few test cases that the user could enter. $ cp le1 le2 $ ls -l $ who char *strtok(char *str, const char *delim) is a useful function for parsing the input string. You can see below how it used " " as a delimiter to get everything before that character. You can specify " " as a delimiter to split the string into arguments. The rst time you use it, give it a string as the rst parameter to tokenize. This will return the rst token. You can then use it subsequent times with NULL as the rst parameter and it will return subsequent tokens in order. After the last token was returned, it returns NULL. For example if you wrote: char input [ ] = "Hello World ! " ; strtok ( input , " ") ; strtok (NULL, " ") ; strtok (NULL, " ") ; ...the rst call would return "Hello". The second call would return "World!". And the third call would return NULL.

image text in transcribed

include #incl ude unistd.h nelude stdib.h nclude 0) wait (NULL.) else f (PID0) child process execlp (emd, emd, NULL.) erec cannot retwrn. If so do the following fprintf (stderr, "Cannot execute %s ", cmxl ) ; exit (1)/ erec Jailed/ else f PID1) fprintf (stderr"Cannot create a new process) exit (2) shell.c include #incl ude unistd.h nelude stdib.h nclude 0) wait (NULL.) else f (PID0) child process execlp (emd, emd, NULL.) erec cannot retwrn. If so do the following fprintf (stderr, "Cannot execute %s ", cmxl ) ; exit (1)/ erec Jailed/ else f PID1) fprintf (stderr"Cannot create a new process) exit (2) shell.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students explore these related Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 3 weeks ago