473,408 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

C++/Fork

83
Hi, im working with fork, but I have a problem in this code:


Expand|Select|Wrap|Line Numbers
  1. int childp=0;
  2. int fork_return = fork();
  3. if(!fork_return){
  4. childp++;
  5.             cout << childp;
  6. }
when I run this, childp++ does nothing, the number is the same ( 0 ), every time.

Is there a way to get this to work?
Aug 28 '08 #1
6 4691
What the heck !!
It gives me 1 only
g++ compiler and on RHEL5
Aug 28 '08 #2
Adam01
83
WTF!

What it is Im doing, Its a server, and im trying to limit the clients to 10,
And what I want is a value to increment when a client connects, and the value the decrement when the client disconnects.
so heres the basic code:

Expand|Select|Wrap|Line Numbers
  1. for(;;){
  2. sin_size = sizeof their_addr;
  3. clientsocket = accept(serversocket, (struct sockaddr *)&their_addr, &sin_size);
  4. if(childrun < CHILDS){
  5. if(!fork()){
  6. childrun++;
  7. {message.write("Client-"); message.write(childrun);  message.write(": connected.<br>\n");} 
  8. for(;;)
  9. {
  10. // the cient stuff 
  11. }
  12.  
  13. close(clientsocket);
  14. childrun--;
  15. message.write("::Client Disconnected.<br>\n");
  16. exit(0);
  17. }
  18. close(clientsocket);
  19. }
  20. }
  21.  
And the output is:

Client connected:--
::Sending welcome message: Sent
Server: Ping
Client-1: Pong.
Client-1: This is the first client
Client connected: --
::Sending welcome message: Sent.
Server: Ping
Client-1: Pong.
Client-1: <-- ??? this is the second client
Aug 28 '08 #3
Adam01
83
So basicly, how can you change values in a fork/child, and the parent having that value.

so:

int a = 3;
if(!fork()){
a = 1;
}
cout << a; //a would still be 3.
Aug 28 '08 #4
mac11
256 100+
I'm not sure what you guys expect. It might help for you to remember that when you fork() the parent and child each get their own copy of everything. So if the child changes his variable the parent won't see it (unless you set up some shared memory or something).

Does that help?
Aug 28 '08 #5
Adam01
83
I guess I could use pointers.
Aug 28 '08 #6
gpraghuram
1,275 Expert 1GB
Since the parent and child have 2 different copies of the variable a you cant simply use the variable or pointer to accompolish this.
You should have a IPC to do this like PIPE or shared memory.


Thanks
Raghu
Aug 29 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
9
by: Gilles Ganault | last post by:
Hello I need to launch a Python script, and fork it so that the calling script can resume with the next step will the Python script keeps running. I tried those two, but they don't work, as...
2
by: Radz | last post by:
when the fork system call is executed, a new process is created. The original process is called the parent process whereas the new process is called the child process. The new process consists of a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.