The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

Similar documents
File and Directories. Advanced Programming in the UNIX Environment

Preview. Review. System Data Files (Password File) System Data Files (Password File) System Data Files (Password File)

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Files and Directories

Lecture 23: System-Level I/O

File IO and command line input CSE 2451

UNIX input and output

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

All the scoring jobs will be done by script

Lecture 21 Systems Programming in C

File System User API

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

All the scoring jobs will be done by script

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Input / Output Functions

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

File Input and Output (I/O)

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

UNIX System Programming

Computer Programming: Skills & Concepts (CP) Files in C

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Operating System Labs. Yuanbin Wu

structs as arguments

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Files and Directories

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Operating System Labs. Yuanbin Wu

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Files and Directories Objectives Additional Features of the File System Properties of a File. Three major functions that return file information:

File (1A) Young Won Lim 11/25/16

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Standard C Library Functions

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

System-Level I/O Nov 14, 2002

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

ENG120. Misc. Topics

The course that gives CMU its Zip! I/O Nov 15, 2001

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Files and Directories E. Im

System-Level I/O : Introduction to Computer Systems 16 th Lecture, March 23, Instructors: Franz Franchetti & Seth Copen Goldstein

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

0UNIT-III UNIX FILE SYSTEM

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

CSC 271 Software I: Utilities and Internals

A Typical Hardware System The course that gives CMU its Zip! System-Level I/O Nov 14, 2002

CS 201. Files and I/O. Gerson Robboy Portland State University

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

PROGRAMMAZIONE I A.A. 2017/2018

CMPSC 311- Introduction to Systems Programming Module: Input/Output

2009 S2 COMP File Operations

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Programming & Data Structure

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Goals of this Lecture

Standard File Pointers

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CE Lecture 11

Naked C Lecture 6. File Operations and System Calls

Computer Programming Unit v

INTRODUCTION TO UNIX FILE SYSTEM:

Introduction to Computer Systems , fall th Lecture, Oct. 19 th

System- Level I/O. CS 485 Systems Programming Fall Instructor: James Griffioen

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Contents. A Review of C language. Visual C Visual C++ 6.0

CSE 333 SECTION 3. POSIX I/O Functions

System-Level I/O March 31, 2005

System Software Experiment 1 Lecture 7

C PROGRAMMING. Characters and Strings File Processing Exercise

CSE 410: Systems Programming

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

C programming basics T3-1 -

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Input/Output and the Operating Systems

CS240: Programming in C

COMP 2355 Introduction to Systems Programming

HIGH LEVEL FILE PROCESSING

Foundations of Computer Systems

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

25.2 Opening and Closing a File

Transcription:

Preview The link() System Call link(), unlink() System Call remove(), rename() System Call Symbolic link to directory Symbolic link to a executable file symlink() System Call File Times utime() System Call mkdir() and rmdir() System Call chdir() Systemcall Standard IO Libaries System Files and Information Password File Any file can have multiple directory entries pointing to its. The way we can create a link to an existing file is with the link system call. Prototype: int link (const char *existingpath, const char *newpath) Returns: 0 if OK, return -1 on error 1 2 The link() System Call The link() System Call int link (const char *existingpath, const char *newpath) Returns: 0 if OK, return -1 on error Lecture The link system call create a new directory entry newpath that references the existing file existingpath. The creation and increment of the link count be done automatically by kernel. Only a super-user process can create a new link that points to a directory. Because link system calls to a directory can cause loops in the file system. cosc 220 cosc350 chapter2 lecture lec11 link.c 3 4 * link.c * The link() System Call int main () struct stat buff; * get attributes for the file link.c* if (stat ("link.c", &buff) < 0) err_sys("stat error for "); printf ("link count for a file link.c was %d \n", buff.st_nlink); * increase the of link by one * if (link ("Lecturecosc350lecturelec11link.c", "Lecturecosc350lecturelink.c") <0) * get attributes for the file link.c* if (stat ("link.c", &buff) < 0) err_sys("stat error for "); printf ("link count for a file link.c is now %d \n", buff.st_nlink); By using the unlink system call, we can remove an existing directory entry. Prototype: int unlink (const char *pathname) Returns: 0 if OK, return -1 on error exit(0); 5 6 1

and ory Block and ory Block directory list Block directory list Block Move to Move to 7 8 The unlink() system call removes the directory entry and decrement the link count of the file referenced by pathname. To unlink a file, we must have write permission and execute permission in the directory containing the directory entry. The superuer can call unlink with pathname specifying a directory, but rmdir() system call should be used instead of unlink() * linkunlink.c * int main () * increase the of link by one * if (link ("Lecturecosc350lecturelec11linktest.txt", "Lecturecosc350lecturelinktest.txt") <0) printf ("create new link\n"); sleep (30); * unlink existing file * if (unlink("lecturecosc350lecturelec11linktest.txt")<0) err_sys("unlink Eror"); printf("file unlinked\n"); sleep(30); * bring back to before modification* if (link ("Lecturecosc350lecturelinktest.txt", "Lecturecosc350lecturelec11linktest.txt") <0) printf ("link back\n"); if (unlink("lecturecosc350lecturelinktest.txt")<0) err_sys("unlink Eror"); printf("file unlinked\n"); exit(0); 9 10 remove and rename System Call remove and rename System Call We can unlink a file or directory with the remove() system call. For a file remove() is identical to unlink() For a directory remove() is identical to rmdir() A file or directory is renamed with rename Prototype: int remove(const char *pathname); int rename (const char * oldname, const char* newname); Returns: 0 if ok, -1 on a error * removeex.c * int main(int argc, char *argv[]) int fd; * file descriptors * if ((fd = open(argv[1], O_WRONLY O_CREAT, S_IRUSR S_IWUSR S_IRGRP S_IWGRP S_IROTH S_IWOTH))<1) printf("no file\n"); printf("a file %s is created\n", argv[1]); printf("10 seconds later %s will be renamed to newname\n", argv[1]); sleep(10); rename(argv[1], "newname"); printf ("Renamed..."); printf("10 seconds later newname will be removed\n"); sleep(10); printf ("Removed..."); close(fd); remove ("newname"); return 0; 11 12 2

Hard link is directly point to the of the file. A symbolic link is an indirect pointer to a file or directory. Symbolic link were introduced to get around the limitation of hard link. Hard link require that link and the file reside in the same file system. Only the super user can create a hard link to a directory * linktodir.c * int main () * increase the of link by one * * since I am not super user, system create symbolic link instead hard link * if (link ("Lecturecosc350lecturelec11", "Lecturecosc350lec11") <0) exit(0); 13 14 Anyone can create a symbolic link. It can be used to move a file or an entire directory hierarch to some other location on a system. It can also be used to execute a file Create a symbolic link link from to 12 directory 1 2 ln s..12 link Symbolic link 3 link hello.c hello.o hello 15 16 The symlink() System Call hi Create a symbolic link hi from to 12 hello executable code 1 2 ln s..12 hello hi Symbolic link 3 A symbolic link can created with the symlink system call. Prototype: int symlink (const char *actualpath, const char *sympath) A new directory entry, sympath, is created that points to actualpath. hello.c hello.o hello 17 18 3

The symlink() System Call * symlink.c * int main() if ( symlink("lecturecosc350lecturelec11linkunlink", "Lecturecosc350lecturelnkunlnk") <0) err_sys("symbolic Link Creation Error \n"); printf("a symbolic link is created \n"); return 0; File Times Three time fields are maintained for each file. st_atime time of last access i.e. read st_mtime time of last modification i.e. write st_ctime time of last file status change i.e. chmod chown 19 20 The utime() System Call * timeexchange.c: exchange times between two files * #include <utime.h> The access time and the modification time of a file can be changed with the utime system call. Prototype: #include <utime.h> int utime(const chr *pathname, const construct utimebuf *times); The structure used in utime system call is: struct utimebuf time_t actime; time_t modtime; int main (int argc, const char *argv[]) struct stat statbuf0, statbuf1; struct utimbuf timebuf0, timebuf1; if (argc < 3) err_sys ("Argument Error"); * get first file information * if (stat(argv[1], &statbuf0) <0) err_sys("stat Error"); timebuf0.actime = statbuf0.st_atime; timebuf0.modtime = statbuf0.st_mtime; * get second file information * if (stat (argv[2], &statbuf1) <0) err_sys("stat Error"); timebuf1.actime = statbuf1.st_atime; timebuf1.modtime = statbuf1.st_mtime; * change time information between two file * if (utime (argv[1], &timebuf1) <0) err_sys("time change Error1"); if (utime (argv[2],&timebuf0) <0) err_sys("time Change Error"); 21 exit (0); 22 mkdir and rmdir System Call The entries for. and.. are automatically created. Prototype: int mkdir(const char *pathname, mode_t mode) int rmdir (const char *pathname) chdir System Call When a user logs in to a Linux or Unix system, the user normally starts at the directory specified by the file etcpasswd file. We can change working directory by using the chdir system call Prototype # include <unistd.h> int chdir (const char *pathname); 23 24 4

chdir System Call ******************************************** MyChdir.c ******************************************** printf ("Access Error for %s\n",str); int main() if (chdir ("cosc220")< 0) err_sys ("change directory "); printf("change directory successded\n"); exit (0); 25 Standard IO Library When we open a file with system call, kernel returns file descriptor which can be used for read and write to the file When we open a file with standard IO functions, we say that we have associated a stream with the file. When we open a stream with standard IO library fopen, it returns a pointer to FILE object. The file object is a structure that contains all the information required by the standard IO library to manage the stream. 26 Standard IO Library Standard IO Libraries Three streams are predefined and automatically available to a process: standard input, standard output and standard error. These three standard IO streams are referred through the predefined file pointers stdin, stdout, and stderr. These pointers are defined in <stdio.h> fopen, fclose fread, fwrite fflush fseek fgetc getc, getchar fputc putc, putchar fgets, gets printf, fprintf and sprintf scanf, fscanf and sscanf 27 28 System Files and Information System File (Password File) There are several data files required for normal operation: the password file etcpasswd The group file etcgroup Two files are used as a system data. Ex) etcpasswd is used every time a user log in to the Linux system. The Linux password file contains the fields. It is defined in <pwd.h> Historically the password file has been stored in etcpasswd and has been an ASCII file. Each line contains the seven fields separated by colons. 29 30 5

System File (Password File) char *pw_name User's login name. uid_t pw_uid Numerical user ID. gid_t pw_gid Numerical group ID. char *pw_dir Initial working directory. char *pw_shell Program to use as shell. 31 6