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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san

advertisement
  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. }
[[email protected] sanfoundry]#

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 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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ls
san san.c
[[email protected] sanfoundry]# ./san
[[email protected] sanfoundry]# ls
san san.c sanfoundry.txt
[[email protected] sanfoundry]# vim sanfoundry.txt
[[email protected] sanfoundry]#

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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
write: Bad file descriptor
[[email protected] sanfoundry]#

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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
5
[[email protected] 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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
[[email protected] sanfoundry]# vim san.c
[[email protected] sanfoundry]#
advertisement

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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ls
san san.c
[[email protected] sanfoundry]# ./san

[[email protected] sanfoundry]# ls
san san.c sanfoundry.txt
[[email protected] sanfoundry]# vim sanfoundry.txt
[[email protected] 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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
d
[[email protected] sanfoundry]#

8. 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.        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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
linux
[[email protected] 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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
#include
[[email protected] 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:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected]ost sanfoundry]# ./san
-1
[[email protected] sanfoundry]#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Reference Books in Linux Commands & Shell Programming.
Here’s the list of Best Reference 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.

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!

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn