Interprocess communication using pipes and fork


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Interprocess communication using pipes and fork
# 1  
Old 10-14-2004
Data Interprocess communication using pipes and fork

I'm very worried. I have an assignment that is due in 3 weeks, and also tute exercises which I can't seem to understand and work out.

Okay, the question:
The parent process will convert the command arguments into integer values using atoi() and store them into an integer array which you will create using malloc() ONLY and is to be a local variable declared in main(). The parent process will then send the values of the array to Process B by writing to pipe1's output stream.

My question here is when i pass it through the pipe to Process B the output is always incorrect, gives me some rubbish. The examples which I've found also always pass string thru the pipe. Can I pass the pointer of the array to Process B and ask Process B to read the input from there?

I don't know why I find this so difficult to understand and have been looking around the internet for notes or examples to ponder on, but seems like all the examples are passing strings thru pipes.

Really really appreciate any sort of help here.
# 2  
Old 10-14-2004
What do your read() / write() calls look like?
# 3  
Old 10-14-2004
solution

I wrote the solution, but I don't understand how it really works.

#include <sys/types.h>
#include <unistd.h>
#define MSGSIZE 16
main(int argc, char *argv[])
{
pid_t pid;
int p1[2];
int number,i,a;
int *ip, *head;
int inbuf[MSGSIZE];
//A opens pipe
if(pipe(p1)==-1)
{
perror("main err");
exit(1);
}
//A forks B
switch(pid=fork())
{
case -1:
perror("error");
exit(2);
case 0:
close(p1[1]);
for(a=1;a<argc;a++)
{
read(p1[0], inbuf, MSGSIZE);
printf("%d", inbuf[0]);
//why must i specify inbuf[0]? why can't i just write inbuf?

}
break;
default:
ip=(int *)malloc(10*sizeof(int));
head=ip;
close(p1[0]);

for(i=1;i<argc;i++)
{
*ip = atoi(argv[i]);
write(p1[1], ip, MSGSIZE);
ip++;
}

}
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt. just select an exploit. This is my code:... (2 Replies)
Discussion started by: dano88
2 Replies

2. Programming

C, unix, pipes, fork, recursion

Hi, I will try to keep my post as compressed as my title was. I am writing on pseudo code on a recursive function that I want to read from the one-above function-run and then give the result to the function-run down below until a stop is triggered. Example: $ ls -la | grep x | sort In my... (2 Replies)
Discussion started by: tarasque
2 Replies

3. Programming

C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this : I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected,... (2 Replies)
Discussion started by: timmyyyyy
2 Replies

4. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

5. Shell Programming and Scripting

Logging/Reading Interprocess Communications

Greetings, I'm posting this in the shell scripting forum because I'm hoping this can be done in BASH or PERL. If not, I'm still open to suggestions of other ways to do it: I've got an iPhone app that's sending some encrypted (SSL) traffic to a server and I'd like to be able to read the... (0 Replies)
Discussion started by: FiZiX
0 Replies

6. UNIX for Dummies Questions & Answers

Interprocess Communication

Hiya Everybody just joined, Not sure if this is the right section:o I require abit of an assistance with IPC! I know there are different types of IPC porcesses like signals, semaphores, mutexes, shared memory, message queues, pipes and sockets. Now say a system has a number of... (4 Replies)
Discussion started by: G.I.Joe
4 Replies

7. UNIX for Advanced & Expert Users

Inter-process communication:pipes,doors,etc.

Hi, I am thinking about writing a log daemon for a multi-processed ksh application (yes - I know that high-level language would be a better option). My question is as follows: If many processes (many scripts) will try writing to a single log file: print "message" > common.log Will it work or... (2 Replies)
Discussion started by: adderek
2 Replies

8. UNIX for Advanced & Expert Users

Interprocess communication status

hello gurus, I was wondering if someone would help me shed more light on this command. What I know so far is ipcs - stands for inter-process communication status and it reports on the following types of system resources. 1) Message queues 2) Shared memory and 3) Semaphores Please explain... (4 Replies)
Discussion started by: jerardfjay
4 Replies

9. UNIX for Advanced & Expert Users

Pipe, interprocess communication

Earlier I posted a question regarding this issue. I managed to go a step further. Anyway, this is another similar question Write a programme that creates a ring of three processes connected by pipes. The first process should prompt the user for a string and then send it to the second process .... (2 Replies)
Discussion started by: scmay
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question