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

Os Prac 2

os parctical

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)
15 views4 pages

Os Prac 2

os parctical

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/ 4

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

You might also like