Os Lab Experiment 2
Os Lab Experiment 2
#include<stdio.h>
#include <sys/types.h>
main ()
int pid;
pid=fork();
if(pid==0)
}
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.
int main() {
char buffer[100];
int n;
return 0;
}
OUTPUT: