Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Moti Geva

Similar presentations


Presentation on theme: "Operating Systems Moti Geva"— Presentation transcript:

1 Operating Systems Moti Geva geva.biu@gmail.com
The basics of files Operating Systems Moti Geva

2 Users UNIX manages users
Users are declared in the /etc/passwd file – discussed later... Each user has a unique user id (UID) Each user may belong to a many groups Each group has a group ID (GID) Groups are declared in /etc/groups

3 Ownership Each file belongs to a single user
Each file belongs to a single group A user may have permissions to a file even if he isn’t the owner or in the file’s group How is that?

4 Levels of permissions User The owner of the file Group
Not the user but belongs to the file’s group Other Not the user and not in the group Also known as “the world”

5 Who are the “others”? An example... Users: Avi Ben Gil Dan Eitan
Group: Students (Avi, Ben, Gil) Teachers (Gil, Dan) Files: Teachers Eitan File3 Gil File2 Students Avi File1 Group User/ Owner Name Who are the “others”?

6 Types of pemissions Three types of permissions Read (4) Write (2)
Execute (1) Each one of ugo (user, group, other) has it’s own RWX permission allowed or revoked How many bits are needed for this?

7 An example...

8 The almighty root Permission apply to all users and groups
With one exception...the “root” user root is the administrator of the OS All privileges are given to the root. If not he can take them Powerful yet dangerous...should be used only for special reasons

9 Changing permission attributes
chown - change the ownership (user) chown [options]... user file chgrp - change the group chgrp [options]... group file chmod – change pemission chmod [[options]... mode file

10 Basic file operations We will consider open, read, write, close system calls Other file-system system-calls will be addressed at a later time But first a little about error-handling

11 errno Almost every system-call that fails does the following
Error indication (func. return value) Specific error is kept in “errno” errno definition is in errno.h #include <errno.h> extern int errno;

12 errno usage The program should check what caused the error and manage the error if possible The program can print the error using perror() function call #include <stdio.h> void perror(const char *s); #include <string.h> char *strerror(int errnum); #include <stdio.h> // deprecated const char *sys_errlist[]; int sys_nerr;

13 File descriptors A file descriptor is used for mapping I/O between the process and the OS Each file descriptor represents an I/O resource It is used in various system calls such as read() and write()

14 Process descriptor table
Each process has a single PDT Process descriptor table A file descriptor (fd) is an entry/offset in the PDT Special file descriptors: 0 – STDIN (standard input) 1 – STDOUT (standard output) 2 – STDERR (standard error)

15 STDIN, STDOUT, STDERR Where are they used?
When using “scanf” the output is read from STDIN When using “printf” the output is printed into STDOUT When using perror the output is directed to STDERR

16 The open() system call open, creat - open and possibly create a file or device #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode);

17 The close() system call
close - close a file descriptor #include <unistd.h> int close(int fd);

18 The read() system call read - read from a file descriptor
#include <sys/types.h> #include <unistd.h> ssize_t read(int fd, void *buf, size_t count);

19 The write() system call
write - write to a file descriptor #include <sys/types.h> #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count);

20 The ‘umask’ ‘umask’ is the file creation mask
Whenever a file is created the mode (permissions) of the file is set to be mode & ~umask This means that the specified mode will be reduced according to permission not specified by umask Files permission can be changed manually at a later time

21 umask example umask is set to be umask = 042
(meaning do not give read permission to ‘group’ and write permission to ‘other’) If open() is called as fd = open(“hello”, O_CREAT | O_WRONLY, 0755); (meaning rwx to user rx to group and rx to other) The resulted file (if created) will be 0715

22 Changing the umask Synopsis #include <sys/stat.h>
int umask(int mask); Description umask sets the umask to mask & Return value The previous value of the mask is returned

23 dup() and dup2() NAME dup, dup2 - duplicate a file descriptor SYNOPSIS
#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); DESCRIPTION dup() and dup2() create a copy of the file descriptor oldfd. dup() uses the lowest-numbered unused descriptor for the new descrip‐ tor. dup2() makes newfd be the copy of oldfd, closing newfd first if neces‐ sary

24 IO Redirection Redirection is the changing the standard input/output/error to a file Examples: Get STDIN from a file (tee < file.txt) Send STDOUT to a file (ls > ls.txt) Send STDERR to a file (ls 2> ls.txt) How can we implement using dup()/dup2()? Example: dup.c


Download ppt "Operating Systems Moti Geva"

Similar presentations


Ads by Google