=== initial commit ===
Complete both tasks below.
The code for the second task needs a Unix-like environment to work! That includes Linux, macos, Cygwin, WSL, BSD, etc.
If you want to test if your environment is set up for it, compile and run the
testdir.c program in the examples/
directory. (You can
type make
in the examples/
directory.) It should print Testing: PASS
.
Add your answers inline, below, with your pull request.
- List all of the main states a process may be in at any point in time on a standard Unix system. Briefly explain what each of these states means.
ANSWER:
Start or create - Initial state. When we type command into the shell or activate the aplication, the OS is invoked to create a new process to run the program.
Ready - A process is ready to run. Process may come to this state after “start” state or while running it by but interrupted by the scheduler to assign CPU to some other process.
Running - In this state, a process is running on a processor. The instructions is being executed.
Waiting - Sometimes process needs to stops running and wait. It might be waiting for a resource, space to become available to user input.
Terminated or exit - When the process finishes its executing or it is terminated by OS, it is moved to terminated state.
- What is a zombie process? How does one get created? How does one get destroyed?
ANSWER:
This usually happens in the program that has parent-child functions. The child function has finished or terminated then send an exit status to its parent function. The child remains in a zombie state which means it has executed but not exited until the parent function acknowledges.
- What are some of the benefits of working in a compiled language versus a non-compiled language? More specifically, what benefits are there to be had from taking the extra time to compile our code?
ANSWER:
The significant benefit of the compiled language must be its execution speed. And it is more efficient than interpreted languages like JS or Python. It is because the compiled language speaks directly to the machine. It converted into machine code.
Write a program in C, lsls.c
, that prints out a directory listing for the
directory the user specifies on the command line. If the user does not specify a
directory, print out the contents of the current directory, which is called .
.
$ ./lsls
.
..
lsls.c
lsls
$ ./lsls /home/exampleuser
.
..
.config
.vim
.yarnrc
.bashrc
foo.c
.vscode
Downloads
.gitconfig
.bash_history
.viminfo
src
Hint: Start by just printing out the contents of the current directory .
,
and then add the command line parsing later after you have it working.
You are expected to use Google to find examples of how to use these functions. Also see Details, below.
- Call
opendir()
. - Then repeatedly call
readdir()
--printing the filenames as you go--until it lets you know there are no more directory entries by returningNULL
. - Then call
closedir()
.
You don't have to write the three functions, above. They're system calls built into the OS.
You will be using functionality included in <dirent.h>
. This header file holds
the declarations for DIR
, struct dirent
, opendir()
, readdir()
, and
closedir()
, below.
-
DIR *opendir(char *path)
: This function opens the directory named inpath
(e.g..
) and returns a pointer to a variable of typeDIR
that will be used later. If there is an error,opendir()
returnsNULL
.You should check for errors. If there is one, print an error message and exit (using the
exit()
function). -
struct dirent *readdir(DIR *d)
: Reads the next directory entry from theDIR
returned byopendir()
. Returns the result as a pointer to astruct dirent
(see below). ReturnsNULL
if there are no more directory entires. -
closedir(DIR *d)
: Close a directory (opened previously withopendir()
) when you're done with it.
The struct dirent *
returned by readdir()
has the following fields in it:
struct dirent {
ino_t d_ino // file serial number
char d_name[] // file name, a string
};
(You don't need to declare this struct dirent
type. It's already included in
<dirent.h>
.)
For output, you should print the field d_name
from your struct dirent *
variable, e.g.
struct dirent *ent;
// ... some of your code ...
ent = readdir(d);
printf("%s\n", ent->d_name);
To parse the command line, you'll have to look at argc
and argv
specified in
your int main(int argc, char **argv)
function. Example code to print all
command line arguments can be found in commandline.c.
Modify that example to look at the command line parameters, if any, and pass
those to opendir()
.
Modify the program to print out the file size in bytes as well as the name.
Example output (suggestion: use %10lld
to print the size in a field of width
10):
$ ./lsls
224 .
992 ..
1722 lsls.c
8952 lsls
You'll need to use the stat()
call in <sys/stat.h>
.
-
int stat(char *fullpath, struct stat *buf)
: For a given full path to a file (i.e. the path passed toopendir()
following by a/
followed by the name of the file ind_name
), fill the fields of astruct stat
that you've pointed to. Returns-1
on error.// Example stat() usage struct stat buf; stat("./lsls.c", &buf); printf("file size is %lld\n", buf.st_size);
Instead of a size in bytes for a directory (which is marginally useful), replace
the number with the string <DIR>
.
Example output:
$ ./lsls
<DIR> .
<DIR> ..
1717 lsls.c
8952 lsls
The st_mode
field in the struct stat
buffer holds information about the file
permissions and type of file.
If you bitwise-AND the value with S_IFDIR
and get a non-zero result, the file
is a directory.
(If you bitwise-AND the value with S_IFREG
and get a non-zero result, the file
is a regular file, as opposed to a device node, symbolic link, hard link,
directory, named pipe, etc.)