Please, need help with C code in Linux please! Process control. Fairly simple co
ID: 3592648 • Letter: P
Question
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 don’t have to give the full path name of the command.
execlp (" ls " , " ls " , "a" , "l " , NULL) ;
4) execvp: Same as exexv except that we don’t 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.
char input [ ] = "Hello World ! " ;
strtok ( input , " ") ; strtok (NULL, " ") ; strtok (NULL, " ") ;
include stdio h include unistd.h include sldlib.h> include sys wait.h nclude 0) wait (NULL): else if (PID-0) /* chald proces8 */. execlp (emd, cmd, NULL.) . erec cannot return. I/ so do the following fprintf (stderr, "Cannot execute %s'n " , crnd); exit(1); / czec foiled / else if ( PID=-1) fprintf (stderr "Cannot creae a new processn" exit (2) shell.eExplanation / Answer
The first step is to open a text editor and type the source code for this program as follows:
#include <stdio.h>
main()
{
printf("Hello, world! ");
return 0;
}
This code should (at least initially) be written exactly as shown. The file should then be saved (i.e., written to the hard disk) using the save command of the text editor.
The file can be given any desired name, but it is usually best to use a name that is descriptive of the program. The name must end with a .c extension in order to indicate to the compiler that it is C source code; if the .c extension is omitted, an error message will be returned. (Extensions usually are not necessary for files in Unix-like operating systems, but this is an exception.) Thus, a good choice for the above example is hello.c.
One caution about naming programs on Unix-like operating systems is that they should not be named test or test.c. This could confuse the operating system, because there is already a built-command named test.
Closer Look at "Hello, World!"
The first line in a C program, which is called a preprocessor directive, always begins with a pound sign ( # ). Preprocessing, which is carried out by the compiler prior to compilation, causes the contents of the indicated header file(s) (which is in glibc) to be copied into the current file, effectively replacing the directive line with the contents of that file. Thus, in this case, the preprocessor directive #include <stdio.h> causes the file named stdio.h that resides in glibc to be copied into the source code of hello.c. stdio.h provides the basic input and output facilities for C.
The preprocessor directive is customarily followed by a blank line. As is the case with a number of things in programming, this line is not necessary for a program to function correctly but is, rather, a matter of style. Good style can make programs much easier to read and therefore easier to detect errors and make improvements; it is thus important to develop good style habits right from the beginning when learning programming.
Every C program has exactly one function named main(). A function is a set of one or more statements that are enclosed in curly brackets, perform some operation and return a single value to the program in which they reside; it could also be looked at as a subprogram. The main() function is the starting point of any program; that is, it is where the program begins execution (i.e., running); any other functions are subsequently executed.
A statement is an instruction in a program. Each statement ends with a semicolon. There are two statements in hello.c, both of which are in the main() function.
Parentheses surround the argument list for every function. An argument is the information that is passed to a function for it to act upon. The two functions in this example are main() and printf(); the printf() function is located inside of the main() function. Even if no arguments are actually used, as is the case with main(), the parenthesis are still included.
printf()'s name reflects the fact that C was first developed in the early 1970s when display monitors were rare, and thus all computer output was generally printed out on paper. However, today printf() by default actually sends its output to the monitor screen (referred to as standard output), not to a printer. The f in printf stands for formatted.
Any string (i.e., sequence of printable characters) that is used as an argument to printf() is enclosed in double quotes. The at the end of the string in hello.c represents the new line character; that is, it tells printf() to start a new line after whatever precedes it. This causes whatever next appears on the display screen to begin on a new line rather than on the same line as the output of hello.c.
The second statement in the main function is return 0;. It is not necessary for the functioning of this simple program; however, it is included to illustrate how a second statement can fit into a program. It is also included because similar statements are required in more complex programs and thus it is useful to become familiar with it from the beginning.
Each curly bracket is placed on a separate line in this example in order to make it more obvious and to thus make it easier to detect missing or incorrect brackets. However, this is not necessary, and brackets are often included on the same lines as other code, depending on the particular programmer's style. What is more important is to maintain a consistent style.
C programs are not fussy about the way they are laid out (in contrast to some other programming languages). For example, hello.c could be written very compactly in just two lines without affecting performance as follows:
#include <stdio.h>
main(){printf("Hello, world! ");}
Such extreme compactness is unusual, however, and it can make more complex programs very difficult to read.
Compiling hello.c
After a program has been planned, written and checked for obvious errors, it is ready to compile. Compilation consists of two main steps, compilation itself (inclusive of preprocessing) followed by linking (i.e., connection of multiple files in a program); the compiler usually performs the linking automatically.
The program hello.c can be compiled with the GCC as follows:
gcc -o hello hello.c
The -o option informs the compiler of the desired name for the executable (i.e., runnable) file that it produces. The name used in this example is hello, but it could just as easily be anything else desired, such as hello.exe, file1 or Bonjour. If the -o option is omitted, the compiler will give the name a.out to the executable file.
Compilation for hello.c should take only about a second or two on a reasonably fast personal computer because the program is so simple. However, compilation can take hours in the case of very large and complex programs.
The ls command (whose purpose is to list the contents of a directory) can be used to confirm both that the source code file was saved and that the new file containing the compiled program was actually created, i.e.,
ls hello.c hello