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
This blog is meant for my personal reference and keeping track of what I have learnt. Most articles are found through search engines, if you are the author of the article and would like to remove from this blog, please contact me.
Thursday, June 9, 2011
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: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:
bg &
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
3719 ... info ... program name 7156 ... info ... program name
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:
kill 7156
There is a short form to start both bg (in background) and fg (in foreground) at the same time:
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:
bg & fg
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.
server -4 2 6 -10 & client
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:A list of shared memory segments will be shown. Then, use command ipcrm to remove those un-wanted ones:
ipcs -m
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.
ipcrm -m xxxx
shmdt() and shmctl() - detaching and removing a shared memory segment
Detaching and Removing a Shared Memory Segment - shmdt() and 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:
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:
shmdt(shm_ptr);
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().
shmctl(shm_id, IPC_RMID, NULL);
Communicating Between Parent and Child
Communicating Between Parent and Child
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:
#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"); }
Communicating between two separate processes
Communicating Between Two Separate Processes
#define NOT_READY -1 #define FILLED 0 #define TAKEN 1 struct Memory { int status; int data[4]; };
Subscribe to:
Posts (Atom)