Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program that uses two processes (not threads) to sort the regular files in a directory and the sub-directories based on file sizes.

Write a C program that uses two processes (not threads) to sort the regular files in a directory and the sub-directories based on file sizes. The program spawns one process (i.e., child process) to scan the directory and the sub-directories for regular files. When the (child) process finds a regular file, it obtains the size, and sends the size and the pathname of the file to the parent process through a pipe or FIFO. The parent process sorts (in ascending order) and organizes the size and pathname information. When the child process finishes scanning, it notifies the parent process, and the parent process prints out the sizes and pathnames of the files. 1. Name the program in the pattern hw4_1.c. 2. The pathname of the directory is specified in the command line. The format of the command for running the program is as follows (Important! If you don't follow strictly, the script testing your program may not be able to run your program correctly, and you may get lower grades). ./your_program pathname_of_directory 3. All the sub-directories should be scanned. 4. The program only checks the sizes of regular files. It does not check the sizes of directories or symbolic links. Do not include the sizes of directories or symbolic links in the results. 5. Print out the results on the screen in text format, one line for each file with file size followed by a tabular symbol ('\t') and the file pathname. Programs using different formats in outputs will fail the tests and lead to lower grades. 6. The parent process needs to use a structure to contain the information of each file, which is dynamically allocated when the information is received, and use a data structure, such as a linkedlist, a tree, or a hash table, to organize the structures of files. Using an array to organize the information of the files is no suggested, since it is unknown how many files are there in the directory. 7. The parent process needs to sort the files incrementally. It maintains a sorted list. When new information arrives from the child, it updates the list. In this way, the parent process can provide a sorted list immediately after the child process finishes scanning. This is why the program uses two processes --- the two processes can run in parallel to shorten execution time. If the parent process were to start sorting after the child process finishes, there would be no need to use two processes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions