1. What is the output of this program?
#include<stdio.h>
#include<string.h>
int main()
{
int fd[2];
int count;
char buffer[6];
if( pipe(fd) != 0)
perror("pipe");
memset(buffer,'\0',6);
count=write(fd[1],"Linux",6);
read(fd[0],buffer,6);
printf("%s\n",buffer);
return 0;put
}
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
Explanation: The data in buffer is written by the pipe.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Linux
[root@localhost sanfoundry]#
2. What is the output of this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd[2];
int child;
char buff[6];
if(pipe(fd) != 0)
perror("pipe");
child=fork();
switch(child){
case -1 :
perror("fork");
exit(1);
case 0 :
if (write(fd[1],"Linux",6) != 6)
perror("write");
break;
default :
read(fd[0],buff,6);
printf("%s\n",buff);
break;
}
return 0;
}
a) this program will print the string “Linux”
b) this program will print nothing
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: One process can read and write to another process by pipe.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Linux
[root@localhost sanfoundry]#
3. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd;
char buff[512];
if( mkfifo("/tmp/test_fifo",0666) == -1)
perror("mkfifo");
fd = open("/tmp/test_fifo",O_RDONLY);
read(fd,buff,512);
printf("%s\n",buff);
return 0;
}
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
Explanation: In this program the fifo is opened in read only mode. Hence the process will remain block on read.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
^Z
[12]+ Stopped ./san
[root@localhost sanfoundry]#
4. This program will print the _____ string.
#include<stdio.h>
int main()
{
int fd[2];
char buff[11];
if (pipe(fd) != 0)
perror("pipe");
write(fd[1],"Sanfoundry",11);
lseek(fd[0],0,3);
read(fd[0],buff,11);
printf("%s\n",buff);
return 0;
}
a) “Sanfoundry”
b) “San”
c) “foundry”
d) none of the mentioned
View Answer
Explanation: The “lseek” system call does not work with pipes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoundry
[root@localhost sanfoundry]#
5. What is the output of this program?
#include<stdio.h>
int main()
{
if (mkfifo("/tmp/test_fifo",0666) != 0)
perror("mkfifo");
if (mkfifo("/tmp/test_fifo",0666) != 0)
perror("mkfifo");
return 0;
}
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
Explanation: 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]#
6. What is the output of this program?
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fd, count;
char buff[10];
if (mkfifo("/tmp/test_fifo",0666) != 0)
perror("mkfifo");
fd = open("/tmp/test_fifo",O_RDONLY|O_NONBLOCK);
count = read(fd,buff,10);
printf("%d\n",count);
return 0;
}
a) 0
b) -1
c) 10
d) none of the mentioned
View Answer
Explanation: The read system call will return 0 bytes.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
0
[root@localhost sanfoundry]#
7. This program will print the value
#include<stdio.h>
#include<fcntl.h>
int main()
{
int rfd, wfd, count;
char buff[11];
if (mkfifo("/tmp/test_fifo",0666) != 0)
perror("mkfifo");
wfd = open("/tmp/test_fifo",O_WRONLY|O_NONBLOCK);
count = write(wfd,"Sanfoundry",11);
printf("%d\n",count);
rfd = open("/tmp/test_fifo",O_RDONLY|O_NONBLOCK);
count = read(rfd,buff,11);
return 0;
}
a) 0
b) -1
c) 11
d) none of the mentioned
View Answer
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:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
-1
[root@localhost sanfoundry]#
8. What is the output of this program?
#include<stdio.h>
int main()
{
int fd[3],count;
if (pipe(fd) != 0)
perror("pipe");
count = write(fd[2],"Hello",6);
printf("%d\n",count);
return 0;
}
a) 6
b) 0
c) -1
d) segmentation fault
View Answer
Explanation: Data can be written only in fd[1].
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
-1
[root@localhost sanfoundry]#
9. In this program the fifo “my_fifo”
#include<stdio.h>
int main()
{
if (mkfifo("my_fifo",0666) != 0)
perror("mkfifo");
return 0;
}
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
Explanation: None.
Output:
[root@localhost sanfoundry]# ls
san san.c
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost 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
[root@localhost sanfoundry]#
10. What is the output of this when the pipe is successfully created?
#include<stdio.h>
int main()
{
int ret_val;
int fd[2];
ret_val = pipe(fd);
printf("%d\n",ret_val);
return 0;
}
a) 0
b) -1
c) 1
d) none of the mentioned
View Answer
Explanation: 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]#
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.
- Buy Linux Books
- Apply for Linux Internship
- Buy Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship