Parent,child wait,signal


 
Thread Tools Search this Thread
Top Forums Programming Parent,child wait,signal
# 15  
Old 06-06-2011
So, read takes the bytes partially? i should put more read functions underneath?
# 16  
Old 06-06-2011
It can take bytes partially but I have no idea if that's the case right now because
a) You haven't posted enough of the program
b) You haven't checked read's return value
c) You're feeding the array into printf("%s") which expects strings, even though
c) read doesn't read strings! It puts no null terminator in it for you! Use write or fwrite!
# 17  
Old 06-07-2011
Corona,

Till now i was considering that pipes and fifo are same except the fact that fifo's existence can be control since it has a name and sometimes resides in a file system.

But now i have doubt, is there any other difference between them considering implementation and usage.

This is what i was referring to
Quote:
A pipe is a mechanism for interprocess communication; data written to the pipe by one process can be read by another process. The data is handled in a first-in, first-out (FIFO) order. The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe.
A FIFO special file is similar to a pipe, but instead of being an anonymous, temporary connection, a FIFO has a name or names like any other file. Processes open the FIFO by name in order to communicate through it.
# 18  
Old 06-07-2011
Ok i ll send you everything in case you understand what is hhappening. I do everything as i saw it online.I keep getting an Input/Output error on read.

Code:
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/wait.h>
#include  <stdio.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#define MAX_BUF 5000

int main(int argc, char **argv)
{
    pid_t pid;
    int i,fd;
    char buf[MAX_BUF]="",data[MAX_BUF]="";
    char * myfifo = "/cygdrive/c/a/progsys/tmp";
     if (mkfifo(myfifo, 0666) != 0)
        perror("mkfifo() error");
    for(i=1;i<5;i++)
    {
        strcat(data,argv[i]);
        strcat(data," ");    
    }
    fprintf(stderr,"#%s#",data);
    pid = fork();
    if (pid == -1) 
    { 
        perror("Can't create child\n");
        exit(1);
    }
    else 
        if (pid == 0) 
        {
   
            if(execl("b.exe","b.exe",NULL) == -1)
                fprintf(stderr,"execl Error!\n");
        }
        else 
            {
                if(fd = open(myfifo, O_WRONLY)<0)  perror("open() error for read end");
                   if (write(fd, data, strlen(buf))<0);    perror("write() error");
                printf("Parent ended ");
                close(fd);
                fd = open(myfifo, O_RDONLY);
            }
}

Child
Code:
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <sys/wait.h>
#include  <stdio.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#define MAX_BUF 5000

int main(int argc, char **argv)
{
    int fd,i,j,iteration=1,lowerBound,upperBound,numPoints,numread;
    char * myfifo = "/cygdrive/c/a/progsys/tmp";
    char buf[MAX_BUF]="",permbuf[MAX_BUF]="",sourcefile[MAX_BUF]="",finalsourcefile[MAX_BUF]="";
    if (fd = open(myfifo, O_RDONLY)<0) perror("open() error for read end");
    sleep(5);
    if (numread=read(fd, buf, sizeof(MAX_BUF))<0) perror("read ferror for read end");
    buf[numread]='0';
    write (1,buf,numread);

# 19  
Old 06-07-2011
Lots of errors.

Code:
char * myfifo = "/cygdrive/c/a/progsys/tmp";

I repeat: fifo's might not work on Windows, because windows doesn't even have them. The closest it has is a named pipe that doesn't actually exist as a file anywhere.
Quote:
if (mkfifo(myfifo, 0666) != 0)
perror("mkfifo() error");
You don't actually quit after most errors.
Code:
fprintf(stderr,"#%s#\n",data);

Unless you really want to view everything as one giant messy line, you might want to put an \n in things once in a while.
Code:
        if (pid == 0) 
        {
   
            if(execl("b.exe","b.exe",NULL) == -1)
                fprintf(stderr,"execl Error!\n");

            exit(1);

Can't forget that. If b.exe fails to run for any reason, the child will keep going!
Code:
                   if (write(fd, data, strlen(buf))<0);    perror("write() error");

That extra semicolon means it will always print 'write error', even when there's no write error.
Code:
                printf("Parent ended ");
                close(fd);
                fd = open(myfifo, O_RDONLY);

Why is the parent opening the fifo twice? The child doesn't open it twice, the parent will wait forever for something to open it again.

Why do you have your child code in a separate program when you could put it all in one and save yourself a lot of headaches?
Code:
buf[numread]='0';

0 is not null, 0 is 0. Try '\0'.
Code:
write (1,buf,numread);

...not that you actually need a null terminator any more when you use write(). How about fprintf(stderr, "%d bytes\n", (int)numread); to see how many you actually read?

---------- Post updated at 09:29 AM ---------- Previous update was at 09:19 AM ----------

Lastly: Your main program is writing 0 bytes on purpose because you're checking the length of the wrong buffer -- buf, instead of data.

---------- Post updated at 09:33 AM ---------- Previous update was at 09:29 AM ----------

Also: sizeof(MAX_BUFFER) is probably 4 or 8, not 5000 like you intended. Just MAX_BUFFER is what you want.

---------- Post updated at 09:38 AM ---------- Previous update was at 09:33 AM ----------

Also if(fd = open(myfifo, O_WRONLY)<0) doesn't have enough brackets. fd becomes either 0 or 1 -- which is never the FD of your open fifo! You've been reading from stdin and writing to stdin, not your fifo, I think.

When I correct all that, it works fine, with no sleep().
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question