Debugging with Linux

Total Page:16

File Type:pdf, Size:1020Kb

Debugging with Linux

CS 3100 Operating Systems Fall 2003

Assignment #2 due 9/12 Write a C or C++ program that will act as a shell command-line interpreter for the Linux kernel. Your shell program should use the same style as the Bourne or default Bash shell for running programs. Use the “cmd:” string for your prompt. Namely, when a user types a line such as; word1 word2 word3 the shell interprets word1 as the name of an executable program and words2 and 3 as parameters to be passed to that program in argv. Your shell should search the directory system in the order specified by the PATH environment variable to locate the executable file. If the file is found, it should be passed all parameters in argv and executed. When the executable file has completed or terminated, the shell program should again resume with the next prompt. For example, consider the following sequence of user interactions: cmd: report –ver CPU is a Pentium IV at 1 GHZ cmd: To accomplish this shell program, use the following Linux system service calls; a) int fork ( ); This system call creates a new process that is a copy of the callilng process, except that it has its own memory, process ID, and file descriptors, Initially, the newly-created process memory is filled with a copy of the parent process memory. In other words, the created process begins with a copy of the parent’s code and variables. After fork() has been called, two processes will execute the next program statement – each in its own address space. The parent or original process will see the child process ID returned. The child or creted process will see 0 returned. b) void execvp (char *filename, char *argv[ ]); This system call replaces the code being executed. In other words, a new executable file is read into the code space and executed. The executable file is found using the PATH environment variable. You may think of this call as overlaying a new executable image. As a result, this call never returns if successful – the next statement executed is the first statement in the new program. The variable path is a pointer to a string containing the new executable file while argv is a standard structure of parameters to be passed to the new program. c) void wait ( ); This call suspends the current process until a child terminates. It allows a process to wait until a forked child has completed.

0a0d173164ab5edef9152340f3a656cd.doc 04/07/18

Recommended publications