Pages

Thursday, June 23, 2011

stdin, stdout, and stderr

the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively.

Wednesday, June 22, 2011

What is /dev/shm and its practical usage

/dev/shm is nothing but implementation of traditional shared memoryconcept. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes (if permitted) can access. This will result into speeding up things on Linux.
shm / shmfs is also known as tmpfs, which is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but one which uses virtual memory instead of a persistent storage device.
If you type mount command you will see /dev/shm as a tempfs file system. Therefore, it is a file system, which keeps all files in virtual memory. Everything in tmpfs is temporary in the sense that no files will be created on your hard drive. If you unmount a tmpfs instance, everything stored therein is lost. By default almost all Linux distros configured to use /dev/shm.

Nevertheless, where can I use /dev/shm?

You can use /dev/shm to improve the performance of application software or overall Linux system performance. On heavily loaded system, it can make tons of difference. For example VMware workstation/server can be optimized to improve your Linux host's performance (i.e. improve the performance of your virtual machines).
For example, if you have 8GB RAM then remount /dev/shm as follows:
# mount -o remount,size=8G /dev/shm
To be frank, if you have more than 2GB RAM + multiple Virtual machines, this hack always improves performance.
# mount -t tmpfs -o size=5G,nr_inodes=5k,mode=700 tmpfs /disk2/tmpfs
Above will give you tmpfs instance on /disk2/tmpfs which can allocate 5GB RAM/SWAP in 5K inodes and it is only accessible by root.

Friday, June 17, 2011

POSIX Semaphores

The potential learning curve of System V semaphores is much higher when compared to POSIX semaphores. This will be more understandable after you go through this section and compare it to what you learned in the previous section.

To start with, POSIX comes with simple semantics for creating, initializing, and performing operations on semaphores. They provide an efficient way to handle interprocess communication. POSIX comes with two kinds of semaphores: named and unnamed semaphores.

sem_overview

Name
sem_overview - Overview of POSIX semaphores

Description

POSIX semaphores allow processes and threads to synchronise their actions.
A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)). If the value of a semaphore is currently zero, then a sem_wait(3) operation will block until the value becomes greater than zero.
POSIX semaphores come in two forms: named semaphores and unnamed semaphores.

Thursday, June 16, 2011

lseek() - (C System Call)

lseek is a system call that is used to change the location of the read/write pointer of a file descriptor. The location can be set either in absolute or relative terms.

Required Include Files

Wednesday, June 15, 2011

Lessons learned from implementing Highrise's custom fields feature

We recently added custom fields to Highrise (below) which allow you to keep track of extra details beyond standard contact information like phone, email, address, etc.
After the launch, we had a “looking back” conversation to see what lessons could be learned from the process. Three of our findings:

Thursday, June 9, 2011

"Make'' Your Programs

A General Overview


make is a command generator. Using a description file and some general templates, it creates a sequence of commands for execution by the UNIX shell. These commands commonly relate to the maintenance of the files comprising a software development project. Here, ``maintenance'' refers to a whole array of tasks, ranging from status reporting and the purging of temporary files, to building the final, executable version of a complex group of programs.

The description file is usually named as Makefile or makefile. After this file is created, one can run the program make to make executables as described in the makefile:

make

Wednesday, June 8, 2011

gcc: Compilation Warning: incompatible implicit declaration of built-in function `exit’

reference: ttp://joysofprogramming.com/gcc-incompatible-implicit-declaration-exit/

Posted by Joys of Programming on in C/C++


The purpose of exit() as described by the man page
exit - cause normal process termination
Let’s make use of exit in a simple program
int main(int argc, char*argv[]){
    int status;
exit(status);
    return 0;
}
Now compile the program exit.c…
$ gcc exit.c

Formatting C / C++ code

Formatting C / C++ code

by: WaltP - Aug 11, 2005
(ref: http://www.gidnetwork.com/b-38.html)
To help understand the flow of your C/C++ code, whitespace (spaces, tabs, new lines) are all ignored by the compiler, which means you can scatter this whitespace all over your code and make it readable by everyone. This is especially helpful when asking for help on forums. Very few want to read code that has no formatting. It makes the code hard to understand. Also, lack of formatting make some errors nearly invisibe, like mismatched braces, parentheses, and so on.

background Processes and shared memory status

Background Processes and Shared Memory Status


Background and Foreground Processes

Starting a process in background is easy. Suppose we have a program named bg and another program named fg. If bg must be started in the background, then do the following:

bg &
If there is an & following a program name, this program will be executed as a background process. You can use Unix command ps to take a look at the process status report:

3719 ... info ...  program name
7156 ... info ...  program name
The ps command will generate some output similar to the above. At the beginning of each line, there is a number, the process ID, and the last item is a program name. If bg has been started successfully, you shall see a line with program name bg.To kill any process listed in the ps command's output, note its process ID, say 7156, then use the following

kill  7156
The program with process ID 7156 will be killed. If you use ps to inspect the process status output again, you will not see the process with process ID 7156.Note that any program you start with a command line is, by default, a foreground process. Thus, the following command starts fg as a foreground process:

fg
There is a short form to start both bg (in background) and fg (in foreground) at the same time:

bg & fg
With this technique, the server program can be started as a background process. After the message telling you to start the client, then start the client. The client can be background or a foreground process. In the following, the client is started as a foreground process:

server  -4  2  6  -10 &
client
Since the server and the client will display their output to the same window, you will see a mixed output. Or, you can start processes in different windows.

Checking Shared Memory Status

Before starting your next run, check to see if you have some shared memory segments that are still there. This can be done with command ipcs:

ipcs -m
A list of shared memory segments will be shown. Then, use command ipcrm to remove those un-wanted ones:

ipcrm -m xxxx
where xxxx is the shared memory ID obtained from command ipcs. Note that without removing allocated shared memory segments you may jeopardize the whole system.Use man ipcs and man ipcrm to read more about these two commands.

shmdt() and shmctl() - detaching and removing a shared memory segment

Detaching and Removing a Shared Memory Segment - shmdt() and shmctl()

System call shmdt() is used to detach a shared memory. After a shared memory is detached, it cannot be used. However, it is still there and can be re-attached back to a process's address space, perhaps at a different address. To remove a shared memory, use shmctl().
The only argument of the call to shmdt() is the shared memory address returned by shmat(). Thus, the following code detaches the shared memory from a program:

shmdt(shm_ptr);
where shm_ptr is the pointer to the shared memory. This pointer is returned by shmat() when the shared memory is attached. If the detach operation fails, the returned function value is non-zero.To remove a shared memory segment, use the following code:

shmctl(shm_id, IPC_RMID, NULL);
where shm_id is the shared memory ID. IPC_RMID indicates this is a remove operation. Note that after the removal of a shared memory segment, if you want to use it again, you should use shmget()followed by shmat().

Communicating Between Parent and Child

Communicating Between Parent and Child

The following main function runs as a server. It uses IPC_PRIVATE to request a private shared memory. Since the client is the server's child process created after the shared memory has been created and attached, the child client process will receive the shared memory in its address space and as a result no shared memory operations are required. Click here to download a copy of this file (shm-01.c).

#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>

void  ClientProcess(int []);

void  main(int  argc, char *argv[])
{
     int    ShmID;
     int    *ShmPTR;
     pid_t  pid;
     int    status;

     if (argc != 5) {
          printf("Use: %s #1 #2 #3 #4\n", argv[0]);
          exit(1);
     }

     ShmID = shmget(IPC_PRIVATE, 4*sizeof(int), IPC_CREAT | 0666);
     if (ShmID < 0) {
          printf("*** shmget error (server) ***\n");
          exit(1);
     }
     printf("Server has received a shared memory of four integers...\n");

     ShmPTR = (int *) shmat(ShmID, NULL, 0);
     if ((int) ShmPTR == -1) {
          printf("*** shmat error (server) ***\n");
          exit(1);
     }
     printf("Server has attached the shared memory...\n");

     ShmPTR[0] = atoi(argv[1]);
     ShmPTR[1] = atoi(argv[2]);
     ShmPTR[2] = atoi(argv[3]);
     ShmPTR[3] = atoi(argv[4]);
     printf("Server has filled %d %d %d %d in shared memory...\n",
            ShmPTR[0], ShmPTR[1], ShmPTR[2], ShmPTR[3]);

     printf("Server is about to fork a child process...\n");
     pid = fork();
     if (pid < 0) {
          printf("*** fork error (server) ***\n");
          exit(1);
     }
     else if (pid == 0) {
          ClientProcess(ShmPTR);
          exit(0);
     }

     wait(&status);
     printf("Server has detected the completion of its child...\n");
     shmdt((void *) ShmPTR);
     printf("Server has detached its shared memory...\n");
     shmctl(ShmID, IPC_RMID, NULL);
     printf("Server has removed its shared memory...\n");
     printf("Server exits...\n");
     exit(0);
}

void  ClientProcess(int  SharedMem[])
{
     printf("   Client process started\n");
     printf("   Client found %d %d %d %d in shared memory\n",
                SharedMem[0], SharedMem[1], SharedMem[2], SharedMem[3]);
     printf("   Client is about to exit\n");
}
This program asks for a shared memory of four integers and attaches this shared memory segment to its address space. Pointer ShmPTR points to the shared memory segment. After this is done, we have the following:


Then, this program forks a child process to run function ClientProcess(). Thus, two identical copies of address spaces are created, each of which has a variable ShmPTR whose value is a pointer to the shared memory. As a result, the child process has already known the location of the shared memory segment and does not have to use shmget() and shmat(). This is shown below:


The parent waits for the completion of the child. For the child, it just retrieves the four integers, which were stored there by the parent before forking the child, prints them and exits. The wait()system call in the parent will detect this. Finally, the parent exits.