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

This set of Linux Debugging questions and answers focuses on dup, fcnlt, lseek and read System Calls.

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 the mentioned
View Answer

Answer: b
Explanation: none.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san

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

2. What is the output of this program?

advertisement
advertisement
  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 the mentioned
View Answer

Answer: a
Explanation: This program will write only “Linux” in the file “sanfoundry.txt” because we are writing only 5 bytes with “write” system call.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ls
san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# ls
san san.c sanfoundry.txt
[root@localhost sanfoundry]# vim sanfoundry.txt
[root@localhost sanfoundry]#
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the output of this program?

advertisement
  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 the mentioned
View Answer

Answer: d
Explanation: This program will write nothing in the source file “san.c” because we are opening the file in read only mode.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
write: Bad file descriptor
[root@localhost sanfoundry]#
advertisement

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
View Answer

Answer: a
Explanation: 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]#

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
View Answer

Answer: a
Explanation: The write system call writes at the end of the file because the file is opened with O_APPEND flag.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# vim san.c
[root@localhost sanfoundry]#

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 the mentioned
View Answer

Answer: a
Explanation: We have to use “lseek” system call if we want to read the file from the beginning just after writing into it.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ls
san san.c
[root@localhost sanfoundry]# ./san

[root@localhost sanfoundry]# ls
san san.c sanfoundry.txt
[root@localhost sanfoundry]# vim sanfoundry.txt
[root@localhost sanfoundry]#

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 the mentioned
View Answer

Answer: d
Explanation: Because of “lseek” system call the character “s” is overwritten by character “d” in the file “sanfoundry.txt”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
d
[root@localhost sanfoundry]#

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 the mentioned
View Answer

Answer: a
Explanation: The lseek function allows the file offset to be set beyond the end of the file and if the data is latter written this point, subsequent reads of the data in the gap returns NULL.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
linux
[root@localhost sanfoundry]#

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
View Answer

Answer: b
Explanation: The “dup” system creates the a copy of the file descriptor.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
#include
[root@localhost sanfoundry]#

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 the mentioned
View Answer

Answer: c
Explanation: The “close” system call closes a file descriptor but in the program “fd2” in not a file descriptor. Hence close system call returns -1.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
-1
[root@localhost sanfoundry]#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.