Visit Our UNIX and Linux User Community


Parent,child wait,signal


 
Thread Tools Search this Thread
Top Forums Programming Parent,child wait,signal
# 1  
Old 06-06-2011
Parent,child wait,signal

Hello. I want to make a child do some stuff,wait,then the parent does some stuff and then child does some stuff and waits again.I have made the following but it does not work.Can anybody help me?
Code:
    pid1 = fork();
    if (pid1 == -1) 
    { 
        perror("Can't create child\n");
        exit(1);
    }
    else 
        if (pid1 == 0) 
        {
            ppid=getppid();
            sprintf(ppidstr,"%d", ppid);
            if(execl("myprogram.exe","myprogram.exe",ppidstr) == -1)
                fprintf(stderr,"execl Error!\n");
            wait(NULL);//Wait until the parent opens myfifo
           printf("I am back to do some more stuff");
           exit();
        }
        else 
            {
                waitpid(0,NULL,0);//wait for the child's signal
                int fd = open(myfifo, O_WRONLY);
                write(fd, "Hi", sizeof("Hi"));
                    close(fd);
                  waitpid(0,NULL,0);//wait for the child's signal
                printf("My child ended ");
                exit(1);
            }

# 2  
Old 06-06-2011
execl() destroys your current process, replacing itself with whatever you told it to run. If you want it to run a process and return, try system().
# 3  
Old 06-06-2011
Thank you very much for your quick reply. Actually this is not the whole code. I made it just for the occasion. I wanted the process just to run a program.And then i want the program to run until some point the wait and then run the rest of it. Do i need kill and wait for this?
# 4  
Old 06-06-2011
The fifo by itself can do the waiting that you want to do. main tries to open the fifo, main stops running until something else opens the other end of the fifo. child opens the other end, both start running again.
# 5  
Old 06-06-2011
Oh god! Really? This is very helpful!! Thank you very much!!

---------- Post updated at 03:41 PM ---------- Previous update was at 02:52 PM ----------

Are u sure about what you told me? It doesn't work for me...

---------- Post updated at 03:54 PM ---------- Previous update was at 03:41 PM ----------

I want to open the FIFO in the child.Then send something to parent (just to "wake him", so the child waits) Then i want for the parent to send and when he sends i go on int the child's code. I ll show you my code. Maybe someone can help me, thank you

The parent's code
Code:
                int fd = open(myfifo, O_RDONLY);
                read(fd, buf, sizeof(buf));
                close(fd);
                   printf("PReceived: %s\n", buf);
                fd = open(myfifo, O_WRONLY);
                write(fd, "Hi", sizeof("Hi"));
                    close(fd);
                wait(NULL);
                printf("Me child ended ");
                exit(1);

And the child's code
Code:
    char * myfifo = "/cygdrive/c/a/progsys/tmp",buf[MAX_BUF];
    int ppid=atoi(argv[1]),fd;
    mkfifo(myfifo, 0666);
    printf("Doing stuff");
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hid", sizeof("Hid"));
    close(fd);
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("CReceived: %s\n", buf);
    close(fd);

# 6  
Old 06-06-2011
In one window I just did:
Code:
$ mkfifo xyz
$
$ cat < xyz

It hung just as it should. In a second window I did echo hello > xyz and now the first window looks like:
Code:
$ mkfifo xyz
$
$ cat < xyz
hello
$

(Except that I added red tags to show the new lines.)
# 7  
Old 06-06-2011
I'm sorry...i am new, i have no idea what you just told me.I can only work with code,not in UNIX

---------- Post updated at 04:30 PM ---------- Previous update was at 04:00 PM ----------

Corona688 i think you mistook FIFO with pipes...right?

Previous Thread | Next Thread
Test Your Knowledge in Computers #1000
Difficulty: Medium
Trusted Computer System Evaluation Criteria (TCSEC) is frequently referred to as the Red Book.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How make parent to wait from child process?

Hi all, I am starting mgen5 for sometime depends on input from a file, in a child process. now I want to make parent to wait in this child process till mgen5 finishes, or timeout happens. could anyone please tell me how to make parent to wait in child process in shell script? thanks... (2 Replies)
Discussion started by: girijajoshi
2 Replies

2. Shell Programming and Scripting

parent process needs to wait

I have two scripts lets say A.expect and B.sh needs to be executed. I am executing B.sh from A.expect where B.sh has sleep command. My problem is that when B.sh encounters the sleep command my A.expect starts executing and exits. but my A.expect should execute only after completing B.sh. Is... (3 Replies)
Discussion started by: priya@2012
3 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

5. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

6. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

7. UNIX for Dummies Questions & Answers

Sending signal from child to parent process!

Hi All, I facing a problem in handling signals between parent process communication. I am trying to send a signal(SIGINT) from child to parent. I am using kill function to do so and I am trying to read the signal using sigaction(). But the program is ending abruptly and I am not able to figure out... (4 Replies)
Discussion started by: vkn_1985
4 Replies

8. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

9. Programming

How can I make the parent thread wait

Hi All, I will be glad if you could help me resolve this problem. I have created two detachable threads and wanted to them execute independent of the parent thread ( the main task which creates the detachable threads). But I see no output coming from the execution of two detachable threads.... (4 Replies)
Discussion started by: jayfriend
4 Replies

10. UNIX for Advanced & Expert Users

how to make a parent wait on a child shells running in background?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (1 Reply)
Discussion started by: albertashish
1 Replies

Featured Tech Videos