CS330 Introduction to Operating Systems

Some UNIX System Calls / Library Functions

 

 

Page 1

Page 2

Page 3

Page 4

Page 5

Page 6

Page 7

perror

fork

getpid

getppid

 

getuid

chmod

chdir

getcwd

execl

execlp

exit

_exit

sleep

kill

wait

waitpid

signal

creat

open

close

read

write

 

link

unlink

stat

pipe

dup

dup2

brk

sbrk

 

 

 

Include Files(s)

 

<stdio.h>

Manual Section

3c

Summary

 

void perror( const char *s);

Purpose

Print an error message based on the value of errno.

Return

Success

Failure

Sets errno

 

 

 

 

 

Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

pid_t fork( void );

Purpose

Create a child process.

Return

Success

Failure

Sets errno

0 in child, child PID in parent

-1

Yes

 

 

Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

pid_t getpid( void );

Purpose

Determine the process identifier (pid) for the current process.

Return

Success

Failure

Sets errno

The process ID

-1

Yes

 

 

Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

pid_t getppid( void );

Purpose

Determine the process identifier of the parent process.

Return

Success

Failure

Sets errno

The parent process ID

-1

Yes

 

 


Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

uid_t getuid( void );

Purpose

Obtain the user’s identifier (uid).

Return

Success

Failure

Sets errno

The requested identifier

 

 

 

 


Include Files(s)

 

<sys/types.h>

<sys/stat.h>

Manual Section

2

Summary

 

int chmod( const char *path,

           mode_t mode );

Purpose

Change the access permissions of a file or directory.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

3c

Summary

 

int chdir(const char *path );

Purpose

Change the working directory of a process.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

extern char *getcwd( char *buf,

                     size_t size );

Purpose

Get the current working directory of a process and place it in buf, which contains size bytes.

Return

Success

Failure

Sets errno

A pointer to the current directory name

NULL

Yes

 

 

Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

int execl( const char *file_pathname,

           const char *arg0,

...        . . .

           const char *argn,

           char * /* NULL */ );

Purpose

Obtain code to execute from the executable file specified by a filename optionally preceded by a directory path.

Return

Success

Failure

Sets errno

Does not return

-1

Yes

 

Include Files(s)

 

<sys/types.h>

<unistd.h>

Manual Section

2

Summary

 

int execlp( const char *file,

           const char *arg0,

...        . . .

           const char *argn,

           char * /* NULL */ );

Purpose

Obtain code to execute from the executable file specified by the filename, searching the directories given in the PATH environment variable.

Return

Success

Failure

Sets errno

Does not return

-1

Yes

 

 

Include Files(s)

 

<stdlib.h>

Manual Section

3c

Summary

 

void exit( int status);

Purpose

Terminate the current process by executing the at exit functions, cleaning up standard input/output with _cleanup, and calling _exit to terminate the process.

Return

Success

Failure

Sets errno

No return

No return

 

 

 

Include Files(s)

 

<unistd.h>

Manual Section

2

Summary

 

void _exit( void );

Purpose

Terminate the current process.

Return

Success

Failure

Sets errno

Does not return

Does not return

 

 

 

Include Files(s)

 

<unistd.h>

Manual Section

3c

Summary

 

unsigned sleep( unsigned seconds );

Purpose

Suspend the current process for the specified number of seconds.

Return

Success

Failure

Sets errno

Amount of unslept time remaining

 

 

 

 

Include Files(s)

 

<sys/types.h>

<signal.h>

Manual Section

2

Summary

 

int kill( pid_t pid,

          int sig );

Purpose

Send a specified signal to a specified process.

Return

Success

Failure

Sets errno

0

-1

Yes

 


Include Files(s)

 

<sys/types.h>

<sys/wait.h>

Manual Section

2

Summary

 

pid_t wait( int *stat_loc );

Purpose

Wait for a signal; ordinarily this will be the signal from a terminating child process.

Return

Success

Failure

Sets errno

Child PID

-1

Yes

 

Value stored at stat_loc:

Child terminated normally:

 

byte 3

byte 2

byte 1

byte 0

 

 

exit code

0

 

Child terminated due to an uncaught signal:

 

byte 3

byte 2

byte 1

byte 0

 

 

0

signal #

 

 

Include Files(s)

 

<sys/types.h>

<sys/wait.h>

Manual Section

2

Summary

 

pid_t waitpid( pid_t pid,

               int *stat_loc,

               int options );

Purpose

Wait for a signal from a specified process or group of processes. Ordinarily this will be the signal from a terminating child. 

Return

Success

Failure

Sets errno

Child PID or 0

-1

Yes

 

 

Include Files(s)

 

<signal.h>

 

Manual Section

2

Summary

 

void *signal( int sig,

              void (*disp) (int))) (int);

Purpose

Set the entry in the signal handler table for a specified type of signal.

Return

Success

Failure

Sets errno

Signal’s previous disposition

SIG_ERR

(-1)

Yes

 

 

Include Files(s)

 

<sys/types.h>

<sys/stat.h>

<fcntl.h>

Manual Section

2

Summary

 

int creat ( const char *path,

            mode_t mode );

Purpose

Create a file and open it for writing.  The link count of the new file is set to 1.

Return

Success

Failure

Sets errno

Lowest available file descriptor.

-1

Yes

 


Include Files(s)

 

<sys/types.h>

<sys/stat.h>

<fcntl.h>

Manual Section

2

Summary

 

int open ( const char *path,

            int flags,

            /* mode_t mode */);

Purpose

Opens a file; sets the file status to oflags, and if the file is being created, sets the permission mode to mode.

Return

Success

Failure

Sets errno

Lowest available file descriptor

-1

Yes

 

     Some flags are O_RDONLY, O_WRONLY, O_RDWR, O_NDELAY, O_NONBLOCK,

     O_APPEND, O_CREAT, O_BINARY

 


Include Files(s)

 

<unistd.h>

Manual Section

2

Summary

 

int close ( int filedescr );

Purpose

Closes a file.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

Manual Section

2

Summary

 

int read ( int filedescr,

           char *buf,

           int numbytes );

Purpose

Read (at most) the specified number of bytes from the file identified by the file descriptor and store them at the address given by buf.

Return

Success

Failure

Sets errno

Number of characters read

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

Manual Section

2

Summary

 

int write ( int filedescr,

           char *buf,

           int numbytes );

Purpose

Write (at most) the specified number of bytes to the file identified by the file descriptor, obtaining them starting at the address given by buf.

Return

Success

Failure

Sets errno

Number of characters written

-1

Yes

 


 


Include Files(s)

 

<sys/types.h>

<sys/stat.h>

<fcntl.h>

Manual Section

2

Summary

 

int link ( const char *path1, const char *path2);

Purpose

Create a hard link from the existing file pointed to by path1 to the new name pointed to by path2.  The link count of the existing file is increased by one.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int unlink ( const char *path );

Purpose

Reduce the link count by one for the file specified by path; if the link count reaches zero, remove the file.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<sys/types.h>

<sys/stat.h>

Manual Section

2

Summary

 

int stat ( const char *path,

           structu stat *buf);

Purpose

Obtain file related information.

Return

Success

Failure

Sets errno

 

-1

Yes

 

 

Fields of the stat structure

Type

Field name

Purpose

dev_t

st_dev

/* device that file resides on */

ino_t

st_ino

/* this file’s number */

u_short

st_mode

/* protection */

short

st_nlink;

/* number of hard links to the file */

short

st_uid;

/* user ID of owner */

short

st_gid;

/* group ID of owner */

dev_t

st_rdev;

/* the device identifier (special files only) */

off_t

st_size;

/* total size of file, in bytes */

time_t

st_atime;

/* file data last access time */

time_t

st_mtime;

/* file data last modify time */

time_t

st_ctime;

/* file data last status change time */

long

st_blksize;

/* preferred blocksize for file system I/O */

long

st_blocks;

/* actual number of blocks allocated */

 


 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int pipe ( int filedescr[2]);

Purpose

Create an I/O mechanism called a pipe and sets filedescr[0] to the file descriptor for reading from this pipe and filedescr[1] to the file descriptor for writing to it.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int dup ( int filedescr);

Purpose

Duplicates a file descriptor in the next available position in the file descriptor table.

Return

Success

Failure

Sets errno

file descriptor (nonnegative)

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int dup2 ( int filedescr1, int filedescr2);

Purpose

Causes filedescr2 to refer to the same open file (or pipe, etc.) as filedescr1.

Return

Success

Failure

Sets errno

filedescr2

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int brk (void *endds);

Purpose

Sets the break value (address of  the first location beyond the end of the data segment) to endds and changes the allocated space accordingly.

Return

Success

Failure

Sets errno

0

-1

Yes

 

 


Include Files(s)

 

<unistd.h>

 

Manual Section

2

Summary

 

int sbrk (intptr_t incr);

Purpose

Adds incr function bytes to the break value and changes the allocated space accordingly. The incr function can be negative.

Return

Success

Failure

Sets errno

previous  break value  

(void *)-1

Yes