posix

POSIX_SPAWN with Java?

ProcessBuilder.start and Runtime.exec seem to use fork() on *NIX system, which seems to allocate the child process the same amount of memory as the parent process (see e.g. this question ). This can be painful if you want to launch a process which needs almost no memory from a process that uses lots of memory. Is there any way to laun...

Why use select() instead of sleep()?

I'm working through a chapter about iPhone audio and have come across a section of code that I can't make sense of: while (aqc.playPtr < aqc.sampleLen) { select(NULL, NULL, NULL, NULL, 1.0); } (Full code sample is on pages 163-166). From what I understand of the code the audio is being processed on another thread and the while lo...

Correct use of Stat on C

Why does this work : char *fd = "myfile.txt"; struct stat buf; stat(fd, &buf); int size = buf.st_size; printf("%d",size); But this does not work: char *fd = "myfile.txt"; struct stat *buf; stat(fd, buf); int size = buf->st_size; printf("%d",size); ...

How can I convert a file pointer ( FILE* ) to a file descriptor (fd)?

I have a FILE *, returned by a call to fopen(). I need to get a file descriptor from it, to make calls like fsync(fd) on it. What's the function to get a file descriptor from a file pointer? ...

Problem with getpwuid() and stat()

I have the following code. It gives me a problem with free memory, but I haven't figured out what exactly the problem is. It seems that getpwuid(buf->st_uid); is not getting along with readdir(dirh); or with stat functions. Does anyone know why? buf = (struct stat*) malloc(sizeof(struct stat)); for (dirp[i] = readdir(dirh); dirp...

Send and receive IPv6 link-local multicast UDP datagrams in Python?

This following is a straightforward IPv4 UDP broadcast, followed by listening on all interfaces. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) sock.bind(("", 1337)) sock.sendto("hello world", ("255.255.255.255", 1337)) while True: data, addr = sock.recvfrom(0x10...

Posix AIO Bad/Broken?

I'm working on a TFTP implementation that is transitioning away from a convoluted multi-threaded implementation to a single-thread/single-process implementation which uses a state machine to track the state of the sessions connected. TFTP is simple enough, and the number of concurrent sessions are small enough that the there really is no...

popen equivalent in c++

Is their any C popen() equivalent in C++ to ? ...

Why doesn't POSIX mmap return a volatile void*?

Mmap returns a void*, but not a volatile void*. If I'm using mmap to map shared memory, then another process could be writing to that memory, which means two subsequent reads from the same memory location can yield different values -- the exact situation volatile is meant for. So why doesn't it return a volatile void*? My best guess is ...

Shredding MySQL rows

I know how to remove files in order to make them impossible to recover. But, how do I remove rows from a MySQL table in a POSIX environment in a way that leads to the same results? I'm currently rewriting all data with a nullified string with the same length as the original data before I proceed with deleting the row. Does it work? If no...

How to determine if a file descriptor is seekable?

Is there any portable way (on POSIX systems) to determine if a file descriptor is seekable? My thought is to use lseek(fd, 0, SEEK_CUR); and check if the return value is -1, but I'm uncertain if this could give false negatives or false positives. Using fstat and making assumptions about what types of files are seekable/nonseekable does n...

Ignore SIGINT in Java

I'm using a Java wrapper for a native shared library on Unix (JRI). The native library (a C based REPL implementation for R) handles SIGINT internally. When using the Java wrapper, the Java application quits when I send a SIGINT to the process using: kill -SIGINT pid I'd prefer for the SIGINT to be entirely handled by the native libr...

Ensure a file is not changed while trying to remove it

In a POSIX environment, I want to remove a file from disk, but calculate its checksum before removing it, to make sure it was not changed. Is locking enough? Should I open it, unlink, calculate checksum, and then close it (so the OS can remove its inode)? Is there any way to ensure no other process has an open file descriptor on the file...

32-bit FreeBSD 7.2 and 1GB mmaps

I've been having some trouble with FreeBSD and large mmaps. Linux does not show the same problems. On program startup it can always get the 1 GB map. However, there's a reload operation where the file is replaced and remapped. The new map is usually just a little bigger each time so it doesn't fit neatly into the old mmap location. This...

What is the relation between PATH_MAX and NAME_MAX, and how do I obtain?

In limits.h, and in various places in the POSIX manpages, there are references to PATH_MAX and NAME_MAX. How do these relate to one another? Where is the official documentation for them? How can I obtain them at run time, and (where relevant) compile time for the C, Python, and GNU (shell) environments? ...

How does one define PATH_MAX for a given filesystem?

I'm presently writing a filesystem. The statvfs (and even the statfs) structs contain a field specifying the maximum length of a name in that path. As PATH_MAX is defined in the pathconf manpage (getconf), this means it is defined on a per-directory basis (and thus, determined by the underlying filesystem). How does one specify this valu...

scanf,back reference in awk

Is there any implementation of scanf()(like in C) in awk(POSIX)? I know that awk does not have back reference like in sed and perl. What is the easiest way to simulate in awk? Thanks Nyan ...

Find out default language on Linux

Is there a way to find out the default language of a Linux system from C? Is there a POSIX API for this? E.g. I'd like to have a string in human readable format, i.e. "German" or "Deutsch" on a German system, "French" or "Francais" on a French system etc. Is there something like this? Thanks! ...

Non-blocking socket with poll

A couple of days ago I had to investigate a problem where my application was showing abnormally high CPU usage when it was (apparently) in idle state. I tracked the problem down to a loop which was meant to block on a recvfrom call while the socket had been set to O_NONBLOCK-ing resulting in a spin lock. There were two ways of solving th...

How to retrieve file times with nanosecond precision?

I've just discovered that the stat() call, and the corresponding struct stat, does not contain fields for the file times with precision greater than one second. For setting these times, there are a variety of {f,l}utime{n,}s() functions, but not for getting. How then does obtain these times with nanosecond precision, preferably using PO...