posix

Terminate threads when SIGINT is called - C

谁说胖子不能爱 提交于 2021-02-10 14:51:05
问题 I'm building a generic program written in C-UNIX (using Linux so I don't care about BSD or WIN functions), that creates two threads to handle the communication with a server. void init_threads(int socket_desc) { pthread_t chat_threads[2]; ret = pthread_create(&chat_threads[0], NULL, receiveMessage, (void*)(long)socket_desc); PTHREAD_ERROR_HELPER(ret, "Errore creazione thread ricezione messaggi"); ret = pthread_create(&chat_threads[1], NULL, sendMessage, (void*)(long)socket_desc); PTHREAD

Capturing stdout/stderr separately and simultaneously from child process results in wrong total order (libc/unix)

天涯浪子 提交于 2021-02-10 14:43:49
问题 I'm writing a library that should execute a program in a child process, capture the output, and make the output available in a line by line (string vector) way. There is one vector for STDOUT, one for STDERR, and one for "STDCOMBINED", i.e. all output in the order it was printed by the program. The child process is connected via two pipes to a parent process. One pipe for STDOUT and one for STDERR. In the parent process I read from the read-ends of the pipes, in the child process I dup2() 'ed

Capturing stdout/stderr separately and simultaneously from child process results in wrong total order (libc/unix)

倖福魔咒の 提交于 2021-02-10 14:42:20
问题 I'm writing a library that should execute a program in a child process, capture the output, and make the output available in a line by line (string vector) way. There is one vector for STDOUT, one for STDERR, and one for "STDCOMBINED", i.e. all output in the order it was printed by the program. The child process is connected via two pipes to a parent process. One pipe for STDOUT and one for STDERR. In the parent process I read from the read-ends of the pipes, in the child process I dup2() 'ed

Do we need to call wordfree upon wordexp failure?

对着背影说爱祢 提交于 2021-02-08 19:38:27
问题 Do we need to call wordfree upon wordexp failure? Calling wordfree seems to segfault in some cases (eg when wordfree returns error code with string is "foo 'bar"). This isn't clear from man page, and I've seen wordfree used in some error cases. 回答1: According to the GNU's manual example, it should be called on error only if WRDE_NOSPACE was returned: switch (wordexp (program, &result, 0)) { case 0: /* Successful. */ break; case WRDE_NOSPACE: /* If the error was WRDE_NOSPACE, then perhaps part

Visibility of change to shared memory from shm_open() + mmap()

血红的双手。 提交于 2021-02-08 08:38:21
问题 Let's say I am on CentOS 7 x86_64 + GCC 7. I would like to create a ringbuffer in shared memory. If I have two processes Producer and Consumer, and both share a named shared memory, which is created/accessed through shm_open() + mmap(). If Producer writes something like: struct Data { uint64_t length; char data[100]; } to the shared memory at a random time, and the Consumer is constantly polling the shared memory to read. Will I have some sort of synchronization issue that the member length

Linux newbie: Linux vs POSIX manual

こ雲淡風輕ζ 提交于 2021-02-07 18:47:48
问题 $ apropos mkfifo mkfifo (1) - make FIFOs (named pipes) mkfifo (1posix) - make FIFO special files mkfifo (3) - make a FIFO special file (a named pipe) mkfifo (3posix) - make a FIFO special file mkfifoat (3) - make a FIFO (named pipe) relative to a directory file ... So I have man pages for Linux Programmer’s Manual and POSIX Programmer’s Manual. Which should I prefer and why? (I'm writing a Linux application, no plans to port it AIX, BSD etc) Thanks. 回答1: Basically, the linux manuals are

Why does the C runtime on Mac OS allow both precomposed and decomposed UTF-8?

六月ゝ 毕业季﹏ 提交于 2021-02-07 08:38:52
问题 So we all know that the filesystem on Mac OS has this wacky feature of using fully decomposed UTF-8. If you call POSIX APIs like realpath() , for example, you'll get such a fully decomposed UTF-8 string back from Mac OS. When using APIs like fopen() , however, passing precomposed UTF-8 seems to work as well. Here is a little demo program which attempts to open a file named ä . The first call to fopen() passes a precomposed UTF-8 string, the second call passes a decomposed UTF-8 string and to

Is wget or similar programs always available on POSIX systems?

心不动则不痛 提交于 2021-02-06 14:58:27
问题 Is there an HTTP client like wget/lynx/GET that is distributed by default in POSIX or *nix operating systems that could be used for maximum portability? I know most systems have wget or lynx installed, but I seem to remember installing some Ubuntu server systems using default settings and they had neither wget or lynx installed in the base package. I am writing a shell script for Linux (and probably Mac) to install a piece of software onto the computer. To prevent having to distribute a

How to bypass an ampersand (&) without using quotes when receiving an URL as an argument to a shell script?

余生颓废 提交于 2021-02-05 12:31:06
问题 I'm building a shell script (trying to be POSIX compliant) and I'm stuck in an issue. The script is supposed to receive an URL and do some things with it's content. myscript www.pudim.com.br/?&args=ok The thing is, the ampersand symbol is interpreted as a command additive, and giving to my script only the www.pudim.com.br/? part as an argument. I know that the right workaround would be to surround the URL with quotes but, because I need to use this script several times in a row, I wanted to

Scanf is not waiting for input

。_饼干妹妹 提交于 2021-02-05 11:51:07
问题 I know scanf waits for input. But in this program I have written it is printing hello in an infinite loop. Its not waiting for me to enter. #include <signal.h> #include <stdio.h> #include <string.h> #include <sys/time.h> #include<unistd.h> void timer_handler (int signum) { static int count = 0; printf ("timer expired %d times\n", ++count); } int main () { struct sigaction sa; struct itimerval timer; memset (&sa, 0, sizeof (sa)); sa.sa_handler = &timer_handler; sigaction (SIGALRM, &sa, NULL);