Sponsored Content
Full Discussion: Controlling child processes
Top Forums Programming Controlling child processes Post 50221 by forumGuy on Monday 19th of April 2004 01:05:11 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
isaexec(3C)						   Standard C Library Functions 					       isaexec(3C)

NAME
isaexec - invoke isa-specific executable SYNOPSIS
#include <unistd.h> int isaexec(const char *path, char *const argv[], char *const envp[]); DESCRIPTION
The isaexec() function takes the path specified as path and breaks it into directory and file name components. It enquires from the running system the list of supported instruction set architectures; see isalist(5). The function traverses the list for an executable file in named subdirectories of the original directory. When such a file is located, execve() is invoked with argv[] and envp[]. See exec(2). RETURN VALUES
If no file is located, isaexec() returns ENOENT. Other return values are the same as for execve(). EXAMPLES
Example 1: Example of isaexec() function. On a system whose isalist is sparcv7 sparc the program int main(int argc, char *argv[], char *envp[]) { return (isaexec("/bin/thing", argv, envp)); } will look first for an executable file named /bin/sparcv7/thing, then for an executable file named /bin/sparc/thing. It will invoke execve() on the first executable file it finds named thing. On that same system, a program called /u/bin/tofu can cause either /u/bin/sparcv7/tofu or /u/bin/sparc/tofu to be invoked using the follow- ing code: int main(int argc, char *argv[], char *envp[]) { return (isaexec(getexecname(), argv, envp)); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Safe | +-----------------------------+-----------------------------+ |Interface Stability |Stable | +-----------------------------+-----------------------------+ SEE ALSO
exec(2), getexecname(3C), attributes(5), isalist(5) SunOS 5.10 20 Mar 1998 isaexec(3C)
All times are GMT -4. The time now is 06:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy