posix

Which “fatal” signals should a user-level program catch?

a 夏天 提交于 2019-12-21 04:34:13
问题 First of all, I do know that there was a similar question here in the past. But that question wasn't answered properly. Instead, it diverted into suggestion what to do to catch signals. So just to clarify: I've done whatever needs to be done to handle signals. I have an application that forks a daemon that monitors the main process through pipe. If a main process crashes (e.g. segmentation fault), it has a signal handler that writes all the required info to pipe and aborts. The goal is to

Detect if stdout is redirected to a pipe (not to a file, character device, terminal, or socket)?

青春壹個敷衍的年華 提交于 2019-12-21 04:26:10
问题 Ideally, this would be scriptable in shell, but Perl or Python would be fine. C code could be helpful, but probably fails cost/benefit. I recognize that redirection to a FIFO (named pipe) may be indistinguishable from a real pipe, and that is enough of an edge case that I don't really care. Strict POSIX solutions are best, UNIX/Linux variant-independent are next best, but at least something that works on Darwin (MacOS X) is what I need right now. Before you write your answer - I already know

Is reading /dev/urandom thread-safe?

佐手、 提交于 2019-12-21 04:18:09
问题 This is the code: unsigned int number; FILE* urandom = fopen("/dev/urandom", "r"); if (urandom) { size_t bytes_read = fread(&number, 1, sizeof(number), urandom); DCHECK(bytes_read == sizeof(number)); fclose(urandom); } else { NOTREACHED(); } If not, how do I make it thread-safe? 回答1: As long as each execution of the function is in its own thread (i.e., the local variables number , urandom , bytes_read are not shared between threads), I don't see any thread-safety problems. Each thread will

open() not setting file permissions correctly [duplicate]

无人久伴 提交于 2019-12-21 04:09:04
问题 This question already has answers here : Why are the file permissions changed when creating a file with the open system call on Linux? (3 answers) Closed 4 months ago . I create a file using the code below: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> int main() { const char* filename = "./test.out"; int fd; if(-1 == (fd = open(filename, O_CREAT|O_RDWR, 0666))) { perror("Error"); errno = 0; } else

Can a pipe in Linux ever lose data?

好久不见. 提交于 2019-12-21 03:47:21
问题 And is there an upper limit on how much data it can contain? 回答1: Barring a machine crash, no it can't lose data. It's easy to misuse it and think you're losing data however, either because a write failed to write all the data you requested and you didn't check the return value or you did something wrong with the read. The maximum amount of data it can hold is system dependent -- if you try to write more than that, you'll either get a short write or the writer will block until space is

Can a pipe in Linux ever lose data?

故事扮演 提交于 2019-12-21 03:47:05
问题 And is there an upper limit on how much data it can contain? 回答1: Barring a machine crash, no it can't lose data. It's easy to misuse it and think you're losing data however, either because a write failed to write all the data you requested and you didn't check the return value or you did something wrong with the read. The maximum amount of data it can hold is system dependent -- if you try to write more than that, you'll either get a short write or the writer will block until space is

If fclose(0) is called, does this close stdin?

∥☆過路亽.° 提交于 2019-12-21 03:42:54
问题 If fclose(0) is called, does this close stdin? The reason why I'm asking this is that for some reason, stdin is being closed in my application and I cannot figure out why. I checked for fclose (stdin) and this is not in the application and so I was wondering if fclose(0) could cause undefined behaviour such as closing stdin? If not, what are other ways that stdin could be erroneously closed? 回答1: The signature of fclose is this: int fclose ( FILE * stream ); That means, fclose expects a

Waiting on multiple events C++

杀马特。学长 韩版系。学妹 提交于 2019-12-21 02:19:26
问题 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 different input source? Thanks 回答1: You can listen on multiple file descriptors without using multiple threads using the select(2) system call. You can use

POSIX sh EBNF grammar

寵の児 提交于 2019-12-20 14:21:45
问题 Is there an existing POSIX sh grammar available or do I have to figure it out from the specification directly? Note I'm not so much interested in a pure sh; an extended but conformant sh is also more than fine for my purposes. 回答1: The POSIX standard defines the grammar for the POSIX shell. The definition includes an annotated Yacc grammar. As such, it can be converted to EBNF more or less mechanically. If you want a 'real' grammar, then you have to look harder. Choose your 'real shell' and

revisiting “how do you use aio and epoll together”

不羁的心 提交于 2019-12-20 12:27:08
问题 following the discussion at How do you use AIO and epoll together in a single event loop?. There are in fact 2 "aio" APIs in linux. There's POSIX aio (the aio_* family of functions), included in glibc and libaio developed I believe by RedHat (?), the io_* family. The first one allows registration of notification requests via aio_sigevent aiocb member. That can be easily integrated with ppoll()/pselect() event loops. If you want to integrate POSIX aio with epoll() then you need to translate