Assignment 3, Solutions
Part of
the homework for 22C:112, Fall 2012
|
Now suppose you have decided to make f into a program so you can execute it as an application from within shell scripts as well as from other application programs.
a) Which of the 4 parameters could you pass through to the new version of f() via execve() in essentially the way they were passed in the original. (0.5 point)
Parameter b, the character string.
b) Suggest appropriate compromizes that would allow you to pass the information required for the other parameters despite the barriers created by the execve() mechanism. (0.5 point)
Hint: The key information is all contained in the notes for Friday, Aug. 31.
Parameter a, the open file, can be passed as an open file by making sure that it is opened with the correct "unit number", making sure that the "close on exec" bit is not marked "close-on-exec" and writing the exec'd application to use that "unit number" as its file argument.
Parameter c, the integer, can be passed as the character representation of an integer. For example, the caller can use the sprintf(s,"%d",c) conversion routine to convert c to the string s. On receipt, c=itoa(s) will reverse the conversion.
Parameter d, the object, poses the biggest problems. We could convert the entire representation of the object to textual form and pass it as a string, or we could store the object in a file and pass the open file, thereby allowing the exec'd application to make changes that persist after the application terminates.
A problem: Explain how adding a Unix-style magic number at the head of each object file can be useful in such a system. Your explanation must both identify the system functions that check the magic numbers and what use is made of them. (1 point)
The execve() service can examine the magic number and use it to determine which type of CPU should be set to work executing that file.
int a; static int b; int c( int d ); static int e( int f ) { return c( f ); } int g( int h ) { return e( h ); }
A problem: Which of the identifiers above (a through h) are visible to the linker? The others are private symbols known only to the compiler. (1 point)
a is visible.
b is invisible because it was declared to be static.
c is visible.
d is invisible because it is a local parameter name. e is invisible because it was declared to be static.
f is invisible because it is a local parameter name. g is visible.
h is invisible because it is a local parameter name.