apue | Advanced Programming in UNIX Environment
kandi X-RAY | apue Summary
kandi X-RAY | apue Summary
Advanced Programming in UNIX Environment
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of apue
apue Key Features
apue Examples and Code Snippets
Community Discussions
Trending Discussions on apue
QUESTION
When I ran the APUE sample code shown below on my Mac, it throws a EXC_BAD_ACCESS exception. I have checked the file cursor, and it is in the right position 12. I even try to replace the fprintf
with fputc
. After that, it works fine and the exception is gone. But I want to know what happened out there and why.
ANSWER
Answered 2020-Sep-18 at 11:47As already discussed in the comments, you passed the buffer buf
to fprintf()
and not the FILE
pointer fp
, which causes UB.
Every reasonable compiler will print a warning when you trying to do something like that you enabled the warning flags. You can avoid a lot of debugging when you enable all compiler warnings and only disable specific warnings when you are sure you do not want a warning for some type of error.
As mentioned here How to enable all compiler warnings in CLion?, you can enable warnings by adding the compiler flags to CMakeLists.txt
. For all warnings for C code, adding this line:
QUESTION
I notice that sigaction
has a alternate signal handler that is called when the SA_SIGINFO
flag is set. The alternate function has a void* context
parameter. Now, according to the APUE book, this parameter can be cast into a ucontext_t
structure. My question is that can I not cast it into any structure of my choice ? That way I could pass my own arguments into a signal handler. I noticed a post in stackoverflow stating that we just can't pass arguments to a signal handler as they are a very primitive feature. Is this possible ?
ANSWER
Answered 2020-Jun-03 at 15:10From this sigaction
manual page:
This is a pointer to a
ucontext_t
structure, cast tovoid *
. The structure pointed to by this field contains signal context information that was saved on the user-space stack by the kernel; ... Commonly, the handler function doesn't make any use of the third argument.
So this argument is a kernel-specific pointer to a special signal context. It's not possible to reuse it for user-data.
QUESTION
When I was looking at the C++ std::getline
function in , I accidently run
man getline
in my Ubuntu Terminal I found this function:
ANSWER
Answered 2020-May-13 at 13:49There’s nothing particularly special about this function. It’s specified by POSIX to read a single, newline-delimited line from stream
, into the buffer whose address is given by lineptr
and which must be large enough to hold n
bytes.
The reason lineptr
and n
are pointers is that they are used both as input to the function and potentially output from it. If lineptr
is NULL
on entry, or n
indicates that its size is too small to hold the line read from stream
, then getline
(re)allocates a buffer and updates lineptr
and n
with the corresponding information.
getline
is easier to use than fgets
because the latter will stop reading when it reaches the end of the buffer. So if fgets
tries to read a line longer than the provided buffer, it will return a partial read and the caller will have to read again and connect the multiple parts. In such circumstances, getline
would reallocate the provided buffer, if any.
As explained in the GNU C Library documentation,
Standard C has functions to do this, but they aren’t very safe: null characters and even (for
gets
) long lines can confuse them. So the GNU C Library provides the nonstandardgetline
function that makes it easy to read lines reliably.
(getline
originated in the GNU C Library and was added to POSIX in the 2008 edition.)
QUESTION
This is the fig11.3 in book Advanced programming in the UNIX Environment
...ANSWER
Answered 2020-Apr-24 at 12:19I think that's because according to the manual of pthread_join, it stores the return value of the thread, not a memory address: as you can see on the thr_fn1 and 2 functions, the return value is (void *)1 and (void *)2.
Dereferencing the pointer is pointless, as we are interested in his value, not the address he points to.
The second case uses this technique because ptr is actually a memory address.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install apue
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page