Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > creating proccesses in Unix,Need help

creating proccesses in Unix,Need help
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2005
Status: Offline
Reply With Quote
Sep 6, 2005, 10:10 AM
 
Hi!

I want to fork 2 processes,get their process ids n write into a pipe n read from it.
This is the first thing i want to do.
After this is done,i want to modify the program so that a child process reads from a text file,writes it in to the pipe.Then another child process reads it.For
example:

aaaa 100 200

My code in C :
#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>

void do_write(int fds[])
{
int c;
int rc;

while ((c = getchar()) > 0)
{ /* write the character to the pipe. */
rc = write(fds[1], &c, 1);
if (rc == -1) { /* write failed - notify the user and exit */
perror("Parent: write");

close(fds[1]);
exit(1);
}
}
}


void do_read(int fds[])
{
int c;
int rc;
while ((rc = read(fds[0], &c, 1)) > 0)

/* probably pipe was broken, or got EOF via the pipe. */
exit(0);

}
main()
{

int fds[2];

pipe(fds);

if (fork()==0){
dup2(fds[1],1);
close(fds[0]);
printf("%d",getpid());
do_write(fds);

}

if (fork()==0){
dup2(fds[0],0);
close(fds[1]);
printf("%d",getpid());
do_read(fds);
}

else{
close(fds[0]);
close(fds[1]);
wait(0);
wait(0);
}
}


it is just not givin me the desired output.I am a begginner!
     
Mac Elite
Join Date: Sep 2001
Location: Chico, CA and Carlsbad, CA.
Status: Offline
Reply With Quote
Sep 7, 2005, 03:53 AM
 
Well I wouldn't say that you're a beginner, you'd be surprised how many people don't even know what fork() does or what a PID is.

I'm not very sharp on process stuff yet, though I'm actually in an Operating Systems programming class as we speak. I'd try asking in the Developer Center area as well, they might have an answer for you quicker...
"In Nomine Patris, Et Fili, Et Spiritus Sancti"

     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:51 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2