Tuesday, 25 August 2015

PR( 05) Write a program in C++ to create a RAMDRIVE and associate an acyclic directory structure to it. Use this RAMDRIVE to store input, out files to run a calculator program.

cal.c

#include<iostream>
#include<fstream>
#include<sys/types.h>
#include<stdlib.h>

using namespace std;

int main()
{
    int a,b,c,ch;
    FILE *fp;

    // The two numbers are stored in input.txt file

    fp = fopen("/mnt/ramdisk1/input.txt","r");        // Open input.txt file stored in RAMDISK
    fscanf(fp, "%d", &a);   
    fscanf(fp, "%d", &b);
    fclose(fp);                        // Close the file

    fp = fopen("/mnt/ramdisk1/output.txt","w");        // Open (Create if not exist) the output.txt file stored in RAMDISK
    fprintf(fp, "\n Addition: %d", (a+b));            // Write addition in file
    fprintf(fp, "\n Subtraction: %d", (a-b));        // Write subtraction in file
    fprintf(fp, "\n Multiplication: %d", (a*b));        // Write multiplication in file
    fprintf(fp, "\n Division: %d", (a/b));            // Write division in file

    return 0;
}
 ****************************************************************************************

 ramdisk.c


#include <stdio.h>
#include <stdlib.h>

long long create_ramdisk(void)
{
FILE *fp;
int ch;
long long space = 0;
system("mkdir -p /mnt/ramdisk");
system("/sbin/mke2fs -q /dev/ram");
system("mount /dev/ram /mnt/ramdisk");
system("df /mnt/ramdisk tmp.txt");
fp = fopen("tmp.txt", "r");
if(!fp)
{
printf("Can't open file\n");
return 0;
}
while((ch = fgetc(fp)) != EOF && ch != '\n');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
while((ch = fgetc(fp)) != EOF && ch != ' ');
while((ch = fgetc(fp)) != EOF && ch == ' ');
ungetc(ch, fp);
if(feof(fp))
{
printf("Unexpected read\n");
return 0;
}
fscanf(fp, "%lld", &space);
fclose(fp);
remove("tmp.txt");
return space;
}

void remove_ramdisk(void)
{
system("umount /mnt/ramdisk");
}

int main(void)
{
long long space = create_ramdisk();
printf("Ramdisk has %lld bytes free\n", space);
remove_ramdisk();
return 0;
}


*******************************************************************************************

ramdisk.cpp
#include<iostream>

#include<sys/types.h>
#include<stdlib.h>

using namespace std;

main()
{
    int ch;
    do
    {
        cout<<"\n Switch to the Superuser mode for better results.\n";
        cout<<"\n MENU";
        cout<<"\n 1. Check Free Space";  
        cout<<"\n 2. Create RAMDISK";
        cout<<"\n 3. Copy Files to RAMDISK";
        //cout<<"\n 4. Run Calculator program";
        //cout<<"\n 4. See the contents of Output file";
        cout<<"\n 4. Unmount RAMDISK";
        cout<<"\n 5. Exit";

        cout<<"\n\n Enter Your Choice: ";
        cin>> ch;

        switch(ch)
        {
            case 1:
                system("df -h");
                break;
            case 2:
                system("rmdir /mnt/ramdisk1");  
                system("mkdir /mnt/ramdisk1");
                system("chmod 777 /mnt/ramdisk1");
      
                system("mount -t tmpfs -o size=256m tmpfs /mnt/ramdisk1");
                break;
            case 3:
                // copy input.txt and calculator (object file of calculator.cpp) to RAMDISK
                system("cp input.txt /mnt/ramdisk1");      
                system("cp calculator /mnt/ramdisk1");
                cout<<"Files successfully copied to RAMDISK.";
                break;
            /* case 4:
                system("/mnt/ramdisk1/calculator");
                break; */
            case 4:
                system("umount /mnt/ramdisk1");
                break;
            case 5:
                return 0;                  
        }
        cout<<"Do you want to continue? (1/0): ";
        cin>> ch;
   
    } while(ch == 1);  

    return 0;

}
 

No comments:

Post a Comment