0% found this document useful (0 votes)
14 views4 pages

RDoc 38863

The document is a test solution for a Linux Programming course at ICFAI Foundation for Higher Education, covering key concepts such as system calls, process management, and signal handling. It includes questions and answers on topics like the open system call, differences between fork() and exec(), and the life cycle of signals. Additionally, it provides programming examples demonstrating the use of fcntl() and handling signals with alarm().

Uploaded by

Krishna singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

RDoc 38863

The document is a test solution for a Linux Programming course at ICFAI Foundation for Higher Education, covering key concepts such as system calls, process management, and signal handling. It includes questions and answers on topics like the open system call, differences between fork() and exec(), and the life cycle of signals. Additionally, it provides programming examples demonstrating the use of fcntl() and handling signals with alarm().

Uploaded by

Krishna singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The ICFAI Foundation for Higher Education, Hyderabad

IcfaiTech (Faculty of Science and Technology)


First Semester, A.Y., 2024 - 2025
Test-II Solution and Key
Course Code: BCA 123 Course Title: LINUX PROGRAMMING

Date: 06/11/2024 Maximum Marks: 15 Marks

Note: Answer ALL questions


Part-A
Answer the following [6*0.5=3 Marks]
1.
a. What is a system call? [CO1, PO1]
These are the special functions to access the services in the operating system.
b. Write the syntax of open system and explain its arguments with an example. [CO1, PO3]
int open(const char *path, int oflag, ..)
path: It is the path/filename to open
oflag: : Specifies the mode in which the file should be opened. (read/write/read-write)
O_RDONLY: Open for reading only.
O_WRONLY: Open for writing only.
O_RDWR: Open for both reading and writing.
c. What is the difference between dup() and dup2() system calls? [CO1, PO1]
The dup() system call duplicates a file descriptor, making a copy of it whereas, dup2() system
call duplicates a file descriptor but allows you to specify the new file descriptor number. If the
new file descriptor already exists, it is closed before being reused.
d. What is zombie process? How we can eliminate formation of zombie processes?
[CO2, PO1]
A zombie process is a process whose execution is completed but it still has an entry in the
process table. It can be eliminated from the process table using wait system call.
e. How the priority of a process can be altered? Give an example. [CO2, PO2]
It allows a user to specify a "niceness" value that affects the process's scheduling priority.
int nice(int increment);
Increment: An integer value that indicates how much to increase the niceness of the process.
f. What is a signal? What is the difference between maskable and non-maskable signals?
[CO2, PO2]
Signals are the interrupts that are sent to the program to specify that an important event has
occurred.
Maskable Signals are the signals that the user can enable, disable, change or ignore, whereas
Non-Maskable Signals are the signals that the users cannot change or ignore.
Part-B
Answer the following [3*2=6 Marks]

1. What is purpose of stat() system call? How can we check whether the file is regular file
or directory using stat()? [2 M] [CO2, PO1]
stat() system call retrieves information about a file, such as its size, permissions, and
timestamps. It fills a structure with file details [0.5M].
To check whether a file a regular file or directory st_mode member of the stat structure can
be used. The example is shown below; [0.5M]
if(S_ISREG(st.st_mode)) [1M]
{
printf("\n File is a regular file\n");
}
else if(S_ISDIR(st.st_mode))
{
printf("\n File is a directory\n");
}

2. Differentiate between fork() and exec() system calls? [2 M] [CO1, PO1]


SNo fork() exec()
It is a system call in the C
1. It is a system call of operating system
programming language
It does not creates new process, but it will
2. It is used to create a new process
replace the image of the current process
exec() returns nothing to the calling
3. Its return value is an integer type function if successful. It returns -1 if
unsuccessful
It takes parameters for filename/path,
4. It does not takes any parameters.
process, and environment
In exec() the machine code, data, heap,
It creates a new process that runs
5. and stack of the process are replaced by
independently with separate resources
the new program.
Any four points 4 x 0.5 = 2M

3. Explain the life cycle of signal with neat sketch. [2 M] [CO2, PO1]
A signal goes through three stages: [1M]
Generation: A signal can be generated by the kernel or any of the processes.
Delivery: A signal is said to be pending until it's delivered. Normally a signal is delivered to a
process as soon as possible by the kernel.
Processing: Every signal has an associated default action: ignore the signal; or terminate the
process, sometimes with a core dump; or stop/continue the process. For non-default
behaviour, handler function can be called.
Diagram [1M]
Part-C
Answer the following [2*3=6 Marks]

1. What different operations can be done by fcntl() system call ? Write a program to demonstrate
duplicating file descriptor using fcntl() system call. [CO1, PO8]
The fcntl() function can be used to change the properties of a file that is already open.[0.5M]
The five purposes; [0.5M]
duplicating an existing descriptor
getting or setting file descriptor flags
getting or setting file status flags
getting or setting I/O ownership
getting or setting record locks.
Program: [Total 2M]
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("example.txt", O_RDWR); [0.5M]
if (fd == -1) {
perror("Error opening file");
return 1;
}
// Duplicate file descriptor using fcntl
int newfd = fcntl(fd, F_DUPFD, 0); [1M]
if (newfd == -1) {
perror("Error duplicating file descriptor");
close(fd);
return 1;
}
printf("File descriptor %d duplicated to %d.\n", fd, newfd);
close(fd); [0.5M]
close(newfd);
return 0;
}

2. Explain the process of handling signals. Write a program to demonstrate working of


alarm() signal with appropriate signal handler. [CO2, PO4]
Handling signals [1M]
 Signals are handled by a program/method/routine called signal handler
 A signal handler may be a default signal handler or user defined signal handlers.
 Default signal handler will have a default actions like ignore, terminate, stop and
continue.
 A process can replace the default signal handler for almost all signals by its user’s own
handler function.
 A signal handler function can have any name, but must have return type void and have
one int parameter.
Program for handling alarm signal: [2M]
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void handle_alarm(int sig) [1M]
{
printf("Alarm signal received!\n");
}
int main() { [1M]
signal(SIGALRM, handle_alarm); // Set up signal handler for SIGALRM
alarm(5); // Set alarm for 5 seconds

printf("Waiting for alarm...\n");


pause(); // Wait for signal
return 0;
}

********ALL THE BEST ********

You might also like