SlideShare a Scribd company logo
1 of 31
Chapter 2 Users, Files, and the Manual
Who?Is using the system?
Low Level Read ssize_t read(intfd, void *buf, size_t count);  read()  attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. On success, the number of bytes read is returned (zero  indicates end of file), and the file position is advanced by this number.   It is not an error if this number  is  smaller  than the  number  of  bytes requested;  this may happen for example because fewer bytes are actually available right  now  (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because  read()  was interrupted by a signal.   On error, -1 is returned, and errno is set appropriately.  In this case it  is  left  unspecified whether the file position (if any) changes.
Low Level Open open and possibly create a file or device  #include <fcntl.h> int open(const char *pathname, mode_t mode);  Given  a  pathname for a file, open() returns a file descriptor, a small, non-negative integer for use in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.).
Suppressing Blank Records Remove unused terminal lines  blank user name  Locate the file /usr/include/bits/utmp.h Only print out our LOGIN process  USER_PROCESS 7
Time format Unix stores times as the number of seconds since midnight 1/1/1970, G.M.T. time_t data type is an integer. ctime  #include <time.h>  char *ctime(const time_t*timep);  The call ctime(t) is equivalent to asctime(localtime(t)).  It converts the calendar time t into a null-terminated string of the form 	"Wed Jun 30 21:49:08 1993"
Time format “Wed Jun 30 21:49:08 1993” Printf(“12.12s”, ctime(&t) + 4) Position 4
perror perror - print a system error message #include <stdio.h> Void perror(const char *s); DESCRIPTION The routine perror() produces a message on the standard error output,  describing  the last error encountered during a call to a system or library function.   First (if s is not NULL and  *s  is  not  a  null  byte  (''))  the argument string s is printed, followed by a colon and a blank.   Then  the message and a new-line.
Leek() Base:  SEEK_SET start of file SEEK_CUR current position SEEK_END end of file Example: Lseek(fd, 0 SEEK_END); Write(fd, “hello”, strlen(“hello”); Sets the current position to the end of file and writes a string there.
Leek() Example: Lseek(fd, -(sizeof(structutmp)), SEEK_CUR); Moves to the current position a distance of sizeof(structutmp)bytes before the current position.

More Related Content

What's hot

Linux commd
Linux commdLinux commd
Linux commdragav03
 
Linux commd
Linux commdLinux commd
Linux commdragav03
 
Python 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation CheatsheetPython 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation CheatsheetIsham Rashik
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsChapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsMeenalJabde
 
tac, command in Linux with example
tac, command in Linux with exampletac, command in Linux with example
tac, command in Linux with examplevinodahlawat4
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsMeenalJabde
 
Chapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsChapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsMeenalJabde
 
Linux basic commands_part1
Linux basic commands_part1Linux basic commands_part1
Linux basic commands_part1JASH PATEL
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionShuya Osaki
 
Time space trade off
Time space trade offTime space trade off
Time space trade offanisha talwar
 
Linux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regxLinux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regxKenny (netman)
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheetMartin Cabrera
 
Linux commands
Linux commandsLinux commands
Linux commandsU.P Police
 
Writing a Simple OS for Fun
Writing a Simple OS for FunWriting a Simple OS for Fun
Writing a Simple OS for FunSayeed Mahmud
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionMarina Kolpakova
 
Useful Linux Commands
Useful Linux CommandsUseful Linux Commands
Useful Linux CommandsDeepak Modak
 

What's hot (19)

Linux commd
Linux commdLinux commd
Linux commd
 
Linux commd
Linux commdLinux commd
Linux commd
 
Python 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation CheatsheetPython 3.x File Object Manipulation Cheatsheet
Python 3.x File Object Manipulation Cheatsheet
 
Registers
RegistersRegisters
Registers
 
Linux50commands
Linux50commandsLinux50commands
Linux50commands
 
ARM IMP
ARM IMPARM IMP
ARM IMP
 
Chapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix ConceptsChapter 2 Introduction to Unix Concepts
Chapter 2 Introduction to Unix Concepts
 
tac, command in Linux with example
tac, command in Linux with exampletac, command in Linux with example
tac, command in Linux with example
 
Chapter 3 Using Unix Commands
Chapter 3 Using Unix CommandsChapter 3 Using Unix Commands
Chapter 3 Using Unix Commands
 
Chapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsChapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System Concepts
 
Linux basic commands_part1
Linux basic commands_part1Linux basic commands_part1
Linux basic commands_part1
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 Introduction
 
Time space trade off
Time space trade offTime space trade off
Time space trade off
 
Linux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regxLinux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regx
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Writing a Simple OS for Fun
Writing a Simple OS for FunWriting a Simple OS for Fun
Writing a Simple OS for Fun
 
Code gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introductionCode gpu with cuda - CUDA introduction
Code gpu with cuda - CUDA introduction
 
Useful Linux Commands
Useful Linux CommandsUseful Linux Commands
Useful Linux Commands
 

Viewers also liked

Chapter 4 1
Chapter 4 1Chapter 4 1
Chapter 4 1lopjuan
 
Unix files
Unix filesUnix files
Unix filesSunil Rm
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
10 point sukses kls 8
10 point sukses kls 810 point sukses kls 8
10 point sukses kls 8rumiraia
 
The unix file system
The unix file systemThe unix file system
The unix file systemgsandeepmenon
 
Unix file systems 2 in unix internal systems
Unix file systems 2 in unix internal systems Unix file systems 2 in unix internal systems
Unix file systems 2 in unix internal systems senthilamul
 
Java applets
Java appletsJava applets
Java appletslopjuan
 

Viewers also liked (12)

Chapter 4 1
Chapter 4 1Chapter 4 1
Chapter 4 1
 
Unix files
Unix filesUnix files
Unix files
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
CSV File Manipulation
CSV File ManipulationCSV File Manipulation
CSV File Manipulation
 
10 point sukses kls 8
10 point sukses kls 810 point sukses kls 8
10 point sukses kls 8
 
Kernal
KernalKernal
Kernal
 
The unix file system
The unix file systemThe unix file system
The unix file system
 
Unix file systems 2 in unix internal systems
Unix file systems 2 in unix internal systems Unix file systems 2 in unix internal systems
Unix file systems 2 in unix internal systems
 
Unix File System
Unix File SystemUnix File System
Unix File System
 
Kernal
KernalKernal
Kernal
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Java applets
Java appletsJava applets
Java applets
 

Similar to Chapter 2

Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式艾鍗科技
 
Read write program
Read write programRead write program
Read write programAMI AMITO
 
IN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfIN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfaratextails30
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)mrecedu
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O YourHelper1
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)leonard horobet-stoian
 
18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptxChenamPawan
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Dushmanta Nath
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 

Similar to Chapter 2 (20)

Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
Read write program
Read write programRead write program
Read write program
 
file.ppt
file.pptfile.ppt
file.ppt
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
IN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfIN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdf
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
 
slides3_077.ppt
slides3_077.pptslides3_077.ppt
slides3_077.ppt
 
File management
File managementFile management
File management
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)C library for input output operations.cstdio.(stdio.h)
C library for input output operations.cstdio.(stdio.h)
 
18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx18CS56-UP-Module 3.pptx
18CS56-UP-Module 3.pptx
 
C Language Unit-5
C Language Unit-5C Language Unit-5
C Language Unit-5
 
File management in C++
File management in C++File management in C++
File management in C++
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 

More from lopjuan

Chapter 3
Chapter 3Chapter 3
Chapter 3lopjuan
 
Chapter 4 2
Chapter 4 2Chapter 4 2
Chapter 4 2lopjuan
 
Chapter 1
Chapter 1Chapter 1
Chapter 1lopjuan
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Chapter10
Chapter10Chapter10
Chapter10lopjuan
 
Web services
Web servicesWeb services
Web serviceslopjuan
 
Chapter7
Chapter7Chapter7
Chapter7lopjuan
 

More from lopjuan (7)

Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 4 2
Chapter 4 2Chapter 4 2
Chapter 4 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Java servlets
Java servletsJava servlets
Java servlets
 
Chapter10
Chapter10Chapter10
Chapter10
 
Web services
Web servicesWeb services
Web services
 
Chapter7
Chapter7Chapter7
Chapter7
 

Chapter 2

  • 1. Chapter 2 Users, Files, and the Manual
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. Low Level Read ssize_t read(intfd, void *buf, size_t count); read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
  • 13. Low Level Open open and possibly create a file or device #include <fcntl.h> int open(const char *pathname, mode_t mode); Given a pathname for a file, open() returns a file descriptor, a small, non-negative integer for use in subsequent system calls (read(2), write(2), lseek(2), fcntl(2), etc.).
  • 14.
  • 15.
  • 16. Suppressing Blank Records Remove unused terminal lines  blank user name Locate the file /usr/include/bits/utmp.h Only print out our LOGIN process USER_PROCESS 7
  • 17. Time format Unix stores times as the number of seconds since midnight 1/1/1970, G.M.T. time_t data type is an integer. ctime #include <time.h> char *ctime(const time_t*timep); The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calendar time t into a null-terminated string of the form "Wed Jun 30 21:49:08 1993"
  • 18. Time format “Wed Jun 30 21:49:08 1993” Printf(“12.12s”, ctime(&t) + 4) Position 4
  • 19. perror perror - print a system error message #include <stdio.h> Void perror(const char *s); DESCRIPTION The routine perror() produces a message on the standard error output, describing the last error encountered during a call to a system or library function. First (if s is not NULL and *s is not a null byte ('')) the argument string s is printed, followed by a colon and a blank. Then the message and a new-line.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30. Leek() Base: SEEK_SET start of file SEEK_CUR current position SEEK_END end of file Example: Lseek(fd, 0 SEEK_END); Write(fd, “hello”, strlen(“hello”); Sets the current position to the end of file and writes a string there.
  • 31. Leek() Example: Lseek(fd, -(sizeof(structutmp)), SEEK_CUR); Moves to the current position a distance of sizeof(structutmp)bytes before the current position.