CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

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

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

UNIX System Calls. Sys Calls versus Library Func

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

Building blocks for Unix power tools

Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Lecture files in /home/hwang/cs375/lecture05 on csserver.

THREADS. Jo, Heeseung

LSN 13 Linux Concurrency Mechanisms

4. System Functions and Subroutines

Files and Directories

CptS 360 (System Programming) Unit 6: Files and Directories

Files and Directories Filesystems from a user s perspective

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

OPERATING SYSTEMS: Lesson 2: Operating System Services

CSC 271 Software I: Utilities and Internals

Unix System Programming - Chapter 8

Lecture 23: System-Level I/O

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Operating System Labs. Yuanbin Wu

CS , Spring Sample Exam 3

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

CS 326: Operating Systems. Process Execution. Lecture 5

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Computer Systems Laboratory Sungkyunkwan University

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Processes. Processes (cont d)

Shell Execution of Programs. Process Groups, Session and Signals 1

Parents and Children

Introduction to PThreads and Basic Synchronization

CS2028 -UNIX INTERNALS

which maintain a name to inode mapping which is convenient for people to use. All le objects are

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

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

File I/0. Advanced Programming in the UNIX Environment

UNIT I INTRODUCTION TO UNIX & FILE SYSTEM

Logical disks. Bach 2.2.1

Lecture 21 Systems Programming in C

Design Overview of the FreeBSD Kernel CIS 657

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Linux is obsolete 2.0

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

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

RTEMS POSIX API User s Guide

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

File and Directories. Advanced Programming in the UNIX Environment

Advanced Programming in the UNIX Environment W. Richard Stevens

Operating System Labs. Yuanbin Wu

Process Creation in UNIX

Department of Computer Science and Technology, UTU 2014

File Descriptors and Piping

Threads need to synchronize their activities to effectively interact. This includes:

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

Process Management! Goals of this Lecture!

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

POSIX PTHREADS PROGRAMMING

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

pthreads CS449 Fall 2017

All the scoring jobs will be done by script

Operating Systems. Lecture 05

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

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

ANSI/IEEE POSIX Standard Thread management

File Systems. q Files and directories q Sharing and protection q File & directory implementation

Shared Memory Memory mapped files

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

11/3/71 SYS BREAK (II)

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

Process Management! Goals of this Lecture!

Process Management 1

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

Noorul Islam College Of Engineering, Kumaracoil MCA Degree Model Examination (October 2007) 5 th Semester MC1642 UNIX Internals 2 mark Questions

CS631 - Advanced Programming in the UNIX Environment

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

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

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

PROCESS CONTROL: PROCESS CREATION: UNIT-VI PROCESS CONTROL III-II R

UNIT I Linux Utilities

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

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

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Synchronization. Semaphores implementation

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

Outline. Relationship between file descriptors and open files

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

read(2) There can be several cases where read returns less than the number of bytes requested:

17: Filesystem Examples: CD-ROM, MS-DOS, Unix

All the scoring jobs will be done by script

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Naked C Lecture 6. File Operations and System Calls

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Transcription:

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions Described below is a subset of UNIX system calls, library functions, and Pthreads (that is, POSIX Threads) functions. It is not meant to be comprehensive, nor is it meant to describe every possible parameter and result in complete detail. It should, however, prove useful in preparing solutions to many of the assignments for the course. The form for the entries is similar to that used in the UNIX manual pages, but there is no explicit division of the information into sections. Instead most entries provide a synopsis of the item (including the header file, or files, that should be included), a brief description of its purpose, and an indication of the returned value and the most common errors that might be encountered. The entries are given in alphabetical order. Each of the Pthreads entries begins with pthread_. access int access (const char *path, int mode) alarm brk access checks to see if the file named by path can be accessed as indicated by mode, which is the bitwise-or of R_OK, W_OK, and/or X_OK (for read, write, and execute/search permission, respectively) or F_OK (for testing the file s existence only). 0 is returned if the specified access mode is permitted. access returns 1 and sets the global variable errno if an error is encountered. unsigned alarm (unsigned secs) alarm sets a timer to deliver the SIGALRM signal to the calling process after secs seconds. The call supersedes any previous unexpired alarm in effect. Calling alarm with secs equal 0 cancels any pending alarm. alarm returns the amount of time, in seconds, remaining on the timer associated with a previous alarm call. If no alarm is currently set, the returned value is 0. This function is usually marked as obsolete, because the more modern setitimer system call can be used to achieve the a similar function. It can still be used, however. int brk (void *addr) brk changes the address associated with the end of the data segment for the calling process. This address, specified by the addr argument, is called the program break. Increasing the address causes memory to be allocated to the process, and decreasing the address deallocated memory. brk returns 0 on success. Applications rarely use brk directly, and instead depend on library mechanisms (like malloc in C and new in C++) to manage dynamically allocated memory. brk returns 1 and sets the global variable errno if an error is encountered.

chdir int chdir (const char *path) chdir changes the path to the working directory for the calling process to path, which must be the pathname of a directory. The working directory is used as the starting point for path searches of pathnames not beginning with a slash. chdir returns 0 on success. chdir returns 1 and sets the global variable errno if an error is encountered. chmod #include <sys/types.h> #include <sys/stat.h> int chmod (const char *path, mode_t mode) chmod changes the mode of the existing file at path to mode. Only the owner of the file or the super-user can change the mode of a file. The mode can be specified symbolically (see the full documentation for all the names that can be used), or specified as a numeric argument, for which the 12 low-order bits are significant. The low-order 9 bits are treated as 3 groups of 3 bits each giving the read, write, and execute/search permission for the owner/user, the group owner, and all others, respectively. Thus 0742 would specify read/write/execute for the owner, read for the group, and write for all others. The next higher 3 bits specify the ser user ID, set group ID, and the sticky bit, respectively. See the full documentation for details on these bits. chmod returns 0 on success. chmod returns 1 and sets the global variable errno if an error is encountered. chown int chown (const char *path, uid_t owner, gid_t group) chown changes the owner and group associated with the file at path to owner and group, respectively. The current owner of the file can change the group ownership to that of a group of which he or she is a member. But only the super-user can change the owner of an existing file. chown returns 0 on success. chown returns 1 and sets the global variable errno if an error is encountered. chroot int chroot (const char *dirname) chroot causes the directory named by dirname to become the root directory that is, the starting point for path searches of pathnames beginning with '/'. chroot returns 0 on success. Its use is restricted to the super-user. chroot returns 1 and sets the global variable errno if an error is encountered.

close creat dup dup2 int close (int fd) close deletes one reference to the file that is accessed through the file descriptor fd. If this is the last reference to that instance of an open file, then the file pointer s value is also lost. Close returns 0 on success. All open files are closed when a process terminates. close returns 1 and sets the global variable errno if an error is encountered. #include <fcntl.h> int creat (const char *path, mode_t mode) creat is an early system call used to create a new file with the name given by path, or truncate an existing file with that name to zero length, set the protection mode for the file, and open it for writing only. It yields the same effects as open(path, O_CREAT O_TRUNC O_WRONLY, mode); creat returns 1 and sets the global variable errno if an error is encountered. int dup (int fd) dup arranges for an additional file descriptor (which is indicated by the returned value) to reference the same file as that currently referenced by the file descriptor fd. The value returned will be the smallest unused file descriptor (that is, it does not correspond to any open file). The new file descriptor references the same instance of the open file that fd references. In particular, this means both the new and the old file descriptors share the same file pointer. dup returns 1 and sets the global variable errno if an error is encountered. int dup2 (int oldfd, int newfd) dup2 arranges for the file descriptor with the value newfd to reference the same file as that currently referenced by the file descriptor oldfd. Newfd references the same instance of the open file that oldfd references. In particular, this means both newfd and oldfd share the same file pointer. dup2 returns 0 on success. dup2 returns 1 and sets the global variable errno if an error is encountered. execve int execve (char *filename, char *argv[], char *envp) execve executes the program identified by filename, which must be either a binary executable file or an interpreter script (not covered here). The argv argument is a pointer to an array of character strings that is passed to the main function (at least for programs written in C) of the program. These strings effectively represent the items that would appear on the command line if the program had been invoked by a typical shell.

exit _exit fcntl fork The last entry in the array must be a null pointer, which allows determination of the number of arguments (which is provided as an int argument to the main function). The envp argument is also a pointer to an array of character strings (usually of the form "key=value") that is also passed to the main function of the program. execve does not return if it is successful, since all of the memory belonging to the program executing execve is replaced by the new program s memory content. Execve returns 1 and sets the global variable errno if an error is encountered. #include <stdlib.h> void exit (int status) exit is a library function in the standard C library that, as its last action, invokes the _exit system call, which then terminates the calling process. The exit function does a few things before calling _exit. In particular, it arranges for all of the standard input/output streams to be flushed and closed. It does not return to calling program. void _exit (int status) _exit closes any open file descriptors and terminates the calling process. Note that buffered output from open output streams (such as those used by the I/O libraries in highlevel languages) will not be flushed/written, so output may be lost. Additionally, any existing child processes of the calling process are made wards of the state and are inherited by the process with pid 1. Application programs almost always will more appropriately use the exit function rather than the _exit system call. #include <fcntl.h> int fcntl (int fd, int cmd,...) fcntl is used to effect actions on file descriptors other than things like read, write, and close. The file descriptor to be affected is identified by fd, and the command to be performed is specified by cmd. If cmd requires one or more operands, they are specified after the cmd argument. See the detailed documentation for more information on fcntl. int fork (void) fork creates an exact duplicate of the calling process, with the following exceptions. The new process (called the child ) has a unique process ID. The new process has a different parent process ID. The new process has its own copy of each of the parent s file descriptors. These descriptors reference the same underlying objects as the parent. For example, both the child and its parent share the same file pointer, so lseek in the child affects the file position used by the parent. (This isn t always true for all file descriptors; see the full description of fork for exceptions.)

fstat Fork effectively returns twice if it is successful, once in the parent and once in the child. In the parent, fork returns the process ID of the newly created child process. In the child, fork returns 0. Fork is frequently followed by a call to one of the exec family of system calls (e.g. execve) in the child process. Fork returns 1 in the parent and sets the global variable errno if an error is encountered. #include <sys/types.h> #include <sys/stat.h> int fstat (int fd, struct stat *sb) Fstat obtains information about the open file identified by fd. The sb argument is a pointer to a structure with at least the following components: st_dev st_ino st_mode st_nlink st_uid st_gid st_rdev st_size getgid pid_t getgid (void) device associated with the file s inode file s inode number on the device file s protection mode number of hard links to the file UID of the file s owner GID of the file s owner device type file size, in bytes getgid returns the real group ID of the calling process; there is no error return. The real group ID is that of the user who invoked the program. A program with the setgroup-id mode bit set may execute with a different group ID; that is called the effective group ID and is obtained by invoking the getegid system call. getpid #include <sys/types.h> pid_t getpid (void) getpid returns the process ID of the calling process; there is no error return. getpgrp pid_t getpgrp (void) getpgrp returns the process group ID of the calling process; there is no error return. A process group is used for the distribution of signals, and by terminals to arbitrate requests for their input.

getuid pid_t getuid (void) ioctl kill link getuid returns the real user ID of the calling process; there is no error return. The real user ID is that of the user who invoked the program. A program with the setuser-id mode bit set may execute with a different user ID; that is called the effective user ID and is obtained by invoking the geteuid system call. #include <sys/ioctl.h> int ioctl (int fd, unsigned long request,...) ioctl is used to manipulate the device parameters of special files (like terminals). The particular device to be affected is identified by the open file descriptor fd. The particular request to be performed is specified by request. Any additional information needed by the request will be specified in parameters following request. See the detailed documentation for more information on ioctl. #include <signal.h> int kill (pid_t pid, int sig) kill sends the signal sig to the process or group of processes identified by pid. Normally sig will be one of the signals that could be specified in the sigaction system call, but it can also be 0 which sends no signal, but can be used to verify the validity of pid. If pid is greater than 0, then it identifies a single process. If pid is 0, the signal is sent to all processes whose group ID is the same as the sending process. On success, kill returns 0. kill returns 1 and sets the global variable errno if an error is encountered. See the detailed documentation for more information on kill. int link (const char *path1, const char *path2) link atomically creates a new directory entry as specified by path2 with the attributes of the object pointed to by path1. That is, after successful execution of link, path1 and path2 refer to the same object, which has its link (reference) count incremented. Both path1 and path2 must be in the same file system, and path1 must previously exist. On success, link returns 0. link returns 1 and sets the global variable errno if an error is encountered.

lseek mkdir long lseek (int fd, long offset, int whence) lseek modifies the file position associated with the open file specified by fd. The file position is set to that specified by the whence parameter plus the value specified by the offset parameter. The possible values on whence are: SEEK_CUR SEEK_SET SEEK_END the offset is from the current file position the offset is from the beginning of the file the offset is from the end of the file If the file position is set beyond the end of file, and the file is written, an unwritten gap is created. When bytes in this unwritten gap are read, their values are effectively zero. Upon successful completion, lseek returns a long integer indicating the new setting of the file pointer. On devices incapable of seeking (i.e. a terminal), this position is undefined. Lseek returns 1 and sets the global variable errno if an error is encountered. #include <sys/stat.h> int mkdir (const char *path, mode_t mode) mkdir creates a new directory as specified by path with the access permissions specified by mode and restricted by the umask of the calling process. On success, mkdir returns 0. mkdir returns 1 and sets the global variable errno if an error is encountered. mknod #include <sys/stat.h> int mkdir (const char *path, mode_t mode, dev_t dev) mknod creates a new special file as specified by path with the major and minor device numbers extracted from mode. The access permissions of the new special file are constrained by the umask of the calling process. The dev argument identifies a configuration-dependent specification of a character or block I/O device if mode specifies that path is to be a character or block special file. Otherwise dev is ignored. The use of mknod is restricted to the super-user. On success, mknod returns 0. mknod returns 1 and sets the global variable errno if an error is encountered. mount #include <sys/param.h> #include <sys/mount.h> int mount (const char *type, const char *dir, int flags, void *data) mount grafts a filesystem object onto the system file tree at the point specified by dir. The type argument indicates how to interpret the data argument, which describes the

open filesystem object to be mounted. The flags argument is used to modify default semantics which affect filesystem access. After the filesystem object is successfully mounted on dir, any files in dir are hidden and are unavailable until the filesystem object is unmounted. On success, mount returns 0. mount returns 1 and sets the global variable errno if an error is encountered. See the detailed documentation for more information on mount. #include <fcntl.h> int open (char *path, int flags [, mode]) Open prepares the file specified by path for reading and/or writing as specified by flags and possibly mode. The flags may be specified using a bitwise or of one or more of the following: O_RDONLY O_WRONLY O_RDWR O_CREAT open for reading only open for writing only open for reading and writing create file if it does not exist O_TRUNC truncate size to 0 (and others not discussed here). If the O_CREAT flag is given and the file does not exist, the new file is given the specified mode (as described for the chmod system call), as modified by the process current umask. Upon successful completion, open returns a small integer called a file descriptor. This value is then used in other input/output system calls to reference the open file. Open also causes the file pointer (which marks the byte offset in the file where the next read or write system call will begin) to be set to zero. Open returns 1 and sets the global variable errno if an error is encountered. Common errors include attempting to open a file that doesn t exist, attempting to open a file without authority, attempting to open a directory for writing, and attempting to open a read-only file for writing. Each system also has a limit on the number of files that may be simultaneously open; exceeding this limit will also result in an error return from open. pause int pause (void) pause causes the calling process (or thread) to block (sleep) until a signal is delivered that either terminates the process or causes the invocation of a signal-handling function. The pause system call returns only when a signal is caught and the signal-handling function returns. In this case, pause returns -1, and errno is set to EINTR.

pipe int pipe (int *fd2) Pipe creates a pair of file descriptors which it stores in fd2[0] and fd2[1]. These file descriptors are effectively associated with the ends of an in-memory queue, so that data written into the pipe can later be read from it. fd2[0] is traditionally associated with the end of the pipe which can be read, and fd2[1] with the end of the pipe which can be written. The pipe persists until the file descriptors associated with each end are closed. End of file on a pipe is indicated when read returns 0. This can only occur when all file descriptors associated with the write end of the pipe have been closed. (Older UNIX systems used exclusively in-memory data structures for pipes, and thus a write operation could block if the maximum size of the queue was exceeded. Newer systems use different data structures that preclude this behavior.) Pipe returns 1 and sets the global variable errno if an error is encountered. It returns 0 on successful creation of the pipe. ptrace #include <sys/ptrace.h> long ptrace (enum_ptrace_request request, pid_t pid, void *addr, void *data) ptrace provides a facility to allow one process (the tracer ) to observe and control the execution of another process (the tracee ). The tracer may examine and change the tracee s memory and registers. Debuggers are the primary users of this system call. Details of the ptrace system call are found in the appropriate manual pages. pthread_cond_broadcast int pthread_cond_broadcast (pthread_cond_t *cond) All threads waiting for the condition variable cond are unblocked. If pthread_cond_broadcast is successful, it returns 0. Otherwise it returns a nonzero error code to indicate the error. pthread_cond_destroy int pthread_cond_destroy (pthread_cond_t *cond) All threads waiting for the condition variable cond are unblocked. If pthread_cond_broadcast is successful, it returns 0. Otherwise it returns a nonzero error code to indicate the error. pthread_cond_init int pthread_cond_init (pthread_cond_t *cond, const pthread_condattr_t *attr) A new condition variable is created with attributes as specified by the attr argument. NULL can be used for the attr value to obtain default attributes. If successful, the new condition variable ID will be stored at cond.

If pthread_cond_init is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_cond_signal int pthread_cond_signal (pthread_cond_t *cond) One thread waiting for the condition variable cond is unblocked. If pthread_cond_signal is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_cond_wait int pthread_cond_init (pthread_cond_t *cond, pthread_mutex_t *mutex) The calling thread will be atomically blocked to wait for the specified condition variable cond to be signaled or broadcasted, and releases the lock on mutex (which should be locked before calling this function). When the waiting thread is unblocked, it will reacquire the lock on mutex. If pthread_cond_wait is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_create int pthread_create (pthread_t *thread, const pthread_attr_t attr, void *(*start_routine)(void *), void *arg) pthread_create creates a new thread. thread is set to the ID of the created thread upon successful completion of the creation. attr identifies the attributes for the new thread; using NULL for attr yields the default attributes for the new thread, which are often appropriate. start_routine is the address of the function with which the thread will begin execution; normally this is just the name of a function. Note that this function must have a void * argument and return a void * result. Finally, arg is a pointer to an arbitrary (void) data item; it is passed to the thread s start routine. If the function identified by start_routine returns, the effect is as if there was an implicit call to pthread_exit using the return value of start_routine as the exit status. If pthread_create is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. See the detailed documentation for more information on pthread_create.

pthread_exit void pthread_exit (void *value_ptr) The calling thread is terminated, and returns value_ptr to a thread that has blocked waiting for this thread to terminate. pthread_join int pthread_join (pthread_t *thread, void **value_ptr) After a successful call to pthread_join, the calling thread s execution is suspended (blocked) until the thread specified by thread terminates. If value_ptr is not NULL, then the value returned by pthread_exit is stored there. If pthread_join is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_mutex_destroy int pthread_mutex_destroy (pthread_mutex_t *mutex) This function destroys (reclaims) the mutex identified by mutex. It is inappropriate to destroy a locked mutex If pthread_mutex_destroy is successful, it returns 0. Otherwise it returns a nonzero error code to indicate the error. pthread_mutex_init int pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) A new mutex is created with attributes as specified by the attr argument. NULL can be used for the attr value to obtain default attributes. If successful, the new mutex ID will be stored at mutex. If pthread_mutex_init is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_mutex_lock int pthread_mutex_lock (pthread_mutex_t *mutex) This function locks the unlocked mutex identified by mutex. If the mutex is already locked, the calling thread blocks until the mutex becomes available. If a recursive mutex is specified, the current owner can lock it again without being blocked. The lock count is incremented in this case. If pthread_mutex_lock is successful, it returns 0. Otherwise it returns a non-zero error code to indicate the error.

pthread_mutex_trylock int pthread_mutex_trylock (pthread_mutex_t *mutex) This function attempts to lock the mutex identified by mutex. If that mutex is already locked, the calling thread does not block, but will instead receive the error code EBUSY. If pthread_mutex_trylock is successful in its attempt to lock mutex, it returns 0. Otherwise it returns a non-zero error code to indicate the error. pthread_mutex_unlock int pthread_mutex_unlock (pthread_mutex_t *mutex) read This function attempts to unlock the mutex identified by mutex. If pthread_mutex_unlock is successful, it returns 0. Otherwise it returns a nonzero error code to indicate the error. #include <sys/types.h> #include <sys/uio.h> int read (int fd, void *buf, int nbytes) Read attempts to read nbytes bytes of data from the file referenced by the file descriptor fd into the buffer pointed to by buf. On devices capable of seeking (i.e. a disk file, but not a terminal), read begins transferring data from the location associated with the pointer associated with fd; upon completion of read, the pointer is incremented by the number of bytes actually read. On devices not capable of seeking, read always begins at the current file position. Upon successful completion, read returns the number of bytes actually read and placed in the buffer. If a disk file with sufficient data remaining is read, then the number of bytes read will be the number of bytes requested. If insufficient data is available, then the count of bytes read will be smaller than the number of bytes requested. In particular, reading from a terminal in normal (that is, cooked) mode will return no more than the number of bytes remaining in the current input line. When reading a disk file, a returned value less than nbytes indicates the end of file has been reached. When reading a terminal in normal mode, the end of file is indicated by a returned value of 0. Read returns 1 and sets the global variable errno if an error is encountered. In particular, a bad value for fd will result in an error. rename int rename (char *old, char *new) Rename changes the name of the file with the path old to the path new. Old and new must be of the same type (i.e. regular files or directories) and must reside on the same file system. rename returns 1 and sets the global variable errno if an error is encountered. It returns 0 on success.

rmdir sbrk int rmdir (const char *path) rmdir removes a directory whose name is given by path. The directory must not have any entries other than '.' and '..'. rmdir returns 0 on success. rmdir returns 1 and sets the global variable errno if an error is encountered void *sbrk (intptr_t increment) sbrk adds increment to the address associated with the program break, which is the first address beyond the end of the data region of the process. Calling sbrk with an increment of 0 can be used to find the address currently associated with the program break, as sbrk returns the prior address associated with the program break. The value of increment can be negative to return storage to the system. Applications rarely use sbrk directly, and instead depend on library mechanisms (like malloc in C and new in C++) to manage dynamically allocated memory. sbrk returns (void *) 1 and sets the global variable errno if an error is encountered. setgid int setgid (gid_t gid) setgid sets the real and effective group IDs of the current process to gid. It succeeds and returns 0 if the current effective ID is that of the super-user, or if the specified group ID gid is the same as the real group ID. setgid returns 1 and sets the global variable errno if an error is encountered. setsid pid_t setsid (void) setsid creates a new session if the calling process is not a process group leader. The calling process becomes the leader of the new session and the process group leader of the new process group; it has no controlling terminal. The process group ID and session ID of the calling process are set to the PID of the calling process. The calling process will be the only process in the new process group and the new session. On success, the new session ID of the calling process is returned. setsid returns (pid_t) 1 and sets the global variable errno if an error is encountered. setuid int setuid (uid_t uid) setuid sets the real and effective user IDs of the current process to uid. It succeeds and returns 0 if the current effective user ID is that of the super-user, or if the specified user ID uid is the same as the real user ID.

setuid returns 1 and sets the global variable errno if an error is encountered. sigaction #include <signal.h> int sigaction (int signum, struct sigaction *act, struct sigaction *oldact) sigaction is used to change the action taken by a process on receipt of a specific signal. The signum argument identifies the signal for which the action is to be taken; it can be any valid signal except sigkill and sigstop. If act is not NULL, then it specifies an action (SIG_DFL, SIG_IGN, or the signal handling function s address) and a signal mask to be used when delivering the specified signal. If oldact is not NULL, information about how the signal was previously handled is stored in the structure. See the detailed documentation for more information on sigaction. sigpending #include <signal.h> int sigpending (sigset_t *set) sigpending stores a mask of the signals pending delivery for the calling process at set. Signals may be pending delivery because they are currently masked. If the call succeeds, 0 is returned. sigpending returns 1 and sets the global variable errno if an error is encountered. sigprocmask #include <signal.h> int sigprocmask (int how, const sigset_t *set, sigset_t *old) sigprocmask examines and/or changes the current signal mask for the process. The signal mask specifies those signals that are blocked from delivery to the process. If the set argument is not NULL, the how parameter determines what the system call does. If how is SIG_BLOCK, then the new signal mask is the union of the current signal mask and set. If how is SIG_UNBLOCK, then the new signal mask is the intersection of the current signal mask and the complement of set. If how is SIG_SETMASK, then the new signal mask is replaced by set. The old signal mask for the process is saved at old if it is not NULL, regardless of the value of how. The signals SIGKILL and SIGSTOP cannot be blocked. sigprocmask returns 0 on success. sigprocmask returns 1 and sets the global variable errno if an error is encountered.

sigreturn int sigreturn (...) sigreturn is not intended to be called directly from a user process. Instead, it is called at the end of execution of a user s signal handling function. Its purpose is to restore the context that existed at the time the signal was delivered to the process. It is implementation dependent. sigsuspend #include <signal.h> int sigsuspend (const sigset_t *mask) stat stime sync sigsuspend temporarily replaces the signal mask for the process with mask, and then suspends (blocks) the calling process until one of the unmasked signals is delivered to the process and the appropriate signal handler is invoked, or a signal delivery terminates the process. If a signal delivery terminates the process, then sigsuspend does not return. If a signal handler is invoked, then when it returns, the signal mask for the process is restored to the value it had before sigsuspend was invoked. The signals SIGKILL and SIGSTOP cannot be blocked; their inclusion in mask will be ignored. sigsuspend returns 0 on success. sigsuspend returns 1 and sets the global variable errno if an error is encountered. #include <sys/types.h> #include <sys/stat.h> int stat (char *path, struct stat *sb) stat obtains information about the file pointed to by path. See the description of fstat for details. #include <time.h> int stime (const time_t *t) stime can only be successfully called by the super-user. In that case, the system s idea of the date and time is set from the value of t, which is the number of seconds since midnight UTC on January 1, 1970 (which is known in the UNIX world as the Epoch). stime returns 0 on success. stime returns 1 and sets the global variable errno if an error is encountered. void sync (void) sync causes all buffered modifications to file metadata and data to be queued for writing to the underlying file systems. It is always successful. Note that the write operations will likely not have completed when the system call returns. Some current systems (e.g. Linux) do wait until the write operations have been completed before returning. This still

time times does not guarantee the modified metadata and data have been successfully written to disk, because of the caching operations common in modern disk drives. #include <time.h> time_t time (time_t *tloc) time returns the system s idea of the date and time as the number of seconds past the Epoch (see stime). If tloc is not NULL, the value is also stored there. time returns 1 and sets the global variable errno if an error is encountered. #include <sys/times.h> clock_t times (struct tms *buf) times stores the process use times in the members of the structure pointed to by buf. There are at least four members, each with type clock_t: tms_utime is the time spent in user mode executing instructions of the calling process. tms_stime is the time spent in kernel mode executing actions on behalf of the calling process (typically system calls). tms_cutime and tms_cstime are the same as tms_utime and tms_stime, except they specify times for the child processes on which the current process has successfully waited, including the descendants of those child processes. times returns the number of clock ticks that have elapsed since an arbitrary point in the past. It returns 1 and sets the global variable errno if an error is encountered. The number of clock ticks per second can be determined by calling sysconf(_sc_clk_tck). This function returns a long and the unistd.h header file should be included if it is used. umask #include <sys/types.h> #include <sys/stat.h> mode_t umask (mode_t mask) umask sets the file mode creation mask (called umask) for the process to mask & 0777 (that is, only the low-order 9 bits of mask are used). The previous value of the umask is returned; there is no error return possible. Various system calls (including open and mkdir) use umask to modify the permissions set on newly created files or directories. In particular, permissions in the umask are turned off (set to 0) in the mode used by these system calls. umount #include <sys/mount.h> int umount (const char *target) umount unmounts the uppermost (last) file system mounted on the directory specified by target. It returns 0 if successful. umount returns 1 and sets the global variable errno if an error is encountered.

unlink int unlink (const char *path) utime wait unlink removes a link to the file named by path and reduces the link (reference) count for the file object. If the link count is reduced to 0, then if no processes have the file open, all resources associated with the file are reclaimed. If one or more processes have the file open at the time unlink is executed and the link count is reduced to 0, the resource reclamation is deferred until such time as the processes have each closed the file. unlink returns 0 on success. unlink returns 1 and sets the global variable errno if an error is encountered. #include <sys/types.h> #include <utime.h> int utime (const char *path, const struct utimbuf *times) utime changes the access and modification times of the inode associated with the object identified by path to those in the actime and modtime members of the structure at times. See the full documentation for additional details. utime returns 0 on success. utime returns 1 and sets the global variable errno if an error is encountered. #include <sys/types.h> #include <sys/wait.h> int wait (int *status) wait suspends (blocks) execution of the calling process until any of its child processes terminates. status, if not null, point to a location where information about the terminated process is stored, specifically why the process terminated (normal termination or some other failure) and the least significant 8 bits of the argument to the exit system call or the value returned by a main function. See the full documentation for details on the arguments. On success, wait returns the process ID of the terminated child process. On error (e.g. there were no child processes), wait returns 1. waitpid #include <sys/types.h> #include <sys/wait.h> int waitpid (pid_t pid, int *status, int options) waitpid suspends (blocks) execution of the calling process until the child process specified by pid terminates. pid, when positive, identifies a specific process, but other values may be used. options is often 0. status, if not null, point to a location where information about the terminated process is stored, specifically why the process terminated (normal termination or some other failure) and the least significant 8 bits of the argument to the exit system call or the value returned by a errno function. See the full documentation for details on the arguments.

write int write (int fd, void *buf, int nbytes) Write attempts to write nbytes bytes of data to the file referenced by the file descriptor fd from the buffer pointed to by buf. On devices capable of seeking (e.g. a disk file, but not a terminal), write begins transferring data to the location associated with the pointer associated with fd; upon completion of write, the pointer is incremented by the number of bytes actually written. On devices not capable of seeking, write always begins at the current file position. Upon successful completion, write returns the number of bytes actually written. Write returns 1 and sets the global variable errno if an error is encountered. In particular, a bad value for fd will result in an error.