Linux Debugging

  Home  Operating System Linux  Linux Debugging


“Linux Debugging frequently Asked Questions by expert members with experience in Debugging. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



80 Linux Debugging Questions And Answers

42⟩ What is the output of this program no 9? #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> int main() { struct sockaddr_in addr; int fd; fd = socket(AF_INET,SOCK_STREAM,0); printf("%dn",fd); return 0; } a) -1 b) 3 c) error d) none of the mentioned

c) error

Explanation:

The header file netinet/in.h is required to use the structure sockaddr_in.

Output:

[root@localhost google]# gcc -o san san.c

san.c: In function 'main':

san.c:7:21: error: storage size of 'addr' isn't known

[root@localhost google]#

 152 views

43⟩ What is the output of this program? #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_server, fd_client, len, len_client; struct sockaddr_in add_server, add_client; char buff[10]; fd_server = socket(AF_INET,SOCK_STREAM,0); if (fd_server == -1){ perror("fd_sock"); exit(1); } len = sizeof(add_server); len_client = sizeof(add_client); if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0) perror("bind"); fd_client = accept(fd_server,(struct sockaddr*)&add_client,len_client); if(fd_client == -1) perror("accept"); read(fd_client,buff,10); return 0; } a) segmentation fault b) error at the time of compilation c) syntax error d) none of the mentioned

b) error at the time of compilation

Explanation:

The third argument of the accept is the type of pointer.

Output:

[root@localhost google]# gcc -o san san.c

san.c: In function 'main':

san.c:26:39: warning: passing argument 3 of 'accept' makes pointer from integer without a cast [enabled by default]

/usr/include/sys/socket.h:214:12: note: expected 'socklen_t * __restrict__' but argument is of type 'int'

[root@localhost google]#

 151 views

44⟩ This program can send the request to #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_client,fd, len; struct sockaddr_in add_server; fd_client = socket(AF_INET,SOCK_STREAM,0); if (fd_client == -1){ perror("fd_sock"); exit(1); } add_server.sin_family = AF_INET; add_server.sin_port = ntohs(4001); add_server.sin_addr.s_addr = inet_addr("193.39.0.4"); len = sizeof(add_server); fd = connect(fd_client,(struct sockaddr*)&add_server,len); if(fd == -1) perror("connect"); return 0; } a) the system having IP address 193.39.0.4 b) any system present in the network c) any system of the private network d) none of the mentioned

a) the system having IP address 193.39.0.4

Explanation:

The IP address is mentioned in the proper element of the structure sockaddr_in

 148 views

45⟩ This program is valid for #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_client,fd, len; struct sockaddr_in add_server; fd_client = socket(AF_INET,SOCK_STREAM,0); if (fd_client == -1){ perror("fd_sock"); exit(1); } add_server.sin_family = AF_INET; add_server.sin_port = ntohs(4001); add_server.sin_addr.s_addr = inet_addr("144.29.8.2"); len = sizeof(add_server); fd = connect(fd_client,(struct sockaddr*)&add_server,len); return 0; } a) IPv4 b) IPv6 c) both (a) and (b) d) none of the mentioned

a) IPv4

 165 views

46⟩ What is the output of this program? #include<stdio.h> #include<sys/socket.h> int main() { int ret; ret = shutdown(0,0); printf("%dn",ret); return 0; } a) 0 b) -1 c) can not be determined d) none of the mentioned

b) -1

Explanation:

The shutdown() is used to close a socket and the first argument in shutdown() is socket.

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

-1

[root@localhost google]#

 200 views

47⟩ What is the problem with this server program? #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_server, fd_client, len; struct sockaddr_in add_server; fd_server = socket(AF_INET,SOCK_STREAM,0); if (fd_server == -1){ perror("fd_sock"); exit(1); } add_server.sin_family = AF_INET; add_server.sin_port = htons(4001); add_server.sin_addr.s_addr = inet_addr("122.23.1.1"); len = sizeof(add_server); if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0) perror("bind"); if(listen(fd_server,5) != 0) perror("listen"); fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len); if(fd_client == -1) perror("accept"); return 0; } a) it can not accept the request of any client b) it will give the segmentation fault c) there is no problem with this program d) none of the ment

a) it can not accept the request of any client

 163 views

48⟩ What is the output of this program no 10? #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_server, fd_client, len, len_client; struct sockaddr_in add_server; fd_server = socket(AF_INET,SOCK_STREAM,0); fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len); if(fd_client == -1) perror("accept"); if(listen(fd_server,5) != 0) perror("listen"); return 0; } a) syntax error b) error at the time of compilation c) segmentation fault d) none of the mentioned

d) none of the mentioned

Explanation:

The listen() must always be used before accept().

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

accept: Invalid argument

[root@localhost google]#

 155 views

49⟩ What is the output of this program no 11? #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_server, fd_client, len, len_client; struct sockaddr_in add_server; fd_server = socket(AF_INET,SOCK_STREAM,0); close(fd_server); perror("accept"); if(listen(fd_server,5) != 0) perror("listen"); fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len); if(fd_client == -1) return 0; } a) syntax error b) error at the time of compilation c) segmentation fault d) none of the mentioned

d) none of the mentioned

Explanation:

The program will not work properly because the file descriptor is not available in the for listen() and accept().

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

accept: Success

listen: Bad file descriptor

[root@localhost google]#

 201 views

50⟩ On which system call, this program (process) waits until the server responds? #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_client,fd, len; struct sockaddr_in add_server; fd_client = socket(AF_INET,SOCK_STREAM,0); if (fd_client == -1){ perror("fd_sock"); exit(1); } add_server.sin_family = AF_INET; add_server.sin_port = ntohs(4001); add_server.sin_addr.s_addr = inet_addr("127.0.0.1"); len = sizeof(add_server); fd = connect(fd_client,(struct sockaddr*)&add_server,len); if(fd == -1) perror("connect"); write(fd,"Hellon",6); return 0; } a) socket() b) connect() c) both (a) and (b) d) none of the mentioned

a) socket()

 182 views

51⟩ What is the the response of this server for this client if both programs are running on the same system? /*This is server.c*/ #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd_server, fd_client, len, len_client; struct sockaddr_in add_server, add_client; char buff[10]; fd_server = socket(AF_INET,SOCK_STREAM,0); if (fd_server == -1){ perror("fd_sock"); exit(1); } add_server.sin_family = AF_INET; add_server.sin_port = htons(4001); add_server.sin_addr.s_addr = inet_addr("127.0.0.1"); len = sizeof(add_server); len = sizeof(add_client); if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0) perror("bind"); if(listen(fd_server,5) != 0) perror("listen"); fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len_client); if(fd_client == -1) perror("accept"); read(fd_cl

a) the server will write back to the client whatever the client will write to the server

Explanation:

The loopback address is used as IP address in both the programs.

 220 views

52⟩ What is the output of this program no 12? #include<stdio.h> int main() { int fd_socket; fd_socket = socket(AF_UNIX,SOCK_STREAM,0); printf("%dn",fd_socket); return 0; } a) -1 b) 0 c) any integer value d) none of the mentioned

d) none of the mentioned

Explanation:

To use socket(), the header files sys/types.h and sys/socket.h are required.

Output:

[root@localhost google]# 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 google]#

 172 views

54⟩ By this program the soket "san_sock" will create #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> int main() { struct sockaddr_un add_server; int fd_server; fd_server = socket(AF_UNIX,SOCK_STREAM,0); if(fd_server == -1) perror("socket"); add_server.sun_family = AF_UNIX; strcpy(add_server.sun_path,"san_sock"); if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0) perror("bind"); return 0; } a) in the /tmp directory b) in the /usr directory c) in the present working directory d) none of the mentioned

c) in the present working directory

Output:

[root@localhost google]# ls

san.c

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

[root@localhost google]# ls

san san.c san_sock

[root@localhost google]#

 144 views

55⟩ What is the length of of the queue for pending connections in this program? #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> int main() { struct sockaddr_un add_server; int fd_server; fd_server = socket(AF_UNIX,SOCK_STREAM,0); if(fd_server == -1) perror("socket"); add_server.sun_family = AF_UNIX; strcpy(add_server.sun_path,"server_sock2"); if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0) perror("bind"); if( listen(fd_server,3) != 0) perror("listen"); return 0; } a) 0 b) 1 c) 2 d) 3

d) 3

Explanation:

The second argument of listen() specifies the length for the queue for pending connections.

 153 views

56⟩ What is the output of the program no 13? #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> int main() { struct sockaddr_un add_server, add_client; int fd_server, fd_client; int len; char ch; fd_server = socket(AF_UNIX,SOCK_STREAM,0); if(fd_server == -1) perror("socket"); add_server.sun_family = AF_UNIX; strcpy(add_server.sun_path,"san_sock"); if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0) perror("bind"); if( listen(fd_server,3) != 0) perror("listen"); len = sizeof(add_client); fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len); printf("googlen"); return 0; } a) the program will print the string "google" b) the process will remain block c) segmentation fault d) none of the mentioned

b) the process will remain block

Explanation:

There is no peding request in the queue for listening socket "san_sock".

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

^Z

[4]+ Stopped ./san

[root@localhost google]#

 155 views

57⟩ What is the output of this program? #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> int main() { int fd; fd = socket(AF_UNIX,SOCK_STREAM,0); printf("%dn",fd); return 0; } a) 0 b) 1 c) 2 d) 3

d) 3

Explanation:

The socket() returns the lowest available file descriptor and in this program i.e. 3.

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

3

[root@localhost google]#

 192 views

58⟩ What is the output of this program no 14? #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> #include<errno.h> int main() { struct sockaddr_un addr; int fd; fd = socket(AF_UNIX,SOCK_STREAM,0); if (fd == -1) perror("socket"); addr.sun_family = AF_UNIX; strcpy(addr.sun_path,"san_sock"); if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1) printf("Sanfoudnryn"); return 0; } a) this program will print the string "google" b) this program will not print the string "google" c) segmentation fault d) none of the mentioned

a) this program will print the string "google"

Explanation:

The first argument of the bind() is not a valid file descriptor in this program.

Output:

[root@localhost google]# gcc -o san san.c

[root@localhost google]# ./san

Sanfoudnry

[root@localhost google]#

 154 views

59⟩ What this program is not able to connect with any client program? #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> int main() { struct sockaddr_un add_server, add_client; int fd_server, fd_client; int len; char ch; fd_server = socket(AF_UNIX,SOCK_STREAM,0); if(fd_server == -1) perror("socket"); add_server.sun_family = AF_UNIX; strcpy(add_server.sun_path,"san_sock"); if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0) perror("bind"); len = sizeof(add_client); fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len); printf("googlen"); return 0; } a) the listen() is missing b) the connect() is missing c) the read() and write() are missing d) none of the mentioned

a) the listen() is missing

 185 views

60⟩ What is the output of this program no 15? #include<stdio.h> #include<sys/types.h> #include<sys/un.h> #include<sys/socket.h> int main() { struct sockaddr_un add_server, add_client; int fd_server, fd_client; int len; char ch; fd_server = socket(AF_UNIX,SOCK_STREAM,0); if(fd_server == -1) perror("socket"); add_server.sun_family = AF_UNIX; strcpy(add_server.sun_path,"san_sock"); if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0) perror("bind"); len = sizeof(add_client); fd_client = connect(fd_server,(struct sockaddr*)&add_client,&len); printf("googlen"); return 0; } a) this program will print the string "google" b) segmentation fault c) error d) none of the mentioned

c) error

Explanation:

The syntax of the connect() is wrong. connect() should be used in client program only.

Ouptut:

[root@localhost google]# 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 google]#

 164 views