posix

Odd/Incorrect Semaphore Behavior on OS X

Hi, I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results... #include <iostream> #include <fcntl.h> #include <stdio.h> #include <semaphore.h> int main() { sem_t* test; test = sem_open("test", O_CREAT, 0, 1); int val...

When is it safe to destroy a pthread barrier?

If I have an initialised pthread_barrier_t, when is it safe to destroy it? Is the following example safe? pthread_barrier_t barrier; ... int rc = pthread_barrier_wait(b); if (rc != PTHREAD_BARRIER_SERIAL_THREAD && rc != 0){ perror("pthread_barrier_wait"); exit(1); } if (id == 0){ if(pthread_barrier_destroy(&(threads[t_root].info...

why are some posix shared memory segments and posix semaphores not visible to ipcs

I built a client server application using posix shared memory and posix unnamed semaphores with pshared=1. The semaphores are placed inside the shared memory. The program runs fine, but when I type ipcs -m or ipcs -s, I do not see any shared memory segments or semaphores that I created. Why is it so? /* Server main function for implemen...

C++ - threads and multiple queues

I need to build a system of workers (represented as threads) and (multiple) queues. Individual jobs are waiting in one of the queues and waits for a worker thread to process them. Each worker can process jobs only from some of the queues. No spin-waiting. C/C++, pthreads, standard POSIX. The problem for me is the "multiple queues" thing...

Getting the highest allocated file descriptor

Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process? I know that there's a nice way to get the number on AIX, for example, but I'm looking for a portable method. The reason I'm asking is that I want to close all open file descriptors. My program is a server which runs as root and...

Obtaining Own External IP Address in POSIX C

I'm looking to obtain my own IP Address in order to publish that information in to a Peer-to-Peer network. In POSIX/C we have getaddrinfo(NULL, ...), but this always seems to returns INADDR_ANY or INADDR_LOOPBACK, which is useless to me. Any suggestions? ...

Disconnect and Reconnect a connected datagram socket

Iam trying to create an iterative server based on datagram sockets (UDP). It calls connect to the first client which it gets from the first recvfrom() call (yes I know this is no real connect). After having served this client, I disconnect the UDP socket (calling connect with AF_UNSPEC) Then I call recvfrom() to get the first packet fro...

Waiting on multiple events C++

Is there a recommended way to wait on multiple inputs. For example I would like my program to be able to receive input from 3 sources: Listen on a thread condition e.g. pthread_cond_wait() Take data from Standard input e.g. getline() Listen on a socket e.g. accept() What is the best way to accomplish this? Do I need a thread for each...

Is a secret token generated with /dev/urandom a good way to protect a daemon?

I have a daemon process which spawns subprocesses. Sometimes these subprocesses need to communicate back to the daemon. I want to ensure that only these subprocesses are authorized to communicate with the daemon. I want to implement this as follows: During startup, the daemon generates a random 128-byte secret token by reading /dev/ur...

Redefine signal handling flaws

This is about the design decision and understand the procs and cons for adopting another service. So we have two services with two unrelated servers, one listening on port 10000 and another is a xinetd server responding 3 different requests via 3 different ports (its client uses nc server port1|port2|port3 to retrieve data). One day be...

Signal safe use of sem_wait()/sem_post()

I am trying to create a wrapper on Linux which controls how many concurrent executions of something are allowed at once. To do so, I am using a system wide counting semaphore. I create the semaphore, do a sem_wait(), launch the child process and then do a sem_post() when the child terminates. That is fine. The problem is how to safel...

Get a random, high port number that is still available.

Suppose I want to run a TCP/IP service on some port for IPC. As I'm passing the port number to the processes I want to communicate with anyway, the port number doesn't matter. What's the best way to get a random, high (usually >49152) port number that is still available from the system? Is there something in POSIX I can use? I know FTP ...

How do I reliably track child/grandchild processes on a POSIX system?

I have an interesting (at least to me) problem: I can't manage to find a way to reliably and portably get information on grandchildren processes in certain cases. I have an application, AllTray, that I am trying to get to work in certain strange cases where its subprocess spawns a child and then dies. AllTray's job is essentially to d...

What is the purpose of fork()?

In many programs and man pages of Linux, I have seen code using fork(). Why do we need to use fork() and what is its purpose? ...

What is the easiest way to see if a process with a given pid exists in Python?

In a POSIX system, I want to see if a given process (PID 4356, for example) is running. It would be even better if I could get metadata about that process. ...

How does one easily add posix support to PHP using yum?

I am running CentOS 5.2 and using yum to manage packages. I have had little luck installing php-posix but know with almost 100% certitude that it is a real and available package...somewhere. Has anyone had luck installing it? FWIW, I am using the following: sudo yum install -y php-posix Update: I've realized that this may be an issue ...

Separating objects and source with a makefile

Hi, I have been having troubles getting my makefiles to work the way I want. First off, I would like to say this is POSIX make, as in http://www.opengroup.org/onlinepubs/009695399/utilities/make.html I am needing my build system to work with both BSDs and GNUs(Linux). What I am wanting is a zero maintenance makefile. I want it to just c...

Is there a utility that will convert POSIX to PCRE for PHP?

Is there a utility that will convert POSIX to PCRE for PHP? I'm somewhat confused by the PHP manual on PCRE, and while I'll try to find more info on PCRE, I was wondering if anyone had designed such a utility. Or, if anyone would explain how to convert the following, that would also be fine: ereg("^#[01-9A-F]{6}$", $sColor) But pleas...

Multiline Matching in Haskell Posix

I can't seem to find decent documentation on haskell's POSIX implementation. Specifically the module Text.Regex.Posix. Can anyone point me in the right direction of using multiline matching on a string? A snippet for the curious: > extractToken body = body =~ "<textarea[^>]*id=\"wpTextbox1\"[^>]*>(.*)</textarea>" :: String I'm tryi...

Method to intercept child process filesystem activity

I have a small command-line application written in C that acts as a wrapper/launcher for other programs (think: xargs). The application is written to compile on FreeBSD/Linux (via fork()/exec()) and Windows (CreateProcess()). In addition to being able to intercept, inject, or otherwise manipulate the command-line arguments for the chil...