0% found this document useful (0 votes)
681 views6 pages

Assessment - 6: Question 1: Write The C Program To Implement A Linked List File Allocation Method

The document contains 3 programming questions and their solutions related to different file allocation methods in C: 1) The first question asks to implement a linked list file allocation method and provides the algorithm and source code. 2) The second question asks to implement an indexed file allocation method, providing the algorithm and source code that uses an index block and buffer to store and retrieve files. 3) The third question asks to implement a contiguous file allocation method, outlining an algorithm to sequentially allocate memory to files and print allocation details. Source code is provided that initializes memory and calls a recursive function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
681 views6 pages

Assessment - 6: Question 1: Write The C Program To Implement A Linked List File Allocation Method

The document contains 3 programming questions and their solutions related to different file allocation methods in C: 1) The first question asks to implement a linked list file allocation method and provides the algorithm and source code. 2) The second question asks to implement an indexed file allocation method, providing the algorithm and source code that uses an index block and buffer to store and retrieve files. 3) The third question asks to implement a contiguous file allocation method, outlining an algorithm to sequentially allocate memory to files and print allocation details. Source code is provided that initializes memory and calls a recursive function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assessment - 6

Name: M Vinayak
Regno: 20BDS0303

Question 1: Write the C program to implement a linked list file


allocation method.
Algorithm
1. Start

2. Input and declare the variables

3. Initialize the helper function to return new linked list node from the heap

4. Allocate a new node in a heap and set its data

5. .next pointer of the new node points to nothing

6. Construct three linked list nodes

7. Rearrange the pointers to construct a list

8. Return pointers to construct a list

9. Write helper function to print a linked list

10. Initialize main function

11. 'head' points to the first node

12. Print the linked list

13. End

Source Code

Node(){
Output

Question 2: Write the C program to implement indexed file


allocation method.

Algorithm
1. Start

2. Let n be the size of the buffer

3. Check if there are any


producers If yes, check
if the buffer is full

If no, the producer item is stored in the buffer

4. If the buffer is full, then the producer has to wait

5. Check if there any consumers.


If yes then check whether the buffer is empty
If no then the consumer consumes them into the buffer

6. If the buffer is empty, then the consumer has to wait

7. Repeat checking for the producer and consumer till required

8. End

Source Code
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int files[50], indexBlock[50], indBlock, n;
void recurse1();
void recurse2();
void recurse1() {
printf("Enter the index block: ");
scanf("%d", &indBlock);
if (files[indBlock] != 1) {
printf("Enter the number of blocks and the number of files needed for the index %d on the disk: ", indBlock);
scanf("%d", &n);
}
else {
printf("%d is already allocated\n", indBlock);
recurse1();
}
recurse2();
}
void recurse2() {
int ch;
int flag = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &indexBlock[i]);
if (files[indexBlock[i]] == 0)
flag++;
}
if (flag == n) {
for (int j = 0; j < n; j++) {
files[indexBlock[j]] = 1;
}
printf("Allocated\n");
printf("File Indexed\n");
for (int k = 0; k < n; k++) {
printf("%d -> %d : %d\n", indBlock, indexBlock[k],
files[indexBlock[k]]);
}
}
else {
printf("File in the index is already allocated\n");
printf("Enter another indexed file\n");
recurse2();
}
printf("Do you want to enter more files?\n");
printf("Enter 1 for Yes, Enter 0 for No: ");
scanf("%d", &ch);
if (ch == 1)
recurse1();
else
exit(0);
return;
}
int main()
{
for (int i = 0;i < 50;i++)
files[i] = 0;
recurse1();
return 0;
}

Output
Question 3: Write the C program to implement contiguous file
allocation method.

Algorithm
1. Start the program.

2. Gather information about the number of files.

3. Gather the memory requirement of each file.

4. Allocate the memory to the file in a sequential manner.

5. Select any random location from the available location.

6. Check if the location that is selected is free or not.

7. If the location is allocated set the flag = 1.

8. Print the file number, length, and the block allocated.

9. Gather information if more files have to be stored.

10. If yes, then go to STEP 2.

11. End

Source Code
recurse(files);

exit(0);

for(i=0;i<50;i++)

recurse(files);

Output

You might also like