Linux Debugging Questions & Answers – Unix Domain Sockets

This set of Linux Debugging questions and answers focuses on Unix Domain Sockets.

1. What is the output of this program?

  1.    #include<stdio.h>
  2.  
  3.    int main()
  4.    {
  5.        int fd_socket;
  6.        fd_socket = socket(AF_UNIX,SOCK_STREAM,0);
  7.        printf("%d\n",fd_socket);
  8.        return 0;
  9.    }

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

Answer: d
Explanation: To use socket(), the header files sys/types.h and sys/socket.h are required.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:6:21: error: ‘AF_UNIX’ undeclared (first use in this function)
san.c:6:21: note: each undeclared identifier is reported only once for each function it appears in
san.c:6:29: error: ‘SOCK_STREAM’ undeclared (first use in this function)
[root@localhost sanfoundry]#

2. In this program, the third argument of the socket() is used for _____ potocol.

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/socket.h>
  4.    int main()
  5.    {
  6.        int fd_socket;
  7.        if(socket(AF_UNIX,SOCK_STREAM,0) == -1)
  8.            perror("socket");
  9.        return 0;
  10.    }

a) TCP/IP
b) UDP
c) both TCP/IP and UDP
d) none of mentioned
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. By this program the soket “san_sock” will create

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server;
  9.        int fd_server;
  10.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  11.        if(fd_server == -1)
  12.            perror("socket");
  13.        add_server.sun_family = AF_UNIX;
  14.        strcpy(add_server.sun_path,"san_sock");
  15.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  16.            perror("bind");
  17.        return 0;
  18.    }

a) in the /tmp directory
b) in the /usr directory
c) in the present working directory
d) none of the mentioned
View Answer

Answer: c
Explanation: None.
Output:
[root@localhost sanfoundry]# ls
san.c
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# ls
san san.c san_sock
[root@localhost sanfoundry]#
advertisement

4. What is the length of of the queue for pending connections in this program?

advertisement
  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server;
  9.        int fd_server;
  10.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  11.        if(fd_server == -1)
  12.            perror("socket");
  13.        add_server.sun_family = AF_UNIX;
  14.        strcpy(add_server.sun_path,"server_sock2");
  15.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  16.            perror("bind");
  17.        if( listen(fd_server,3) != 0)
  18.            perror("listen");
  19.        return 0;
  20.    }

a) 0
b) 1
c) 2
d) 3
View Answer

Answer: d
Explanation: The second argument of listen() specifies the length for the queue for pending connections.

5. What is the output of the program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        if( listen(fd_server,3) != 0)
  20.            perror("listen");
  21.        len = sizeof(add_client);
  22.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
  23.        printf("Sanfoundry\n"); 
  24.        return 0;
  25.    }

a) the program will print the string “Sanfoundry”
b) the process will remain block
c) segmentation fault
d) none of the mentioned
View Answer

Answer: b
Explanation: There is no peding request in the queue for listening socket “san_sock”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
^Z
[4]+ Stopped ./san
[root@localhost sanfoundry]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/socket.h>
  4.  
  5.    int main()
  6.    {
  7.        int fd;
  8.        fd = socket(AF_UNIX,SOCK_STREAM,0);
  9.        printf("%d\n",fd);
  10.        return 0;
  11.    }

a) 0
b) 1
c) 2
d) 3
View Answer

Answer: d
Explanation: The socket() returns the lowest available file descriptor and in this program i.e. 3.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
3
[root@localhost sanfoundry]#

7. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.    #include<errno.h>
  6.  
  7.    int main()
  8.    {
  9.        struct sockaddr_un addr;
  10.        int fd;
  11.        fd = socket(AF_UNIX,SOCK_STREAM,0);
  12.        if (fd == -1)
  13.            perror("socket");
  14.        addr.sun_family = AF_UNIX;
  15.        strcpy(addr.sun_path,"san_sock");
  16.        if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
  17.            printf("Sanfoudnry\n");
  18.        return 0;
  19.    }

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

Answer: a
Explanation: The first argument of the bind() is not a valid file descriptor in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoudnry
[root@localhost sanfoundry]#

8. What this program is not able to connect with any client program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        len = sizeof(add_client);
  20.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
  21.        printf("Sanfoundry\n"); 
  22.        return 0;
  23.    }

a) the listen() is missing
b) the connect() is missing
c) the read() and write() are missing
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/un.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        struct sockaddr_un add_server, add_client;
  9.        int fd_server, fd_client;
  10.        int len;
  11.        char ch;
  12.        fd_server = socket(AF_UNIX,SOCK_STREAM,0);
  13.        if(fd_server == -1)
  14.            perror("socket");
  15.        add_server.sun_family = AF_UNIX;
  16.        strcpy(add_server.sun_path,"san_sock");
  17.        if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
  18.            perror("bind");
  19.        len = sizeof(add_client);
  20.        fd_client = connect(fd_server,(struct sockaddr*)&add_client,&len);
  21.        printf("Sanfoundry\n"); 
  22.        return 0;
  23.    }

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

Answer: c
Explanation: The syntax of the connect() is wrong. connect() should be used in client program only.
Ouptut:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:20:46: warning: passing argument 3 of ‘connect’ makes integer from pointer without a cast [enabled by default] /usr/include/sys/socket.h:129:12: note: expected ‘socklen_t’ but argument is of type ‘int *’
[root@localhost sanfoundry]#

10. What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<sys/types.h>
  3.     #include<netinet/in.h>
  4.     #include<sys/socket.h>
  5.     #include<errno.h>
  6.  
  7.     int main()
  8.     {
  9.         struct sockaddr_in addr;
  10.         int fd;
  11.         fd = socket(AF_UNIX,SOCK_STREAM,0);
  12.         if (fd == -1)
  13.             perror("socket");
  14.         addr.sun_family = AF_UNIX;
  15.         strcpy(addr.sun_path,"san_sock");
  16.         if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
  17.             printf("Sanfoudnry\n");
  18.         return 0;
  19.     }

a) error
b) “Sanfoundry”
c) segmentation fault
d) none of the mentioned
View Answer

Answer: a
Explanation: The structure used for AF_UNIX if sockaddr_un.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:14:6: error: ‘struct sockaddr_in’ has no member named ‘sun_family’
san.c:15:2: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default] san.c:15:13: error: ‘struct sockaddr_in’ has no member named ‘sun_path’
[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.