Os Lab Pgm1viva Qns
Os Lab Pgm1viva Qns
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.
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.
an exit status of 0 indicates success, and any non-zero value indicates an error or some specific
condition.
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.
#include <sys/types.h>
#include <sys/wait.h>
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).
2
OPERATING SYSTEMS - BCS303 III SEM B.E
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>
arg0, ...: A list of optional arguments for the program, with a null pointer as the last
argument to end the list.
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;
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(&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;
}
if (child_pid == 0)
{
// Code executed by child processes
printf("Child process %d (PID: %d, Parent PID: %d)\n", i, getpid(), getppid());
// 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
/home/compiler
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
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.
exit(): Terminates the calling process and returns an exit status to the parent 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.
The drawback is Indefinite blocking, where low-priority processes are left waiting
indefinitely for CPU
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
Close
Delete
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