Linux Debugging Questions & Answers – dup, fcntl, lseek and read System Calls

DigitalOcean Referral Badge DigitalOcean Referral Badge

1.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3. 
     
  4.    int main()
  5.    {
  6.        pid_t fd;
  7.        char ch;
  8.        int count;
  9.        fd = open("san.c",O_RDONLY);
  10.        do{
  11.            count = read(fd,&ch,1);
  12.            printf("%c",ch);
  13.        }while(count);
  14.        return 0;
  15.    }

   A.) it will print nothing
   B.) it will print the source code of the source file “san.c”
   C.) segmentation fault
   D.) none of these

Answer: Option 'B'

it will print the source code of the source file “san.c”

DigitalOcean Referral Badge

2.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int fd, count;
  7.        fd = open("sanfoundry.txt",O_WRONLY|O_CREAT);
  8.        count = write(fd,"Linux System Programming",5);
  9.        if(count != 5)
  10.            perror("write");
  11.        return 0;
  12.    }

   A.) it will create a file “sanfoundry.txt” in the present working directory
   B.) it will write the string “Linux System Programming” in the file “sanfoundry.txt”
   C.) all of the mentioned
   D.) none of these

Answer: Option 'A'

it will create a file “sanfoundry.txt” in the present working directory

DigitalOcean Referral Badge

3.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3. 
     
  4.    int main()
  5.    {     
  6.        int fd, count;
  7.        fd = open("san.c",O_RDONLY);
  8.        count = write(fd,"Linux",5);
  9.        if(count != 5)
  10.            perror("write");
  11.        return 0;
  12.    }

   A.) it will write the string “Linux” in the beginning of source file “san.c”
   B.) it will write the string “Linux” in the end of the source file “san.c”
   C.) segmentation fault
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge

4.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<fcntl.h>
  4. 
     
  5.    int main()
  6.    {
  7.        int fd, count;
  8.        char ch, *buff;
  9.        buff = (char *)malloc(sizeof(char)*10);
  10.        fd = open("san.c",O_RDONLY);
  11.        count = read(fd,buff,5);
  12.        printf("%d\n",count);
  13.        return 0;
  14.    }

   A.) 5
   B.) 10
   C.) 0
   D.) -1

Answer: Option 'A'

The “read” system call returns the number of bytes successfully read.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
5
[root@localhost sanfoundry]#

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

5.

In the output of this program, the string “/* Linux */” will be added at the ____ of the source file.

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<fcntl.h>
  4. 
     
  5.    int main()
  6.    {
  7.        int fd;
  8.        fd = open("san.c",O_RDWR|O_APPEND);
  9.        write(fd,"/* Linux */",11);
  10.        return 0;
  11.    }

   A.) end
   B.) beginning
   C.) second line
   D.) third line

Answer: Option 'A'

end

DigitalOcean Referral Badge

6.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<fcntl.h>
  4. 
     
  5.    int main()
  6.    {
  7.        int fd;
  8.        char *buff;
  9.        buff = (char *)malloc(sizeof(char)*5);
  10.        fd = open("sanfoundry.txt",O_RDWR|O_CREAT); 
  11.        write(fd,"Linux",5);
  12.        read(fd,buff,5);
  13.        printf("%s\n",buff);
  14.    }

   A.) it will print nothing
   B.) it will print the string “Linux”
   C.) segmentation fault
   D.) none of these

Answer: Option 'A'

it will print nothing

DigitalOcean Referral Badge

7.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int fd, count;
  7.        char ch;
  8.        fd = open("sanfoundry.txt",O_RDWR|O_CREAT);
  9.        write(fd,"s",1);
  10.        lseek(fd,0,SEEK_SET);
  11.        write(fd,"d",1);
  12.        lseek(fd,0,0);
  13.        read(fd,&ch,1);
  14.        printf("%c\n",ch);
  15.        return 0;
  16.    }

   A.) d
   B.) s
   C.) sd
   D.) none of these

Answer: Option 'D'

none of these

DigitalOcean Referral Badge

8.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3. 
     
  4.    int main()
  5.    {
  6.        int fd, count;
  7.        char ch[10];
  8.        fd = open("sanfoundry.txt",O_RDWR|O_CREAT);
  9.        write(fd,"linux",5);
  10.        lseek(fd,2,SEEK_END);
  11.        write(fd,"san",3);
  12.        lseek(fd,0,0);
  13.        count = read(fd,ch,10);
  14.        printf("%s\n",ch);
  15.        return 0;
  16.    }

   A.) linux
   B.) linuxsan
   C.) linux san
   D.) none of these

Answer: Option 'A'

linux

DigitalOcean Referral Badge

9.

What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<fcntl.h>
  4. 
     
  5.    int main()
  6.    {
  7.        int fd, new_fd;
  8.        char *buff;
  9.        buff = (char *)malloc(sizeof(char)*8);
  10.        fd = open("san.c",O_RDONLY);
  11.        new_fd = dup(fd);
  12.        close(fd);
  13.        read(new_fd,buff,8);
  14.        printf("%s\n",buff);
  15.    }

   A.) this program will not print anything
   B.) this program will print “#include”
   C.) this program will give the segmentation fault
   D.) this program will give the syntax error

Answer: Option 'B'

this program will print “#include”

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

10.

What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<fcntl.h>
  3. 
     
  4.     int main()
  5.     {
  6.         int fd, fd2, ret;
  7.         fd = open("san.c",O_RDONLY);
  8.         ret = close(fd2);
  9.         printf("%d\n",ret);
  10.     }

   A.) 0
   B.) 1
   C.) -1
   D.) none of these

Answer: Option 'C'

-1

DigitalOcean Referral Badge

Linux Debugging Questions & Answers – dup, fcntl, lseek and read System Calls Download Pdf

Recent Posts