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

Os Lab Experiment 2

The document discusses various system calls for process management, file management, and input/output. It provides examples of using fork(), open(), read(), write(), and close() system calls. Example code is given to demonstrate process IDs, file I/O, and reading/writing input and output.

Uploaded by

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

Os Lab Experiment 2

The document discusses various system calls for process management, file management, and input/output. It provides examples of using fork(), open(), read(), write(), and close() system calls. Example code is given to demonstrate process IDs, file I/O, and reading/writing input and output.

Uploaded by

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

EXPERIMENT – 2

Objective – Execute various system calls for


i. Process Management
ii. File Management
iii. Input/Output System Calls
Theory –
1. Process Management
Process management uses certain system calls. They are explained below.
1. To create a new process – fork () is used.
2. To run a new program = exec () is used.
3. To make the process to wait = wait () is used.
4. To terminate the process – exit () is used.
5. To find the unique process id – getpid () is used.
6. To find the parent process id – getppid () is used.
7. To bias the currently running process property – nice () is used.
2(i) Example program for example of fork ()

#include<stdio.h>

#include <sys/types.h>

main ()

int pid;

pid=fork();

if(pid==0)

printf("id of the child process is=%d\n", getpid());

printf("id of the parent process is=%d\n", getppid());

else { printf("id of the parent process is=%d\n", getpid());

printf("id of the parent of parent process is=%d\n", getppid());

}
Output:

File Management
There are four system calls for file management,
1. open ():system call is used to know the file descriptor of user-created files. Since read
and write use file descriptor as their 1st parameter so to know the file descriptor open()
system call is used.
2. read ()system call is used to read the content from the file. It can also be used to read
the input from the keyboard by specifying the 0 as file descriptor.
3. write (): system call is used to write the content to the file.
4. close ():system call is used to close the opened file, it tells the operating system that you are
done with the file and close the file.
2(ii). Example program for file management
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main() {
int n,fd;
char buff[50];
printf("Enter text to write in the file:\n");
n= read(0, buff, 50);
fd=open("file",O_CREAT | O_RDWR, 0777);
write(fd, buff, n);
write(1, buff, n);
int close(int fd);
return 0;
}
3. Input/Output System Calls
Basically there are total 5 types of I/O system calls:
• Create: Used to Create a new empty file.
• open: Used to Open the file for reading, writing or both.
• close: Tells the operating system you are done with a file descriptor and Close the file
which pointed by fd.
• read: From the file indicated by the file descriptor fd, the read() function reads bytes
of input into the memory area indicated by buf.
• write: Writes bytes from buf to the file or socket associated with fd.

2(iii) Example program for Input/output System Calls


#include <stdio.h>
#include <unistd.h>

int main() {
char buffer[100];
int n;

// Read input from the user


write(STDOUT_FILENO, "Enter a message: ", 17);
n = read(STDIN_FILENO, buffer, 100);

// Write the input back to the user


write(STDOUT_FILENO, "You entered: ", 13);
write(STDOUT_FILENO, buffer, n);

return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy