JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 3: Basic Library Functions     Oracle Solaris 11.1 Information Library
search filter icon
search icon

Document Information

Preface

Basic Library Functions

a64l(3C)

abort(3C)

abs(3C)

addrtosymstr(3C)

addsev(3C)

addseverity(3C)

aio_cancel(3C)

aiocancel(3C)

aio_error(3C)

aio_fsync(3C)

aio_read(3C)

aioread(3C)

aio_return(3C)

aio_suspend(3C)

aiowait(3C)

aio_waitn(3C)

aio_write(3C)

aiowrite(3C)

alloca(3C)

alphasort(3C)

ascftime(3C)

asctime(3C)

asctime_r(3C)

asprintf(3C)

assert(3C)

atexit(3C)

atof(3C)

atoi(3C)

atol(3C)

atoll(3C)

atomic_add_16(3C)

atomic_add_16_nv(3C)

atomic_add_32(3C)

atomic_add_32_nv(3C)

atomic_add(3C)

atomic_add_64(3C)

atomic_add_64_nv(3C)

atomic_add_8(3C)

atomic_add_8_nv(3C)

atomic_add_char(3C)

atomic_add_char_nv(3C)

atomic_add_int(3C)

atomic_add_int_nv(3C)

atomic_add_long(3C)

atomic_add_long_nv(3C)

atomic_add_ptr(3C)

atomic_add_ptr_nv(3C)

atomic_add_short(3C)

atomic_add_short_nv(3C)

atomic_and_16(3C)

atomic_and_16_nv(3C)

atomic_and_32(3C)

atomic_and_32_nv(3C)

atomic_and(3C)

atomic_and_64(3C)

atomic_and_64_nv(3C)

atomic_and_8(3C)

atomic_and_8_nv(3C)

atomic_and_uchar(3C)

atomic_and_uchar_nv(3C)

atomic_and_uint(3C)

atomic_and_uint_nv(3C)

atomic_and_ulong(3C)

atomic_and_ulong_nv(3C)

atomic_and_ushort(3C)

atomic_and_ushort_nv(3C)

atomic_bits(3C)

atomic_cas_16(3C)

atomic_cas_32(3C)

atomic_cas(3C)

atomic_cas_64(3C)

atomic_cas_8(3C)

atomic_cas_ptr(3C)

atomic_cas_uchar(3C)

atomic_cas_uint(3C)

atomic_cas_ulong(3C)

atomic_cas_ushort(3C)

atomic_clear_long_excl(3C)

atomic_dec_16(3C)

atomic_dec_16_nv(3C)

atomic_dec_32(3C)

atomic_dec_32_nv(3C)

atomic_dec(3C)

atomic_dec_64(3C)

atomic_dec_64_nv(3C)

atomic_dec_8(3C)

atomic_dec_8_nv(3C)

atomic_dec_ptr(3C)

atomic_dec_ptr_nv(3C)

atomic_dec_uchar(3C)

atomic_dec_uchar_nv(3C)

atomic_dec_uint(3C)

atomic_dec_uint_nv(3C)

atomic_dec_ulong(3C)

atomic_dec_ulong_nv(3C)

atomic_dec_ushort(3C)

atomic_dec_ushort_nv(3C)

atomic_inc_16(3C)

atomic_inc_16_nv(3C)

atomic_inc_32(3C)

atomic_inc_32_nv(3C)

atomic_inc(3C)

atomic_inc_64(3C)

atomic_inc_64_nv(3C)

atomic_inc_8(3C)

atomic_inc_8_nv(3C)

atomic_inc_ptr(3C)

atomic_inc_ptr_nv(3C)

atomic_inc_uchar(3C)

atomic_inc_uchar_nv(3C)

atomic_inc_uint(3C)

atomic_inc_uint_nv(3C)

atomic_inc_ulong(3C)

atomic_inc_ulong_nv(3C)

atomic_inc_ushort(3C)

atomic_inc_ushort_nv(3C)

atomic_ops(3C)

atomic_or_16(3C)

atomic_or_16_nv(3C)

atomic_or_32(3C)

atomic_or_32_nv(3C)

atomic_or(3C)

atomic_or_64(3C)

atomic_or_64_nv(3C)

atomic_or_8(3C)

atomic_or_8_nv(3C)

atomic_or_uchar(3C)

atomic_or_uchar_nv(3C)

atomic_or_uint(3C)

atomic_or_uint_nv(3C)

atomic_or_ulong(3C)

atomic_or_ulong_nv(3C)

atomic_or_ushort(3C)

atomic_or_ushort_nv(3C)

atomic_set_long_excl(3C)

atomic_swap_16(3C)

atomic_swap_32(3C)

atomic_swap(3C)

atomic_swap_64(3C)

atomic_swap_8(3C)

atomic_swap_ptr(3C)

atomic_swap_uchar(3C)

atomic_swap_uint(3C)

atomic_swap_ulong(3C)

atomic_swap_ushort(3C)

attropen(3C)

backtrace(3C)

backtrace_symbols(3C)

backtrace_symbols_fd(3C)

basename(3C)

bcmp(3C)

bcopy(3C)

bindtextdomain(3C)

bind_textdomain_codeset(3C)

bsdmalloc(3MALLOC)

bsd_signal(3C)

bsearch(3C)

bstring(3C)

btowc(3C)

bzero(3C)

calloc(3C)

calloc(3MALLOC)

canonicalize_file_name(3C)

catclose(3C)

catgets(3C)

catopen(3C)

cfgetispeed(3C)

cfgetospeed(3C)

cfsetispeed(3C)

cfsetospeed(3C)

cftime(3C)

chkauthattr(3C)

clearenv(3C)

clearerr(3C)

clock(3C)

clock_getres(3C)

clock_gettime(3C)

clock_nanosleep(3C)

clock_settime(3C)

closedir(3C)

closefrom(3C)

closelog(3C)

cond_broadcast(3C)

cond_destroy(3C)

cond_init(3C)

cond_reltimedwait(3C)

cond_signal(3C)

cond_timedwait(3C)

cond_wait(3C)

confstr(3C)

crypt(3C)

crypt_genhash_impl(3C)

crypt_gensalt(3C)

crypt_gensalt_impl(3C)

cset(3C)

csetcol(3C)

csetlen(3C)

csetno(3C)

ctermid(3C)

ctermid_r(3C)

ctime(3C)

ctime_r(3C)

ctype(3C)

cuserid(3C)

daemon(3C)

dbm_clearerr(3C)

dbm_close(3C)

dbm_delete(3C)

dbm_error(3C)

dbm_fetch(3C)

dbm_firstkey(3C)

dbm_nextkey(3C)

dbm_open(3C)

dbm_store(3C)

dcgettext(3C)

dcngettext(3C)

decimal_to_double(3C)

decimal_to_extended(3C)

decimal_to_floating(3C)

decimal_to_quadruple(3C)

decimal_to_single(3C)

dgettext(3C)

difftime(3C)

directio(3C)

dirfd(3C)

dirname(3C)

div(3C)

dladdr1(3C)

dladdr(3C)

dlclose(3C)

dldump(3C)

dlerror(3C)

dlinfo(3C)

dl_iterate_phdr(3C)

dlmopen(3C)

dlopen(3C)

dlsym(3C)

dngettext(3C)

door_bind(3C)

door_call(3C)

door_create(3C)

door_cred(3C)

door_getparam(3C)

door_info(3C)

door_return(3C)

door_revoke(3C)

door_server_create(3C)

door_setparam(3C)

door_ucred(3C)

door_unbind(3C)

door_xcreate(3C)

double_to_decimal(3C)

drand48(3C)

dup2(3C)

econvert(3C)

ecvt(3C)

_edata(3C)

edata(3C)

enable_extended_FILE_stdio(3C)

encrypt(3C)

_end(3C)

end(3C)

endauthattr(3C)

endexecattr(3C)

endgrent(3C)

endnetgrent(3C)

endprofattr(3C)

endpwent(3C)

endspent(3C)

enduserattr(3C)

endusershell(3C)

endutent(3C)

endutxent(3C)

erand48(3C)

err(3C)

errno(3C)

errx(3C)

_etext(3C)

etext(3C)

euccol(3C)

euclen(3C)

eucscol(3C)

exit(3C)

_exithandle(3C)

extended_to_decimal(3C)

fattach(3C)

__fbufsize(3C)

fclose(3C)

fcloseall(3C)

fconvert(3C)

fcvt(3C)

fdatasync(3C)

FD_CLR(3C)

fdetach(3C)

FD_ISSET(3C)

fdopen(3C)

fdopendir(3C)

FD_SET(3C)

fdwalk(3C)

FD_ZERO(3C)

feof(3C)

ferror(3C)

fflush(3C)

ffs(3C)

ffsl(3C)

ffsll(3C)

fgetattr(3C)

fgetc(3C)

fgetgrent(3C)

fgetgrent_r(3C)

fgetpos(3C)

fgetpwent(3C)

fgetpwent_r(3C)

fgets(3C)

fgetspent(3C)

fgetspent_r(3C)

fgetuserattr(3C)

fgetwc(3C)

fgetws(3C)

fileno(3C)

file_to_decimal(3C)

finite(3C)

__flbf(3C)

floating_to_decimal(3C)

flockfile(3C)

fls(3C)

flsl(3C)

flsll(3C)

_flushlbf(3C)

fmtmsg(3C)

fnmatch(3C)

fopen(3C)

fpclass(3C)

__fpending(3C)

fpgetmask(3C)

fpgetround(3C)

fpgetsticky(3C)

fprintf(3C)

fpsetmask(3C)

fpsetround(3C)

fpsetsticky(3C)

__fpurge(3C)

fputc(3C)

fputs(3C)

fputwc(3C)

fputws(3C)

fread(3C)

__freadable(3C)

__freading(3C)

frealpath(3C)

free(3C)

free(3MALLOC)

free_authattr(3C)

free_execattr(3C)

free_profattr(3C)

free_proflist(3C)

free_userattr(3C)

freopen(3C)

fscanf(3C)

fseek(3C)

fseeko(3C)

fsetattr(3C)

__fsetlocking(3C)

fsetpos(3C)

fsync(3C)

ftell(3C)

ftello(3C)

ftime(3C)

ftok(3C)

ftruncate(3C)

ftrylockfile(3C)

ftw(3C)

func_to_decimal(3C)

funlockfile(3C)

fwide(3C)

fwprintf(3C)

__fwritable(3C)

fwrite(3C)

__fwriting(3C)

fwscanf(3C)

gconvert(3C)

gcvt(3C)

getattrat(3C)

getauthattr(3C)

getauthnam(3C)

getc(3C)

getchar(3C)

getchar_unlocked(3C)

getcpuid(3C)

getc_unlocked(3C)

getcwd(3C)

getdate(3C)

getdelim(3C)

getdtablesize(3C)

getenv(3C)

getexecattr(3C)

getexecname(3C)

getexecprof(3C)

getexecuser(3C)

getextmntent(3C)

getgrent(3C)

getgrent_r(3C)

getgrgid(3C)

getgrgid_r(3C)

getgrnam(3C)

getgrnam_r(3C)

gethomelgroup(3C)

gethostid(3C)

gethostname(3C)

gethrtime(3C)

gethrvtime(3C)

getline(3C)

getloadavg(3C)

getlogin(3C)

getlogin_r(3C)

getmntany(3C)

getmntent(3C)

getnetgrent(3C)

getnetgrent_r(3C)

get_nprocs(3C)

get_nprocs_conf(3C)

getopt(3C)

getopt_clip(3C)

getopt_long(3C)

getopt_long_only(3C)

getpagesize(3C)

getpagesizes(3C)

getpass(3C)

getpassphrase(3C)

getpeerucred(3C)

getpriority(3C)

getprofattr(3C)

getproflist(3C)

getprofnam(3C)

getprogname(3C)

getpw(3C)

getpwent(3C)

getpwent_r(3C)

getpwnam(3C)

getpwnam_r(3C)

getpwuid(3C)

getpwuid_r(3C)

getrusage(3C)

gets(3C)

getspent(3C)

getspent_r(3C)

getspnam(3C)

getspnam_r(3C)

getsubopt(3C)

gettext(3C)

gettimeofday(3C)

gettxt(3C)

getuserattr(3C)

getuserattrnam(3C)

getusernam(3C)

getusershell(3C)

getuseruid(3C)

getutent(3C)

getutid(3C)

getutline(3C)

getutmp(3C)

getutmpx(3C)

getutxent(3C)

getutxid(3C)

getutxline(3C)

getvfsany(3C)

getvfsent(3C)

getvfsfile(3C)

getvfsspec(3C)

getw(3C)

getwc(3C)

getwchar(3C)

getwd(3C)

getwidth(3C)

getws(3C)

getzoneid(3C)

getzoneidbyname(3C)

getzonenamebyid(3C)

glob(3C)

globfree(3C)

gmtime(3C)

gmtime_r(3C)

grantpt(3C)

gsignal(3C)

hasmntopt(3C)

hcreate(3C)

hdestroy(3C)

hsearch(3C)

iconv(3C)

iconv_close(3C)

iconvctl(C)

iconv_open(3C)

iconvstr(3C)

imaxabs(3C)

imaxdiv(3C)

index(3C)

initgroups(3C)

initstate(3C)

innetgr(3C)

insque(3C)

isaexec(3C)

isalnum(3C)

isalpha(3C)

isascii(3C)

isastream(3C)

isatty(3C)

isblank(3C)

iscntrl(3C)

isdigit(3C)

isenglish(3C)

isgraph(3C)

isideogram(3C)

islower(3C)

isnand(3C)

isnanf(3C)

isnumber(3C)

isphonogram(3C)

isprint(3C)

ispunct(3C)

isspace(3C)

isspecial(3C)

is_system_labeled(3C)

isupper(3C)

iswalnum(3C)

iswalpha(3C)

iswascii(3C)

iswblank(3C)

iswcntrl(3C)

iswctype(3C)

iswdigit(3C)

iswgraph(3C)

iswlower(3C)

iswprint(3C)

iswpunct(3C)

iswspace(3C)

iswupper(3C)

iswxdigit(3C)

isxdigit(3C)

jrand48(3C)

killpg(3C)

kva_match(3C)

l64a(3C)

labs(3C)

lckpwdf(3C)

lcong48(3C)

ldiv(3C)

lfind(3C)

lfmt(3C)

lio_listio(3C)

llabs(3C)

lldiv(3C)

lltostr(3C)

localeconv(3C)

localelist(3C)

localelistfree(3C)

localtime(3C)

localtime_r(3C)

lockf(3C)

_longjmp(3C)

longjmp(3C)

lrand48(3C)

lsearch(3C)

madvise(3C)

major(3C)

makecontext(3C)

makedev(3C)

mallinfo(3MALLOC)

malloc(3C)

malloc(3MALLOC)

mallocctl(3MALLOC)

mallopt(3MALLOC)

mapmalloc(3MALLOC)

match_execattr(3C)

mblen(3C)

mbrlen(3C)

mbrtowc(3C)

mbsinit(3C)

mbsrtowcs(3C)

mbstowcs(3C)

mbtowc(3C)

memalign(3C)

memalign(3MALLOC)

membar_consumer(3C)

membar_enter(3C)

membar_exit(3C)

membar_ops(3C)

membar_producer(3C)

memccpy(3C)

memchr(3C)

memcmp(3C)

memcpy(3C)

memmem(3C)

memmove(3C)

memory(3C)

memset(3C)

minor(3C)

mkdtemp(3C)

mkfifo(3C)

mkfifoat(3C)

mkstemp(3C)

mkstemps(3C)

mktemp(3C)

mktime(3C)

mlock(3C)

mlockall(3C)

monitor(3C)

mq_close(3C)

mq_getattr(3C)

mq_notify(3C)

mq_open(3C)

mq_receive(3C)

mq_reltimedreceive_np(3C)

mq_reltimedsend_np(3C)

mq_send(3C)

mq_setattr(3C)

mq_timedreceive(3C)

mq_timedsend(3C)

mq_unlink(3C)

mrand48(3C)

msync(3C)

mtmalloc(3MALLOC)

munlock(3C)

munlockall(3C)

mutex_consistent(3C)

mutex_destroy(3C)

mutex_init(3C)

mutex_lock(3C)

mutex_trylock(3C)

mutex_unlock(3C)

nanosleep(3C)

ndbm(3C)

nftw(3C)

ngettext(3C)

nl_langinfo(3C)

nrand48(3C)

offsetof(3C)

opendir(3C)

openlog(3C)

pclose(3C)

perror(3C)

pfmt(3C)

plock(3C)

popen(3C)

port_alert(3C)

port_associate(3C)

port_create(3C)

port_dissociate(3C)

port_get(3C)

port_getn(3C)

port_send(3C)

port_sendn(3C)

posix_fadvise(3C)

posix_fallocate(3C)

posix_madvise(3C)

posix_memalign(3C)

posix_openpt(3C)

posix_spawn(3C)

posix_spawnattr_destroy(3C)

posix_spawnattr_getflags(3C)

posix_spawnattr_getpgroup(3C)

posix_spawnattr_getschedparam(3C)

posix_spawnattr_getschedpolicy(3C)

posix_spawnattr_getsigdefault(3C)

posix_spawnattr_getsigignore_np(3C)

posix_spawnattr_getsigmask(3C)

posix_spawnattr_init(3C)

posix_spawnattr_setflags(3C)

posix_spawnattr_setpgroup(3C)

posix_spawnattr_setschedparam(3C)

posix_spawnattr_setschedpolicy(3C)

posix_spawnattr_setsigdefault(3C)

posix_spawnattr_setsigignore_np(3C)

posix_spawnattr_setsigmask(3C)

posix_spawn_file_actions_addclose(3C)

posix_spawn_file_actions_addclosefrom_np(3C)

posix_spawn_file_actions_adddup2(3C)

posix_spawn_file_actions_addopen(3C)

posix_spawn_file_actions_destroy(3C)

posix_spawn_file_actions_init(3C)

posix_spawnp(3C)

printf(3C)

printstack(3C)

priv_addset(3C)

priv_allocset(3C)

priv_basicset(3C)

priv_copyset(3C)

priv_delset(3C)

priv_emptyset(3C)

priv_fillset(3C)

priv_freeset(3C)

priv_getbyname(3C)

priv_getbynum(3C)

priv_getsetbyname(3C)

priv_getsetbynum(3C)

priv_gettext(3C)

priv_ineffect(3C)

priv_intersect(3C)

priv_inverse(3C)

priv_isemptyset(3C)

priv_isequalset(3C)

priv_isfullset(3C)

priv_ismember(3C)

priv_issubset(3C)

priv_set(3C)

priv_set_to_str(3C)

priv_str_to_set(3C)

priv_union(3C)

pselect(3C)

pset_getloadavg(3C)

psiginfo(3C)

psignal(3C)

pthread_atfork(3C)

pthread_attr_destroy(3C)

pthread_attr_getdetachstate(3C)

pthread_attr_getguardsize(3C)

pthread_attr_getinheritsched(3C)

pthread_attr_getschedparam(3C)

pthread_attr_getschedpolicy(3C)

pthread_attr_getscope(3C)

pthread_attr_getstack(3C)

pthread_attr_getstackaddr(3C)

pthread_attr_getstacksize(3C)

pthread_attr_init(3C)

pthread_attr_setdetachstate(3C)

pthread_attr_setguardsize(3C)

pthread_attr_setinheritsched(3C)

pthread_attr_setschedparam(3C)

pthread_attr_setschedpolicy(3C)

pthread_attr_setscope(3C)

pthread_attr_setstack(3C)

pthread_attr_setstackaddr(3C)

pthread_attr_setstacksize(3C)

pthread_barrierattr_destroy(3C)

pthread_barrierattr_getpshared(3C)

pthread_barrierattr_init(3C)

pthread_barrierattr_setpshared(3C)

pthread_barrier_destroy(3C)

pthread_barrier_init(3C)

pthread_barrier_wait(3C)

pthread_cancel(3C)

pthread_cleanup_pop(3C)

pthread_cleanup_push(3C)

pthread_condattr_destroy(3C)

pthread_condattr_getclock(3C)

pthread_condattr_getpshared(3C)

pthread_condattr_init(3C)

pthread_condattr_setclock(3C)

pthread_condattr_setpshared(3C)

pthread_cond_broadcast(3C)

pthread_cond_destroy(3C)

pthread_cond_init(3C)

pthread_cond_reltimedwait_np(3C)

pthread_cond_signal(3C)

pthread_cond_timedwait(3C)

pthread_cond_wait(3C)

pthread_create(3C)

pthread_detach(3C)

pthread_equal(3C)

pthread_exit(3C)

pthread_getconcurrency(3C)

pthread_getschedparam(3C)

pthread_getspecific(3C)

pthread_join(3C)

pthread_key_create(3C)

pthread_key_create_once_np(3C)

pthread_key_delete(3C)

pthread_kill(3C)

pthread_mutexattr_destroy(3C)

pthread_mutexattr_getprioceiling(3C)

pthread_mutexattr_getprotocol(3C)

pthread_mutexattr_getpshared(3C)

pthread_mutexattr_getrobust(3C)

pthread_mutexattr_gettype(3C)

pthread_mutexattr_init(3C)

pthread_mutexattr_setprioceiling(3C)

pthread_mutexattr_setprotocol(3C)

pthread_mutexattr_setpshared(3C)

pthread_mutexattr_setrobust(3C)

pthread_mutexattr_settype(3C)

pthread_mutex_consistent(3C)

pthread_mutex_destroy(3C)

pthread_mutex_getprioceiling(3C)

pthread_mutex_init(3C)

pthread_mutex_lock(3C)

pthread_mutex_reltimedlock_np(3C)

pthread_mutex_setprioceiling(3C)

pthread_mutex_timedlock(3C)

pthread_mutex_trylock(3C)

pthread_mutex_unlock(3C)

pthread_once(3C)

pthread_rwlockattr_destroy(3C)

pthread_rwlockattr_getpshared(3C)

pthread_rwlockattr_init(3C)

pthread_rwlockattr_setpshared(3C)

pthread_rwlock_destroy(3C)

pthread_rwlock_init(3C)

pthread_rwlock_rdlock(3C)

pthread_rwlock_reltimedrdlock_np(3C)

pthread_rwlock_reltimedwrlock_np(3C)

pthread_rwlock_timedrdlock(3C)

pthread_rwlock_timedwrlock(3C)

pthread_rwlock_tryrdlock(3C)

pthread_rwlock_trywrlock(3C)

pthread_rwlock_unlock(3C)

pthread_rwlock_wrlock(3C)

pthread_self(3C)

pthread_setcancelstate(3C)

pthread_setcanceltype(3C)

pthread_setconcurrency(3C)

pthread_setschedparam(3C)

pthread_setschedprio(3C)

pthread_setspecific(3C)

pthread_sigmask(3C)

pthread_spin_destroy(3C)

pthread_spin_init(3C)

pthread_spin_lock(3C)

pthread_spin_trylock(3C)

pthread_spin_unlock(3C)

pthread_testcancel(3C)

ptrace(3C)

ptsname(3C)

putc(3C)

putchar(3C)

putchar_unlocked(3C)

putc_unlocked(3C)

putenv(3C)

putmntent(3C)

putpwent(3C)

puts(3C)

putspent(3C)

pututline(3C)

pututxline(3C)

putw(3C)

putwc(3C)

putwchar(3C)

putws(3C)

qeconvert(3C)

qfconvert(3C)

qgconvert(3C)

qsort(3C)

quadruple_to_decimal(3C)

raise(3C)

rand(3C)

random(3C)

rand_r(3C)

rctlblk_get_enforced_value(3C)

rctlblk_get_firing_time(3C)

rctlblk_get_global_action(3C)

rctlblk_get_global_flags(3C)

rctlblk_get_local_action(3C)

rctlblk_get_local_flags(3C)

rctlblk_get_privilege(3C)

rctlblk_get_recipient_pid(3C)

rctlblk_get_value(3C)

rctlblk_set_local_action(3C)

rctlblk_set_local_flags(3C)

rctlblk_set_privilege(3C)

rctlblk_set_recipient_pid(3C)

rctlblk_set_value(3C)

rctlblk_size(3C)

rctl_walk(3C)

readdir(3C)

readdir_r(3C)

realloc(3C)

realloc(3MALLOC)

realpath(3C)

reboot(3C)

re_comp(3C)

re_exec(3C)

regcmp(3C)

regcomp(3C)

regerror(3C)

regex(3C)

regexec(3C)

regfree(3C)

remove(3C)

remque(3C)

resetmnttab(3C)

rewind(3C)

rewinddir(3C)

rindex(3C)

rwlock(3C)

rwlock_destroy(3C)

rwlock_init(3C)

rw_rdlock(3C)

rw_tryrdlock(3C)

rw_trywrlock(3C)

rw_unlock(3C)

rw_wrlock(3C)

scandir(3C)

scanf(3C)

schedctl_exit(3C)

schedctl_init(3C)

schedctl_lookup(3C)

schedctl_start(3C)

schedctl_stop(3C)

sched_getparam(3C)

sched_get_priority_max(3C)

sched_get_priority_min(3C)

sched_getscheduler(3C)

sched_rr_get_interval(3C)

sched_setparam(3C)

sched_setscheduler(3C)

sched_yield(3C)

seconvert(3C)

seed48(3C)

seekdir(3C)

select(3C)

sema_destroy(3C)

sema_init(3C)

semaphore(3C)

sema_post(3C)

sema_trywait(3C)

sema_wait(3C)

sem_close(3C)

sem_destroy(3C)

sem_getvalue(3C)

sem_init(3C)

sem_open(3C)

sem_post(3C)

sem_reltimedwait_np(3C)

sem_timedwait(3C)

sem_trywait(3C)

sem_unlink(3C)

sem_wait(3C)

setattrat(3C)

setauthattr(3C)

setbuf(3C)

setbuffer(3C)

setcat(3C)

setenv(3C)

setexecattr(3C)

setgrent(3C)

sethostname(3C)

_setjmp(3C)

setjmp(3C)

setkey(3C)

setlabel(3C)

setlinebuf(3C)

setlocale(3C)

setlogmask(3C)

setnetgrent(3C)

setpriority(3C)

setprofattr(3C)

setprogname(3C)

setpwent(3C)

setspent(3C)

setstate(3C)

settimeofday(3C)

setuserattr(3C)

setusershell(3C)

setutent(3C)

setutxent(3C)

setvbuf(3C)

sfconvert(3C)

sgconvert(3C)

shm_open(3C)

shm_unlink(3C)

sig2str(3C)

sigaddset(3C)

sigdelset(3C)

sigemptyset(3C)

sigfillset(3C)

sigfpe(3C)

sighold(3C)

sigignore(3C)

siginterrupt(3C)

sigismember(3C)

siglongjmp(3C)

signal(3C)

sigpause(3C)

sigqueue(3C)

sigrelse(3C)

sigset(3C)

sigsetjmp(3C)

sigsetops(3C)

sigstack(3C)

sigtimedwait(3C)

sigwaitinfo(3C)

single_to_decimal(3C)

sleep(3C)

smt_pause(3C)

snprintf(3C)

sprintf(3C)

srand(3C)

srand48(3C)

srandom(3C)

sscanf(3C)

ssignal(3C)

stack_getbounds(3C)

_stack_grow(3C)

stack_inbounds(3C)

stack_setbounds(3C)

stack_violation(3C)

stdio(3C)

stpcpy(3C)

stpncpy(3C)

str2sig(3C)

strcasecmp(3C)

strcasestr(3C)

strcat(3C)

strchr(3C)

strchrnul(3C)

strcmp(3C)

strcoll(3C)

strcpy(3C)

strcspn(3C)

strdup(3C)

strdupa(3C)

strerror(3C)

strerror_r(3C)

strfmon(3C)

strftime(3C)

string(3C)

string_to_decimal(3C)

strlcat(3C)

strlcpy(3C)

strlen(3C)

strncasecmp(3C)

strncat(3C)

strncmp(3C)

strncpy(3C)

strndup(3C)

strndupa(3C)

strnlen(3C)

strnstr(3C)

strpbrk(3C)

strptime(3C)

strrchr(3C)

strsep(3C)

strsignal(3C)

strspn(3C)

strstr(3C)

strtod(3C)

strtof(3C)

strtoimax(3C)

strtok(3C)

strtok_r(3C)

strtol(3C)

strtold(3C)

strtoll(3C)

strtoul(3C)

strtoull(3C)

strtoumax(3C)

strtows(3C)

strxfrm(3C)

swab(3C)

swapcontext(3C)

swprintf(3C)

swscanf(3C)

sync_instruction_memory(3C)

sysconf(3C)

syslog(3C)

system(3C)

tcdrain(3C)

tcflow(3C)

tcflush(3C)

tcgetattr(3C)

tcgetpgrp(3C)

tcgetsid(3C)

tcsendbreak(3C)

tcsetattr(3C)

tcsetpgrp(3C)

tdelete(3C)

td_event_addset(3C_DB)

td_event_delset(3C_DB)

td_event_emptyset(3C_DB)

td_event_fillset(3C_DB)

td_eventisempty(3C_DB)

td_eventismember(3C_DB)

td_init(3C_DB)

td_log(3C_DB)

td_sync_get_info(3C_DB)

td_sync_get_stats(3C_DB)

td_sync_setstate(3C_DB)

td_sync_waiters(3C_DB)

td_ta_clear_event(3C_DB)

td_ta_delete(3C_DB)

td_ta_enable_stats(3C_DB)

td_ta_event_addr(3C_DB)

td_ta_event_getmsg(3C_DB)

td_ta_get_nthreads(3C_DB)

td_ta_get_ph(3C_DB)

td_ta_get_stats(3C_DB)

td_ta_map_addr2sync(3C_DB)

td_ta_map_id2thr(3C_DB)

td_ta_map_lwp2thr(3C_DB)

td_ta_new(3C_DB)

td_ta_reset_stats(3C_DB)

td_ta_setconcurrency(3C_DB)

td_ta_set_event(3C_DB)

td_ta_sync_iter(3C_DB)

td_ta_sync_tracking_enable(3C_DB)

td_ta_thr_iter(3C_DB)

td_ta_tsd_iter(3C_DB)

td_thr_clear_event(3C_DB)

td_thr_dbresume(3C_DB)

td_thr_dbsuspend(3C_DB)

td_thr_event_enable(3C_DB)

td_thr_event_getmsg(3C_DB)

td_thr_getcxregs(3C_DB)

td_thr_getcxregsize(3C_DB)

td_thr_getfpregs(3C_DB)

td_thr_getgregs(3C_DB)

td_thr_get_info(3C_DB)

td_thr_getxregs(3C_DB)

td_thr_getxregsize(3C_DB)

td_thr_lockowner(3C_DB)

td_thr_setcxregs(3C_DB)

td_thr_set_event(3C_DB)

td_thr_setfpregs(3C_DB)

td_thr_setgregs(3C_DB)

td_thr_setprio(3C_DB)

td_thr_setsigpending(3C_DB)

td_thr_setxregs(3C_DB)

td_thr_sigsetmask(3C_DB)

td_thr_sleepinfo(3C_DB)

td_thr_tlsbase(3C_DB)

td_thr_tsd(3C_DB)

td_thr_validate(3C_DB)

tell(3C)

telldir(3C)

tempnam(3C)

termios(3C)

textdomain(3C)

tfind(3C)

thr_continue(3C)

thr_create(3C)

thr_exit(3C)

thr_getconcurrency(3C)

thr_getprio(3C)

thr_getspecific(3C)

thr_join(3C)

thr_keycreate(3C)

thr_keycreate_once(3C)

thr_kill(3C)

thr_main(3C)

thr_min_stack(3C)

thr_self(3C)

thr_setconcurrency(3C)

thr_setprio(3C)

thr_setspecific(3C)

thr_sigsetmask(3C)

thr_stksegment(3C)

thr_suspend(3C)

thr_yield(3C)

timeradd(3C)

timerclear(3C)

timercmp(3C)

timer_create(3C)

timer_delete(3C)

timer_getoverrun(3C)

timer_gettime(3C)

timerisset(3C)

timer_settime(3C)

timersub(3C)

tmpfile(3C)

tmpnam(3C)

tmpnam_r(3C)

toascii(3C)

_tolower(3C)

tolower(3C)

_toupper(3C)

toupper(3C)

towctrans(3C)

towlower(3C)

towupper(3C)

truncate(3C)

tsearch(3C)

ttyname(3C)

ttyname_r(3C)

ttyslot(3C)

twalk(3C)

tzset(3C)

u8_strcmp(3C)

u8_textprep_str(3C)

u8_validate(3C)

ualarm(3C)

uconv_u16tou32(3C)

uconv_u16tou8(3C)

uconv_u32tou16(3C)

uconv_u32tou8(3C)

uconv_u8tou16(3C)

uconv_u8tou32(3C)

ucred_free(3C)

ucred_get(3C)

ucred_getegid(3C)

ucred_geteuid(3C)

ucred_getgroups(3C)

ucred_getlabel(3C)

ucred_getpflags(3C)

ucred_getpid(3C)

ucred_getprivset(3C)

ucred_getprojid(3C)

ucred_getrgid(3C)

ucred_getruid(3C)

ucred_getsgid(3C)

ucred_getsuid(3C)

ucred_getzoneid(3C)

ucred_size(3C)

ulckpwdf(3C)

ulltostr(3C)

umem_alloc(3MALLOC)

umem_cache_alloc(3MALLOC)

umem_cache_create(3MALLOC)

umem_cache_destroy(3MALLOC)

umem_cache_free(3MALLOC)

umem_debug(3MALLOC)

umem_free(3MALLOC)

umem_nofail_callback(3MALLOC)

umem_zalloc(3MALLOC)

ungetc(3C)

ungetwc(3C)

unlockpt(3C)

unordered(3C)

unsetenv(3C)

updwtmp(3C)

updwtmpx(3C)

usleep(3C)

utmpname(3C)

utmpxname(3C)

valloc(3C)

valloc(3MALLOC)

vasprintf(3C)

verr(3C)

verrx(3C)

vfprintf(3C)

vfscanf(3C)

vfwprintf(3C)

vfwscanf(3C)

vlfmt(3C)

vpfmt(3C)

vprintf(3C)

vscanf(3C)

vsnprintf(3C)

vsprintf(3C)

vsscanf(3C)

vswprintf(3C)

vswscanf(3C)

vsyslog(3C)

vwarn(3C)

vwarnx(3C)

vwprintf(3C)

vwscanf(3C)

wait3(3C)

wait(3C)

wait4(3C)

waitpid(3C)

walkcontext(3C)

warn(3C)

warnx(3C)

watchmalloc(3MALLOC)

watof(3C)

watoi(3C)

watol(3C)

watoll(3C)

wcpcpy(3C)

wcpncpy(3C)

wcrtomb(3C)

wcscasecmp(3C)

wcscat(3C)

wcschr(3C)

wcscmp(3C)

wcscoll(3C)

wcscpy(3C)

wcscspn(3C)

wcsdup(3C)

wcsetno(3C)

wcsftime(3C)

wcslen(3C)

wcsncasecmp(3C)

wcsncat(3C)

wcsncmp(3C)

wcsncpy(3C)

wcsnlen(3C)

wcspbrk(3C)

wcsrchr(3C)

wcsrtombs(3C)

wcsspn(3C)

wcsstr(3C)

wcstod(3C)

wcstof(3C)

wcstoimax(3C)

wcstok(3C)

wcstol(3C)

wcstold(3C)

wcstoll(3C)

wcstombs(3C)

wcstoul(3C)

wcstoull(3C)

wcstoumax(3C)

wcstring(3C)

wcswcs(3C)

wcswidth(3C)

wcsxfrm(3C)

wctob(3C)

wctomb(3C)

wctrans(3C)

wctype(3C)

wcwidth(3C)

windex(3C)

wmemchr(3C)

wmemcmp(3C)

wmemcpy(3C)

wmemmove(3C)

wmemset(3C)

wordexp(3C)

wordfree(3C)

wprintf(3C)

wrindex(3C)

wscanf(3C)

wscasecmp(3C)

wscat(3C)

wschr(3C)

wscmp(3C)

wscol(3C)

wscoll(3C)

wscpy(3C)

wscspn(3C)

wsdup(3C)

wslen(3C)

wsncasecmp(3C)

wsncat(3C)

wsncmp(3C)

wsncpy(3C)

wspbrk(3C)

wsprintf(3C)

wsrchr(3C)

wsscanf(3C)

wsspn(3C)

wstod(3C)

wstok(3C)

wstol(3C)

wstostr(3C)

wstring(3C)

wsxfrm(3C)

mutex_init

, mutex_lock

, mutex_trylock

, mutex_unlock

, mutex_consistent

, mutex_destroy

- mutual exclusion locks

Synopsis

cc –mt [ flag... ] file... [ library... ]
#include <thread.h> 
#include <synch.h> 

int mutex_init(mutex_t *mp, int type, void * arg);
int mutex_lock(mutex_t *mp);
int mutex_trylock(mutex_t *mp);
int mutex_unlock(mutex_t *mp);
int mutex_consistent(mutex_t *mp);
int mutex_destroy(mutex_t *mp);

Description

Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call for a mutex lock by way of mutex_lock() will cause another thread that is also trying to lock the same mutex to block until the owner thread unlocks it by way of mutex_unlock(). Threads within the same process or within other processes can share mutexes.

Mutexes can synchronize threads within the same process or in other processes. Mutexes can be used to synchronize threads between processes if the mutexes are allocated in writable memory and shared among the cooperating processes (see mmap(2)), and have been initialized for this task.

Initialize

Mutexes are either intra-process or inter-process, depending upon the argument passed implicitly or explicitly to the initialization of that mutex. A statically allocated mutex does not need to be explicitly initialized; by default, a statically allocated mutex is initialized with all zeros and its scope is set to be within the calling process.

For inter-process synchronization, a mutex needs to be allocated in memory shared between these processes. Since the memory for such a mutex must be allocated dynamically, the mutex needs to be explicitly initialized using mutex_init().

The mutex_init() function initializes the mutex referenced by mp with the type specified by type. Upon successful initialization the state of the mutex becomes initialized and unlocked. Only the attribute type LOCK_PRIO_PROTECT uses arg. The type argument must be one of the following:

USYNC_THREAD

The mutex can synchronize threads only in this process.

USYNC_PROCESS

The mutex can synchronize threads in this process and other processes. The object initialized with this attribute must be allocated in memory shared between processes, either in System V shared memory (see shmop(2)) or in memory mapped to a file (see mmap(2)). If the object is not allocated in such shared memory, it will not be shared between processes.

The type argument can be augmented by the bitwise-inclusive-OR of zero or more of the following flags:

LOCK_ROBUST

The mutex can synchronize threads robustly. At the time of thread or process death, either by calling thr_exit() or exit() or due to process abnormal termination, the lock is unlocked if is held by the thread or process. The next owner of the mutex will acquire it with an error return of EOWNERDEAD. The application must always check the return value from mutex_lock() for a mutex of this type. The new owner of this mutex should then attempt to make the state protected by the mutex consistent, since this state could have been left inconsistent when the last owner died. If the new owner is able to make the state consistent, it should call mutex_consistent() to restore the state of the mutex and then unlock the mutex. All subsequent calls to mutex_lock()will then behave normally. Only the new owner can make the mutex consistent. If for any reason the new owner is not able to make the state consistent, it should not call mutex_consistent() but should simply unlock the mutex. All waiting processes will be awakened and all subsequent calls to mutex_lock() will fail in acquiring the mutex with an error value of ENOTRECOVERABLE. If the thread or process that acquired the lock with EOWNERDEAD terminates without unlocking the mutex, the next owner will acquire the lock with an error value of EOWNERDEAD.

The memory for the object to be initialized with this attribute must be zeroed before initialization. Any thread or process interested in the robust lock can call mutex_init() to potentially initialize it, provided that all such callers of mutex_init() specify the same set of attribute flags. In this situation, if mutex_init() is called on a previously initialized robust mutex, mutex_init() will not reinitialize the mutex and will return the error value EBUSY.

LOCK_RECURSIVE

A thread attempting to relock this mutex without first unlocking it will succeed in locking the mutex. The mutex must be unlocked as many times as it is locked.

LOCK_ERRORCHECK

Unless LOCK_RECURSIVE is also set, a thread attempting to relock this mutex without first unlocking it will return with an error rather than deadlocking itself. A thread attempting to unlock this mutex without first owning it will return with an error.

LOCK_PRIO_INHERIT

When a thread is blocking higher priority threads because of owning one or more mutexes with the LOCK_PRIO_INHERIT attribute, it executes at the higher of its priority or the priority of the highest priority thread waiting on any of the mutexes owned by this thread and initialized with this attribute.

LOCK_PRIO_PROTECT

When a thread owns one or more mutexes initialized with the LOCK_PRIO_PROTECT attribute, it executes at the higher of its priority or the highest of the priority ceilings of all the mutexes owned by this thread and initialized with this attribute, regardless of whether other threads are blocked on any of these mutexes. When this attribute is specified, arg must point to an int containing the priority ceiling.

See pthread_mutexattr_getrobust(3C) for more information about robust mutexes. The LOCK_ROBUST attribute is the same as the POSIX PTHREAD_MUTEX_ROBUST attribute.

See pthread_mutexattr_settype(3C) for more information on recursive and error checking mutex types. The combination (LOCK_RECURSIVE | LOCK_ERRORCHECK) is the same as the POSIX PTHREAD_MUTEX_RECURSIVE type. By itself, LOCK_ERRORCHECK is the same as the POSIX PTHREAD_MUTEX_ERRORCHECK type.

The LOCK_PRIO_INHERIT attribute is the same as the POSIX PTHREAD_PRIO_INHERIT attribute. The LOCK_PRIO_PROTECT attribute is the same as the POSIX PTHREAD_PRIO_PROTECT attribute. See pthread_mutexattr_getprotocol(3C), pthread_mutexattr_getprioceiling(3C), and pthread_mutex_getprioceiling(3C) for a full discussion. The LOCK_PRIO_INHERIT and LOCK_PRIO_PROTECT attributes are mutually exclusive. Specifying both of these attributes causes mutex_init() to fail with EINVAL.

Initializing mutexes can also be accomplished by allocating in zeroed memory (default), in which case a type of USYNC_THREAD is assumed. In general, the following rules apply to mutex initialization:

These rules do not apply to LOCK_ROBUST mutexes. See the description for LOCK_ROBUSTabove. If default mutex attributes are used, the macro DEFAULTMUTEX can be used to initialize mutexes that are statically allocated.

Default mutex initialization (intra-process):

mutex_t mp;
mutex_init(&mp, USYNC_THREAD, NULL);

or

mutex_t  mp  =  DEFAULTMUTEX;

Customized mutex initialization (inter-process):

mutex_init(&mp, USYNC_PROCESS, NULL);

Customized mutex initialization (inter-process robust):

mutex_init(&mp, USYNC_PROCESS | LOCK_ROBUST, NULL);

Statically allocated mutexes can also be initialized with macros specifying LOCK_RECURSIVE and/or LOCK_ERRORCHECK:

mutex_t mp = RECURSIVEMUTEX;

Same as (USYNC_THREAD | LOCK_RECURSIVE)

mutex_t mp = ERRORCHECKMUTEX;

Same as (USYNC_THREAD | LOCK_ERRORCHECK)

mutex_t mp = RECURSIVE_ERRORCHECKMUTEX;

Same as (USYNC_THREAD | LOCK_RECURSIVE | LOCK_ERRORCHECK)

Lock and Unlock

A critical section of code is enclosed by a the call to lock the mutex and the call to unlock the mutex to protect it from simultaneous access by multiple threads. Only one thread at a time may possess mutually exclusive access to the critical section of code that is enclosed by the mutex-locking call and the mutex-unlocking call, whether the mutex's scope is intra-process or inter-process. A thread calling to lock the mutex either gets exclusive access to the code starting from the successful locking until its call to unlock the mutex, or it waits until the mutex is unlocked by the thread that locked it.

Mutexes have ownership, unlike semaphores. Although any thread, within the scope of a mutex, can get an unlocked mutex and lock access to the same critical section of code, only the thread that locked a mutex should unlock it.

If a thread waiting for a mutex receives a signal, upon return from the signal handler, the thread resumes waiting for the mutex as if there was no interrupt. A mutex protects code, not data; therefore, strongly bind a mutex with the data by putting both within the same structure, or at least within the same procedure.

A call to mutex_lock() locks the mutex object referenced by mp. If the mutex is already locked, the calling thread blocks until the mutex is freed; this will return with the mutex object referenced by mp in the locked state with the calling thread as its owner. If the current owner of a mutex tries to relock the mutex, it will result in deadlock.

The mutex_trylock() function is the same as mutex_lock(), respectively, except that if the mutex object referenced by mp is locked (by any thread, including the current thread), the call returns immediately with an error.

The mutex_unlock() function are called by the owner of the mutex object referenced by mp to release it. The mutex must be locked and the calling thread must be the one that last locked the mutex (the owner). If there are threads blocked on the mutex object referenced by mp when mutex_unlock() is called, the mp is freed, and the scheduling policy will determine which thread gets the mutex. If the calling thread is not the owner of the lock, no error status is returned, and the behavior of the program is undefined.

Destroy

The mutex_destroy() function destroys the mutex object referenced by mp. The mutex object becomes uninitialized. The space used by the destroyed mutex variable is not freed. It needs to be explicitly reclaimed.

Return Values

If successful, these functions return 0. Otherwise, an error number is returned.

Errors

The mutex_init() function will fail if:

EINVAL

The value specified by type is invalid, or the LOCK_PRIO_INHERIT and LOCK_PRIO_PROTECT attributes are both specified.

The mutex_init() function will fail for LOCK_ROBUST type mutex if:

EBUSY

The mutex pointed to by mp was previously initialized and has not yet been destroyed.

EINVAL

The mutex pointed to by mp was previously initialized with a different set of attribute flags.

The mutex_trylock() function will fail if:

EBUSY

The mutex pointed to by mp is already locked.

The mutex_lock() and mutex_trylock() functions will fail for a LOCK_RECURSIVE mutex if:

EAGAIN

The mutex could not be acquired because the maximum number of recursive locks for the mutex has been reached.

The mutex_lock() function will fail for a LOCK_ERRORCHECK and non-LOCK_RECURSIVE mutex if:

EDEADLK

The caller already owns the mutex.

The mutex_lock() function may fail for a non-LOCK_ERRORCHECK and non-LOCK_RECURSIVE mutex if:

EDEADLK

The caller already owns the mutex.

The mutex_unlock() function will fail for a LOCK_ERRORCHECK mutex if:

EPERM

The caller does not own the mutex.

The mutex_lock() or mutex_trylock() functions will fail for LOCK_ROBUST type mutex if:

EOWNERDEAD

The last owner of this mutex died while holding the mutex. This mutex is now owned by the caller. The caller must now attempt to make the state protected by the mutex consistent. If it is able to clean up the state, then it should restore the state of the mutex by calling mutex_consistent() and unlock the mutex. Subsequent calls to mutex_lock() will behave normally, as before. If the caller is not able to clean up the state, mutex_consistent() should not be called but the mutex should be unlocked. Subsequent calls to mutex_lock() will fail to acquire the mutex, returning with the error value ENOTRECOVERABLE. If the owner who acquired the lock with EOWNERDEAD dies, the next owner will acquire the lock with EOWNERDEAD.

ENOTRECOVERABLE

The mutex trying to be acquired was protecting the state that has been left unrecoverable when the mutex's last owner could not make the state protected by the mutex consistent. The mutex has not been acquired. This condition occurs when the lock was previously acquired with EOWNERDEAD and the owner was not able to clean up the state and unlocked the mutex without calling mutex_consistent().

The mutex_consistent() function will fail if:

EINVAL

The caller does not own the mutex or the mutex is not a LOCK_ROBUST mutex having an inconsistent state (EOWNERDEAD).

Examples

Single Gate

The following example uses one global mutex as a gate-keeper to permit each thread exclusive sequential access to the code within the user-defined function “change_global_data.” This type of synchronization will protect the state of shared data, but it also prohibits parallelism.

/* cc thisfile.c -lthread */
#define _REENTRANT
#include <stdio.h>
#include <thread.h>
#define NUM_THREADS 12
void *change_global_data(void *);     /*  for thr_create()   */
main(int argc,char * argv[])    {
       int i=0;
       for (i=0; i< NUM_THREADS; i++)    {
               thr_create(NULL, 0, change_global_data, NULL, 0, NULL);
       }
       while ((thr_join(NULL, NULL, NULL) == 0));
}

void * change_global_data(void *null){
       static mutex_t   Global_mutex;
       static int       Global_data = 0;
       mutex_lock(&Global_mutex);
       Global_data++;
       sleep(1);
       printf("%d is global data\n",Global_data);
       mutex_unlock(&Global_mutex);
       return NULL;
}

Multiple Instruction Single Data

The previous example, the mutex, the code it owns, and the data it protects was enclosed in one function. The next example uses C++ features to accommodate many functions that use just one mutex to protect one data:

/* CC thisfile.c -lthread   use C++ to compile*/

#define _REENTRANT
#include <stdlib.h>
#include <stdio.h>
#include <thread.h>
#include <errno.h>
#include <iostream.h>
#define NUM_THREADS 16
void *change_global_data(void *);     /*  for thr_create()   */

class Mutected {
       private:
               static mutex_t      Global_mutex;
               static int          Global_data;
       public:
               static int          add_to_global_data(void);
               static int          subtract_from_global_data(void);
};

int Mutected::Global_data = 0;
mutex_t Mutected::Global_mutex;

int Mutected::add_to_global_data()  {
       mutex_lock(&Global_mutex);    
       Global_data++;
       mutex_unlock(&Global_mutex);    
       return Global_data;
}

int Mutected::subtract_from_global_data()   {
       mutex_lock(&Global_mutex);    
       Global_data--;
       mutex_unlock(&Global_mutex);
       return Global_data;
}

void
main(int argc,char * argv[])  {
       int i=0;
       for (i=0;i< NUM_THREADS;i++)  {
            thr_create(NULL,0,change_global_data,NULL,0,NULL);
       }
       while ((thr_join(NULL,NULL,NULL) == 0));
}

void * change_global_data(void *)    {
       static int switcher = 0;
       if ((switcher++ % 3) == 0)   /* one-in-three threads subtracts */
               cout << Mutected::subtract_from_global_data() << endl;    
       else
               cout << Mutected::add_to_global_data() << endl;        
       return NULL;
}

Interprocess Locking

A mutex can protect data that is shared among processes. The mutex would need to be initialized as USYNC_PROCESS. One process initializes the process-shared mutex and writes it to a file to be mapped into memory by all cooperating processes (see mmap(2)). Afterwards, other independent processes can run the same program (whether concurrently or not) and share mutex-protected data.

/* cc thisfile.c -lthread */
/* To execute, run the command line "a.out 0 &; a.out 1" */

#define _REENTRANT
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <thread.h>
#define INTERPROCESS_FILE "ipc-sharedfile"
#define NUM_ADDTHREADS 12
#define NUM_SUBTRACTTHREADS 10
#define INCREMENT '0'
#define DECREMENT '1'
typedef struct { 
               mutex_t     Interprocess_mutex;
               int         Interprocess_data;
} buffer_t;
buffer_t *buffer;

void *add_interprocess_data(), *subtract_interprocess_data();
void create_shared_memory(), test_argv();
int zeroed[sizeof(buffer_t)];
int ipc_fd, i=0;

void
main(int argc,char * argv[]){
    test_argv(argv[1]);

    switch (*argv[1])  {
    case INCREMENT:
         /* Initializes the process-shared mutex */
         /* Should be run prior to running a DECREMENT process */
         create_shared_memory();
         ipc_fd = open(INTERPROCESS_FILE, O_RDWR);
         buffer = (buffer_t *)mmap(NULL, sizeof(buffer_t),
             PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
         buffer->Interprocess_data = 0;
         mutex_init(&buffer->Interprocess_mutex, USYNC_PROCESS,0);
         for (i=0; i< NUM_ADDTHREADS; i++)
         thr_create(NULL, 0, add_interprocess_data, argv[1],
             0, NULL);
         break;

    case DECREMENT:
         /* Should be run after the INCREMENT process has run. */
         while(ipc_fd = open(INTERPROCESS_FILE, O_RDWR)) == -1)
             sleep(1);
         buffer = (buffer_t *)mmap(NULL, sizeof(buffer_t),
             PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
         for (i=0; i< NUM_SUBTRACTTHREADS; i++)
         thr_create(NULL, 0, subtract_interprocess_data, argv[1],
             0, NULL);
         break;
    } /* end switch */

    while ((thr_join(NULL,NULL,NULL) == 0));
} /* end main */

void *add_interprocess_data(char argv_1[]){
    mutex_lock(&buffer->Interprocess_mutex);
    buffer->Interprocess_data++;
    sleep(2);
    printf("%d is add-interprocess data, and %c is argv1\n",
        buffer->Interprocess_data, argv_1[0]);
    mutex_unlock(&buffer->Interprocess_mutex);
    return NULL;
}

void *subtract_interprocess_data(char argv_1[])    {
    mutex_lock(&buffer->Interprocess_mutex);
    buffer->Interprocess_data--;
    sleep(2);
    printf("%d is subtract-interprocess data, and %c is argv1\n",
        buffer->Interprocess_data, argv_1[0]);
    mutex_unlock(&buffer->Interprocess_mutex);
    return NULL;
}

void create_shared_memory(){
    int i;
    ipc_fd = creat(INTERPROCESS_FILE, O_CREAT | O_RDWR );
    for (i=0; i<sizeof(buffer_t); i++){
        zeroed[i] = 0;
        write(ipc_fd, &zeroed[i],2);
    }
    close(ipc_fd);
    chmod(INTERPROCESS_FILE, S_IRWXU | S_IRWXG | S_IRWXO);
}

void test_argv(char argv1[])   {
    if (argv1 == NULL)   {
    printf("use 0 as arg1 for initial process\n \
    or use 1 as arg1 for the second process\n");
    exit(NULL);
    }
}

Solaris Interprocess Robust Locking

A mutex can protect data that is shared among processes robustly. The mutex would need to be initialized as USYNC_PROCESS | LOCK_ROBUST. One process initializes the robust process-shared mutex and writes it to a file to be mapped into memory by all cooperating processes (see mmap(2)). Afterwards, other independent processes can run the same program (whether concurrently or not) and share mutex-protected data.

The following example shows how to use a USYNC_PROCESS | LOCK_ROBUST type mutex.

/* cc thisfile.c -lthread */
 /* To execute, run the command line "a.out & a.out 1" */
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <thread.h>
 #define INTERPROCESS_FILE "ipc-sharedfile"
 typedef struct {
           mutex_t   Interprocess_mutex;
           int       Interprocess_data;
 } buffer_t;  
 buffer_t *buffer;
 int make_date_consistent();
 void create_shared_memory();
 int zeroed[sizeof(buffer_t)];
 int ipc_fd, i=0;
 main(int argc,char * argv[])  {
     int rc;
     if (argc > 1) {
         while((ipc_fd = open(INTERPROCESS_FILE, O_RDWR)) == -1)
             sleep(1);
         buffer = (buffer_t *)mmap(NULL, sizeof(buffer_t),
                   PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
         mutex_init(&buffer->Interprocess_mutex,
                     USYNC_PROCESS | LOCK_ROBUST,0);
     } else {
         create_shared_memory();
         ipc_fd = open(INTERPROCESS_FILE, O_RDWR);
         buffer = (buffer_t *)mmap(NULL, sizeof(buffer_t),
               PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0);
         buffer->Interprocess_data = 0;
         mutex_init(&buffer->Interprocess_mutex,
                     USYNC_PROCESS | LOCK_ROBUST,0);
     }
     for(;;) {
         rc = mutex_lock(&buffer->Interprocess_mutex);
         switch (rc) {
             case EOWNERDEAD:
               /*
                * The lock is acquired.
                * The last owner died holding the lock.
                * Try to make the state associated with
                * the mutex consistent.
                * If successful, make the robust lock consistent.
                */
               if (make_data_consistent())
                   mutex_consistent(&buffer->Interprocess_mutex);
                   mutex_unlock(&buffer->Interprocess_mutex);
                   break;
             case ENOTRECOVERABLE:
               /*
                * The lock is not acquired.
                * The last owner got the mutex with EOWNERDEAD
                * and failed to make the data consistent.
                * There is no way to recover, so just exit.
                */
               exit(1);
             case 0:
               /*
                * There is no error - data is consistent.
                * Do something with data.
                */
               mutex_unlock(&buffer->Interprocess_mutex);
               break;
        } 
   }
} /* end main */
void create_shared_memory() {
      int i;
      ipc_fd = creat(INTERPROCESS_FILE, O_CREAT | O_RDWR );
      for (i=0; i<sizeof(buffer_t); i++) {
           zeroed[i] = 0;
           write(ipc_fd, &zeroed[i],2);
      } 
      close(ipc_fd);
      chmod(INTERPROCESS_FILE, S_IRWXU | S_IRWXG | S_IRWXO);
 }

 /* return 1 if able to make data consistent, otherwise 0. */
 int make_data_consistent () {
       buffer->Interprocess_data = 0;
       return (1);
 }

Dynamically Allocated Mutexes

The following example allocates and frees memory in which a mutex is embedded.

struct record {
        int field1;
        int field2;
        mutex_t m;
} *r;
r = malloc(sizeof(struct record));
mutex_init(&r->m, USYNC_THREAD, NULL);
/*
  * The fields in this record are accessed concurrently
  * by acquiring the embedded lock.
  */

The thread execution in this example is as follows:

Thread 1 executes:                 Thread 2 executes:

...                                ...
mutex_lock(&r->m);                 mutex_lock(&r->m);
r->field1++;                       localvar = r->field1;
mutex_unlock(&r->m);               mutex_unlock(&r->m);
...                                ...

Later, when a thread decides to free the memory pointed to by r, the thread should call mutex_destroy( ) on the mutexes in this memory.

In the following example, the main thread can do a thr_join( ) on both of the above threads. If there are no other threads using the memory in r, the main thread can now safely free r:

for (i = 0; i < 2; i++)
       thr_join(0, 0, 0);
mutex_destroy(&r->m);   /* first destroy mutex */
free(r);                /* then free memory */

If the mutex is not destroyed, the program could have memory leaks.

Attributes

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
MT-Safe

See Also

mmap(2), shmop(2), pthread_mutexattr_getprioceiling(3C), pthread_mutexattr_getprotocol(3C), pthread_mutexattr_getrobust(3C), pthread_mutexattr_gettype(3C), pthread_mutex_getprioceiling(3C), pthread_mutex_init(3C), attributes(5), mutex(5), standards(5)

Notes

Previous releases of Solaris provided the USYNC_PROCESS_ROBUST mutex type. This type is now deprecated but is still supported for source and binary compatibility. When passed to mutex_init(), it is transformed into (USYNC_PROCESS | LOCK_ROBUST). The former method for restoring a USYNC_PROCESS_ROBUST mutex to a consistent state was to reinitialize it by calling mutex_init(). This method is still supported for source and binary compatibility, but the proper method is to call mutex_consistent().

The USYNC_PROCESS_ROBUST type permitted an alternate error value, ELOCKUNMAPPED, to be returned by mutex_lock() if the process containing a locked robust mutex unmapped the memory containing the mutex or performed one of the exec(2) functions. The ELOCKUNMAPPED error value implies all of the consequences of the EOWNERDEAD error value and as such is just a synonym for EOWNERDEAD. For full source and binary compatibility, the ELOCKUNMAPPED error value is still returned from mutex_lock() in these circumstances, but only if the mutex was initialized with the USYNC_PROCESS_ROBUST type. Otherwise, EOWNERDEAD is returned in these circumstances.

The mutex_lock(), mutex_unlock(), and mutex_trylock() functions do not validate the mutex type. An uninitialized mutex or a mutex with an invalid type does not return EINVAL. Interfaces for mutexes with an invalid type have unspecified behavior.

Uninitialized mutexes that are allocated locally could contain junk data. Such mutexes need to be initialized using mutex_init().

By default, if multiple threads are waiting for a mutex, the order of acquisition is undefined.