0% found this document useful (0 votes)
5 views27 pages

L8 - SystemCalls - 1

The document discusses system calls in operating systems, highlighting their differences from function calls and detailing the steps involved in making a system call. It provides examples of common system calls, such as 'getuid()', 'fork()', and 'exec()', and explains their significance in managing file I/O operations. Additionally, it covers the importance of the operating system in controlling I/O for safety and fairness, and includes practical examples of using system calls in C programming.

Uploaded by

avkbhavansurya
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)
5 views27 pages

L8 - SystemCalls - 1

The document discusses system calls in operating systems, highlighting their differences from function calls and detailing the steps involved in making a system call. It provides examples of common system calls, such as 'getuid()', 'fork()', and 'exec()', and explains their significance in managing file I/O operations. Additionally, it covers the importance of the operating system in controlling I/O for safety and fairness, and includes practical examples of using system calls in C programming.

Uploaded by

avkbhavansurya
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/ 27

L8_System Calls

TEAM
CSE A Mr. Chakravartula Raghavachari
CSE B Dr. Gireesh Kumar T
CSE C Bindu K.R
CSE D Dr. Remya Krishnan P (Mentor)

CSE E Mr. Baskar A

Dr. Arumuga Arun R/


CSE F
M. Chakravartula Raghavachari

This Photo by Unknown author is licensed under CC BY-NC.


Slides Courtesy:Understanding Operating Systems, Fifth Edition
System Calls
Today's Lecture directory

File
BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS 2
System Calls versus Function Calls
Function Call

Process

fnCall()

Caller and callee are in the same


Process
- Same user
- Same “domain of trust”
BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS
3 3
System Calls versus Function Calls
Function Call System Call

Process Process

fnCall() sysCall()

OS

Caller and callee are in the same


Process
- Same user - OS is trusted; user is not.
- Same “domain of trust” - OS has super-privileges; user does not
BINDU K R ,DEPT OF CSE, ASC, CBE - Must
19CSE213take
OPERATINGmeasures to prevent abuse
SYSTEMS
4 4
System Calls

System Calls
A request to the operating system to perform some activity
System calls are expensive
The system needs to perform many things before executing a system call
•The computer (hardware) saves its state
•The OS code takes control of the CPU, privileges are updated.
•The OS examines the call parameters
•The OS performs the requested function
•The OS saves its state (and call results)
•The OS returns control of the CPU to the caller

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


5 5
Steps for Making a System Call
(Example: read call)

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


6 6
Examples of System Calls

Example:
getuid() //get the user ID
fork() //create a child process
exec() //executing a program
Don’t confuse system calls with libc calls
Differences?
Is printf() a system call?
Is rand() a system call?

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


7 7
C

System calls vs. libc


Each I/O system call has corresponding
procedure calls from the standard I/O library.

Use man –s 2 Use man –s 3


BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS
8 8
C

File System and I/O Related System Calls

A file system: A hierarchical


arrangement of directories.
In Linux/Unix, the root file system
starts with "/“

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


9 9
Why does the OS control I/O?

⚫ Safety
◼ The computer must ensure that if my program
has a bug in it, then it doesn't crash or mess
up
◼ the system,
◼ other programs that may run at the same time or
later.
⚫ Fairness
◼ Make sure other programs have a fair use of
device
BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS
10 10
Directory System calls
#include <sys/dir.h>

And the function


DIR *opendir(char* dir_name)
Opens a directory given by dir_name and provides a pointer to access files within the
directory The open DIR stream can be used to access a struct that contains the file information.
The function
struct dirent *readdir(DIR* dp)

struct dirent {
u-long d_ino; /* i-node number for the dir entry */
u_short d_reclen; /* length of this record */
u_short d_namelen ; /* length of the string in d_name */
char d_name[MAXNAMLEN+1] ; /* directory name */
};

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


11 11
Accessing File status
stat(char* file, struct stat *buf);
fstat(int fd, struct stat *buf);
// defines a struct stat to hold file information

struct stat buf;


// now the file information is placed in the buf

stat(“filename”, &buf) ;

the stat structure contains useful information such as


st_atime --- Last access time
st_mtime --- last modify time
st_ctime --- Last status change time
st_size --- total size of file
st_uid – user ID of owner
st_mode – file status (directory or not)

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


12 12
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int main()
{
struct stat statbuf;
char dirpath[256];
getcwd(dirpath,256);
DIR *dir = opendir(dirpath);
struct dirent *dp;
for (dp=readdir(dir); dp != NULL ; dp=readdir(dir)){
stat(dp->d_name, &statbuf);

printf("the file name is %s \n", dp->d_name);


printf("dir = %d\n", S_ISDIR(statbuf.st_mode));
printf("file size is %ld in bytes \n", statbuf.st_size);
printf("last modified time is %ld in seconds \n",statbuf.st_mtime);
printf("last access time is %ld in seconds \n",statbuf.st_atime);
printf("The device containing the file is %d\n", statbuf.st_dev);
printf("File serial number is %d\n\n", statbuf.st_ino);
}

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS 13


System Calls for I/O
There are 5 basic system calls that Unix provides for file I/O
int open(char *path, int flags [ , int mode ] ); (check man –s 2 open)

int close(int fd);

int read(int fd, char *buf, int size);

int write(int fd, char *buf, int size);

off_t lseek(int fd, off_t offset, int whence);

Remember: these are different from regular procedure calls


Some library calls themselves make a system call
(e.g. fopen() calls open())

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


14 14
open
int open(char *path, int flags [ , int mode ] )

makes a request to the operating system to use a file.


The 'path' argument specifies the file you would like to use
The 'flags' and 'mode' arguments specify how you would like to use it.
If the operating system approves your request, it will return a file descriptor to you.

This is a non-negative integer.


Any future accesses to this file needs to provide this file descriptor
If it returns -1, then you have been denied access;
check the value of global variable "errno" to determine why (or use perror() to print
corresponding error message).

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


15 15
r

Standard Input, Output and Error


Now, every process in Unix starts out with three
file descriptors predefined:
File descriptor 0 is standard input.
File descriptor 1 is standard output.
File descriptor 2 is standard error.
You can read from standard input, using read(0,
...), and write to standard output using write(1, ...)
or using two library calls
printf
scanf

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


16 16
Example 1
//open system call
#include <fcntl.h>
#include <errno.h>

int main(int argc, char** argv) {


int fd;
fd = open("myfile1.txt", O_RDONLY);
printf("%d\n", fd);
if (fd==-1) {
fprintf (stderr, "Error Number %d\n", errno);
perror("Program");
}
}

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


17 17
Example 1

#include <fcntl.h>
#include <errno.h>
extern int errno; How to modify the example to
print the program name before
main() { the error message?

int fd;
fd = open("myfile.txt", O_RDONLY);
printf("%d\n", fd);
if (fd==-1) {
printf ("Error Number %d\n", errno);
perror("Program");
}
}

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


18 18
Close

int close(int fd)


Tells the operating system you are done with a file descriptor.
#include <fcntl.h>
main(){
int fd1, fd2;

if(( fd1 = open(“myfile.txt", O_RDONLY)) < 0){


perror(”myfile.txt");
exit(1);
}
if (close(fd1) < 0) {
perror(”myfile.txt"); Why do we need to close a file?
exit(1);
} After close, can you still use the
printf("closed the fd's\n");} file descriptor?

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


19 19
read(…)
int read(int fd, char *buf, int size) tells the operating system
To read "size" bytes from the file specified by "fd“ into the
memory location pointed to by "buf".
It returns how many bytes were actually read (why?)
•0 : at end of the file
•< size : fewer bytes are read to the buffer
(why?)
•== size : read the specified # of bytes
Things to be careful about
buf must point to valid memory not smaller than the specified size
•Otherwise, what could happen?
fd should be a valid file descriptor returned from open() to perform read
operation
•Otherwise, what could happen?

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


20 20
Example 2
//open system call
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char** argv) {
char *c;
int fd, sz;
c = (char *) malloc(100 * sizeof(char));
fd = open(“myfile.txt", O_RDONLY);
if (fd < 0) { perror(”myfile.txt"); exit(1); }

sz = read(fd, c, 10);
printf("called read(%d, c, 10), which read %d bytes.\n”, fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: %s\n", c);

close(fd);
}
BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS
21 21
write(…)
int write(int fd, char *buf, int size)

writes the bytes stored in buf to the file specified by fd


It returns the number of bytes actually written, which is
usually “size” unless there is an error
Things to be careful about buf must be at least as long as
“size”
The file must be open for write operations

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


22 22
Example 3
#include <fcntl.h>
#include<stdio.h>
int main()
{
int fd, sz;

fd = open("myfile1.txt", O_RDWR | O_CREAT | O_APPEND, 0644);


if (fd < 0) { perror("r1"); exit(1); }

sz = write(fd, "15CSE213\n", strlen("15CSE213\n"));

printf("called write(%d, \"15CSE213\\n\", %d), which returned %d\n", fd, strlen("15CSE213\n"), sz);

close(fd);
}

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


23 23
lseek

All open files have a "file pointer" associated with them to


record the current position for the next file operation
When file is opened, file pointer points to the beginning of the file
After reading/write m bytes, the file pointer moves m bytes forward

off_t lseek(int fd, off_t offset, int whence) moves the file
pointer explicitly
The 'whence' argument specifies how the seek is to be done
•from the beginning of the file
•from the current value of the pointer, or
•from the end of the file
The return value is the offset of the pointer after the lseek

How would you know to include sys/types.h and


unistd.h?
Read "man -s 2 lseek"

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


24 24
lseek example
c = (char *) malloc(100 * sizeof(char));
fd = open(“foo.txt", O_RDONLY);
if (fd < 0) { perror(”foo.txt"); exit(1); }

sz = read(fd, c, 10);
printf("We have opened foo.txt, and called read(%d, c, 10).\n”, fd);
c[sz] = ‘\0’;
printf(“Those bytes are as follows: %s\n”, c);

I = lseek(fd, 0, SEEK_CUR);
printf(“lseek(%d, 0, SEEK_CUR) returns the current offset = %d\n\n”, fd, i);

printf(“now, we seek to the beginning of the file and call read(%d, c, 10)\n”, fd);
lseek(fd, 0, SEEK_SET);
sz = read(fd, c, 10);
c[sz] = ‘\0’;
printf("The read returns the following bytes: %s\n", c);
…:

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS


25 25
System Calls

directory
Summary
File

BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS 26


Thankyou
Questions?

27 BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS

You might also like