global variable in parent and children processes

Question about processes dynamic pool. I need to keep information of free processes. If count of free processes will be less than N, I should create new processes. But,I know that free variable will be the same in every process. How to make free variable "global" and changing in child processes will change variable in parent, and then parent can check this and make more children processes? Something like shared memory and other IPC. Little confused with them.

 for (i=0;i<5;i++) // create 5 pre-forks
        if (pid==0) //child


    while (1) 
        if (pid==0) // child
             newsock = accept(listensock, NULL,NULL);
             free--; //children is busy
             send(newsock, buffer, nread, 0); 
        else if (pid>0) // parent
            if ...// if less than n fork() more


As you said, you can use shared memory to store a variable shared between different processes. One of the processes must create that shared memory with shmget

shmget needs a key to identify the shared memory region, the size, and some additional options. The common options for create are IPCCREAT | 0666 to create it with unix permissions 0666

The other processes call shmget with 0 as the last parameter to use the already initializated segment.

To connect the process direction space with the shared memory you must use shmat


#define KEY ((key_t) 1234)  //Any value, just dont overlap other applications in the machine
#define SIZE sizeof(int)

int* free;

int id = shmget(KEY, SIZE, IPCCREAT | 0666);
if (id < 0) //Error...

free = (int*) shmat(id, 0, 0);
if (free <= (int*)(0)) // shmat failed...

//At this point, you can use free as a normal variable
*free = 5;
//As various processes can access free, you must use any control mechanism (mutex, semaphores...)

shmdt(id); //Unlink shared memory segment

Need Your Help

Garbled output in AES/CBC/NoPadding Decryption

java javascript encryption aes cryptojs

I'm trying to decrypt text in java that is encrypted using CryptoJS. I've read on other posts that they use different default modes and padding so I set them both(java/cryptojs) both to use

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.