Operating Systems Cheat Sheet
Operating Systems Cheat Sheet
Operating Systems Cheat Sheet
#include<pthread.h>
pthread_t thread;
int ret = pthread_create(&thread, attr_usually_NULL, funcName, (void*)&arg);
/// One can allocate a thread_t[N] and use loops, identifying threads by index.
void* retVal;
pthread_join(thread, &retVal); //running thread wait for given thread’s termination
pthread_exit(0); // terminates thread
pthread_mutex_t mutex;
pthread_mutex_init(&lock, attr_usually_NULL); //initialized and unlocked
// ALT for static mutexs: pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
pthread_mutex_trylock(&mutex); //if locked, continue instead of sleep
pthread_cond_t condVar;
pthread_cond_init(&condVar, attr_usually_NULL);
pthread_cond_wait(&condVar, &mutex); //wait for a signal
pthread_cond_timedwait(&condVar, &mutex, &timeToWait);
pthread_cond_signal(&condVar); //restart a waiting thread
pthread_cond_broadcast(&condVar); //restart all waiting threads
/// APPENDIX 1: timedwait example /////////////////////////////////
struct timespec timeToWait; //
struct timeval now; //
int rt; //
gettimeofday(&now,NULL); //
timeToWait.tv_sec = now.tv_sec+5; //
timeToWait.tv_nsec = (now.tv_usec+1000UL*timeInMs)*1000UL; //
pthread_mutex_lock(&fakeMutex); //
rt = pthread_cond_timedwait(&fakeCond, &fakeMutex, &timeToWait); //
pthread_mutex_unlock(&fakeMutex); //
printf("\nDone\n"); //
/// APPENDIX 1 END ////////////////////////////////////////////////
#include<semaphore.h>
sem_t sem;
sem_init(&sem, isShared_int, initValue_int);
sem_wait(&sem); //decrements the semaphore’s value by one if it’s positive.
//o.w. blocking until value is increased or the call is interrupted by a signal.
sem_post(&sem); //increments semaphore’s value. If it was zero,
//allow a blocking thread to return from the sem_wait(&sem) call.
sem_trywait(&sem); //isn’t blocking even if semaphore wasn’t successfully locked.
System Calls
NAME
unistd.h - standard symbolic constants and types
SYNOPSIS
#include <unistd.h>
DESCRIPTION
The <unistd.h> header defines miscellaneous symbolic constants and types,
and declares miscellaneous functions. The contents of this header are
shown below.
Version Test Macros
The following symbolic constants are defined:
_POSIX_VERSION
Integer value indicating version of the ISO POSIX-1 standard (C
language binding).
_POSIX2_VERSION
Integer value indicating version of the ISO POSIX-2 standard
(Commands).
_POSIX2_C_VERSION
Integer value indicating version of the ISO POSIX-2 standard (C
language binding).
_XOPEN_VERSION
Integer value indicating version of the X/Open Portability Guide to
which the implementation conforms.
_POSIX_VERSION is defined in the ISO POSIX-1 standard. It changes with
each new version of the ISO POSIX-1 standard.
_POSIX2_VERSION is defined to have the value of the ISO POSIX-2
standard's POSIX2_VERSION limit. It changes with each new version of the
ISO POSIX-2 standard.
_POSIX2_C_VERSION is defined in the ISO POSIX-2 standard. It changes with
each new version of the ISO POSIX-2 standard. When the C language binding
option of the ISO POSIX-2 standard and therefore the X/Open POSIX2 C-
language Binding Feature Group is not supported, _POSIX2_C_VERSION will
be set to -1.
_XOPEN_VERSION is defined as an integer value equal to 500.
_XOPEN_XCU_VERSION is defined as an integer value indicating the version
of the XCU specification to which the implementation conforms. If the
value is -1, no commands and utilities are provided on the
implementation. If the value is greater than or equal to 4, the
functionality associated with the following symbols is also supported
(see Mandatory Symbolic Constants and Constants for Options and Feature
Groups ):
_POSIX2_C_BIND
_POSIX2_C_VERSION
_POSIX2_CHAR_TERM
_POSIX2_LOCALEDEF
_POSIX2_UPE
_POSIX2_VERSION
_POSIX_ASYNC_IO
_POSIX_PRIO_IO
_POSIX_SYNC_IO
Synchronised input or output operations may be performed for the
associated file.
NULL
Null pointer
The following symbolic constants are defined for the access() function:
R_OK
W_OK
X_OK
F_OK
The constants F_OK, R_OK, W_OK and X_OK and the expressions R_OK|W_OK,
R_OK|X_OK and R_OK|W_OK|X_OK all have distinct values.
The following symbolic constants are defined for the confstr() function:
_CS_PATH
If the ISO POSIX-2 standard is supported, this is the value for the
environment variable that finds all standard utilities. Otherwise
the meaning of this value is unspecified.
_CS_XBS5_ILP32_OFF32_CFLAGS
_CS_XBS5_ILP32_OFF32_LDFLAGS
_CS_XBS5_ILP32_OFF32_LINTFLAGS
_CS_XBS5_ILP32_OFFBIG_CFLAGS
_CS_XBS5_ILP32_OFFBIG_LDFLAGS
_CS_XBS5_ILP32_OFFBIG_LIBS
_CS_XBS5_ILP32_OFFBIG_LINTFLAGS
_CS_XBS5_LP64_OFF64_CFLAGS
_CS_XBS5_LP64_OFF64_LDFLAGS
If sysconf(_SC_XBS5_LP64_OFF64) returns -1, the meaning of this
value is unspecified. Otherwise, this value is the set of final
options to be given to the cc and c89 utilities to build an
application using a programming model with 64-bit int, long,
pointer, and off_t types.
_CS_XBS5_LP64_OFF64_LIBS
_CS_XBS5_LP64_OFF64_LINTFLAGS
_CS_XBS5_LPBIG_OFFBIG_CFLAGS
_CS_XBS5_LPBIG_OFFBIG_LDFLAGS
_CS_XBS5_LPBIG_OFFBIG_LIBS
_CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
The following symbolic constants are defined for the lseek() and fcntl()
functions (they have distinct values):
SEEK_SET
SEEK_CUR
SEEK_END
_SC_2_C_BIND
_SC_2_C_DEV
_SC_2_C_VERSION
_SC_2_FORT_DEV
_SC_2_FORT_RUN
_SC_2_LOCALEDEF
_SC_2_SW_DEV
_SC_2_UPE
_SC_2_VERSION
_SC_ARG_MAX
_SC_AIO_LISTIO_MAX
_SC_AIO_MAX
_SC_AIO_PRIO_DELTA_MAX
_SC_ASYNCHRONOUS_IO
_SC_ATEXIT_MAX
_SC_BC_BASE_MAX
_SC_BC_DIM_MAX
_SC_BC_SCALE_MAX
_SC_BC_STRING_MAX
_SC_CHILD_MAX
_SC_CLK_TCK
_SC_COLL_WEIGHTS_MAX
_SC_DELAYTIMER_MAX
_SC_EXPR_NEST_MAX
_SC_FSYNC
_SC_GETGR_R_SIZE_MAX
_SC_GETPW_R_SIZE_MAX
_SC_IOV_MAX
_SC_JOB_CONTROL
_SC_LINE_MAX
_SC_LOGIN_NAME_MAX
_SC_MAPPED_FILES
_SC_MEMLOCK
_SC_MEMLOCK_RANGE
_SC_MEMORY_PROTECTION
_SC_MESSAGE_PASSING
_SC_MQ_OPEN_MAX
_SC_MQ_PRIO_MAX
_SC_NGROUPS_MAX
_SC_OPEN_MAX
_SC_PAGESIZE
_SC_PAGE_SIZE
_SC_PASS_MAX (LEGACY)
_SC_PRIORITIZED_IO
_SC_PRIORITY_SCHEDULING
_SC_RE_DUP_MAX
_SC_REALTIME_SIGNALS
_SC_RTSIG_MAX
_SC_SAVED_IDS
_SC_SEMAPHORES
_SC_SEM_NSEMS_MAX
_SC_SEM_VALUE_MAX
_SC_SHARED_MEMORY_OBJECTS
_SC_SIGQUEUE_MAX
_SC_STREAM_MAX
_SC_SYNCHRONIZED_IO
_SC_THREADS
_SC_THREAD_ATTR_STACKADDR
_SC_THREAD_ATTR_STACKSIZE
_SC_THREAD_DESTRUCTOR_ITERATIONS
_SC_THREAD_KEYS_MAX
_SC_THREAD_PRIORITY_SCHEDULING
_SC_THREAD_PRIO_INHERIT
_SC_THREAD_PRIO_PROTECT
_SC_THREAD_PROCESS_SHARED
_SC_THREAD_SAFE_FUNCTIONS
_SC_THREAD_STACK_MIN
_SC_THREAD_THREADS_MAX
_SC_TIMERS
_SC_TIMER_MAX
_SC_TTY_NAME_MAX
_SC_TZNAME_MAX
_SC_VERSION
_SC_XOPEN_VERSION
_SC_XOPEN_CRYPT
_SC_XOPEN_ENH_I18N
_SC_XOPEN_SHM
_SC_XOPEN_UNIX
_SC_XOPEN_XCU_VERSION
_SC_XOPEN_LEGACY
_SC_XOPEN_REALTIME
_SC_XOPEN_REALTIME_THREADS
_SC_XBS5_ILP32_OFF32
_SC_XBS5_ILP32_OFFBIG
_SC_XBS5_LP64_OFF64
_SC_XBS5_LPBIG_OFFBIG
The following symbolic constants are defined as possible values for the
function argument to the lockf() function:
F_LOCK
F_ULOCK
F_TEST
_PC_ASYNC_IO
_PC_CHOWN_RESTRICTED
_PC_FILESIZEBITS
_PC_LINK_MAX
_PC_MAX_CANON
_PC_MAX_INPUT
_PC_NAME_MAX
_PC_NO_TRUNC
_PC_PATH_MAX
_PC_PIPE_BUF
_PC_PRIO_IO
_PC_SYNC_IO
_PC_VDISABLE
STDIN_FILENO
STDOUT_FILENO
STDERR_FILENO
Type Definitions
The size_t, ssize_t, uid_t, gid_t, off_t and pid_t types are defined as
described in <sys/types.h>.
Declarations
The following are declared as functions and may also be defined as
macros. Function prototypes must be provided for use with an ISO C
compiler.