posix

What is the Difference Between read() and recv() , and Between send() and write()?

会有一股神秘感。 提交于 2019-12-29 02:21:07
问题 What is the difference between read() and recv() , and between send() and write() in socket programming in terms of performances, speed and other behaviors? 回答1: The difference is that recv() / send() work only on socket descriptors and let you specify certain options for the actual operation. Those functions are slightly more specialized (for instance, you can set a flag to ignore SIGPIPE , or to send out-of-band messages...). Functions read() / write() are the universal file descriptor

Can I call accept() for one socket from several threads simultaneously?

ⅰ亾dé卋堺 提交于 2019-12-28 18:13:07
问题 I am using Linux 3.2.0, x86_64. Can I call accept() for one socket from several threads simultaneously? 回答1: Yes, you can call accept() on the same listening socket from multiple threads and multiple process though there might not be as much point to it as you think. The kernel will only allow one to succeed. When this is done with processes it is known as pre-forking and it saves the expense of a fork() for every new connection. But when you are dealing with threads you can more easily have

Using Struct Stat()

北慕城南 提交于 2019-12-28 15:22:13
问题 I'm trying to figure out how exactly to use stat() to capture information about a file. What I need is to be able to print several fields of information about a file. So.. #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> using namespace std; int main() { struct stat buf; stat("file",&buf); ... cout << st_dev << endl; cout << st_ino << endl; cout << st_mode << endl; cout << st_nlink << endl; cout << st_uid << endl; cout << st_gid << endl; cout << st_rdev <<

Linux shared memory: shmget() vs mmap()?

三世轮回 提交于 2019-12-28 07:39:14
问题 In this thread the OP is suggested to use mmap() instead of shmget() to get shared memory in Linux. I visited this page and this page to get some documentation, but the second one gives an obscure example regarding mmap() . Being almost a newbie, and needing to share some information (in text form) between two processes, should I use the shmget() method or mmap() ? And why? 回答1: Both methods are viable. mmap method is a little bit more restrictive then shmget , but easier to use. shmget is

POSIX sh equivalent for Bash’s printf %q

南楼画角 提交于 2019-12-28 05:33:24
问题 Suppose I have a #!/bin/sh script which can take a variety of positional parameters, some of which may include spaces, either/both kinds of quotes, etc. I want to iterate "$@" and for each argument either process it immediately somehow, or save it for later. At the end of the script I want to launch (perhaps exec ) another process, passing in some of these parameters with all special characters intact. If I were doing no processing on the parameters, othercmd "$@" would work fine, but I need

How do I synchronize access to shared memory in LynxOS/POSIX?

不打扰是莪最后的温柔 提交于 2019-12-28 05:31:09
问题 I am implementing two processes on a LynxOS SE (POSIX conformant) system that will communicate via shared memory. One process will act as a "producer" and the other a "consumer". In a multi-threaded system my approach to this would be to use a mutex and condvar (condition variable) pair, with the consumer waiting on the condvar (with pthread_cond_wait ) and the producer signalling it (with pthread_cond_signal ) when the shared memory is updated. How do I achieve this in a multi-process,

Win32 API analog of sending/catching SIGTERM

拟墨画扇 提交于 2019-12-28 04:23:11
问题 Under POSIX OS there is signal API that allows to send a signal to process to shut it down with kill and you can catch it with sigaction and do what you need; However, Win32 is not POSIX system, so: How can I handle shutdown events that may come, for example from "End Process" in "Task manager"? What is the standard API for sending shutdown signal to Win32 application? I'm not talking about GUI, I'm talking about TCP/IP server that should be nicely shutdown. that does not run like windows

What is path //, how is it different from /

放肆的年华 提交于 2019-12-28 04:23:08
问题 We know root directory is /, and according to posix, there is another directory // which differs from /. When you ls / and ls //, the output is the same, so as stat, however if you cd / and cd //, they are different, though the directory content are the same. That really confused me. Anyone got an answer? 回答1: From Bash FAQ: E10) Why does `cd //' leave $PWD as `//'? POSIX.2, in its description of `cd', says that *three* or more leading slashes may be replaced with a single slash when

How are the O_SYNC and O_DIRECT flags in open(2) different/alike?

你离开我真会死。 提交于 2019-12-28 02:24:12
问题 The use and effects of the O_SYNC and O_DIRECT flags is very confusing and appears to vary somewhat among platforms. From the Linux man page (see an example here), O_DIRECT provides synchronous I/O, minimizes cache effects and requires you to handle block size alignment yourself. O_SYNC just guarantees synchronous I/O. Although both guarantee that data is written into the hard disk's cache, I believe that direct I/O operations are supposed to be faster than plain synchronous I/O since they

How to redirect the output back to the screen after freopen(“out.txt”, “a”, stdout)

戏子无情 提交于 2019-12-27 22:09:33
问题 #include <stdio.h> int main() { printf("This goes to screen\n"); freopen("out.txt", "a", stdout); printf("This goes to out.txt"); freopen("/dev/stdout", "a", stdout); printf("This should go to screen too, but doesn't\n"); return 0; } I call freopen to redirect the stdout to out.txt then I print something on the file, now I want to redirect it back to the screen, but freopen("/dev/stdout", "a", stdout); doesn't work. Is there any way to do that using ANSI C or POSIX system calls? 回答1: I can't