Results 1 to 6 of 6
Hello again,
As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which ...
- 09-16-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 25
How do use Globals when using fork?
Hello again,
As part of my ongoing project I need to put together a messaging server of sorts. So far I have coded a server which accepts multiple connections, which then runs a function that will perform a task, see below.
On this sever I have a global structure which is populated using the data found in a read() buffer.
However with each fork() that runs the below function each process is handed a blank structure. How could I go about allowing each fork() child process access the same structure?
My code should hopefully show what I am trying to do?
Code:void listen_for_client(int sock) { char buffer[256]; int n; int len = 0; int r =0; char num[5] = {0}; char name[15] = {0}; char *ptr = NULL; extern sThisUnitsBay *bayBank; n=1; while(n>0){ memset(buffer, 0, sizeof(buffer)); n = read(sock,buffer,255); if(n<0)error("Error reading from socket"); ptr = strstr(buffer, ">>"); ptr++;ptr++; strncpy(num, ptr, 1); ptr++;ptr++; len = strcspn(ptr, "&"); strncpy(name, ptr, len); name[len]=0; if(strstr(buffer, "PASS")) r = 1; else r = 0; if(strstr(buffer, ">>1")){ sprintf(bayBank->bay1.name, "%s", name); bayBank->bay1.r = r; } if(strstr(buffer, ">>2")){ sprintf(bayBank->bay2.name, "%s", name); bayBank->bay2.r = r; } if(strstr(buffer, ">>3")){ sprintf(bayBank->bay3.name, "%s", name); bayBank->bay3.r = r; } if(strstr(buffer, ">>4")){ sprintf(bayBank->bay4.name, "%s", name); bayBank->bay4.r = r; } system("clear"); printf("BAY: 1\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay1.name,bayBank->bay1.r); printf("BAY: 2\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay2.name,bayBank->bay2.r); printf("BAY: 3\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay3.name,bayBank->bay3.r); printf("BAY: 4\r\nNAME: %s\r\nRESULT: %d\r\n\n",bayBank->bay4.name,bayBank->bay4.r); } }
- 09-16-2010 #2Just Joined!
- Join Date
- Oct 2009
- Posts
- 85
Just for a clarification, where does the extern pointer refers to?
One more thing if you do a fork, the parent and child are in different process context. so you should prefer to use a IPC mechanism like pipe, messagequeue or something like Shared memory(better for your case)
- 09-16-2010 #3
do you need to fork off a child process, or could what you need to do be accomplished with a thread running inside the process, thereby giving all process context to this thread?
- 09-17-2010 #4Just Joined!
- Join Date
- Oct 2008
- Posts
- 7
If you need access to the global band structure you should be using a thread for each client instead of forking to a different process. You COULD do it with different processes, just that it's more difficult. I suggest you look at the pthread library. Also, mind the fact that access to the shared resource must be synchronized.
- 09-17-2010 #5Just Joined!
- Join Date
- Jul 2010
- Posts
- 25
- 09-17-2010 #6
fork does have uses, but most of the time in client server application server should be spawning threads to do client requests rather than fork off new process
fork should be reserved for spawning daemons and the like (and college OS course)


Reply With Quote
