Controlling child processes


 
Thread Tools Search this Thread
Top Forums Programming Controlling child processes
# 15  
Old 03-30-2004
Thank you for the info and all your help.
FG
# 16  
Old 04-19-2004
Quote:
Originally posted by Driver
Actually, it's not the stack that limits the usability of signal handlers. Unless you specify your own stack for signal handling (using the sigaltstack() function), its use of memory will be much the same as that of a simple nested function call, including the usual automatic expansion of stack space if its current end is reached.

The real problem ...
That's what I meant, you put it better. I meant that because it can intercept the stack at any time the code running just prior is unknown and therefore the state of the application's data is generally difficult to deduce.

Anyway, as I stated, and you agreed, it is really just personal preference which method is to be used. I dislike signals for such things as this. Besides, which signal do you use, user1/user2 there are no "sync" or "comm" signals. To me it just seems like a bad approach to use signals. But you're right, technically there is no "data" to necessitate a pipe. However one could argue that a signal is just another form a data, just transmitted through a different, and in my opinion less reliable, manner.

Well, for what its worth, don't take this as argumentative because its not meant to be. Debating personal preference is, however, sometimes worthless...so I guess I'll shut up now Smilie.
# 17  
Old 04-19-2004
Thank you all for the help, decided to go with a shared memory solution:
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>

main(int argc, char* argv[]) {

	int res; /* child id */
	int i, j, m, n;
	int shm_id, shm_value, child_num;
	int fd;
	int write_value, num_char;
	int  *  shm_ptr;
	char buf [sizeof(int)];
	 
	if( argc!= 4 || ( (n=atoi(argv[1])) <= 0 ) || ( (m=atoi(argv[2]))  <= 0) ) {
        fprintf(stderr,"Usage: %s <n - number of processes> <m - times process prints> <file - name of file>\n", argv[0]);
        fflush(stderr);
		exit(1);
    }

	/*Open file for writing*/
	if( (fd = open(argv[3], O_RDWR | O_CREAT, 0755 )) == -1 ){
		fprintf(stdout, "Cannot open file name = %s\n", argv[3]);
		fflush(stdout);
		exit(0);
	}
	/* Init Shared memory */
	shm_id = shmget(IPC_PRIVATE, sizeof(int), 0666 | IPC_CREAT);
	
	/* Attach this process to shared memory buffer */
	if( (shm_ptr = shmat(shm_id, 0, SHM_W)) == (int *) -1 ){
		fprintf(stdout, "Cannot attach process to memory \n");
		fflush(stdout);
		exit(0);
	}
	*shm_ptr = 0;
	for(i = 0; i < n; i++) {
		 if( (res = fork()) < 0 ){
			fprintf(stderr, "Cannot create child %d \n",(i+1));
			fflush(stderr);
			exit(0);
		}

		 if(res == 0){
			/* attach shared memory to this child process */
			if( (shm_ptr = shm_ptr = shmat(shm_id, 0, SHM_R | SHM_W)) == (int *) -1 ){
				fprintf(stdout, "Cannot attach process to memory \n");
				fflush(stdout);
				exit(0);
			}
			child_num = i;
			for( j = 0 ; j < m ; j++ ){
				/* Read the value of the shared memory */
				shm_value = *shm_ptr;
				/*Keep reading from shared memory until the previous process has written value*/
				while (1){
					if( shm_value == child_num){
						break;
					}
					shm_value = *shm_ptr;
				}

				/*Write next child number into shared memory buffer */
				write_value = (shm_value + 1);
				num_char = sprintf(buf, "%d", write_value);
				write(fd, buf, sizeof(int));
				write(fd, "\n", 1);
				*shm_ptr = ((shm_value + 1)%n);
			}
			break;
		 }
		 
	}
	/* Remove shared memory */
	shmctl(shm_id, IPC_RMID, 0);
	close(fd);
	exit(0);
 }

Code tags added for readability -- Perderabo

Last edited by Perderabo; 04-19-2004 at 04:57 PM..
# 18  
Old 04-19-2004
your code would be more easily readable with tabs Smilie, to get them use the

[code]
code here
[/code]

this will look like this:

Code:
code here

Anyway, it seems as though you are entering an infinite while loop "polling" the shared memory for a change in the child. Then you are writing back to the shared memory in said child for another to pick it up. This is all being done without any semaphore protection and therefore it is possible that things could go awry.

At any rate, this polling makes inefficient use of the CPU cycles, IMO. While it gets your assignment done...I just wanted to point out that its probably not an effective way of doing it.

However, it looks like you got it done...congrats.

edit: looks like the child does the shared memory write...this is worse yet.

Last edited by DreamWarrior; 04-19-2004 at 03:35 PM..
# 19  
Old 04-19-2004
Thank you for the feedback,will take it into consideration.
"Because the child is "only reading" the data the semaphore is potentially unneeded, but theoretically a "partial write" by the server could be misinterpreted by the child"
-- Would the partial write matter if you are looking for a specific value?
Just trying to understand this stuff.
Thanks,
FI
# 20  
Old 04-19-2004
Quote:
Originally posted by forumGuy
Thank you for the feedback,will take it into consideration.
"Because the child is "only reading" the data the semaphore is potentially unneeded, but theoretically a "partial write" by the server could be misinterpreted by the child"
-- Would the partial write matter if you are looking for a specific value?
Just trying to understand this stuff.
Thanks,
FI
Theoretically, yes...in practice, you'll probably never see it happen. However when, if, it does you'll be banging your head trying to figure out what went wrong if you don't have proper synchronization. In fact, in multi-process (multi-threaded) applications this is one of the single biggest coding isses, IMO. (More accurately trying to tune said access to a point where it is both reliable and fast).

Look at it like this:

You have multiple processing accessing memory, and therefore there exists the potential for the Kernel to "context switch" in the middle of an instruction. Now, depending on how the read or write is done depends on whether you see a problem.

Lets say your platform uses 32 bit integers. At the very least, on a hardware (CPU instruction) level, you can guarentee the atomic (i.e. single non-context-switchable operation) read/write of only 8 bits of data (more in most cases, but the minimum register size of any CPU used today is probably going to be 8 bits). The CPU intruction set, kernel, compiler, etc all come into play when knowing when gets done when read/writing an integer to/from said shared memory (or in fact any memory). If your architechture can guarentee the atomic read/write of said 32 bits then you're fine. You'll never have a problem because it would be impossible for the application to be switched during the read/write of an integer.

HOWEVER, if it can not, then application 1 could be in the middle of a read or write when the kernel context switches in application 2 and then this incomplete operation is potentially an issue.

The point is, you really don't know what's going on below you and therefore the possibility does exist for this to get confused. This is exactly why semaphores exists to protect and synchronize this access. This way an application knows when its reads/writes can be "safely" done.

For example, let's say a certain platform can only modify 16 bits of data at a time. To tinker with a 32 bit area would require two (or more) operations. If the Kernel allowed a context switch between them, then it'd be possible (although highly unlikely) for the following sequence of events:

process 1: read first 16 bits
process 2: write first 16 bits
process 2: write second 16 bits
process 1: read second 16 bits

now process 1's view is distorted.

While in practice this is probably NEVER going to occur, it could and bugs like these are very hard to track down because reproducing this case would be almost impossible. Best just to code it correctly to insure that everything is ok.
# 21  
Old 04-19-2004
That cleared it up.
Thank you for the info and example,
FI
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get all child processes of a process

is there a universal way of getting the children of a particular process? i'm looking for a solution that works across different OSes...linux, aix, sunos, hpux. i did a search online and i kept finding answers that were specific to Linux..i.e. pstree. i want to be able to specify a process... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

Controlling the Number of Child processes

I am trying to implement the below using Ksh script on a Lx machine. There is a file(input_file) with 100K records. For each of these records, certain script(process_rec) needs to be called with the record as input. Sequential processing is time-consuming and parallel processing would eat up... (2 Replies)
Discussion started by: APT_3009
2 Replies

3. Windows & DOS: Issues & Discussions

Controlling AIX processes remotely using a NET app on a Windows server?

I have a .NET application that remotely starts, stops, and gets status of Windows services and scheduled tasks. I would like to add the capability of starting, stopping, and getting status of remote AIX applications also. Based on some preliminary research, one option may be to use 3rd party .NET... (0 Replies)
Discussion started by: auser1
0 Replies

4. Programming

Controlling a child's stdin/stdout (not working with scp)

All, Ok...so I know I *should* be able to control a process's stdin and stdout from the parent by creating pipes and then dup'ing them in the child. And, this works with all "normal" programs that I've tried. Unfortunately, I want to intercept the stdin/out of the scp application and it seems... (9 Replies)
Discussion started by: DreamWarrior
9 Replies

5. UNIX for Advanced & Expert Users

killing all child processes

Hi, Is there a way I can kill all the child processes of a process, given its process id. Many thanks in advance. J. (1 Reply)
Discussion started by: superuser84
1 Replies

6. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

7. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

8. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

9. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies

10. UNIX for Dummies Questions & Answers

Controlling processes knowing the PID's

Dear all, suppose that I start a process (named "father"). "father" starts in turns a process called "child" with an execv call (after a fork). In this way "father" will be notified if "chlid" crashes (SIGCHILD mechanism). The problem is: if "father" crashes, how can I do to be recreate a... (1 Reply)
Discussion started by: npalmentieri
1 Replies
Login or Register to Ask a Question