Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer each of the following questions completely and briefly: (1) What is the k

ID: 3794595 • Letter: A

Question

Answer each of the following questions completely and briefly:

(1) What is the kernel space and what is the user space in a computer system?

(2) Describe how file sharing can be accomplished by using two different kinds of links (hard and soft) in UNIX. Why hard link is not a type of file, but soft link is a type of file? Explain.

(3) Explain the details of st_mode in struct stat? What is the integer in octal (base 8) for mask of file type (S_IFMT)?

4. Using the layered approach in systems programming design, explain the relationship among the following concepts: user, shell, commands, shell scripts, application programs, C standard library functions, system calls, operating system kernel, interrupt, kernel mode and user mode. You can explain them by using both words and diagrams.




5. Suppose there is a file named filename in your current directory, write a C program to print out the user’s (owner’s) read, write, execut permissions for this file, and the type of this file.

Explanation / Answer

Answer1

Kernel Space:

User Space

Answer 2a

Hard Link : (a second filename for the same file)A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file as consisting of a set of information that has a single name. However, it is possible to create a number of different names that all refer to the same contents. Commands executed upon any of these different names will then operate upon the same file contents. A hard link increments the link count.

Soft Link : (Pointer to file) A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file.A soft link doesn't increments the link count.

Answer 2b

Hard link cannot be used for directory and cannot be created across file systems and is once in exactly one parent directory and no more therfore they are not like a file whereas soft link solves the above problems and can be thought of as a file.

Answer 3a

st_mode(A member of stat):

For symbolic links, the st_mode member shall contain meaningful information, which can be used with the file type macros described below, that take a mode argument. The st_size member shall contain the length, in bytes, of the pathname contained in the symbolic link. File mode bits and the contents of the remaining members of the stat structure are unspecified. The value returned in the st_size field shall be the length of the contents of the symbolic link, and shall not count a trailing null if one is present.

The following macros shall be provided to test whether a file is of the specified type. The value m supplied to the macros is the value of st_mode from a stat structure. The macro shall evaluate to a non-zero value if the test is true; 0 if the test is false.

S_ISBLK(m)
Test for a block special file.
S_ISCHR(m)
Test for a character special file.
S_ISDIR(m)
Test for a directory.
S_ISFIFO(m)
Test for a pipe or FIFO special file.
S_ISREG(m)
Test for a regular file.
S_ISLNK(m)
Test for a symbolic link.
S_ISSOCK(m)
Test for a socket.

The implementation may implement message queues, semaphores, or shared memory objects as distinct file types. The following macros shall be provided to test whether a file is of the specified type. The value of the buf argument supplied to the macros is a pointer to a stat structure. The macro shall evaluate to a non-zero value if the specified object is implemented as a distinct file type and the specified file type is contained in the stat structure referenced by buf. Otherwise, the macro shall evaluate to zero.

S_TYPEISMQ(buf)
Test for a message queue.
S_TYPEISSEM(buf)
Test for a semaphore.
S_TYPEISSHM(buf)
Test for a shared memory object.

The implementation may implement typed memory objects as distinct file types, and the following macro shall test whether a file is of the specified type. The value of the buf argument supplied to the macros is a pointer to a stat structure. The macro shall evaluate to a non-zero value if the specified object is implemented as a distinct file type and the specified file type is contained in the stat structure referenced by buf. Otherwise, the macro shall evaluate to zero.

S_TYPEISTMO(buf)
Test macro for a typed memory object.

Answer 3b

It is a bit mask for file type.

bitwise AND-ing directly with mystat.st_mode (mystat.st_mode & ~S_IFMT) means to consider only the bits involved to determine the file type (regular file, socket, block or char device, etc.)

doing a bitwise AND with the bitwise negated bitmask S_IFMT (~S_IFMT) means to ignore the bits explained above, keeping just the ones need to determine file permission (the 9 lines below that command)