Visit Our UNIX and Linux User Community


Parent,child wait,signal


 
Thread Tools Search this Thread
Top Forums Programming Parent,child wait,signal
# 8  
Old 06-06-2011
Quote:
Originally Posted by Cuervo
Corona688 i think you mistook FIFO with pipes...right?
Nope. Look at what I said more closely. I never said anything about writing to the fifo, but what happens when you open it.

Lemme write you a little example.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
        pid_t pid;
        int fd;
        mkfifo("./myfifo", 0660);

        pid=fork();
        if(pid == 0)    // child code
        {
                fprintf(stderr, "C\tWaiting 3 seconds\n");
                sleep(3);
                fprintf(stderr, "C\tOpening fifo\n");
                fd=open("./myfifo", O_RDONLY);
                fprintf(stderr, "C\tOpening fifo\n");
                fd=open("./myfifo", O_RDONLY);
                exit(0);
        }
        else if(pid > 0)        // parent code
        {
                fprintf(stderr, "P\tOpening fifo\n");
                fd=open("./myfifo", O_WRONLY);
                fprintf(stderr, "P\tFIFO opened\n");
                exit(0);
        }

        perror("Couldn't fork");
        return(1);
}

The parent creates the fifo, then creates the child, then tries to immediately open the fifo.

The child waits 3 seconds before opening the fifo, forcing the parent to wait 3 seconds as well. The fifo will finish opening only when there's something trying to open both ends.
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 06-06-2011
mmm i see...thank you!! i ll find a way to use this.Thank you very much!!

---------- Post updated at 05:27 PM ---------- Previous update was at 05:18 PM ----------

Still...the waiting part doesn't work when it is done by two different programs right?

---------- Post updated at 05:35 PM ---------- Previous update was at 05:27 PM ----------

Let me explain what i mean.I want to make the parent process wait but through a separate program.So...
Parent
Code:
        if (pid1 == 0) 
        {
            ppid=getppid();
            sprintf(ppidstr,"%d", ppid);
            if(execl("b.exe","b.exe",ppidstr) == -1)
                fprintf(stderr,"execl Error!\n");
        }
        else 
            {
                fd2 = open(myfifo2, O_RDONLY);
                read(fd2, buf, sizeof(MAX_BUF));
                   printf("PReceived: %s\n", buf);
                printf("My child ended ");
            }

And this is a separate program
Code:
int main(int argc, char **argv)
{
    char * myfifo1 = "/cygdrive/c/a/progsys/tmp1",buf[MAX_BUF];
    char * myfifo2 = "/cygdrive/c/a/progsys/tmp2";
    mkfifo(myfifo1, 0666);
    mkfifo(myfifo2, 0666);

    printf("Doing stuff");
    int fd2 = open(myfifo2, O_WRONLY);
    write(fd2, "Hid", sizeof("Hid"));
    int ppid=atoi(argv[1]),fd;
    int fd1 = open(myfifo1, O_RDONLY);

To be more specific, i want the "Doing stuff" to appear first...but it doesn't
# 10  
Old 06-06-2011
there's a reason I was using stderr, printf() is buffered. That printf(), without a newline, doesn't print until your program quits! Especially weird things can happen to printf statements in a fork, too -- if you have text waiting in a buffer like that, it will be printed twice. Try printing to stderr. It prints immediately with no buffer.

Also, try writing hid\n instead of hid. I think fifos and pipes may line-buffer.

Last edited by Corona688; 06-06-2011 at 09:01 PM..
# 11  
Old 06-06-2011
OK, i did everything like you said. It worked fine and I can manage two different programs like this. The only program i have is that when i write on the FIFO the other program can't read it immediatelly. I think that the first program doesn't have enough time to write on the FIFO so that the other can read.I had to place a sleep(5) so that it can collect the data properly. Is there a way to lose the sleep?

---------- Post updated at 07:44 PM ---------- Previous update was at 07:07 PM ----------

Also i can only send 3-4 characters through the FIFO, is this normal?
# 12  
Old 06-06-2011
You've only posted extremely incomplete parts of your program so I have no idea what's happening, let alone what's going wrong.
# 13  
Old 06-06-2011
I understand, i will post the important stuff. When i try to execute this fprintf(stderr,"\n!%s!",buf); on the child i only get 3-4 chars. argv is 3 5 6 asdf and i only get 3 5 on the child.
I am grateful for you helping me, i don't want to impose. If you have time please just take a look. Thank you!!

Parent
Code:
if (pid == 0) 
        {
   
            if(execl("b.exe","b.exe",NULL) == -1)
                fprintf(stderr,"execl Error!\n");
        }
        else 
            {
                fd = open(myfifo, O_WRONLY);
                   write(fd, data, MAX_BUF);
                printf("Parent ended ");
                close(fd);
                fd = open(myfifo, O_RDONLY);
            }

Child

Code:
#define MAX_BUF 5000

int main(int argc, char **argv)
{
    int fd,i,j,iteration=1,lowerBound,upperBound,numPoints;
    char * myfifo = "/cygdrive/c/a/progsys/tmp";
    char buf[MAX_BUF]="",permbuf[MAX_BUF]="",sourcefile[MAX_BUF]="",finalsourcefile[MAX_BUF]="";
    fd = open(myfifo, O_RDONLY);
    sleep(5);
    read(fd, buf, sizeof(MAX_BUF));
    fprintf(stderr,"\n!%s!",buf);

# 14  
Old 06-06-2011
read() isn't a string function. It doesn't give you a null terminator. You have to check its return value to see how much you read, and print it with write() fwrite() or the like.

Your program is still extremely incomplete, so I have no idea what you're writing to the fifo.

Also, I see you're using cygwin, not UNIX, so the behavior of this program may be very suspect in any case. Windows doesn't actually have named pipe files.

Last edited by Corona688; 06-06-2011 at 11:50 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #193
Difficulty: Easy
Python was selected the Programming Language of the Year in 2007, 2010, and 2018.
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