L8 - SystemCalls - 1
L8 - SystemCalls - 1
TEAM
CSE A Mr. Chakravartula Raghavachari
CSE B Dr. Gireesh Kumar T
CSE C Bindu K.R
CSE D Dr. Remya Krishnan P (Mentor)
File
BINDU K R ,DEPT OF CSE, ASC, CBE 19CSE213 OPERATING SYSTEMS 2
System Calls versus Function Calls
Function Call
Process
fnCall()
Process Process
fnCall() sysCall()
OS
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
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?
⚫ 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>
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 */
};
stat(“filename”, &buf) ;
#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");
}
}
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)
printf("called write(%d, \"15CSE213\\n\", %d), which returned %d\n", fd, strlen("15CSE213\n"), sz);
close(fd);
}
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
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);
…:
directory
Summary
File