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

DigitalOcean Referral Badge DigitalOcean Referral Badge

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

Answer: Option 'A'

this program will print the string “Linux”

DigitalOcean Referral Badge

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 these

Answer: Option 'A'

this program will print the string “Linux”

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;
  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 these

Answer: Option 'B'

this program will print nothing

DigitalOcean Referral Badge

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 these

Answer: Option 'A'

“Sanfoundry”

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

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 these

Answer: Option 'B'

this program will create one named pipe “test_fifo” in the /tmp directory

In this program when the mkfifo executes second time, the fifo already exists. Hence it gives error.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
mkfifo: File exists
[root@localhost sanfoundry]#

DigitalOcean Referral Badge

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 these

Answer: Option 'A'

0

DigitalOcean Referral Badge

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 these

Answer: Option 'B'

-1

DigitalOcean Referral Badge

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

Answer: Option 'C'

-1

DigitalOcean Referral Badge

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 these

Answer: Option 'B'

will be created in present working directory

DigitalOcean Referral Badge
    DigitalOcean Referral Badge DigitalOcean Referral Badge

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 these

Answer: Option 'A'

The “pipe” system call returns 0 on the successfull creation of the pipe.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]#

DigitalOcean Referral Badge

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

Recent Posts