0% found this document useful (0 votes)
39 views

Os Lab Pgm1viva Qns

The document discusses system calls related to process control and file management in operating systems. It describes common system calls like fork(), exec(), wait(), open(), read(), write(), close() and getpid() and provides examples of using them in C programs to manage processes and files.

Uploaded by

geetha megharaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Os Lab Pgm1viva Qns

The document discusses system calls related to process control and file management in operating systems. It describes common system calls like fork(), exec(), wait(), open(), read(), write(), close() and getpid() and provides examples of using them in C programs to manage processes and files.

Uploaded by

geetha megharaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OPERATING SYSTEMS - BCS303 III SEM B.

System calls

 System calls provide an interface between user-level applications and the underlying
hardware through operating system.

 System calls allow user programs to request services from the operating system's kernel.

 reading from or writing to files, creating new processes, managing memory etc.

 System calls are accessed through library functions provided by the operating system.

 Allow programmers to invoke system calls without need to write assembly language code.

Process control

fork(): Creates a new process by duplicating the current process. The parent process and the child
process can run concurrently.

• Parent process creates child process


• pid – Process ID
 Parent gets pid of the created child
 Child gets 0
 If -1: error in process creation

Wait() -
 blocks the calling process until one of its child processes exits or a signal is
received
 After child process terminates, parent continues its execution after wait system call

1
OPERATING SYSTEMS - BCS303 III SEM B.E

Exit() function is used to terminate the program and return an exit status to the parent process.

The argument passed to exit() represents the exit status.

an exit status of 0 indicates success, and any non-zero value indicates an error or some specific
condition.

Ex : exit(100); means the program is exiting with an exit status of 100.

wait() or waitpid(): These system calls are used to wait for child processes to complete. They allow
a parent process to synchronize its execution with the termination of its child processes and obtain
their exit statuses.

The syntax for waitpid() is as follows:

#include <sys/types.h>
#include <sys/wait.h>

waitpid(pid_t pid, int *status, int options);

pid: The process ID of the child process you want to wait for. Use -1 to wait for
any child process.
status: A pointer to an integer where the exit status of the terminated process
is stored.
options: Additional options (use 0 for default behavior).

WIFEXITED(status) : used to determine if a child process terminated normally

WIFEXITED(status) returns true (non-zero) if the child process terminated normally.

2
OPERATING SYSTEMS - BCS303 III SEM B.E

exec() - replaces the program executed by a process

o Replaces the current process with a new process. There are several variants of the
exec() system call, such as execl(), execv(), etc., which allow different ways of
specifying the command to be executed.

Ex: execlp
Replaces the calling process with a new process. This has the effect of running a new
program with the process ID of the calling process.

#include <unistd.h>

int execlp(const char *path, const char *arg0, ..., NULL);

path : the location of the program you want to run in a file.

arg0, ...: A list of optional arguments for the program, with a null pointer as the last
argument to end the list.

 execlp("/bin/ls", "ls", "-l", NULL);

The output shows the details of files and directories in the current directory

Program 1
Develop a c program to implement the Process system calls (fork (), exec(), wait(), create process,
terminate process)

Steps :
Create a child process
• Run separate codes in parent and child processes

• Child process
• Print its and its parent’s pid
• Run some commands
• Exit

• Parent process
• Wait till child is alive
• Exit

3
OPERATING SYSTEMS - BCS303 III SEM B.E

a) Single Process
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
pid_t child_pid;
int status;

// Create a child process

child_pid = fork();
if (child_pid < 0)
{
// Fork failed
perror("Fork failed");
exit(1);
}
else
if (child_pid == 0)
// Child process
printf("Child process (PID: %d) is running.\n", getpid());
// Replace the child process with a new program
execlp("/bin/ls", "ls", "-l", NULL);
// If exec fails, the code below will be executed
perror("Exec failed");
exit(1);
}
else
{
// Parent process
printf("Parent process (PID: %d) created a child (PID: %d).\n", getpid(), child_pid);

// Wait for the child process to finish

wait(&status);
if (WIFEXITED(status))
{
printf("Child process (PID: %d) exited with status %d.\n", child_pid,
WEXITSTATUS(status));
}
else
{
printf("Child process (PID: %d) did not exit normally.\n", child_pid);
}
4
OPERATING SYSTEMS - BCS303 III SEM B.E

}
return 0;
}

OUTPUT
Child process (PID: 48492) is running.

total 44
-rw-r--r-- 1 compiler compiler 2 Nov 9 06:55 Employee.txt
-rw-r--r-- 1 compiler compiler 16 Nov 9 08:20 INPUT.txt
-rw-r--r-- 1 compiler compiler 0 Nov 9 06:28 Students.dat
-rw-r--r-- 1 compiler compiler 11 Nov 9 07:52 archivo.txt
-rw-r--r-- 1 compiler compiler 84 Nov 9 06:55 car_data.txt
-rw-r--r-- 1 compiler compiler 35 Nov 9 07:12 example.txt
-rw-r--r-- 1 compiler compiler 35 Nov 9 07:11 o.out
-rw-r--r-- 1 compiler compiler 36 Nov 9 06:49 result.dat
-rw-r--r-- 1 compiler compiler 2709 Nov 9 08:53 sorting_comparison.csv
-rw-r--r-- 1 compiler compiler 96 Nov 9 06:36 student.dat
-rw-r--r-- 1 compiler compiler 21 Nov 9 06:30 swap.txt
-rw-r--r-- 1 compiler compiler 2003 Nov 9 09:09 zapis.txt

b) Multiple processes

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
int num_processes = 5; // Number of child processes to create
pid_t child_pid;

printf("Parent process (PID: %d)\n", getpid());

for (int i = 1; i <= num_processes; ++i)


{
child_pid = fork();
if (child_pid == -1)
{
perror("Fork failed");
exit(EXIT_FAILURE);
5
OPERATING SYSTEMS - BCS303 III SEM B.E

}
if (child_pid == 0)
{
// Code executed by child processes
printf("Child process %d (PID: %d, Parent PID: %d)\n", i, getpid(), getppid());

// Execute different commands in each child process


if (i == 1)
{
execl("/bin/ls", "ls", "-l", NULL);
}
else if (i == 2)
{
execl("/bin/pwd", "pwd", NULL);
}
else if (i == 3)
{
execl("/bin/ps", "ps", NULL);
}
else if (i == 4)
{
execl("/bin/date", "date", NULL);
}
else if (i == 5)
{
execl("/bin/echo", "echo", "Hello from child process", NULL);
}
else
{
printf("Invalid child process number\n");
}

// If execl fails
perror("Exec failed");
exit(EXIT_FAILURE);
}
}
// Parent process waits for all child processes to complete
for (int i = 1; i <= num_processes; ++i) {
wait(NULL);
}
printf("Parent process exiting\n");
return 0;
}

OUTPUT
6
OPERATING SYSTEMS - BCS303 III SEM B.E

Parent process (PID: 46100)


Child process 1 (PID: 46101, Parent PID: 46100)
Child process 2 (PID: 46102, Parent PID: 46100)
Child process 3 (PID: 46103, Parent PID: 46100)
Child process 4 (PID: 46104, Parent PID: 46100)
Child process 5 (PID: 46105, Parent PID: 46100)

Hello from child process

/home/compiler

Thu Nov 9 09:39:06 UTC 2023

total 4
-rw-r--r-- 1 compiler compiler 6 Nov 9 08:17 'BHAVAN KALYAN'
-rw-r--r-- 1 compiler compiler 0 Nov 9 09:34 output.txt
-rw-r--r-- 1 compiler compiler 0 Nov 9 06:09 sample.txt

PID TTY TIME CMD


46093 pts/169 00:00:00 dash
46100 pts/169 00:00:00 kPVgeBKZ5k.o
46103 pts/169 00:00:00 ps
Parent process exiting

7
Operating System Lab Programs (BCS303)

open(): Opens a file and returns a file descriptor that can be used for subsequent read or write
operations.

read() and write(): Reads data from a file descriptor (such as a file or a socket) or writes data to a
file descriptor, respectively.

close(): Closes a file descriptor, releasing the associated resources.

exit(): Terminates the calling process and returns an exit status to the parent process.

getpid(): Returns the process ID of the calling process.

kill(): Sends a signal to a process or a group of processes, allowing one process to communicate
with another.

malloc(), free(), brk(), sbrk(): Memory management system calls used for dynamic memory
allocation and deallocation.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

1. What is an operating system?


The operating system is a software program that facilitates computer hardware to
communicate and operate with the computer software. An operating system acts as a
GUI between the User and the computer System.

2. What is the main purpose of an operating system?


An operating system acts as an intermediary between the user and computer hardware.
The purpose of an operating system is to provide an environment to execute user
programs conveniently and efficiently.
An operating system is a software that manages computer hardware. The hardware must
provide appropriate mechanisms to ensure the correct operation of the computer system

3. What are the different operating systems?


Batched operating systems
Distributed operating systems
Timesharing operating systems
Multi-programmed operating systems
Real-time operating systems
4. what are the components of computer system?
Computer system mainly consists of four components-

Hardware – provides basic computing resources CPU, memory, I/O devices


Operating system - Controls and coordinates use of hardware among various
applications and users
Application programs – define the ways in which the system resources are used to
solve the computing problems of the users, Word processors, compilers, web
browsers, database systems, video games
Users - People, machines, other computers

5. What is multiprocessor system? What are advantages of Multiprocessor systems


Systems that have two or more processors in close communication, sharing the
computer bus, the clock, memory, and peripheral devices are the multiprocessor
systems.
Advantages:
a. Increased throughput
b. Economy of scale
c. Increased reliability

6. What are different types of multiprocessor systems


Asymmetric multiprocessing – (Master/Slave architecture)
Symmetric multiprocessing (SMP)

7. What is Clustered Systems


Clustered systems are two or more individual systems connected together via network
and sharing software resources.
There are two types of Clustered systems – asymmetric and symmetric

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

8. What is Distributed systems?


Individual systems that are connected and share the resource available in network is
called Distributed system. Access to a shared resource increases computation speed,
functionality, data availability, and reliability.

9. What is a real-time system?


Real-time system is used when rigid-time requirements have been placed on the operation
of a processor. It contains a well-defined and fixed time constraints.

10. What are the Dual-Mode Operation of computer system


The system works in two separate modes of operation:
User mode
Kernel mode (supervisor mode, system mode, or privileged mode).

11. What is kernel?


Kernel is the core and most important part of a computer operating system which
provides basic services for all parts of the OS.

12. What is monolithic kernel?


A monolithic kernel is a kernel which includes all operating system code is in single
executable image.

13. What do you mean by a process?


An executing program is known as process. There are two types of processes:
Operating System Processes
User Processes

14. What is the difference between process and program?


A program while running or executing is known as a process. Program is a passive entity
whereas process is active entity

15. What is a Thread?


A thread is a basic unit of CPU utilization. It consists of a thread ID, program counter,
register set and a stack. Thread is also called lightweight process. Threads improve the
application performance through parallelism. For example, in a browser, multiple tabs
can be different threads. MS Word uses multiple threads, one thread to format the text,
another thread to process inputs, etc.

16. What are the differences between process and thread?


A thread has its own program counter (PC), a register set, and a stack space. Threads
are not independent of one another, like processes. Threads share their code section,
data section, and OS resources like open files and signals with other threads.

17. What is Context Switching?


The task of switching a CPU from one process to another process is called context
switching.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

18. What is PCB?


process control block (PCB) contains information about the process, i.e. registers,
priority, program counter etc. The process table is an array of PCBs, that means
logically contains a PCB for all of the current processes in the system.

19. What is a dispatcher?


Dispatcher gives control of the CPU to the process selected by the short-term scheduler.
i. Its function involves:
Switching context
Switching to user mode &
Jumping to the proper location in the user program to restart that program
20. Define the term dispatch latency?
Dispatch latency is the amount of time taken by the dispatcher to
stop one process and
start another running.
21. What are the goals of CPU scheduling?
Maximize CPU utilization [Keep CPU as busy as possible].
Maximize throughput [Number of processes executed per time unit]
Minimize turnaround time [Time taken by a process to finish execution]
Minimize waiting time [Time a process waits in ready queue]
Minimize response time [Time when a process produces the first response]

22. What is FCFS scheduling?


In First Come, First Served is CPU scheduling algorithm the process that requests the
CPU first is allocated with the CPU first. Its implementation is managed by a FIFO
queue.

23. What are the different scheduling algorithms?


First-Come, First-Served (FCFS) Scheduling.
Shortest-Job-Next (SJN) Scheduling.
Priority Scheduling.
Shortest Remaining Time.
Round Robin(RR) Scheduling.
Multiple-Level Queues Scheduling.

24. Describe the objective of multi-programming.


Multi-programming increases CPU utilization by organizing jobs (code and data) so
that the CPU always has one to execute. The main objective of multi-programming is to
keep multiple jobs in the main memory. If one job gets occupied with IO, the CPU can
be assigned to other jobs.
25. What is the time-sharing system?
Time-sharing is a logical extension of multiprogramming. The CPU performs many
tasks by switches that are so frequent that the user can interact with each program while
it is running. A time-shared operating system allows multiple users to share computers
simultaneously.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

26. what is a Thread?


A thread is also known as a lightweight process. The idea is to achieve parallelism by
dividing a process into multiple threads. Threads within the same process run in shared
memory space,
27. Briefly explain FCFS.
FCFS stands for First Come First served. In the FCFS scheduling algorithm, the job that
arrived first in the ready queue is allocated to the CPU. FCFS is a non-preemptive
scheduling algorithm. A process holds the CPU until it either terminates or performs
I/O. Thus, if a longer job has been assigned to the CPU then many shorter jobs after it
will have to wait.
28. What is the RR scheduling algorithm?
A round-robin scheduling algorithm is used to schedule the process fairly for equal time
quantum.
Round-robin is cyclic in nature, so starvation doesn’t occur
Round-robin is a variant of first-come, first-serve scheduling
No priority or special importance is given to any process or task
RR scheduling is also known as Time slicing scheduling

29. Write a name of classic synchronization problems?


Bounded-buffer
Readers-writers
Dining philosophers
Sleeping barber

30. What is a bootstrap program in the OS?


When a system is switched on, ‘Bootstrap’ program is executed. It is the initial
program to run in the system. This program is stored in read-only memory
(ROM). It initializes the CPU registers, memory, device controllers and other
initial setups. The program also locates and loads, the OS kernel to the memory.
Then the OS starts with the first process to be executed (ie. ‘init’ process) and
then wait for the interrupt from the user.

31. What is aging in Operating System?


Aging is a technique used to avoid the starvation in resource scheduling system.

32. What is IPC?


Inter-process communication (IPC) is a mechanism that allows processes to
communicate with each other and synchronize their actions. The communication
between these processes can be seen as a method of cooperation between them.

33. what is convoy effect?


Convoy effect: All other processes wait for one big process to get off the CPU.

34. what is the drawback of priority scheduling ?

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

The drawback is Indefinite blocking, where low-priority processes are left waiting
indefinitely for CPU

35. What is deadlock?


A waiting process in the system is never again able to change state, because the
resources it has requested are held by other waiting processes. This situation is called a
Deadlock.

36. What are the four necessary and sufficient conditions behind the deadlock?
These are the 4 conditions:
1) Mutual Exclusion Condition
2) Hold and Wait Condition
3) No-Preemptive Condition
4) Circular Wait Condition

37. What is Banker's algorithm?


Banker's algorithm is used to avoid deadlock situation in system.

38. Write different types of address binding?


Address binding of instructions to memory-addresses can happen at 3 different stages.
Compile-time Address Binding
Load time Address Binding
Execution time Address Binding

39. What is the use of paging in operating system?


Paging is a memory-management scheme. This allows the physical-address space of a
process to be non-contiguous. Paging is used to solve the external fragmentation problem
in operating system. This technique ensures that the data needed is available as quickly as
possible.

40. What is the concept of demand paging?


Demand paging mechanism of swapping in only the necessary pages in to memory.
Thus it avoids reading unused pages and decreases the swap time and amount of
physical memory needed.

41. What is virtual memory?


Virtual memory is a technique that allows for the execution of partially loaded process.

42. What is thrashing?


Thrashing is a phenomenon in virtual memory scheme when the processor spends most
of its time in swapping pages, rather than executing instructions. This high paging
activity is called thrashing. The phenomenon of excessively moving pages back and forth
between main memory and secondary memory is called thrashing.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

43. How many types of fragmentation occur in Operating System?


There are two types of fragmentation:
Internal fragmentation: It is occurred when we deal with the systems that have fixed size
allocation units.
External fragmentation: It is occurred when we deal with systems that have variable-size
allocation units.

44. What is semaphore?


Semaphore is a protected variable or abstract data type that is used to lock the resource
being used. The value of the semaphore indicates the status of a shared resource.
There are two types of semaphore:
Binary semaphores
Counting semaphores

45. What is a binary Semaphore?


Binary semaphore takes only 0 and 1 as value and used to implement mutual exclusion
and synchronize concurrent processes.

46. What are the attributes of a file


Name: The symbolic file name in human readable form.
Identifier: This unique tag, usually a number, identifies the file within the file
system
Type: indicates type of the files.
Location: Is a pointer to a device and to the location of the file on that device.
Size: The current size of the file (in bytes, words, or blocks)
Protection: Access-control information
Time, date, and user identification: creation, last modification, and last use.

47. What is a File allocation table?


A section of disk at the beginning of each volume is set aside to contain the table. The
table has one entry for each disk block and is indexed by block number

48. What is seek time?


Seek Time: Seek time is the time taken to locate the disk arm to a specified track where
the data is to be read or written

49. When does trashing occur?


Thrashing specifies an instance of high paging activity. This happens when the system is
spending more time paging instead of executing.

50. What are the various functions of File Management System?


The various functions present in File Management System are the functions to:
Create
Open
Read

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

Close
Delete

51. What is Process Control Block (PCB)?


A data structure used to store the state of the process is called a Process Control Block

52. What are the Data Items in Process Control Block?


The Data Items in Process Control Block are:
Process State
Process Number
Program Counter
Registers
Memory Limits
List of Open Files

53. What is a Peterson's Solution?


Peterson's solution to the critical section problem. The solution makes sure that no two
processes access shared resource at the same time.

54. What are the Operations in Semaphores?


The Operations in Semaphores are:
Wait or P Function ()
Signal or V Function ()

55. What is a Critical Section Problem?


The section of a program that attempts to access shared resources is called as the Critical
Section.
The operating system prevent processes from entering the critical section because more
than one process cannot operate shared resources at once and has to authorize processes
to access shared resources.

56. What are the methods of Handling Deadlocks?


The methods of handling deadlock are:
Deadlock Prevention
Deadlock Detection and Recovery
Deadlock Avoidance
Deadlock Ignorance

57. How can we avoid Deadlock?


We can avoid Deadlock by using Banker's Algorithm.

58. How can Deadlock situation can be recovered in Operating System?


Recovery: Allow the process to enter the deadlock state.
deadlock is recovered by terminating or aborting all deadlocked processes one at a time.
Process Pre-Emption is another technique used for Deadlocked Recovery.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF


Operating System Lab Programs (BCS303)

59. What is Address Translation in Paging?


Logical address is the address that the CPU creates for each page referred in the
secondary memory, the physical address is the actual location of the frame where page is
residing. Mechanism of translating logical address to physical address is called address
translation.

60. What are Page Replacement Algorithms in Operating Systems?


The Page Replacement Algorithms in Operating Systems are:
First In First Out
Optimal
Least Recently Used
Most Recently Used

61. In which Page Replacement Algorithm does Belady's Anomaly occur?


In First in First out Page Replacement Algorithm Belady's Anomaly occurs.

62. What are the Disk Scheduling Algorithms used in Operating Systems?
The Disk Scheduling Algorithms used in Operating Systems are:
First Come First Serve
Shortest Seek Time First
LOOK
SCAN
C SCAN
C LOOK

63. What is Monitors in the context of Operating Systems?


A monitor is a synchronization tool. It helps in controlled access to shared data.

Dr Geetha C Megharaj, Dept. of CSE(AI&ML), Dr TTIT, KGF

You might also like