Area Architecture and Computer Technology University Carlos III of Madrid
OPERATING SYSTEMS 1. Practice operating system calls
Degree in Computer Engineering
course 2018/2019
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
I index
1. Practice Statement
two
1.1. Practice Description 3
mycat 3 myls 4 MySize 5
1.2. Source Code Support 6 1.3. corrector provided 6
2. Delivery 7
2.1. Delivery term 7 2.2. Procedure for issuing practices 7 2.3. Submit documentation 7
3. Standards 9 4. Annex (system calls). 10
4.1. Calls related system files
4.2. System calls related to directories 4.3. Manual (man function).
5. References
10 eleven 12 13
one
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Practice Statement
This practice allows students to become familiar with the operating system calls (specifically, the file system) following the POSIX standard. Unix allows the system to make calls directly from a program in a language of high level, particularly in language C.
Most of the inputs / outputs (I / S) on Unix files can only be made using five calls: open, read, write, lseek and close.
For the operating system kernel, all open files are identified by file descriptors. A file descriptor is a non-negative integer. When we opened, open, a file that already exists, the kernel returns a file descriptor to the process. When we read or write from / to a file, we identify the file with the file descriptor that was returned by the call described above.
Each open file has a position the read / write current ( current file offset ).
He is represented by a non-negative integer that measures the number of bytes from the beginning of the file. The read and write operations normally start at the current position and cause an increase in that position, equal to the number of bytes read or written. By default, this position is initialized to 0 when a file is opened, unless the option is specified O_APPEND. The current position ( current_offset) an open file can be explicitly changed using the system call lseek.
To manipulate directories, you can use system calls opendir, readdir Y
closedir. An open directory is identified by a descriptor directory, which is a pointer to a type DIR ( DIR *). When we open a directory opendir, the core returns a directory descriptor on which you can read the directory
entries using function calls readdir. The call readdir returns a directory entry in a pointer to a structure dirent
( struct dirent *). This structure contains fields corresponding to that entry as the entry name or the type (if a normal file, if another directory, symbolic links, etc.). Successive function calls
readdir iran returning successive entries of an open directory.
two
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Practice Description
It is intended to implement three programs using C calls to the system described above. These
programs will mycat, myls Y MySize. To do this, they will have the corresponding code files mycat.c, myls.c Y mysize.c.
mycat
The first program, mycat, will open a specified file as an argument and displays the contents on the standard output (console) using calls open, read, write Y close.
For it:
It opens in open file as parameter.
It will read the contents of the file using an intermediate buffer 1024 bytes ( read).
Will write ( write) the contents of the buffer to the standard output. Use constant STDOUT_FILENO as the value of descriptor to write to the standard output.
Finally close the descriptor by close.
Use: ./ mycatrequirements:
The program should display the entire contents of the file.
The program should display exactly the same result command cat.
The program should return -1 if not happened any input argument.
The program should return -1 if an error opening the file ( eg file does not exist).
Suggested test one: Note that the program output on a file matches the mandate cat ( no arguments) on the same file.
one Meet this test is not guaranteed to have the highest score in the exercise. This is only a suggestion for students to check the
./ Mycat p1_pruebas / f1.txt
Name1 name2 Name3
V 32 M 35 V 53
09834320 32478973 98435834
24500 27456 45000
general operation of your program. Students must also meet other program requirements, perform the appropriate code,
comment, test extreme cases, and generally meet the other requirements described in the statement of practice.
3
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
myls
The second program, myls, will open a specified as an argument (or the current directory if no directory is specified as an argument) directory, and the screen displays the name of all entries in the directory, printing one entry per line. For it:
You get a directory to list of arguments the program or obtain the current directory by calling getcwd. Use constant PATH_MAX maximum size you can have the current directory path.
It opens in opendir.
Then he read each of the directory entries by readdir and will print the name of the input by printf.
Finally close the directory descriptor by closedir.
Use 1: ./ myls
requirements:
The program should list all the directory entries in the order in which go calling back readdir, and showing each entry in a line.
The program should list directory entries passed as a parameter (use 1) or the current directory if they have not passed any parameters (use 2).
The program should display exactly the same result as the command ls.
The program should return -1 if there was an error opening the directory ( eg the directory does not exist).
Suggested test two: Note that the program output on a directory matches the mandate ls -1 -f on the same directory: ls -1 -f
two Meet this test is not guaranteed to have the highest score in the exercise. This is only a suggestion for students to check the
./ Myls p1_pruebas / dirC f1.txt dirA f2.txt
. ..
general operation of your program. Students must also meet other program requirements, perform the appropriate code,
comment, test extreme cases, and generally meet the other requirements described in the statement of practice.
4
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
MySize
The third program, MySize, You get the current directory and list all files containing regular and its size in bytes. For it:
You get the current directory by calling getcwd. Use constant PATH_MAX maximum size you can have the current directory path.
It opens in opendir.
Then he read on directory entries by readdir.
or If the entry is a regular file (field d_type structure dirent equal to the constant DT_REG)
It opens the file by open.
It moves the pointer to the end of the file and the value obtained by lseek.
It closes the file by close.
It finally print the file name (field d_name structure dirent), followed by a tab
character, and the size obtained by lseek, ending with a line break.
This procedure is repeated for each of the directory entries.
Finally close the directory descriptor by closedir.
Use: ./ MySize requirements:
The program should display the name and size of all regular files in the directory, in the order in which go calling back readdir, and displaying the data of each file in a line.
The program will only show regular data files.
The show program the data in the following Format:
The program should return -1 if there was an error opening the directory.
p1_pruebas cd /
../ MySize
f1.txt 87 f2.txt 87
5
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Source Code Support
To facilitate the realization of this practice is available p1_llamadas_2019.zip file containing the source code support. To extract its contents execute the following:
unzip p1_llamadas_2019.zip
To extract its contents, the directory is created you p1_llamadas /, where practice should be developed. Within this directory have included the following files:
Makefile
Source file for the tool make. It should not be changed. Automatic recompilation him only the source files are modified is achieved. use $ make to compile programs, and $ make clean to remove the compiled files.
mycat.c
C source file where students must encode the program mycat.
myls.c
C source file where students must encode the program myls.
mysize.c
C source file where students must encode the program MySize.
p1_tests /
This directory contains files and directories example, to run and test programs.
corrector_ssoo_p1_2019.py
Corrector practice. Check the format of delivery and basic functionality.
corrector provided
Students are provided the script python corrector_ssoo_p1_2019.py verifying that the format of deliverable practice is correct (naming conventions follow, is well compressed and executes basic tests). The corrector must run on Linux machines computer lab classrooms IT department or guernika.lab.inf.uc3m.es server.
The command to run the checker is as follows: corrector_ssoo_p1_2019.py python
Being entregable.zip the file to be delivered by global classroom (see next section). Example:
$ Python corrector_ssoo_p1.py ssoo_p1_100254896_100047014.zip
The corrector will print messages on screen indicating the final results (0 bad 1 good).
6
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Delivery
Delivery term
The deadline for delivery of practical GLOBAL CLASSROOM date will be the Sunday March 3, 2019 ( until 23: 55h).
Procedure for issuing practices
Delivery practices must be done electronically. AULA GLOBAL some links will be enabled through which can make delivery practices. Specific, an entregador for the code of practice will be enabled, and another type TURNITIN memory for practice.
Submit documentation
It should deliver a compressed zip file with the name ssoo_p1_AAAAAAAAA_BBBBBBBBB_CCCCCCCC.zip where A A, B and C B C are the SSAs of the group members. In case of solitary practice, the format will be ssoo_p1_AAAAAAAAA.zip. The zip file will be delivered in the corresponding code of practice deliverer. The file should contain:
or Makefile or mycat.c or myls.c
or mysize.c
The memory It will be delivered in PDF format in a file called ssoo_p1_AAAAAAAAA_BBBBBBBBB_CCCCCCC.pdf. Will only be corrected and graded reports in pdf format. Must contain at least the following sections:
Code description detailing the main functions implemented. NOT include source code of practice in this section. Any code will be automatically ignored.
Battery of tests used and results obtained. It will be given highest score to advanced testing, extreme cases, and generally those tests to ensure the proper functioning of the practice in all cases. Must take into account:
7
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
or Compile a program correctly without warnings ( warnings) do not It is guaranteed to work properly.
or Avoid duplicate tests that evaluate the same program streams. The
score this section is not measured by the number of tests, but the degree of coverage of them. Better is little evidence evaluating different cases many tests always assess the same case.
conclusions problems encountered, how they were solved, and personal opinions.
The following aspects are also punctuate the presentation practice:
Must contain cover, the authors of the practice and its ISAs.
Table of contents must contain.
The memory must have page numbers on every page (except the cover).
The text of the report must be justified.
Test plan:
Detail the purpose of the test, the execution procedure, the expected output, and optionally the output
obtained by the actual implementation.
Avoid duplicate tests (eg same type of parameters with different values).
Tabular information for better presentation.
Compiling without errors is not proof, and no guarantees cover all aspects of practice. Analyze the code for potential sources of error can help build a good test plan.
The memory will have a maximum of 8 pages (including cover, indexes and sections).
Pdf file will be delivered in the corresponding memory practice (entregador TURNITIN) entregador. NOTE: The only recorded version of his practice is the last delivered. Valuing this is the only valid and final.
8
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Rules
one) Practices that do not compile or not conforming to the functionality and requirements
raised, they will score 0.
two) Copied special attention to detect functionality between two practices will be provided.
In case of finding common implementations in two practices, the students involved (copied and copying) will lose the qualifications obtained by continuous assessment.
3) Programs must compile without warnings.
4) Programs should run under a Linux system, it is not allowed the
conducting practice for Windows systems. In addition, to ensure the proper functioning of practice should be checked compilation and execution in the computer labs of the university or the guernika.lab.inf.uc3m.es server. If the code presented does not compile or does not work on these platforms implementation will not be considered correct.
5) A program mentioned, get a rating of 0.
6) Delivering practice is done through global classroom, as detailed in
Delivery section of this document. delivery via email without prior approval is not allowed.
7) Must be respected at all times the format of the input and output indicated in each program to be implemented.
8) You should perform error handling in each of the programs beyond explicitly requested in each section.
Delivered programs that do not follow these rules will not be considered approved.
9
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Annex (system calls).
System calls provide the interface between the operating system and a running program. UNIX allows making system calls directly from a program in high-level language, especially in C language, in which case calls resemble function calls, as if they were defined in a standard library. The general format of a system call is:
status = funcion_estandar (arg1, arg2, ..)
If we make a call unsuccessful return in the variable status a value -1. In the global variable errno the error number, with which we can obtain the association error with what has actually happened in the file is placed errno.h, ( contained in the path: / usr / src. Linux: / usr / src / linux / include / asm / errno.h).
Calls related system files
int open (const char * path, int flag, )
Open or create a specified file by path. The open file can be used for reading, writing, or both, depending on specified by flag. Returns a file descriptor that can be used for reading or writing to the file. More help:
man 2 open
int close (int fd)
Closes an open file descriptor associated with the above Fildes. If n = -1 Error closing the file. More help: man 2 close
ssize_t read (int fd, void * buf, size_t nbyte)
Attempts to read a file (the file descriptor Fildes was obtained to open it) many bytes as indicated nbyte, placing the information read from the memory address buf.
Returns the number of bytes read (which may be less than or equal to nbyte). If return = 0 End of file (EOF). If return = -1 read error.
More help: man 2 read
ssize_t write (int fd, const void * buf, size_t nbyte)
10
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
Tries to write to a file (the file descriptor Fildes was obtained to open it) many bytes as indicated nbyte, taking them from the memory address indicated buf.
N returns the number of bytes that have actually been written (which may be less than or equal to
nbyte). If return = -1 write error.
Every write ( and each read), automatically updates the current file position used to determine the position in the file following write or read.
More help: man 2 write
off_t lseek (fd int, off_t offset, int whence)
Modifies the value of the pointer descriptor Fildes in the file, the explicit position offset from the reference imposed in origin so that calls read or write They can start anywhere in the file.
If return = -1 positioning error.
the parameter whence You can take the following values:
SEEK_SET from the beginning of the file.
SEEK_CUR from the current position.
SEEK_END from the end of the file.
the parameter offset It expressed in bytes, and takes a positive or negative value. Example:
abcdefghi lseek (5.4, SEEK_SET) To move 4 bytes, the next reading would be the e. Open file descriptor fd is 5. More Help:
man 2 lseek
System calls related to directories
DIR * opendir (const char * dirname)
Open an existing directory specified by dirname. Directory returns a descriptor that can be used for reading the directory entries. If return = NULL Error opening directory. More help:
man opendir
struct dirent * readdir (DIR * dirp)
Reads a directory (the directory descriptor dirp was obtained when opened) the
eleven
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
next entry in the directory. The structure dirent It contains a field d_name (char * d_name) with the name of the entry and a field d_type (Unsigned char d_type) the type of entry (file, another directory, etc.).
Subsequent calls to this function on the same descriptor subsequent return directory entries. When there are no more entries to return, this call returns NULL. More help:
man readdir
Closes an open directory above. If return = -1 Error closing directory. More help: man closedir
Manual (man function).
man It is the pager system manual, ie lets you find information about a program, utility or function. See the following example:
man 2 open
A manual page has several parts. These are labeled NAME, SYNOPSIS, DESCRIPTION, OPTIONS, FILES, SEE ALSO, BUGS, and AUTHOR. SYNOPSIS label libraries (identified by the policy are collected
# include) to be included in the user program in C to make use of the corresponding functions. To exit the displayed page, just press the q key.
The most common ways to use man They are as follows:
section man element: Presents page element available in the section of the manual.
man -a element: Presents, sequentially, every page of available item in the manual. Between pages you may decide to skip to the next or leave Pager completely.
man -k keyword: Look up the word-key between the short descriptions and manual pages and presents all you married.
Bibliography
The programming language C: design and implementation of programs Felix Garcia, Jesus Carretero, Javier Alejandro Fernandez and Calderon. Prentice-Hall, 2002.
int closedir (DIR * dirp)
12
Department of Computer Science Degree in Computer Engineering
Operating Systems (2018 2019)
Practice 1 Calls to OS
The UNIX System SR Bourne Addison-Wesley, 1983.
Advanced UNIX Programming Rochkind MJ Prentice-Hall, 1985.
Operating Systems: A vision applied Jesus Carretero, Felix Garcia, Pedro Miguel and Fernando Perez. McGraw-Hill, 2001.
Programming Utilities and Libraries SUN Microsystems, 1990.
Unix man pages ( man function)
13
Reviews
There are no reviews yet.