Q1: Consider the following code snippet using the fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors. (2024 SET 1)
The total number of times the printf statement is executed is ___
(a) 8
(b) 12
(c) 14
(d) 18
Ans: (c)
Sol: The printf statement is executed 14 times. The code repeatedly forks processes inside a while loop. Each iteration doubles the number of processes. Initially, one process prints "hello". After each fork, both the parent and child processes print "hello". The loop iterates three times, resulting in 2^3 = 8 processes. Additionally, two processes from the previous iteration wait for each other before proceeding. Thus, the total count is 8 + 4 + 2 = 14 "hello" messages printed before the loop terminates.
Q2: Which one or more of the following options guarantee that a computer system will transition from user mode to kernel mode? (2023)
(a) Function Call
(b) malloc Call
(c) Page Fault
(d) System Call
Ans: (c, d)
Sol:
A. function call: functions may be user defined so It also does not guarantee to switch from user to kernel mode.
B. Malloc () call: It can be done in both user and kernel modes. Hence, It cannot guarantee switching.
C. Page fault : When a page fault occurs, required page must be loaded from secondary to main memory. It will be done in privileged mode and guarantee user mode to kernel mode switching.
D. System call : System calls are executed in kernel mode only. It definitely guarantee switching.
Hence Ans is C,D
Q3: Which of the following standard C library functions will always invoke a system call when executed from a single-threaded process in a UNIX/Linux operating system? (2021 SET 1)
[MSQ]
(a) exit
(b) malloc
(c) sleep
(d) strlen
Ans: (a, c)
Sol: System calls are used to get some service from operating system which generally requires some higher level of privilege.
This question uses two important words “always” and “standard C library functions”.
Let’s check options
Answer : A,C
Q4: The following C program is executed on a Unix / Linux system: (2019)
The total number of child process created is __________ .
(a) 31
(b) 63
(c) 5
(d) 6
Ans: (a)
Sol: Answer is 31
Fork is called whenever i is even, so we can re-write the code as
fork() will be called 5 times( i = 0, 2, 4, 6, 8, )
∴Total number of process 25= 32
Total number of child process would be 25 - 1 = 31
Q5: Identify the correct order in which a server process must invoke the function calls accept, bind, listen, and recv according to UNIx socket API. (2015 SET 2)
(a) listen, accept, bind, recv
(b) bind, listen, accept, recv
(c) bind, accept, listen, recv
(d) accept, listen, bind, recv
Ans: (b)
Sol: Answer: (B)
Bind: Binds the socket to an address
Listen: Waits for connections to the socket
Accept: Accepts a connection to the socket
Recv: Receives data from connection
From Man page of accept:
It extracts the first connection request on the queue of pending connections for the listening socket, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket is unaffected by this call.
Q6: A process executes the code (2012)
fork();
fork();
fork();
The total number of child processes created is
(a) 3
(b) 4
(c) 7
(d) 8
Ans: (c)
Sol: At each fork() the no. of processes becomes doubled. So, after 3 fork calls, the total no. of processes will be 8. Out of this 1 is the parent process and 7 are child processes. So, total number of child processes created is 7.
Q7: Consider the execution of the following commands in a shell on a Linux operating system. (2008)
bash$ cat alpha
Mathematics
bash$ In alpha beta
bash$ rm alpha
bash$ cat >> beta << SAME
Information Technology
SAME
bash$ cat beta
The output of the last command will be:
(a) Mathematics Information Technology SAME
(b) Mathematics Information Technology
(c) Information Technology
(d) Information Technology SAME
Ans: (d)
Q8: A process executes the following code (2008)
for (i = 0; i < n; i + +) fork( );
The total number of child processes created is
(a) n
(b) 2 n − 1
(c) 2 n
(d) 2 n + 1 − 1
Ans: (b)
Sol: Each fork() creates a child which start executing from that point onward. So, number of child processes created will be 2n − 1.
At each fork, the number of processes doubles like from 1 - 2 - 4 - 8... 2nOf
these except 1, all are child processes.
Q9: A client process P needs to make a TCP connection to a server process S. Consider the following situation: the server process S executes a socket(), a bind() and a listen() system call in that order, following which it is preempted. Subsequently, the client process P executes a socket() system call followed by connect() system call to connect to the server process S. The server process has not executed any accept() system call. Which one of the following events could take place? (2008)
(a) connect() system call returns successfully
(b) connect() system call blocks
(c) connect() system call returns an error
(d) connect() system call results in a core dump
Ans: (c)
Sol: First thing to note: All the sockets are by default in BLOCKING mode. What do we mean by blocking ??
Blocking mode means that when we make a system call, it blocks the caller for the time "when call() is made till the job is done OR an error returns ". We can set each socket to Non-blocking explicitly. Setting to Non-Blocking means we are telling the kernel that "If the system call cant be completed without putting process to sleep then DON'T put the process to sleep . Instead return with an ERROR immediately and continue the process" which can be checked for the completion by the caller in between the execution of other tasks.
Now coming to this question:
Suppose connect() is in default blocking mode then calling connect() sends SYN packet to the server. Since server has not executed any accept() call it can not acknowledge the SYN packet. Connect() in blocking mode keep sending SYN packets at fixed intervals(first after 6 sec, second after 24 sec typically until 75 sec latest). This is done until an error ETIMEDOUT is returned by the TCP.(in this case, else there are several other type of errors returned in case No port exists for that connection or server id not listening etc.)
Here, option (B) saying that connect() blocks is not entirely wrong but since we know that accept() call is not made by server, connect() WILL NOT WAIT FOREVER and SO IT CAN NOT BLOCK. It will ultimately return with an ERROR message.
So, option (C) is CORRECT.
Core dump thing I don't know about!
But once connect() returns error that socket can not be reused and must be CLOSED.
And a non-blocking connect() is never blocked and immediately returns with an error if connection is not successful although IT CONTINUES WITH TRYING TO CONNECT .Error here just means that it returns a message saying "I could not connect immediately BUT i am trying AND you can check it in between.
Hope it clears a bit
Q10: Which of the following system calls results in the sending of SYN packets? (2008)
(a) socket
(b) bind
(c) listen
(d) connect
Ans: (d)
Sol:
When connect( ) is called by client, following three way handshake happens to establish the connection in TCP.
Q11: The contents of the text file t1 txt containing four lines are as follows : (2007)
a1 b1
a2 b2
a3 b2
a4 b1
The contents of the text file t2 txt containing five lines are as follows :
a1 c1
a2 c2
a3 c3
a4 c3
a5 c4
Consider the following Bourne shell script :
Which one of the following strings will NOT be present in the output generated when the above script in run? (Note that the given strings may be substrings of a printed line.)
(a) "b1 c1"
(b) "b2 c3"
(c) "b1 c2"
(d) "b1 c3
Ans: (c)
Q12: A user level process in Unix traps the signal sent on a Ctrl-C input, and has a signal handling routine that saves appropriate files before terminating the process. When a Ctrl-C input is given to this process, what is the mode in which the signal handling routine executes? (2005)
(a) User mode
(b) Kernel mode
(c) Superuser mode
(d)Privileged mode
Ans: (a)
Sol:
Now see , kernel mode = Privileged mode
Hence option A is correct answer.
Q13: The shell command (2005)
find -name passwd -print
is executed in /etc directory of a computer system running Unix. Which of the following shell commands will give the same information as the above command when executed in the same directory?
(a) ls passwd
(b)cat passwd
(c) grep print passwd
(d) grep name passwd
Ans: (d)
Sol: The correct option is D grep name passwd
Grep is a unix command which can be used to find file names with a watching string.
Q14: A student wishes to create symbolic links in a computer system running Unix. Three text files named "file 1", "file 2" and "file 3" exist in her current working directory, and the student has read and write permissions for all three files. Assume that file 1 contains information about her hobbies, file 2 contains information about her friends and file 3 contains information about her courses. The student executes the following sequence of commands from her current working directory. (2005)
ln -s file 1 file 2
ln -s file 2 file 3
Which of the following types of information would be lost from her file system?
I. Hobbies
II. Friends
III. Courses
(a) I and II only
(b) II and III only
(c) II only
(d) I and III only
Ans: (b)
Sol: Correct option: B
As ln -s is a symbolic link. In this case
File 3 ⇒ File 2 ⇒ File ⇒ 1Hobbies (actual data).
So File2 and File 3contents are lost.
Q15: Consider the following code fragment: (2005)
Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE
(a) u = x + 10 and v = y
(b) u = x + 10 and v != y
(c) u + 10 = x and v = y
(d) u + 10 = x and v != y
Ans: (c)
Sol: The correct option is C u + 10 = x and v = y
Fork ( ) returns 0 is child process and process ID of child process is parent process.
In child (x), a = a + 5
In paraent (u), a = a -5
Therefore, x = u + 10
The physical addresses of parent and child processes will be different but our program access virtual addresses. The child process gets an exact copy of parent process and virtual address of ‘a’ doesn’t change in child process, Therefore v = y.
10 videos|99 docs|33 tests
|
1. What is a system call in computer science? |
2. How does a system call work in a computer system? |
3. What are some common examples of system calls in operating systems? |
4. How are system calls different from library functions in programming? |
5. Why are system calls important in computer science and operating systems? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|