0% found this document useful (0 votes)
3 views6 pages

Week 2 Operating System.: 1) What Is A System Call? Explain Any 3 System Calls With Appropriate Example Program

The document explains system calls, which are requests made by programs to the operating system for services. It details three specific system calls: fork() for creating processes, exec() for replacing a process, and wait() for synchronizing parent and child processes. Additionally, it discusses the advantages of multiprocessor systems and time-sharing systems, highlighting increased throughput, improved performance, and cost-effectiveness.

Uploaded by

deepak kumbhar
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)
3 views6 pages

Week 2 Operating System.: 1) What Is A System Call? Explain Any 3 System Calls With Appropriate Example Program

The document explains system calls, which are requests made by programs to the operating system for services. It details three specific system calls: fork() for creating processes, exec() for replacing a process, and wait() for synchronizing parent and child processes. Additionally, it discusses the advantages of multiprocessor systems and time-sharing systems, highlighting increased throughput, improved performance, and cost-effectiveness.

Uploaded by

deepak kumbhar
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/ 6

Week 2 – Operating System.

1) What is a system call? Explain any 3 system calls with appropriate example program.

A system call is a programmatic way in which a computer program requests a service from the kernel of
the operating system. It acts as a bridge between the application and the operating system. System calls
provide essential functionality for processes, such as reading from and writing to files, creating new
processes, and communicating with hardware devices.

Here are three commonly used system calls with appropriate example programs:

a) fork()

The fork() system call is used to create a new process by duplicating the existing process. The new
process is called the child process, and the existing process is called the parent process.

Example Program:
#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid;

pid = fork();

if (pid == 0) {

// Child process
printf("Child process: Hello, World! (PID: %d)\n", getpid());

} else if (pid > 0) {

// Parent process

printf("Parent process: Child PID is %d\n", pid);

} else {
// Fork failed

perror("fork");

return 1;

return 0;

b) exec()

The exec() system calls are used to replace the current process with a new process. There are several
variants of the exec() system call, such as execl(), execp(), execv(), etc.

Example Program using execl():

#include <stdio.h>

#include <unistd.h>

int main() {

printf("Executing ls command:\n");
execl("/bin/ls", "ls", "-l", NULL);

perror("execl"); // If exec fails

return 1;

c) wait()

The wait() system call is used to make the parent process wait until the child process has completed its
execution.

Example Program:

#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {
pid_t pid;

pid = fork();

if (pid == 0) {

// Child process

printf("Child process: Hello, World! (PID: %d)\n", getpid());

} else if (pid > 0) {

// Parent process
printf("Parent process: Waiting for child to complete...\n");

wait(NULL);

printf("Parent process: Child has completed.\n");

} else {

// Fork failed

perror("fork");

return 1;

return 0;

2) Write a simple program for Process creation with sample output.

Here's a simple C program to demonstrate process creation:

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid;

pid = fork();
if (pid == 0) {

// Child process

printf("Child process: Hello, World! (PID: %d)\n", getpid());

} else if (pid > 0) {

// Parent process

printf("Parent process: Child PID is %d\n", pid);

} else {

// Fork failed

perror("fork");
return 1;

return 0;

Sample Output:

arduino

Parent process: Child PID is 12345

Child process: Hello, World! (PID: 12345)

3) Differentiate between wait() and exit() system calls.

wait()

wait() is used by the parent process to wait for the child process to complete its execution.

It suspends the execution of the calling process until a child has terminated.

It can also retrieve the exit status of the terminated child process.

Example:

pid_t pid = fork();

if (pid == 0) {

// Child process

exit(0);

} else if (pid > 0) {


// Parent process

wait(NULL);

exit()

exit() is used to terminate the calling process.

It immediately ends the process and returns an exit status to the operating system.

Example:

if (some_condition) {

printf("Exiting the program...\n");


exit(0);

4) What are the advantages of a multiprocessor system? Explain about time-sharing system.

Advantages of a Multiprocessor System:

Increased Throughput: Multiple processors can execute multiple tasks simultaneously, leading to
increased system throughput.

Improved Performance: It allows parallel processing, which can significantly improve the performance of
applications that are designed to be parallelized.

Fault Tolerance: In case one processor fails, the remaining processors can continue to execute tasks,
ensuring system availability.

Scalability: Multiprocessor systems are scalable, as more processors can be added to increase the
system's processing capacity.

Time-Sharing System:

A time-sharing system allows multiple users to interact with a computer system simultaneously by
dividing the CPU time into multiple time slots, and each user gets a small portion of CPU time. It uses the
concept of multitasking to switch between different tasks so quickly that the users can interact with the
system almost simultaneously.

Advantages of Time-Sharing System:

High Utilization: It maximizes CPU and resource utilization by allowing multiple users to share the CPU
time.
Interactive Computing: It provides a responsive computing environment where multiple users can
interact with the system in real-time.

Cost-Effective: It allows a large number of users to use the system with fewer resources, making it a
cost-effective solution for shared computing environments.

In a time-sharing system, the operating system uses scheduling algorithms like Round Robin, Priority
Scheduling, etc., to allocate CPU time to different tasks or processes efficiently.

You might also like