Sponsored Content
Full Discussion: Father & children
Top Forums Programming Father & children Post 97408 by Jariya on Monday 30th of January 2006 04:44:32 AM
Old 01-30-2006
Father & children

Hello, i should finished this program, if anyone could tell me whats wrong... This is an optional university work, though i cant leave this nearly finished. I need to see where is my error Smilie

What my program should do.
The user must type "num_proc" ( number of children). The program creates a ring of num_proc processes with num_proc pipes, although i only use one variable ("tubo2") to access pipes.father and children will change the content of "frase" twice.


Code:
int main(int argc, char* argv[])
{
        int tubo1[2], tubo2[2], i,num_proc=0,numero=0,longitud=0,pid=0;
        char frase[MAXNAME];
      
        ...

        pipe(tubo1);

        dup2(tubo1[0],tubo2[0]);
        dup2(tubo1[1],tubo2[1]);

        close(tubo1[0]);
        close(tubo1[1]);


        /* Creating num_proc number of processes */

        for( i=0; i<num_proc; i++)
        {
                pipe(tubo1);

                if ( (pid=fork()) == -1 )
                        printf("\nError. Fork\n\n");
                else
                {
                        if ( pid )
                        {
                                /* Fatherīs code */

                                dup2(tubo1[0],tubo2[0]);
                                close(tubo1[0]);
                                close(tubo1[1]);
                        }
                        else
                        {
                                dup2(tubo1[1],tubo2[1]);
                                close(tubo1[0]);
                                close(tubo1[1]);
                                break;
                        }
                }
        }


        if ( pid )
        {
                /* Father */

                for (i=0; i<2; i++)
                {
                        /* "frase" modification. Father starts. */

                        write(tubo2[1], frase, longitud*sizeof(char));
                        read(tubo2[0], frase, longitud*sizeof(char));
                }
        }
        else
        {
                /* Children code */
                for (i=0; i<2; i++)
                {
                        read(tubo2[0], frase, MAXNAME*sizeof(char))
                        /* "frase" modification */
                        write(tubo2[1], frase, longitud*sizeof(char));
                }
        }

 return 0;
}


Last edited by Perderabo; 01-30-2006 at 02:13 PM.. Reason: Add code tags for readability
 

8 More Discussions You Might Find Interesting

1. Programming

how to creat 1 parent to call 3 children

hi there, im trying to produce this program that would run at first, and when it runs it will fork one child process to a program and then another forking to run this other program, and then another one . i cant seem to get it right can someone help me please here is what is got so far: int... (1 Reply)
Discussion started by: zmanultra
1 Replies

2. UNIX for Advanced & Expert Users

How to follow processes and their children with ps

Hi, I often need to find the child processes of a parent process. There may be a string of 4-5. That is, PPID 884 spawns 890, which spawns 894, which spawns 1017. I'd like to be able to see all of them without having to type in a number of ps -ef commands. Process groups and session ID's are... (2 Replies)
Discussion started by: mschwage
2 Replies

3. Shell Programming and Scripting

Prepend name of directory to children folders

Hi, I am a shell scripting newbie. I am in need of a shell script that will prepend the name of the parent directory to the child directory. For example if the shell script called rename.sh is invoked with ">rename.sh /home/foobar/Simple" and the structure of the folder Simple is : Simple... (7 Replies)
Discussion started by: kalichar
7 Replies

4. UNIX for Advanced & Expert Users

Fork() 1 Parent 3 Children

Hi, as I understand fork(), it makes a copy of the parent which becomes a child. But is there anyway to make three children for that one parent. So in other words, if I look up the getppid() of the children, I want them to have the same value?? Thanks in advance to any help! (1 Reply)
Discussion started by: MS_CC
1 Replies

5. UNIX for Dummies Questions & Answers

How can i use fork,sleep,wait and write in a process with father and son..??

Hi.. I was unable to do (gcc code) which refers to the fork,wait,sleep and write.. what i want to do: A process of father create (fork) a son and will sleep 90 seconds..After this, son process create a grandchild and will sleep 60 seconds..Grandchild process will sleep for 30 seconds..After... (3 Replies)
Discussion started by: gumlucin
3 Replies

6. Shell Programming and Scripting

Killing process and children

Hi all, I have been searching all day for a nice solution to this problem. I have three scripts. A start script, a child script and a stop script. Script A (scripta.sh) Its Child Script B (scriptb.sh) Script C (kill_process.sh $PID) Script A correctly traps the kill command sent from... (6 Replies)
Discussion started by: mark007
6 Replies

7. Shell Programming and Scripting

Doing simple math for children.

Hello! I saw that there were a few differente ways to do math within bash scripts. expr and bc are possibilities. But which one to use when? I want to make an simple bash script for children to do math. The script must ask only questions like 1*1= till 10*10= ... No + or - or /... (10 Replies)
Discussion started by: ugurgazi
10 Replies

8. Shell Programming and Scripting

pstree but without other children of ancestors

Hi, I want to display the process tree of a given PID, however, I don't want to see other children of the ancestors that don't reach the current PID. My goal is, from the tree result, i have to fetch a particular parent process by keyword, and if any other children from parents have the same... (2 Replies)
Discussion started by: ysrini
2 Replies
dup2(3C)						   Standard C Library Functions 						  dup2(3C)

NAME
dup2 - duplicate an open file descriptor SYNOPSIS
#include <unistd.h> int dup2(int fildes, int fildes2); DESCRIPTION
The dup2() function causes the file descriptor fildes2 to refer to the same file as fildes. The fildes argument is a file descriptor refer- ring to an open file, and fildes2 is a non-negative integer less than the current value for the maximum number of open file descriptors allowed the calling process. See getrlimit(2). If fildes2 already refers to an open file, not fildes, it is closed first. If fildes2 refers to fildes, or if fildes is not a valid open file descriptor, fildes2 will not be closed first. The dup2() function is equivalent to fcntl(fildes, F_DUP2FD, fildes2). RETURN VALUES
Upon successful completion a non-negative integer representing the file descriptor is returned. Otherwise, -1 is returned and errno is set to indicate the error. ERRORS
The dup2() function will fail if: EBADF The fildes argument is not a valid open file descriptor. EBADF The files2 argument is negative or is not less than the current resource limit returned by getrlimit(RLIMIT_NOFILE, ...). EINTR A signal was caught during the dup2() call. EMFILE The process has too many open files. See fcntl(2). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
close(2), creat(2), exec(2), fcntl(2), getrlimit(2), open(2), pipe(2), lockf(3C), attributes(5), standards(5) SunOS 5.10 19 Dec 2003 dup2(3C)
All times are GMT -4. The time now is 01:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy