posix

Scanf is not waiting for input

倖福魔咒の 提交于 2021-02-05 11:50:24
问题 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);

Simple synchronization with C signals

这一生的挚爱 提交于 2021-02-05 06:40:31
问题 I'm trying to solve an exercise which requires that : "the starting process must fork two times. The father and the children must synchronize to write, one after another, in the first position of a temporary file reading the characters written on three different files (one for each process). The program must use signals to implement the synchronization mechanism." So far i've tried to solve this by doing so : P1 (the father) starts reading/writing first. Before stopping himself (through a

Is it safe to call dlclose(NULL)?

怎甘沉沦 提交于 2021-02-05 02:52:52
问题 I experience a crash when I pass a null pointer to dlclose . Should I check for null before calling dlclose ? POSIX tells nothing about this: http://pubs.opengroup.org/onlinepubs/7908799/xsh/dlclose.html Is it undefined behaviour or a bug in dlclose implementation? 回答1: This is tricky. POSIX states that if handle does not refer to an open object, dlclose() returns a non-zero value from which you could infer that it should detect, for an arbitrary pointer, whether that pointer refers to an

Remove all punctuation except underline between characters in R with POSIX character class

点点圈 提交于 2021-02-04 19:45:48
问题 I would like to use R to remove all underlines expect those between words. At the end the code removes underlines at the end or at the beginning of a word. The result should be 'hello_world and hello_world' . I want to use those pre-built classes. Right know I have learn to expect particular characters with following code but I don't know how to use the word boundary sequences. test<-"hello_world and _hello_world_" gsub("[^_[:^punct:]]", "", test, perl=T) 回答1: You can use gsub("[^_[:^punct:]]

Can you portably read sensitive input from the commandline?

扶醉桌前 提交于 2021-02-04 19:40:07
问题 The bash builtin read has a flag -s that prevents it from echoing whatever is being read from the commandline. After searching opengroup.org and filtering through all the other meanings for read , I still haven't found a POSIX/portable equivalent. Is there a reasonable way to do this? In bash it's easy enough: $ bash -c 'read -sp "What is your password? " password; printf "\n%s\n" "$password"' What is your password? I'll never tell! But in sh… $ dash -c 'printf "What is your password? "; read

Allocating initialized, aligned memory

笑着哭i 提交于 2021-02-04 17:23:27
问题 I'm writing a program (in C++) in which I need to allocate arrays whose starting addresses should be aligned with the cache line size. When I allocate these arrays I also want the memory initialized to zero. Right now I have it working using the posix_memalign function. This works well for getting memory aligned arrays but the arrays are uninitilized. Is there a better function I can use to zero out the arrays when I initialize them or do I just have to settle for writing a separate loop to

POSIX: Return value from write() on UDP socket

你说的曾经没有我的故事 提交于 2021-01-29 10:29:38
问题 On a POSIX system, if sock_fd is an UDP socket, will write(sock_fd, data, size) always return size or -1? I.e., if write() does not fail, will the requested chunk of data always be written in its entirety, implying that, if the return value is not -1, then I can always ignore the actual return value? It seems to me that this should be the case, but no man page seems to state it clearly. EDIT: I suppose there are two possible answers. Either it is stated somewhere, but I haven't found it, or

Initializing shared memory using mmap() for a 2D array, is it necessary to also map memory for subsequent pointers? Should I use shm instead?

元气小坏坏 提交于 2021-01-29 08:48:51
问题 I am using mmap() to initialize a shared memory space for parent and child processes and the object happens to be a double char pointer (i.e. char** ). It's necessary because I'm going to be storing user input to this 2D array in a child process and the parent will be accessing this same data after the child has terminated. It wasn't hard to work this out after reading a little bit of documentation on mmap() , and it feels a lot like malloc() , but less of a commitment. Consider the code

Why does field splitting not occur after parameter expansion in an assignment statement in shell?

為{幸葍}努か 提交于 2021-01-29 04:00:48
问题 Consider the following two assignments. $ a="foo bar" $ b=$a $ b=foo bar bash: bar: command not found Why does the second assignment work fine? How is the second command any different from the third command? I was hoping the second assignment to fail because b=$a would expand to b=foo bar Since $a is not within double-quotes, foo bar is not quoted, therefore field-splitting should occur (as per my understanding) which would result in b=foo to be considered an assignment and bar to be a

R POSIX %H:%M:%S Time Average

[亡魂溺海] 提交于 2021-01-28 12:04:40
问题 I have a data frame of character times that I need to average by provider, but I'm not sure how to average them using just the time without the date. For the example below: provider time USA 9:26:46 USDA 9:26:18 USDA 9:10:17 OIL 10:00:00 USA 6:20:56 USDA 7:19:13 OIL 11:00:00 The correct output for OIL would be the average between 10:00 and 11:00, and would look like: provider average OIL 10:30 Does anyone know how to average just time without incorporating date using POSIX? 回答1: mean works as