unix-programming

Getting the Program Process ( Service and Daemon ) on Linux in C

I'd like to know how one would create an application that starts in the background. Im currently creating a Webserver in C as a little project, both to learn some old C and Linux Socket Programming. But my current concern is; How do i get the current process number? I want to get this because when i start the process, i want to displa...

File based Configuration handling in C ( Unix )

This is probably one of the most common tasks / problems when programming; You need to store the configuration of your application somewhere. While im trying to create a webserver and / or other applications, I'd like to keep the code as clean as possible since my main interest in programming is Architecture. This results in me wanting ...

How to copy a few chars from a char[] to a char* in C?

Yo! I'm trying to copy a few chars from a char[] to a char*. I just want the chars from index 6 to (message length - 9). Maybe the code example will explain my problem more: char buffer[512] = "GET /testfile.htm HTTP/1.0"; char* filename; // I want *filename to hold only "/testfile.htm" msgLen = recv(connecting_socket, buffer, 512, 0...

Get MIME type from filename in C

I want to get the MIME type from a filename using C. Is there a way to do this without using a textfile containing MIME types and file extensions (i.e. Apache's file mime.types)? Maybe there is a function to get the MIME type using the filename? I rather not use the file extension if I don't have to. ...

C, HTTP 1.1 and Socket Send troubles

As an addition to my previous post on Creating a web server in pure C, I'm having some trouble with the Sending function. Here's two code snippets: int Send(char *message) { int length, bytes_sent; length = strlen(message); bytes_sent = send(connecting_socket, message, length, 0); return bytes_sent; } ...

Converting to Multi-Threaded Socket Application

As I am currently doing this project in only C, I've up untill this point only used my webserver as a single threaded application. However, I dont want that anymore! So I have the following code that handles my Work. void BeginListen() { CreateSocket(); BindSocket(); ListenOnSocket(); while ( 1 ) ...

Get info for Common Log Format from socket in C

I want to create a log file for my webserver written in C under UNIX. The logfile should be formatted according to Common Log Format, but I don't know how to get the ident and authuser from the connecting socket. Is there a simple way to get these from the socket? ...

Problem with Closing Sockets. Program Halts.

I am having some trouble with this code. The problem is when i Request the Server to send me some data and the client just Disconnects when the server tries to send me data, the application Exists. Here's the lines I think cause the problem int SendBinary(int *byte, int length) { int bytes_sent; bytes_sent = send(connecting_so...

What's the equivalent of Windows' QueryPerformanceCounter on OSX?

I'm porting a library from Windows to *NIX (currently OSX), does anyone now what function can I use instead of Microsoft's QueryPerformanceCounter and QueryPerformanceFrequency? ...

File format of spool file

Hi, I wrote a shell script and created a spool file during programming for saving the data. By default file is saved in ANSI format. But I want to save the generated file in UTF 16 format so that all the special characters will properly saved. Waiting for a quick reply!! Thanks in advance. Abhijeet ...

Hanging a Child Process

I am trying to test out my system and wish to emulate a condition, where the child process gets hung. For doing this, I am trying to attach the child process to GDB and putting a break on it. But things don't seem to be going as expected. Also, in the same vein, how do I know that a spawned child process is not progressing, but is hung?...

Will static linking on one unix distribution work but not another?

If I statically link an executable in ubuntu, is there any chance that that executable won't work within another distribution such as mint os? or fedora? I know processor types are affected, but other then that is there anything else I have to be wary of? Sorry if this is a dumb question. Thanks for any help ...

C++ - How to set file permissions (cross platform)

Hi guys. I am using C++ ofstream to write out a file. I want to set the permissions to be only accessible by the user: 700. In unix; I suppose I can just issue a system("chmod 700 file.txt"); but I need this code to work on windows as well. I can use some windows api; but what is the best c++ cross platform way to do this? ...

Creating a project, from Makefile to static/dynamic libraries in UNIX

Guys, would you describe a few things about c++ building blocks, on unix. I want to create an application that links against static libs and dynamic libs (.so). Question 1: How do I create static library using gcc/g++ ?How do I make my app link against it. Question 2: How to specify it in the makefile, linking against static and dy...

msync equivalent in Windows

what is the equivalent of msync [unix sys call] in windows? I am looking for MSDN api in c,C++ space. More info on msync can be found at http://opengroup.org/onlinepubs/007908799/xsh/msync.html ...

Remove file in C++ under UNIX

How do you guys typically delete files on Linux OS? I am thinking of using the unlink function call, but I wonder if you have a better idea, as the C++ standard has no mention of file deletion operation and it is system dependent. ...

Reference for proper handling of PID file on Unix

Where can I find a well-respected reference that details the proper handling of PID files on Unix? On Unix operating systems, it is common practice to “lock” a program (often a daemon) by use of a special lock file: the PID file. This is a file in a predictable location, often ‘/var/run/foo.pid’. The program is supposed to check when i...

How to limit I/O consumption of Python processes (possibly using ionice)?

I would like a particular set of Python subprocesses to be as low-impact as possible. I'm already using nice to help limit CPU consumption. But ideally I/O would be limited as well. (If skeptical, please humor me and assume there is value in doing this; it doesn't matter how long they take to run, there can be a lot of them, and there...

Using SO_REUSEADDR - What happens to previously open socket?

Hi Everyone, In network programming in unix, I have always set the SO_REUSEADDR option on the socket being used by server to listen to connections on. This basically says that another socket can be opened on the same port on the machine. This is useful when recovering from a crash and the socket was not properly closed - the app can be ...

Sed: Change case of substitution group

How can I change the case of a matching group from lower to uppercase with sed Unix command? Thanks Martin ...