Hot answers tagged c
101
Summary: The primary reason for switching from GCC to Clang is the incompatibility of GCC's GPL v3 license with the goals of the FreeBSD project. There are also political issues to do with corporate investment, as well as user base requirements. Finally, there are expected technical advantages to do with standards compliance and ease of debugging. Real world ...
58
You can't just ./fork.c (it's not a program; it's the source for a program): this assumes that the file is a script (which it isn't) and treats it accordingly. (However, as noted in another answer, there are compilers (like Tiny C Compiler) that can execute C code without explicitly compiling it)
Since it's a C program, you have to compile the program. Try ...
21
That's not a program, that's the source code for a program.
C is a compiled language, meaning it must be "compiled" into machine-readable instructions before you can run it. As you are using C, the "C Compiler" (cc) can do this.
cc -o fork for.c # compile the code
chmod +x fork # ensure it it executable
./fork # run the compiled program
...
16
The new process will be created within the fork() call, and will start by returning from it just like the parent. The return value (which you stored in retval) from fork() will be:
0 in the child process
The PID of the child in the parent process
-1 in the parent if there was a failure (there is no child, naturally)
Your testing code works correctly; it ...
14
You're right that you'll end up with the same executable at the end (albeit with a different name); in the first case gcc will actually create a bunch of temporary object files that it removes after linking, versus the second case where you're making the object files yourself.
The main reason to do things the second way is to allow for incremental building. ...
10
One thing worth considering is that FreeBSD is currently using GCC 4.2.1 as noted above thus the performance comparisons aren't of 4.5 or even 4.6 aren't truly relevant to the project. Therefore, the questions you should be asking are:
What are the performance gains of the new Clang vs the older GCC that the project uses?
How do the same binaries compiled ...
10
Because getc() buffers the read data before returning it, so a call to getc() does not necessarily result in a call to read(). read() is a system call, which takes much longer time to accomplish than a normal function call because the kernel has more operations to do. When you enter the kernel space, it changes your stack, saves all the context, deals with ...
9
I thought that fork() creates a same process, so I initially that that in that program, the fork() call would be recursively called forever. I guess that new process created from fork() starts after the fork() call?
Yes. Let's number the lines:
int main (int argc, char **argv)
{
int retval; /* 1 */
...
9
t_open() and its associated /dev/tcp and such are part of the TLI/XTI interface, which lost the battle for TCP/IP APIs to BSD sockets.
On Linux, there is a /dev/tcp of sorts. It isn't a real file or kernel device. It's something specially provided by Bash, and it exists only for redirections. This means that even if one were to create an in-kernel /dev/tcp ...
8
Man pages for the standard C library are included in the man-pages package. For the C++ STL library the man-pages and HTML documentation are included in the libstdc++-docs packages. Thus,
yum install man-pages libstdc++-docs
should install them. You can test if they are available via:
man std::iostream
man fopen
Kind of off-topic: IMHO the libstdc++ ...
8
The standard way to log from a C program is syslog.
Start by including the header file:
#include <syslog.h>
Then early in your program, you should configure syslog by calling openlog:
openlog("programname", 0, LOG_USER);
The first argument is the identification or the tag, which is automatically added at the start of each message. Put your ...
8
A device that has the functions of a physical terminal without actually being one. Created by terminal emulators such as xterm. More detail is in the manpage pty(7).
Traditionally, UNIX has a concept of a controlling terminal for a group of processes, and many I/O functions are built with terminals in mind. Pseudoterminals handle, for example, some control ...
8
There are several possibilities.
One method is to compile using
:!gcc file.c
But a nicer strategy would be to have a Makefile and compile just using
:make
Where the easiest Makefile would look like.
program:
gcc file.c
Others can explain this a lot better.
7
You could move the espeak binary to something like espeak-real, and replace it with a small script that sets LD_PRELOAD before exec'ing espeak-real.
#! /bin/bash
export LD_PRELOAD=/your/lib.so
exec espeak-real "$@"
(stdin/out/err redirections take care of themselves.)
7
The start address is the address of main(), right?
Not really: The start of a program isn't really main(). By default, GCC will produce executables whose start address corresponds to the _start symbol. You can see that by doing a objdump --disassemble Q1. Here's the output on a simple program of mine that only does return 0; in main():
0000000000400e30 ...
6
In the C API, system calls return a negative value to indicate an error, and the error code in errno gives more information on the nature of the error. Your man page should explain the possible errors on your system. There are two standard error codes:
EAGAIN indicates that the new process cannot be created due to a lack of available resources, either ...
6
Use either skill -u UID or pkill -u UID.
Skill was a linux-specific and is now outdated, and pkill is more portable (Linux, Solaris, BSD)
I think, both utilites have no access to any list other than full list of processes (or readdir of /proc). I think, they itterate over /proc and check every process for match.
PS: Another tool is killall .
To get list ...
6
I'm a pretty hard-core Emacs user but still, for developing C++ I prefer Qt-Creator (don't be afraid because of the name, it works well for non-QT-projects) as Emacs lacks good project support and stable code completion
The pros:
Can import CMakeFiles.txt into an automatically created project
Best code completion you'll find on Linux, sometimes even ...
6
Neither. If you want to have it behave properly like a real daemon you should place it using the init system - /etc/init.d ( and make appropriate runlevel links in the appropriate /etc/rc.X folders )
Run a search or have a look at something like this: http://serverfault.com/questions/204695/comprehensive-guide-to-init-d-scripts
5
As seen earlier, vfork does not allow the child process to access the parent's memory. exit is a C library function (that's why it's often written as exit(3)). It performs various cleanup tasks such as flushing and closing C streams (the files open through functions declared in stdio.h) and executing user-specified functions registered with atexit. All these ...
5
This has more to do with C and C++ than Unix, and as such belongs to SO.
To answer your question, the <> indicates headers in the standard library and "" the libraries written specifically for the project.
From the K&R:
Any source line of the form
#include "filename"
or
#include <filename>
is replaced by the contents ...
4
The exact rules followed by the gcc compiler for finding include files are explained at: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
A quick command-line trick to find out where an include file comes from is the following:1
echo '#include <unistd.h>' | gcc -E -x c - > unistd.preprocessed
Then, if you look at the unistd.preprocessed file, ...
4
The answer is in the name -- "Pseudo" meaning "not genuine but having the appearance of".
With the first terminals, there was always a piece of hardware attached with associated device, be it display hardware or a serial port.
With xwindows, telnet and ssh, there came a need for software "Pseudo devices" to do the job of standing in for display hardware. ...
4
You can use the Glibc manual as a reference. It's not for absolute beginners, but if you're reasonably fluent in C you should be able to read a section and write a working program.
You can find the source of the GNU tools on the GNU website; the easiest way to get it is to obtain the source packages on your Linux distribution (e.g. apt-get source coreutils ...
4
If you have an executable and you want to see where it's picking up libraries, run
ldd /path/to/executable
This will account for libraries on the default search path as well as libraries in this executable's rpath if any.
On Linux, paths to system libraries are cached for efficiency. /sbin/ldconfig -p displays the contents of the cache (it's stored in ...
4
It most likely means that your system doesn't have the development portion of the threading library installed. You can find out what thread -pthread flag does on your platform with the following command:
gcc -dumpspecs | grep "%{pthread"
I get this:
%{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT}
%{pthread:-lpthread} %{shared:-lc} ...
4
To build an interactive application you can open /dev/tty, it will return a file descriptor to the controlling terminal:
int ttyfd = open("/dev/tty", O_RDWR);
You can use it instead of STDIN_FILENO or STDOUT_FILENO (those could be redirected to something different than the terminal when the program is started).
Here is some example:
#include ...
4
Ext4 can use 1kB, 2kB or 4kB as the block size; as far as I know the default on Ubuntu is 4kB. Note that here, a block is the size of a file chunk, which is constant for a given filesystem. The file you describe has two blocks that are not zeroes: the one containing hello (surrounded by a bunch of zeroes — 3616 before and 474 after), and the one containing ...
4
Well-linked one is e.g. GNU libc documentation:
http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?%28libc.info.gz%29Top
(or its equivalent installed locally)
See "file system interface" from the top page.
Man pages carry hyperlinks but you need proper man browser instead of default one. For console, try pinfo, it provides very simple ...
4
Before reading the other responses, my guess was that pid_t exists for portability reasons. In the Good Ol' Days, some Unixes had short PIDs, others had int PIDs, so you define a system-specific type for PID. I can't recall any pain involved in using int for a file descriptor, even in the very early days of 64-bitness.
Only top voted, non community-wiki answers of a minimum length are eligible