Question

In: Computer Science

Write source code in C to simulate the contiguous file allocation with the following conditions: •...

Write source code in C to simulate the contiguous file allocation with the following conditions:

• Prompt the user to ender the no of files

• Enter the name of the file

• Enter the Starting block number

• Enter no of block occupied by the file

i.Condition: No two files must have the same block(if the user enter the same block no present in the previous file prompt the user “Block already in use”)

#include<stdio.h>

#include<conio.h>

struct

{

    char fname[10];

} f[10];

main()

{

    int n,i,j,b[20],sb[20],t[20],x,c[20][20];

    printf("Enter no. of files: ");

    scanf("%d",&n);

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

    {

        printf("Enter file %d name: ", i+1);

        scanf("%s", f[i].fname);

        printf("Enter no. of blocks occupied by file %s: ",f[i].fname);

        scanf("%d",&b[i]);

        printf("Enter the starting block of file %s: ",f[i].fname);

        scanf("%d",&sb[i]);

        t[i]=sb[i];

        for(j=0;j<b[i];j++)

            c[i][j]=sb[i]++;

    }

   

    printf("Filename\tStart block\tlength\n");

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

        printf("%s\t %d \t%d\n",f[i].fname,t[i],b[i]);

    printf("blocks occupiedate:\n");

   

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

    {

        printf("file name %s ",f[i].fname);

        for(j=0;j<b[i];j++)

            printf("\t%d",c[i][j]);

        printf("\n");

    }

}

I've been trying to solve this question:

Condition: No two files must have the same block(if the user enter the same block no present in the previous file prompt the user “Block already in use”), "what should I write to implement IF CONDITION" in this part of question? I wrote the whole code but still I have to perform code with this condition.

Solutions

Expert Solution

At first according to the question, you should take the input of starting block of file before the no. of blocks to allocate. Because if the starting block lies in the blocks previoisly allocated, then we need to tell user that "Block already in use".

Also, if the starting block is valid but the block size overlaps with the other block previously allocated, this will also be a problem. So we need to check for this condition also.

So, the reading input section of your code will look like this:

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

    {

        printf("Enter file %d name: ", i+1);

        scanf("%s", f[i].fname);

         printf("Enter the starting block of file %s: ",f[i].fname);

        scanf("%d",&sb[i]);

        //check is starting block no. lies in any
        // of the previously allocated blocks.
        int flag = 0;


        for(int k = 0; k < i; k++){

            //if starting block no. lies within the block already present
            if(sb[i] >= sb[k] - b[k] && sb[i] <= sb[k]){
                //change in flag value will indicate
                //that block is already allocated
                flag = 1;
                break;
            }
        }

        if(flag){
            printf("Block already in use!\n");
            i--;
            continue;
        }

        printf("Enter no. of blocks occupied by file %s: ",f[i].fname);

        scanf("%d",&b[i]);


        //Check if the block size allocated do not overlap
        // with previously allocated block
        flag = 0;

        for(int k = 0; k < i; k++){

            //if block overlaps with the block already present
            if(sb[i] + b[i] >= sb[k] - b[k] && sb[i] + b[i] <= sb[k]){

                //change in flag value will indicate
                //that block is already allocated
                flag = 1;
                break;
            }
        }

        if(flag){
            printf("Block already in use!\n");
            i--;
            continue;
        }

        t[i]=sb[i];

        for(j=0;j<b[i];j++)

            c[i][j]=sb[i]++;

    }

The output is:

If this helps you, do upvote as it motivates us a lot!


Related Solutions

Write an x86 assembly language program that performs equivalently to the C++ source code file shown...
Write an x86 assembly language program that performs equivalently to the C++ source code file shown below.Please note that commented out behavior must be implemented in x86 assembly language. There is no standard, portable way to perform some of these actions in C++. #include void main() { // Use registers for these in your x86 assembly language program // Only use the .data segment for string (character array) variables int eax; int esi; int ecx; int edi; // Loop the...
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program...
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program to schedule disk in seek optimization.
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
use modelsim write Verilog code for the following digital logic circuits and then simulate them by...
use modelsim write Verilog code for the following digital logic circuits and then simulate them by writing a testbench module for each of them , (a)The FSMs for the snail problem that is in the slides (a snail crawls over a tape that has 0 and 1 and smiles if it has detected the '10' bits using both Moore and Mealy FSM. Note that the pattern is '10' not '01' as in the slides. (b) A rock-paper-scissor game played by...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from...
What is the purpose of the XDC file? Simulate the behavioral code Map the I/O from VHDL to Basys3 Board Edit the Basys3 Board device type Run the synthesis What is the purpose of declaring a “signal”? Which file contains the list of input combinations used to simulate a circuit in Xilinx Vivado?   XDC Design Testbench What is the purpose of using ieee.std_logic_unsigned.alL or ieee.std_logic_signed.all in lab4 and lab6 testbench2?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT