0% found this document useful (0 votes)
7 views9 pages

Os 4 - Merged

The document outlines experiments for an Operating System Lab focusing on UNIX system calls related to process management, file management, and input/output operations. It includes example programs demonstrating the use of system calls like fork(), exec(), open(), read(), and write(). Additionally, it presents a C program for simulating the First Come First Serve (FCFS) CPU scheduling algorithm, detailing the calculation of waiting and turnaround times.

Uploaded by

AKSHAT GUPTA
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)
7 views9 pages

Os 4 - Merged

The document outlines experiments for an Operating System Lab focusing on UNIX system calls related to process management, file management, and input/output operations. It includes example programs demonstrating the use of system calls like fork(), exec(), open(), read(), and write(). Additionally, it presents a C program for simulating the First Come First Serve (FCFS) CPU scheduling algorithm, detailing the calculation of waiting and turnaround times.

Uploaded by

AKSHAT GUPTA
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/ 9

AKSHAT GUPTA

2300321540020
AKSHAT GUPTA
2300321540020
AKSHAT GUPTA
2300321540020
ABES ENGINEERING COLLEGE [Department of CSE-Data Science]
Operating System Lab [BCS-451]
2nd Year||4th Semester||Section: A||Group:A1

Experiment – 2

Objective: Execute various UNIX system calls for


i. Process management ii. File management iii. Input/output Systems calls

1. Introduction to System Calls


A system call is a programmatic way in which a computer program requests a service from the kernel of the
operating system on which it is executed. It provides an interface between a process and the operating system,
allowing user-level processes to request services such as file operations, process management, and
communication. i. Process Management
Process management system calls in UNIX are used to create, execute, and terminate processes. These calls
allow a process to create child processes, execute programs, and manage process attributes.

Common System Calls:

1. fork() – Creates a new child process.


2. exec() – Replaces the current process memory with a new program.
3. wait() – Waits for a child process to terminate.
4. exit() – Terminates the process.
5. getpid() – Gets the process ID.
6. getppid() – Gets the parent process ID.

Example Program: Using fork(), getpid(), wait()

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h> #include

<sys/wait.h>

int main() {

pid_t pid = fork();

if (pid < 0) {

// Fork failed

perror("Fork failed");

return 1;

AKSHAT GUPTA 2300321540020


} else if (pid == 0) {

// Child process

printf("Child Process:\n");

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

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

} else {

// Parent process

wait(NULL); // Wait for child to complete

printf("Parent Process:\n");

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

return 0;

Output
Child Process:

Child PID: 3452

Parent PID: 3451

Parent Process:

Parent PID: 3451.

ii.File Management System Calls


File management system calls in UNIX/Linux are used to create, open, read, write, and close files. These
calls allow users to manipulate files at a low level using file descriptors.

Common System Calls:


1. open() – Opens a file.
2. read() – Reads data from a file.
3. write() – Writes data to a file.
4. close() – Closes a file descriptor.
5. lseek() – Moves the read/write file offset.
6. unlink() – Deletes a file

Example Program: Using open(), write(), read(), close()


#include <stdio.h>

AKSHAT GUPTA 2300321540020


#include <fcntl.h>

#include <unistd.h>

int main() {

int fd;

char buffer[50]; // Create and open a file for writing

fd = open("sample.txt", O_CREAT | O_WRONLY, 0644);

if (fd < 0) {

perror("File creation failed");

return 1;

write(fd, "Hello, this is a system call example.\n", 39);

close(fd);

// Open the file for reading

fd = open("sample.txt", O_RDONLY);

if (fd < 0) {

perror("File open failed");

return 1;

read(fd, buffer, sizeof(buffer));

printf("File content:\n%s\n", buffer);

close(fd);

return 0;

Output :

File content:

Hello, this is a system call example.

iii.Input/Output (I/O) System Calls

AKSHAT GUPTA 2300321540020


I/O system calls in UNIX allow interaction with devices like the

keyboard, monitor, or files using input/output streams. These are

lower-level calls for reading input and writing output.

Common System Calls:

1. read() – Reads data from a file descriptor (keyboard, file, etc.).


2. write() – Writes data to a file descriptor (terminal, file, etc.).
3. dup() / dup2() – Duplicates file descriptors.
4. ioctl() – Device-specific input/output operations.
5. lseek() – Moves the file descriptor offset for reading/writing.

Example Program: Using read() and write() for Terminal I/O


#include <stdio.h> #include

<unistd.h> int main() {

char buffer[100];

int n;

// Write a message to the terminal

write(1, "Enter some text: ", 17);

// Read input from the terminal (file descriptor 0)

n = read(0, buffer, sizeof(buffer));

// Write the input back to the terminal (file descriptor 1)

write(1, "You entered: ", 13);

write(1, buffer, n);

return 0;

Output (Example):
Enter some text: Hello System Calls!

You entered: Hello System Calls!

AKSHAT GUPTA 2300321540020


ABES ENGINEERING COLLEGE [Department of CSE-Data Science]
Operating System Lab [BCS-451]
2nd Year||4th Semester||Section: A||Group:A1

Experiment – 3

Objective: Write a C program to simulate the First Come First Serve (FCFS)
non-preemptive CPU scheduling algorithm.

1.1 Program (C Code)

#include<stdio.h>

#include<conio.h>

void main() {

int bt[20], wt[20], tat[20], i, n;

float wtavg, tatavg;

clrscr();

printf("\nEnter the number of processes -- ");

scanf("%d", &n);

for(i = 0; i < n; i++) {

printf("\nEnter Burst Time for Process %d -- ", i);

scanf("%d", &bt[i]);

wt[0] = wtavg = 0;

tat[0] = tatavg = bt[0];

for(i = 1; i < n; i++) {

wt[i] = wt[i-1] + bt[i-1];

tat[i] = tat[i-1] + bt[i];

wtavg = wtavg + wt[i];

tatavg = tatavg + tat[i];

printf("\n\tPROCESS\t\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");

AKSHAT GUPTA
2300321540020
ABES ENGINEERING COLLEGE [Department of CSE-Data Science]
Operating System Lab [BCS-451]
2nd Year||4th Semester||Section: A||Group:A1
for(i = 0; i < n; i++) {

printf("\n\tP%d\t\t%d\t\t%d\t\t%d", i, bt[i], wt[i], tat[i]);

printf("\n\nAverage Waiting Time -- %f", wtavg/n);

printf("\nAverage Turnaround Time -- %f", tatavg/n);

getch();

1.2 Input/Output Example

AKSHAT GUPTA
2300321540020

You might also like