apue | Advanced Programming in the UNIX Environment | Interpreter library
kandi X-RAY | apue Summary
kandi X-RAY | apue Summary
Advanced Programming in the UNIX Environment - 3rd Edition.
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.
QUESTION
I was reading the APUE(Advanced Programming in the UNIX Environment), and come across this question when I see $3.11:
...ANSWER
Answered 2018-May-31 at 12:34In Linux there are blocking and non-blocking system calls. The write
is an example of blocking system call, which means the execution thread will be blocked until the write
completes. So once the user process called write
, it can not execute anything else until the system call is complete. So from user thread perspective it will behave like atomic [although at kernel level lot many things can happen and kernel execution of system call can be interrupted many times].
QUESTION
The unix domain sockets address structure is defined as:
...ANSWER
Answered 2019-Dec-31 at 17:38the correct way is to use sizeof (struct sockaddr_un)
actually.
QUESTION
ANSWER
Answered 2019-Nov-18 at 16:56The regex seems ok, you forgot to assign the new value to the text variable:
QUESTION
The function putenv
is not a thread safe function, so I guess if I call pthread_mutex_lock
before calling putenv
, can I make putenv
"thread safe" in this way?
I tried it but when I run it, segmentation fault came out.
Here is the code:
...ANSWER
Answered 2019-Sep-26 at 09:39putenv
is reentrant in newer versions of glibc. The problem is that putenv
does not copy the string that is given to it, and therefore you cannot base it on your stack. Try keeping your char env[100]
in a place where it will not be destroyed at the function's end.
The putenv() function is not required to be reentrant, and the one in glibc 2.0 is not, but the glibc 2.1 version is.
...
Since version 2.1.2, the glibc implementation conforms to SUSv2: the pointer string given to putenv() is used. In particular, this string becomes part of the environment; changing it later will change the environment. (Thus, it is an error to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment.)
QUESTION
I run ./umask as the following step:
...ANSWER
Answered 2019-Jul-23 at 16:40It's embarrassed for me,I make a mistake. My friend and I share the same virtual machine,he read the book faster than me.He runs 4-12 program,which the code like the following:
QUESTION
I want to map a pcap file in /dev/zero and let other program to read the /dev/zero. Is it possible to do? I read the book APUE and saw the Memory mapping of /dev/zero, but I think it is not what I want to do. Is there anyone can help me?
...ANSWER
Answered 2019-Jun-20 at 14:06Your question is a bit confusing.
Let me see if I understand. Please correct me where needed:
You have a .pcap file that you can open and load its contents in a program--let's call the program "A".
You want to make that .pcap file's contents available in memory buffer that you share with another program--let's call the program "B".
Based on the example in the APUE book, your idea is to mmap /dev/zero in program A, copy the contents of the .pcap file into the mapped memory segment, and then expect that when program B also maps /dev/zero, it will see the contents of the .pcap file.
Is that what you're going for?
If so, then I don't think you'll be able to use /dev/zero. Each of the two programs, when it mmap's /dev/zero, will get a separate zero-filled instance of the memory map. The only way for the two programs to share, in that case, would be for one to be a fork() of the other.
However, you could create a named shared memory object other than "/dev/zero" (call it "/tmp/mypcap" or something) and then multiple programs could share it.
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