IPC - pipes between parent and child process


 
Thread Tools Search this Thread
Top Forums Programming IPC - pipes between parent and child process
# 1  
Old 11-20-2010
IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process code need the data on the file descriptor which is written by the parent process, my problem is i can't make the child process to execute the code after the parent process writes the data on the file descriptor.

Thanks

PS: I don't want to use semaphores yet.

Last edited by DukeNuke2; 11-21-2010 at 04:43 AM..
# 2  
Old 11-21-2010
Subroutine popen() is the easiest way to create a child and send it a stream. Otherwise pipe, fork, close, fdup, and if desired, exec*(). Oh, fflush() if using FILE*.!
# 3  
Old 11-21-2010
I've been awared of that, but my goal is to learn how to synchronize read/write file descriptors without using semaphores, my professor said that is possible and told us o theorical pseudo-code.
# 4  
Old 11-21-2010
Using for instance a pipe between the parent and the child (write end at the parent, read end at the child), you may synchronize as follows: the child waits (i.e. reads the pipe using a blocking read) until the parent tells the child to go ahead (by writing something into the pipe). You ensure that the child will only execute once the parent told to do so.

If you need to synchronize the other way around (parent waits - child tells), use a pipe in the other direction (write end at the child, read end at the parent). And if you need to synchronize in both directions, you will need two pipes.

HTH,
Loïc
# 5  
Old 11-21-2010
Well, it sounds like you need a protocol, or operate your pipes full duplex with at state machine at each end to take care of activities on that overlaid protocol. Decoding & analyzing professors is much more complex than decoding & analyzing an API. Smilie

If you find pipes, sockets, shmem, semaphores, queues and all that a bit restrictive, you can mmap() a file in both processes, creating an area of shared memory, not to be confused with the silly UNIX ipc shared memory that needs root setup and such dusty old stuff, and the nice thing is that you can peek at the mmap90'd file during or after for you edification about what is going on. It is really fast, RAM write snoop invalidating cache fast. You can even have a file for each writer, so there is no ambiguity about who put the file in that state.
# 6  
Old 11-22-2010
Quote:
Originally Posted by pharaoh
Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process code need the data on the file descriptor which is written by the parent process, my problem is i can't make the child process to execute the code after the parent process writes the data on the file descriptor.

Thanks

PS: I don't want to use semaphores yet.
If you are using pipe between the parent child for communication and if you are closing proper descriptors in child & parents; they should work well. Its a pretty simple thing to code. However, this pipe method always results into half duplex communication channel.

Make sure to use two instances of pipes to make it a full duplex communication.

I guess, you don't require code examples (it's pretty simple one as I said) but should you really require, do let me know.

P.S. You really don't need any synchronization mechanism including semaphores. If child is not able to communicate, just look at your code and what descriptors you are reading/writing on.

Last edited by Praveen_218; 11-22-2010 at 01:21 PM.. Reason: Typo errors
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. IP Networking

Message passing from child to parent using pipes

Hi, I am trying my hand in networking programming in C, and got stuck in piping. I was following some tutorial and did the forking like : while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) ... (4 Replies)
Discussion started by: abhi1988sri
4 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. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

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

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

8. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

9. Programming

Implementing 2 pipes between a parent and child process

Hi all, I'm trying to write a program that has some data it wants to send through a filter program(in this case tr), and then recieve the output from that filter program. The way I'm trying to do it is by setting up two pipes between the programs and piping the data in through one pipe and back... (2 Replies)
Discussion started by: bwgoudey
2 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question