Avijitcse207

download Avijitcse207

of 8

Transcript of Avijitcse207

  • 8/8/2019 Avijitcse207

    1/8

    HOMEWORK No.3

    Course :Linux Programming Code : CSE207

    Homework Title/No.:___Avijit Kumar__________ Course code: _______CSE 207_______

    Course Instructor: ___Atul Malhotra________ Course Tutor (if applicable) :________________

    Date of Allotment: ____________ Date of Submission: ______07/11/10____________

    Students Roll No. : ___RF1902A20_______ Section No.: ________F1902________

    Declaration:

    I declare that this assignment is my individual work. I have not copied from any other students

    work or from any other source except where due acknowledgement is made explicitly in the text,nor has any part been written for me by another person.

    Students Signature: ______Avijit Kumar_________

    Evaluators comment:

    __________________________________________________________________________

    Marks obtained: __________________________ out of: __________________________

    Content of Homework should start from this page only:

    PART-A

    Ques 1: Write a C program to create a file at least of 2 MB with command

    ls -R / >file1.txt and move contents of the file to another file using

    a) System call(open, read and buffer of 10 KB)

    b) Library functions

    1. fgets, fputs

    2. fgetc, fputc

    c) System call(with buffer of 20 bytes)

    Also calculate and compare the time taken by each of the above and

    which is better.

  • 8/8/2019 Avijitcse207

    2/8

    Ans: By system call and 2048 size buffer

    #include

    #include

    #include

    #include

    int main()

    {

    char bl[20480];

    int in,out;

    int nread;

    in=open("file.in",O_RDONLY);

    out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

    while((nread=read(in,block,sizeof(bl)))>0)

    write(out,bl,nread);

    exit(0);

    }

    By using library functions

    #include

    #include

    int main()

    {

    int c;

    FILE *in, *out;

  • 8/8/2019 Avijitcse207

    3/8

    in=fopen("file.in","r");

    out=fopen("file.out","w");

    while((c=fgetc(in))!=EOF)

    fputc(c,out);

    exit(0);

    }

    #include

    #include

    #include

    #include

    int main()

    {

    char bl[16348];

    int in,out;

    int nread;

    in=open("file.in",O_RDONLY);

    out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

    while((nread=read(in,block,sizeof(block)))>0)

    write(out,block,nread);

    exit(0);

    }

    2. WAP to read a C program file and count the following in the program.

    1. Total no. of statements

    2. Total no. of included files

    3. Total no. of blocks and brackets

  • 8/8/2019 Avijitcse207

    4/8

    Ans :2

    a. to count number of statements

    #include

    #include

    #include

    #include

    #include

    int main()

    {

    char bl[20];

    int count=0;

    int in, out;

    int nread;

    in=open("file.in",O_RDONLY);

    out=open("file.out",O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);

    while((nread=read(in,bl,sizeof(bl)))>0)

    {

    write(out,bl,nread);

    count ++;

    }

    printf("No. of statemnets is %d",count);

    exit(0);

    }

    b. to count number of included files

  • 8/8/2019 Avijitcse207

    5/8

    3. Write a C program which takes directory name as input and delete

    all files, within that directory.

    Ans:3

    rmdir -p --ignore-fail-on-non-empty ./sources/glibc-build/rm -rf ow

    Q4. Illustrate through example in which scenario would you prefer using perror ( ) and in

    which stderror ( ).

    Answer: The perror() function shall map the error number accessed through the symbol errno to

    a language-dependent error message, which shall be written to the standard error stream as

    follows:

    First (if s is not a null pointer and the character pointed to by s is not the null byte), thestring pointed to by s followed by a colon and a .

    Then an error message string followed by a .

    The perror() function shall not change the orientation of the standard error stream.

    Printing an Error Message for a Function

    The following example replaces bufptr with a buffer that is the necessary size. If an error occurs,the perror() function prints a message and the program exits.

    #include

    #include

    ...

    char *bufptr;

    size_t szbuf;

    ...

    if ((bufptr = malloc(szbuf)) == NULL) {

    perror("malloc"); exit(2);

    }

  • 8/8/2019 Avijitcse207

    6/8

    ...

    EXAMPLE:

    /* perror example */

    #include

    int main (){

    FILE * pFile;

    pFile=fopen ("unexist.ent","rb");if (pFile==NULL)

    perror ("The following error occurred");

    elsefclose (pFile);

    return 0;

    }

    Q5.Write a C program to input file name and change its permissions to reading by all and

    writing by owner only.

    ANSWER:

    #include

    #include #include

    main(){

    pid_t pid;char *parmList[] = {"/bin/chmod", "0700", "/home/op/biaoaai/bead",NULL};

    if ((pid = fork()) ==-1) //fork failed

    perror("fork error");else if (pid == 0) { //child (pid==0 if child)

    execvp("chmod", parmList);

    printf("Return not expected. Must be an execve error.n");

    } else { //parent (pid==child process)//parent specific code goes here

    }

    }

  • 8/8/2019 Avijitcse207

    7/8

    Q6 To create a following tree structure in current directory.

    Answer:

    1- Check which directory you are currently active in prior to using the "mkdir" command.

    This will eliminate errors later if you are in a directory in which you don't have suchcommand privileges. You can check this by using the "pwd" command and pressing

    "Enter."

    2- Type "mkdir [directory]" at the command prompt to make the directory. Use the name of

    your new directory in place of the [directory] command line operator. To create a

    directory called "business," you would type "mkdirbusiness." Be aware that this will

    create the directory within the current working directory.

    3- Use the "-p" command line parameter to create subdirectories within a parent directory.

    For example, you would type "mkdir -p games/strategy/chess" to create a directory tree

    including "strategy/chess" within the "games" directory.

    4- Create a new directory in a directory other than the current working directory using the "-

    p" command line parameter as you did to create a directory tree. In this manner however,

    it is important to include the slash "/" prior to the first directory's name. In other words, if

    Your Rollno

    Previous

    RollnoNext Rollno

    http://www.ehow.com/business/http://www.ehow.com/business/http://www.ehow.com/business/
  • 8/8/2019 Avijitcse207

    8/8

    you wanted to create a "chess" directory in a "games" directory that already exists you

    would type "mkdir -p /games/chess."

    5- Make multiple directories at once using the "mkdir" command. You can do this by

    including a space in between each of the directories you intend to create. For example,the command "mkdir -pgamesbin lib personal" would create a "games," "bin," "lib" and

    "personal" directory all at once.

    http://www.ehow.com/hobbies-games/http://www.ehow.com/hobbies-games/http://www.ehow.com/hobbies-games/http://www.ehow.com/hobbies-games/