Linux Debugging Questions & Answers – Named and Un-named Pipe Calls

«
»
This set of Linux Debugging questions and answers focuses on Named and Un-named Pipe Calls.

1. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<string.h>
  3.  
  4.    int main()
  5.    {
  6.        int fd[2];
  7.        int count;
  8.        char buffer[6];
  9.        if( pipe(fd) != 0)
  10.            perror("pipe");
  11.        memset(buffer,'\0',6);
  12.        count=write(fd[1],"Linux",6);
  13.        read(fd[0],buffer,6);
  14.        printf("%s\n",buffer);
  15.        return 0;put
  16.    }

a) this program will print the string “Linux”
b) this program will print nothing because the buffer is empty
c) segmentation fault
d) none of the mentioned
View Answer

Answer: a
Explanation: The data in buffer is written by the pipe.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
Linux
[[email protected] sanfoundry]#
advertisement

2. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int fd[2];
  7.        int child;
  8.        char buff[6];
  9.        if(pipe(fd) != 0)               
  10.            perror("pipe");
  11.        child=fork();
  12.        switch(child){
  13.            case -1 :
  14.                perror("fork");
  15.                exit(1);
  16.            case 0 :
  17.                if (write(fd[1],"Linux",6) != 6)
  18.                    perror("write");
  19.                    break;
  20.                default :
  21.                    read(fd[0],buff,6);
  22.                    printf("%s\n",buff);
  23.                    break;
  24.        }
  25.        return 0;
  26.    }

a) this program will print the string “Linux”
b) this program will print nothing
c) segmentation fault
d) none of the mentioned
View Answer

Answer: a
Explanation: One process can read and write to another process by pipe.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
Linux
[[email protected] sanfoundry]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.  
  4.    int main()
  5.    {
  6.        int fd;
  7.        char buff[512];
  8.        if( mkfifo("/tmp/test_fifo",0666) == -1)
  9.            perror("mkfifo");       
  10.        fd = open("/tmp/test_fifo",O_RDONLY);
  11.        read(fd,buff,512);
  12.        printf("%s\n",buff);
  13.        return 0;
  14.    }

a) this program will print the garbage of 512 bytes
b) this program will print nothing
c) segmentation fault
d) none of the mentioned
View Answer

Answer: b
Explanation: In this program the fifo is opened in read only mode. Hence the process will remain block on read.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
^Z
[12]+ Stopped ./san
[[email protected] sanfoundry]#
advertisement

4. This program will print the _____ string.

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        int fd[2];
  6.        char buff[11];
  7.        if (pipe(fd) != 0)
  8.            perror("pipe");
  9.        write(fd[1],"Sanfoundry",11);
  10.        lseek(fd[0],0,3);
  11.        read(fd[0],buff,11);
  12.        printf("%s\n",buff);
  13.        return 0;
  14.    }

a) “Sanfoundry”
b) “San”
c) “foundry”
d) none of the mentioned
View Answer

Answer: a
Explanation: The “lseek” system call does not work with pipes.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
Sanfoundry
[[email protected] sanfoundry]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        if (mkfifo("/tmp/test_fifo",0666) != 0)
  6.            perror("mkfifo");
  7.        if (mkfifo("/tmp/test_fifo",0666) != 0)
  8.            perror("mkfifo");
  9.        return 0;
  10.    }

a) this program will create two named pipes “test_fifo” in the /tmp directory
b) this program will create one named pipe “test_fifo” in the /tmp directory
c) segmentation fault
d) none of the mentioned
View Answer

Answer: b
Explanation: In this program when the mkfifo executes second time, the fifo already exists. Hence it gives error.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
mkfifo: File exists
[[email protected] sanfoundry]#
advertisement

6. 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 buff[10];
  8.        if (mkfifo("/tmp/test_fifo",0666) != 0)
  9.            perror("mkfifo");
  10.        fd = open("/tmp/test_fifo",O_RDONLY|O_NONBLOCK);
  11.        count = read(fd,buff,10);
  12.        printf("%d\n",count);
  13.        return 0;
  14.    }

a) 0
b) -1
c) 10
d) none of the mentioned
View Answer

Answer: a
Explanation: The read system call will return 0 bytes.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
0
[[email protected] sanfoundry]#

7. This program will print the value

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.  
  4.    int main()
  5.    {
  6.        int rfd, wfd, count;
  7.        char buff[11];
  8.        if (mkfifo("/tmp/test_fifo",0666) != 0)
  9.            perror("mkfifo");
  10.        wfd = open("/tmp/test_fifo",O_WRONLY|O_NONBLOCK);
  11.        count = write(wfd,"Sanfoundry",11);
  12.        printf("%d\n",count);
  13.        rfd = open("/tmp/test_fifo",O_RDONLY|O_NONBLOCK);
  14.        count = read(rfd,buff,11);
  15.        return 0;
  16.    }

a) 0
b) -1
c) 11
d) none of the mentioned
View Answer

Answer: b
Explanation: The “write” system call will return -1 because named pipe is open only for writing. In named pipe writing can only be done when it is open for read and write both.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
-1
[[email protected] sanfoundry]#

8. What is the output of this program?

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        int fd[3],count;
  6.        if (pipe(fd) != 0)
  7.            perror("pipe");
  8.        count = write(fd[2],"Hello",6);
  9.        printf("%d\n",count);
  10.        return 0;
  11.    }

a) 6
b) 0
c) -1
d) segmentation fault
View Answer

Answer: c
Explanation: Data can be written only in fd[1].
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
-1
[[email protected] sanfoundry]#
advertisement

9. In this program the fifo “my_fifo”

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        if (mkfifo("my_fifo",0666) != 0)
  6.            perror("mkfifo");
  7.        return 0;
  8.    }

a) can not be created
b) will be created in present working directory
c) will have the execute permissions
d) none of the mentioned
View Answer

Answer: b
Explanation: None.
Output:
[[email protected] sanfoundry]# ls
san san.c
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
[[email protected] sanfoundry]# ll
total 12
prw-r–r–. 1 root root 0 Aug 21 15:04 my_fifo
-rwxr-xr-x. 1 root root 4925 Aug 21 15:04 san
-rw-r–r–. 1 root root 99 Aug 21 15:01 san.c
[[email protected] sanfoundry]#

10. What is the output of this when the pipe is successfully created?

  1.     #include<stdio.h>
  2.  
  3.     int main()
  4.     {
  5.         int ret_val;
  6.         int fd[2];
  7.         ret_val = pipe(fd);
  8.         printf("%d\n",ret_val);
  9.         return 0;
  10.     }

a) 0
b) -1
c) 1
d) none of the mentioned
View Answer

Answer: a
Explanation: The “pipe” system call returns 0 on the successfull creation of the pipe.
Output:
[[email protected] sanfoundry]# gcc -o san san.c
[[email protected] sanfoundry]# ./san
0
[[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