0% found this document useful (0 votes)
26 views

CS962 Assignment 1

Uploaded by

Amit Choubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CS962 Assignment 1

Uploaded by

Amit Choubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Assignment 1

CS962: Operating System Principles


Due Date: 30-07-2024 23:55

1 Infinite Chain of Unary Operations [40 Marks]


In this question, you need to write three c programs defined in Part1/square.c, Part1/double.c
and Part1/root.c which perform square, double and square root operations respectively on
a non-negative integer such that generated executables with these programs can be chained in
any pattern.
The order of the operations in the chained pattern would be from left to right. The program
also takes operation offset as an argument; a non-negative integer offset decides which oper-
ation to perform first in left to right order, then completes the chain of operations.
In the left to right order, the first operation is at offset 0, second operation is at offset 1, third
operation is at offset 2, and so on. The value of offset is always less than the total operations
specified.

Synopsis
$ ./double root double root square integer_number operation_offset

Example 1
$ ./root square double 4 0
8

Since, the order of the operations in the chained pattern is from left to right and offset is 0.
Hence, the chained pattern should be viewed as, starting with square:
double(square(root(4))) = 8

Example 2
$ ./square root double root 8 2
4

The order of the operations in the chained pattern is from left to right and offset is 2. Hence,
the chained pattern should be viewed as, starting with double:
root(square(root(double(8)))) = 4

Example 3
$ ./square root double 4 1
16

The order of the operations in the chained pattern is from left to right and offset is 1. Hence,
the chained pattern should be viewed as, starting with root:
square(double(root(4))) = 16

Output
Print the final result only (as shown in the example).
1
Note
• At least 1 unary operation and utmost 100 unary operations will be specified during
testing.

• If your implementation results in parent-child relationship between processes, then parent


process must wait for its child process to exit.

• You can assume that the result of operations will always fit in unsigned long long data
type.

• If square root of number is a fraction then round off the result to the nearest integer, e.g.
2.5 should be rounded to 3 and 2.4 should be rounded to 2. This needs to be done every
time square root is performed. For example: Answer for chain of operations done below
will be 8.

./root square root square root square double 3 0

Error handling
In case of any error print “UNABLE TO EXECUTE” as output.

System calls and library functions


You must only use the below mentioned APIs to implement this question.

- fork - malloc
- exec* family - free
- str* family - exit
- ato* family - wait/ waitpid
- printf, sprintf - sqrt
- round

Testing
Run the script Part1/run tests.sh for running the provided sample test cases which contains
3 test cases. A sample output after running the script would be:
Test 1 is Passed
Test 2 is Passed
Test 3 is Passed

2 Find utility [60 Marks]


In this problem, you will implement the variants of find utility of the Linux distributions.
The problem consists of three parts. Follow the description of each part to understand the
requirement and the desired output.

2.1 Task 1: Basic find [15 marks]


In this task, you need to implement the basic find utility. The program should list all the
files present in the root directory. The absolute path to the root directory will be provided as
command line argument.
Figure 1 shows the structure of a directory dir1 located in path /home/os/Documents/dir1.
The listing 1 shows the corresponding output for the directory structure shown in the figure 1.
Do not include any directory in your output

2
dir1

dir11

file11.txt

file12.txt
dir12

dir21

file23.txt
file21.txt

file22.txt
file1.txt

file2.txt

Figure 1: The directory structure for task1

Listing 1: Task1 output


$ g c c f i n d t a s k 1 . c −o t a s k 1
$ . / t a s k 1 /home/ o s / Documents / d i r 1
/home/ o s / Documents / d i r 1 / f i l e 1 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 1 / f i l e 1 1 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 1 / f i l e 1 2 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / d i r 2 1 / f i l e 2 3 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 2 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 1 . t x t
/home/ o s / Documents / d i r 1 / f i l e 2 . t x t
$
You can use the below mentioned APIs to implement this part of the assignment. Refer to
man page of these APIs to know about their usage.

pipe lstat stat close


perror exit sprintf free

2.2 Task 2: Filter out the files [15 marks]


In this part, you will extend the find utility from task 1. Given a size X KB and file type Z, Z
will be a integer constant in range 0 to 7. Your program should print all files having size >= X
and file type <= Z. Different file type available on Linux are listed in table 1. We have added
”UNKNOWN” to identify an unsupported file type.
HINT: You need to create a custom-mapping from Linux file type to Integer type assigned
to each file in table 1

Syntax
$./task2 <relative path to a directory> <size in KB> <file type integer>

You can use the below mentioned APIs to implement this part of the assignment. Refer to
man page of these APIs to know about their usage.

pipe lstat stat close


perror exit sprintf free

3
dir1 (4096 B)

dir11 (4096 B)

file11.txt (2285 B)

file12.txt (0 B)

dir12 (4096 B)

dir21 (4096 B)

file23.txt (3351 B)
file21.txt (161918 B)

file22.txt (209340 B)
file1.txt ( 39 B)

file2.txt ( 59 B)

Figure 2: The directory structure for task 2

Output Category FileType Integer


”FIFO” Pipe (FIFO special) 0
”CHR” Character special 1
”DIR” Directory 2
”BLK” Block special 3
”REG” Regular file 4
”LNK” Symbolic link 5
”SOCK” Socket 6
”UNKNOWN” None of the Above 7

Table 1: Different file types in Linux

Example
Figure 2 shows the structure of a directory called dir1 (present in the path /home/os/Docu-
ments) with size of each file in Bytes

Output
The listing 2 list the desired output for directory structure shown in the figure 2

Listing 2: Task2 output


$ g c c f i n d t a s k 2 . c −o t a s k 2
$ . / t a s k 2 /home/ o s / Documents / d i r 1 3 4
/home/ o s / Documents / d i r 1 / d i r 1 1 4KB DIR
/home/ o s / Documents / d i r 1 / d i r 1 2 4KB DIR
/home/ o s / Documents / d i r 1 / d i r 1 2 / d i r 2 1 4KB DIR
/home/ o s / Documents / d i r 1 / d i r 1 2 / d i r 2 1 / f i l e 2 3 . t x t 3KB REG
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 2 . t x t 158KB REG
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 1 . t x t 204KB REG
$

Task 3: Parallel find [30 marks]


Your task is to parallelize the find implementation. Given a root directory, create a child
process to list all the files within a subdirectory in the root. You should create a child process
for every root subdirectory (refer to the Example). The child should identify all the files in the
subdirectory and communicate the list to the parent through pipe. The parent will read the
list from the pipe and write it to standard output.
4
2.2.1 Example
The listing 3 lists the output corresponding to the directory structure in figure 1

Listing 3: Task3 output


$ g c c f i n d t a s k 3 . c −o t a s k 3
$ . / t a s k 3 /home/ o s / Documents / d i r 1
/home/ o s / Documents / d i r 1 / f i l e 1 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 1 / f i l e 1 1 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 1 / f i l e 1 2 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / d i r 2 1 / f i l e 2 3 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 2 . t x t
/home/ o s / Documents / d i r 1 / d i r 1 2 / f i l e 2 1 . t x t
/home/ o s / Documents / d i r 1 / f i l e 2 . t x t
$
The child process will be created for listing files of dir11 and dir12. During processing of
subdirectory dir12, new child process should not be created for directory dir21
NOTE:
The directory structure in the task3 can contain a symbolic link. A symbolic link is a
special type of file on Linux that points to another file or directory. Symbolic links can be
present anywhere in the root (in this example, dir1 ) directory tree. You should resolve the
symbolic links and list the name of file (if a link points to a file) and all files in the directory
pointed by a symbolic link instead of reporting the size of the symbolic link file.
Using pipe is mandatory for communicating the list of files in the subdirectory processed
by the child to the parent.
Not adhering to the specification will lead to a deduction of the marks
Assumption: A symbolic link will never point to itself recursively. The size of filepath of
all the files in a subdirectory of relative path to a directory will be always < 2KB.

System calls and library functions allowed


- fork - malloc
- free - stat
- opendir - lstat
- readdir - readlink
- closedir - strlen
- read - open
- write - close
- strcpy - strcat
- strcmp - strto* family
- ato* family - wait/waitpid
- printf family - exit
- pipe

Implementation
• Navigate to the Part2 folder of code base, there is a find task1.c find task2.c, and
find task3.c files. You should implement each task in the corresponding files.

• The testcase folder contains the dir1 directory for testing the output of your program.

5
3 Submission
• Make sure that your implementation doesn’t print unnecessary data. Your output should
match exactly with the expected output specified in each question.

• You have to submit zip file named your roll number.zip Eg: 12345.zip containing only
the following files in specified folder format:

12345.zip
|
|------ 12345
|------- Part1
| |----- square.c
| |----- double.c
| |----- root.c
|
|-------- Part2
|----- find_task1.c
|----- find_task2.c
|----- find_task3.c

• Your submission will be evaluated on a recent Linux distribution (Ubuntu 22.04) with
GCC compiler

• Include a README report describing the instructions for compiling and executing the
program

• Use of Boost library is not allowed. Boost is not part of the standard C/C++ distribution

• You will get 0 if we identify any plagiarism in your submission.

You might also like