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 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;...
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?
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling...
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling algorithm. It should implement threads for the algorithm plus the main thread using a linked list to represent the list of jobs available to run. Each node will represent a Job with the following information: int ProcessID int time time needed to finish executing The thread representing the scheduler algorithm should continue running until all jobs end. This is simulated using the time variable...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT