I am trying to do a bidirectional pipe, the parent sends n number (int) to the child and the child return them doubled. I can't figure out what's my error?
I scanned the number n is the parent, sent it through fd1[1], and then proceeded to send those n numbers for the child to double.
In the child, I read the number n and then for every number I read, I double and send back.
int main(){
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0){
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++){
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
}
close(fd1[0]);
close(fd2[1]);
}
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++){
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
}
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;}
Fixing the parent loop to test p and not n fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
{
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %d\n", n);
for (int i = 0; i < n; i++)
{
printf("child dozes...\n");
sleep(3);
printf("child wakes...\n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
{
fprintf(stderr, "child exits after read failure\n");
exit(1);
}
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]\n", buf2);
}
close(fd1[0]);
close(fd2[1]);
printf("Child done\n");
exit(0);
}
else
{
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]\n", buf1);
printf("parent dozes...\n");
sleep(3);
printf("parent wakes...\n");
for (int i = 0; i < p; i++)
{
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]\n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %s\n", buf2);
}
close(fd1[1]);
close(fd2[0]);
wait(NULL);
}
return 0;
}
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if and else blocks. It doesn't detect failures in pipe() or fork() which is suboptimal. The child exit(0) is not crucial any more.
Related
I am new to pipes but how do I redirect the output from child_1 to the input for child_2?
I am trying to pass the value from the parent to child_1, adds 1 to the value, print the value, then use that output and pass it into child_2, add 1 again, and finally print the value.
The code below has the right output value for child_1, but not for child_2, how do I redirect the output from child_1 to the input for child_2?
Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[]) {
int fd[2];
int PID;
pipe(fd); //fd1[0] = read | fd1[1] = write
PID = fork(); // spawn child_1
if (PID < 0){ // failed to fork
perror("Unable to fork child");
exit(1);
}
if (PID > 0) { // parent
int value = 100;
// since parent is only writing, close the reading end of pipe
close(fd[0]);
// write the data to the write end of the pipe
write(fd[1], &value, sizeof(int));
// then close the writing end of the pipe (parent)
close(fd[1]);
/**********************************************************/
} else { // child 1
int val = 0;
// read from the parent pipe
read(fd[0], &val, sizeof(int));
val += 1;
// is this how to redirect from one pipe to another?
dup2(fd[0], fd[1]);
// this prints the right value for val (val [101] = value [100] + 1)
printf("Child [%d] read value %d\n", getpid(), val);
// close the reading end of the pipe for child_1
close(fd[0]);
int PID2 = fork(); // make child 2
if(PID2 == 0) { // child 2
int val2 = 0;
close(0); // close stdin since we are trying to take the value from child_1
// read input from child_1 pipe (NOT WORKING?)
read(fd[0], &val2, sizeof(int));
val2 += 1;
printf("Child [%d] out %d\n", getpid(), val2);
close(fd[0]);
}
}
return EXIT_SUCCESS;
}
The way you have things set up, there's no need to use dup2() or any other I/O redirection.
Add #include <unistd.h> to the list of include files (and remove #include <string.h> — it seems to be unused)
Delete: dup2(fd[0], fd[1]);
Delete: close(fd[0]);
Delete: close(0);
Before the second fork(), add write(fd[1], &val, sizeof(val));
When you have close(fd[0]) in the first child, you effectively close fd[0] for the second child too.
You should check the status of the read and write operations before using the results.
Those changes lead to:
/* SO 7383-1815 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int fd[2];
int PID;
pipe(fd);
PID = fork();
if (PID < 0)
{
perror("Unable to fork child");
exit(EXIT_FAILURE);
}
if (PID > 0)
{
int value = 100;
close(fd[0]);
write(fd[1], &value, sizeof(int));
close(fd[1]);
}
else
{
int val = 0;
if (read(fd[0], &val, sizeof(val)) != sizeof(val))
{
perror("read() failed in child 1");
exit(EXIT_FAILURE);
}
val += 1;
printf("Child [%d] read value %d\n", getpid(), val);
if (write(fd[1], &val, sizeof(val)) != sizeof(val))
{
perror("write() failed in child 1");
exit(EXIT_FAILURE);
}
int PID2 = fork();
if (PID2 == 0)
{
int val2 = 0;
if (read(fd[0], &val2, sizeof(val2)) != sizeof(val2))
{
perror("read() failed in child 2");
exit(EXIT_FAILURE);
}
val2 += 1;
printf("Child [%d] out %d\n", getpid(), val2);
close(fd[0]);
close(fd[1]);
}
}
return EXIT_SUCCESS;
}
When compiled (cleanly with options set fussy), it produces output such as:
Child [34520] read value 101
Child [34521] out 102
I believe this is what was wanted.
I´m having troubles to understand how pipes and fork work together for a online course. Firstly, I want to type two strings below a printf and the when all read/write end cat both string into one.
I´m stuck in the read/write for both childs, the problems is that it didn´t match what I want because first print the strings and then open the channel to type. Maybe this is a small matter but I am starting using C for a online course. read and write function are compulsory.
String 1:
sdfsdfsdf
String 2:
Sdfsdfdsfsdfsd
String cat:
sdfsdfsdfSdfsdfdsfsdfsd
here is my code so far.
int main()
{
int fd1[2],fd2[2];
int status, pid;
pipe(fd1);
printf("String 1: \n");
pid = fork();
if(pid == 0) /* child 1*/
{
close(fd1[0]);
char cad1[100];
read(0,&cad1,100);
write(fd1[1],&cad1, 100);
close(fd1[1]);
exit(0);
}
else /* father*/
{
close(fd1[1]);
printf("String 2: \n");
pipe(fd2);
pid = fork();
if(pid == 0) /* child 2 */
{
close(fd2[0]);
close(fd1[0]);
char cad2[100];
read(0,&cad2,100);
write(fd2[1],&cad2, 100);
close(fd2[1]);
exit(0);
}
}
close(fd2[0]);
/* wait every child */
wait(&status);
wait(&status);
return 0;
}
My output is like this:
String 1:
String 2:
cvbcvbcvbcvb
cvbcvbcvbcvb
To cat my code (not implemented in the code I think to call both pipe into to char [] variable before cat.
Any suggestion to improve my code.
Thanks in advance.
Here's my code, based loosely on yours. As I said in a comment, the parent code needs to read from the two pipes to collect the data from the children, and then concatenate them. You need to pay attention to how many bytes are read so that you don't write more bytes than were read. The second child should close both fd1[0] and fd1[1] as it will use neither of them. You'll need to worry about newlines and null bytes — the information returned by read() is not a string and will include the newline.
I opted to use fgets() to read from standard input since it does return a string and zapped the newlines. I wrote some information to standard output, too. The concatenated string is created using snprintf().
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
static inline void syserr_exit(const char *fmt, ...)
{
va_list args;
int errnum = errno;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (errnum != 0)
fprintf(stderr, "%d: %s\n", errnum, strerror(errnum));
exit(EXIT_FAILURE);
}
int main(void)
{
int fd1[2], fd2[2];
int pid1, pid2;
if (pipe(fd1) < 0 || pipe(fd2) < 0)
syserr_exit("Failed to create two pipes: ");
else if ((pid1 = fork()) < 0)
syserr_exit("Failed to fork() child 1: ");
else if (pid1 == 0)
{
/* Child 1*/
close(fd1[0]);
close(fd2[0]);
close(fd2[1]);
char cad1[100];
printf("String 1: \n");
fflush(stdout);
if (fgets(cad1, sizeof(cad1), stdin) == NULL)
syserr_exit("child 1 - failed to read from standard input: ");
cad1[strcspn(cad1, "\r\n")] = '\0';
write(fd1[1], cad1, strlen(cad1));
/* Should error check the write! */
printf("child 1 wrote [%s] to the parent process\n", cad1);
close(fd1[1]);
exit(0);
}
else if ((pid2 = fork()) < 0)
syserr_exit("Failed to fork child 2: ");
else if (pid2 == 0)
{
/* Child 1*/
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
printf("String 2: \n");
fflush(stdout);
char cad2[100];
if (fgets(cad2, sizeof(cad2), stdin) == NULL)
syserr_exit("child 2 - failed to read from standard input: ");
cad2[strcspn(cad2, "\r\n")] = '\0';
write(fd2[1], cad2, strlen(cad2));
/* Should error check the write! */
printf("child 2 wrote [%s] to the parent process\n", cad2);
close(fd2[1]);
exit(0);
}
else
{
/* Parent */
char buffer1[100];
char buffer2[100];
close(fd2[1]);
close(fd1[1]);
ssize_t sz1 = read(fd1[0], buffer1, sizeof(buffer1));
buffer1[sz1] = '\0';
close(fd1[0]);
ssize_t sz2 = read(fd2[0], buffer2, sizeof(buffer2));
buffer2[sz2] = '\0';
close(fd2[0]);
size_t tlen = sz1 + sz2 + sizeof("[]+[]");
char concat[tlen];
snprintf(concat, sizeof(concat), "[%s]+[%s]", buffer1, buffer2);
/* wait for both children */
int s1, s2;
int c1 = wait(&s1);
int c2 = wait(&s2);
printf("The one child (%d) exited with status 0x%.4X\n", c1, s1);
printf("T'other child (%d) exited with status 0x%.4X\n", c2, s2);
printf("Received from %d (%zu bytes) [[%s]]\n", pid1, sz1, buffer1);
printf("Received from %d (%zu bytes) [[%s]]\n", pid2, sz2, buffer2);
printf("Concatenated data: <<%s>>\n", concat);
}
return 0;
}
One sample run yielded:
$ ./pipe43
String 1:
String 2:
Vultures keep the world clean.
child 2 wrote [Vultures keep the world clean.] to the parent process
Hypersonic transport planes are as ubiquitous as pink elephants
child 1 wrote [Hypersonic transport planes are as ubiquitous as pink elephants] to the parent process
The one child (69005) exited with status 0x0000
T'other child (69004) exited with status 0x0000
Received from 69004 (63 bytes) [[Hypersonic transport planes are as ubiquitous as pink elephants]]
Received from 69005 (30 bytes) [[Vultures keep the world clean.]]
Concatenated data: <<[Hypersonic transport planes are as ubiquitous as pink elephants]+[Vultures keep the world clean.]>>
$
I want to read the integer from the pipe and then print it, but everytime it prints garbage value. Someone that can help me out?
int main()
{
int pid1 = fork();
int fd1[2];
pipe(fd1);
if (pid1 == 0) // Child process
{
int x;
close(fd1[1]);
read(fd1[0], &x, sizeof(int));
printf("I'm the First Child and I received: %d\n", x); // <--- Here it prints garbage value
close(fd1[0]);
}
else // Parent process
{
int x = 5;
close(fd1[0]);
write(fd1[1], &x, sizeof(int));
close(fd1[1]);
}
}
I had to fork() after creating the pipes. So the code looks like this:
int main()
{
int fd1[2];
pipe(fd1);
int pid1 = fork();
if (pid1 == 0) // Child process
{
int x;
close(fd1[1]);
read(fd1[0], &x, sizeof(int));
printf("I'm the First Child and I received: %d\n", x);
close(fd1[0]);
}
else // Parent process
{
int x = 5;
close(fd1[0]);
write(fd1[1], &x, sizeof(int));
close(fd1[1]);
}
}
Here is what my code is intended to do :
The child should:
1. Sends a character to the parent
2. Receive integer from parent and print it
The parent should:
1. Read the character sent from the child and print it
2. Cast it to an integer and send the result to the child
Here is the code I wrote:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
int fd1[2];
int fd2[2];
pid_t p;
if (pipe(fd1)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}
p = fork();
if (p<0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
if (p==0){
char c='a';
int received;
close(fd1[0]);
write(fd1[1], c, sizeof(char));
close(fd1[1]);
close(fd2[1]);
read(fd2[0], received, sizeof(int));
printf("Printing from child ");
printf(" ");
printf("%d", received);
close(fd2[0]);
}
if (p >0)
{
char received;
close(fd1[1]);
read(fd1[0], received, sizeof(char));
printf("Printing from parent ");
printf(" ");
printf("%c", received);
close(fd1[0]);
close(fd2[0]);
int test=(int)received;
write(fd2[1], test, sizeof(test));
close(fd2[1]);
}
}
My current output is the following: Printing from parent Printing from child 0
I am assuming the parent is reading from the pipe before the child writes to it, how to fix that?
I am assuming the parent is reading from the pipe before the child writes to it, how to fix that?
This assumption is false. The error is one that a good compiler should have warned about - you missed that not the values of the variables c, received and test have to be passed to write and read, but their addresses:
write(fd1[1], &c, sizeof(char));
…
read(fd2[0], &received, sizeof(int));
…
read(fd1[0], &received, sizeof(char));
…
write(fd2[1], &test, sizeof(test));
May I ask how the computer ensures the scenario I assumed doesn't happen?
The read from the pipe, just as with a terminal device, simply blocks until there's something to read (provided that the file descriptor hasn't explicitly been set to non-blocking).
I wrote a C program that is supposed to create a certain number of child processes, each child process having to change 1 letter from a string. The string and the number of child processes are read from the keyboard.
I want to do it using pipes. It should work like this: The parent changes one letter, then the first child takes the string modified by the parent and changes one more letter. The second child takes the string modified by the first one (2 letters are already changed) and changes one more and so on. I am new to C and am not quite sure how it all works, especially pipes.
Also can the children be linked between them through the pipe, or can they only be linked to the parent and it has to be something like: first child changes a letter, gives the string back to the parent and then the second child reads from there, modifies letter and gives back.
If it's like that, is there any way to make sure that this doesn't happen: Apples becomes AppleD and then AppleX and then AppleQ?
For example:
input:
3 Apples
output:
Applex Appldx Apqldx
My problem is: I don't get any output from the children. Unsure what I'm doing wrong. Help would be much appreciated, thanks in advance!
Here's my code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
void error(char* msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
char* modify(char msg[])
{
srand(time(NULL));
int pos1=rand()%((int)strlen(msg));
srand(time(NULL));
int pos2=rand()%26;
srand(time(NULL));
int big=rand()%2;
if(big==1)
{
msg[pos1]=(char)(((int)'A')+pos2);
}
else
{
msg[pos1]=(char)(((int)'a')+pos2);
}
return msg;
}
int main(int argc, char *argv[])
{
if(argc!=3)
{
error("Wrong number of arguments\n");
}
int nrch;
nrch=atoi(argv[1]);
char* msg=argv[2];
printf("Parent: erhalten: %s\n", msg);
int i=0;
msg=modify(argv[2]);
printf("Parent: weiter: %s\n", msg);
pid_t pids[10];
int fd[2];
if(pipe(fd) == -1)
{
error("Can't create the pipe");
}
dup2(fd[1], 1);
close(fd[0]);
fprintf(stdout, msg);
/* Start children. */
for (i = 0; i < nrch; ++i)
{
if ((pids[i] = fork()) < 0)
{
error("Can't fork process");
}
else if (pids[i] == 0)
{
dup2(fd[0], 0);
close(fd[1]);
fgets(msg,255,stdin);
printf("child%d: erhalten: %s\n", (i+1), msg);
modify(msg);
printf("child%d: weiter: %s\n", (i+1), msg);
if (pipe(fd) == -1)
{
error("Can’t create the pipe");
}
fprintf(stdout, msg);
dup2(fd[1], 1);
close(fd[0]);
exit(0);
}
}
/* Wait for children to exit. */
int status;
pid_t pid;
while (nrch > 0)
{
pid = wait(&status);
printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
--nrch;
}
}
One reason you see no output from the children is that you hook their standard output to the write end of the pipe, so when they write to standard output, it goes into the pipe, not to the screen (or wherever you sent the standard output of the program to originally).
Where the children are not going to execute a program that needs standard input and standard output going to the pipe, don't use I/O redirection. Just write to and read from the correct ends of the pipe.
If you've got multiple children, you probably need a pipe per child, but the parent process will need to do the creating. Your code creates a pipe in the child; that pipe is no use because only the child knows about it. You probably can do it all with one pipe, but it becomes indeterminate which sequence the children will run in. If determinacy is important, use multiple pipe() calls, and at least twice as many close() calls.
Single pipe solution
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
static void error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
putc('\n', stderr);
exit(1);
}
static void modify(char msg[])
{
int pos1 = rand() % ((int)strlen(msg));
int pos2 = rand() % 26;
int big = rand() % 2;
if (big == 1)
msg[pos1] = (char)(((int)'A') + pos2);
else
msg[pos1] = (char)(((int)'a') + pos2);
}
static int read_pipe(int fd, char *buffer, size_t buflen)
{
int nbytes = read(fd, buffer, buflen);
if (nbytes <= 0)
error("Unexpected EOF or error reading pipe");
assert((size_t)nbytes < buflen);
buffer[nbytes] = '\0';
return nbytes;
}
int main(int argc, char *argv[])
{
if (argc != 3)
error("Usage: %s number 'message'", argv[0]);
srand(time(NULL));
int nrch = atoi(argv[1]);
char *msg = argv[2];
size_t len = strlen(msg);
printf("Parent: erhalten: %s\n", msg);
modify(msg);
printf("Parent: weiter: %s\n", msg);
int fd[2];
if (pipe(fd) == -1)
error("Can't create the pipe");
if (write(fd[1], msg, len) != (ssize_t)len)
error("Failed to write to pipe");
/* Start children. */
for (int i = 0; i < nrch; ++i)
{
int pid;
if ((pid = fork()) < 0)
error("Can't fork process");
else if (pid == 0)
{
char buffer[255];
int nbytes = read_pipe(fd[0], buffer, sizeof(buffer));
printf("child%d: erhalten (%d): %s\n", (i + 1), nbytes, buffer);
modify(buffer);
printf("child%d: weiter (%d): %s\n", (i + 1), nbytes, buffer);
write(fd[1], buffer, nbytes);
exit(0);
}
else
printf("Random: %d\n", rand());
}
/* Wait for children to exit. */
while (nrch > 0)
{
int status;
pid_t pid = wait(&status);
printf("Child with PID %ld exited with status 0x%.4X.\n", (long)pid, status);
--nrch;
}
char buffer[255];
int nbytes = read_pipe(fd[0], buffer, sizeof(buffer));
printf("Parent: weiter (%d): %s\n", nbytes, buffer);
return 0;
}
Example output
Code in file p1.c:
$ make p1 && ./p1 4 "Absolutely nothing to do with me"
gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror p1.c -o p1
Parent: erhalten: Absolutely nothing to do with me
Parent: weiter: AbsolutEly nothing to do with me
Random: 1120753102
child1: erhalten (32): AbsolutEly nothing to do with me
Random: 918317477
child1: weiter (32): AbsolutEly notzing to do with me
child2: erhalten (32): AbsolutEly notzing to do with me
child2: weiter (32): AbsolwtEly notzing to do with me
Random: 196864950
child3: erhalten (32): AbsolwtEly notzing to do with me
child3: weiter (32): AbsolwtEly notzing to ao with me
Random: 1584398270
Child with PID 42928 exited with status 0x0000.
Child with PID 42927 exited with status 0x0000.
Child with PID 42926 exited with status 0x0000.
child4: erhalten (32): AbsolwtEly notzing to ao with me
child4: weiter (32): AbsolwtEly notzing to ao with Ue
Child with PID 42929 exited with status 0x0000.
Parent: weiter (32): AbsolwtEly notzing to ao with Ue
$
Note the stray use of rand() in the loop. It makes sure the children change different letters in the message. Without that, they all end up changing the same 'random' letter in the same 'random' position in the message.
You can create a multi-pipe solution if you wish. I got what appeared to be deterministic behaviour from the single-pipe solution, though there is no guarantee of the sequencing. If, for example, each child waited for a random delay using nanosleep() or equivalent:
struct timespec nap = { .tv_sec = 0, .tv_nsec = (rand() % 1000) * 1000000 };
nanosleep(&nap, 0);
then you get an arbitrary sequence in the child processing. For example:
Parent: erhalten: Absolutely nothing to do with me
Parent: weiter: Absolutely nothinglto do with me
Random: 2028074573
Random: 988903227
Random: 1120592056
Random: 359101002
child4: erhalten (32): Absolutely nothinglto do with me
child4: weiter (32): vbsolutely nothinglto do with me
Child with PID 43008 exited with status 0x0000.
child3: erhalten (32): vbsolutely nothinglto do with me
child3: weiter (32): vbsolutelyGnothinglto do with me
Child with PID 43007 exited with status 0x0000.
child2: erhalten (32): vbsolutelyGnothinglto do with me
child2: weiter (32): vbsolutelyGnothinglto doawith me
Child with PID 43006 exited with status 0x0000.
child1: erhalten (32): vbsolutelyGnothinglto doawith me
child1: weiter (32): vbsolutelyGnothinglto doawimh me
Child with PID 43005 exited with status 0x0000.
Parent: weiter (32): vbsolutelyGnothinglto doawimh me
Tried to change your code acc to below, not sure if this is exectly what you want.
Anyhow the children are running...
int main(int argc, char *argv[])
{
if (argc != 3)
{
error("Wrong number of arguments\n");
}
int nrch;
nrch = atoi(argv[1]);
char *msg = argv[2];
printf("Parent: erhalten: %s\n", msg);
int i = 0;
msg = modify(argv[2]);
printf("Parent: weiter: %s\n", msg);
pid_t pids[10];
int fd[2];
/* Start children. */
for (i = 0; i < nrch; ++i)
{
if ((pids[i] = fork()) < 0)
{
error("Can't fork process");
}
if (pipe(fd) == -1)
{
error("Can't create the pipe");
}
// printf ( " pids[i] %d , i %d \n", pids[i] , i);
if (pids[i] == 0)
{
if (dup2(fd[0], 0) == -1)
{
error("Can't dup2 (A)");
}
close(fd[1]);
fgets(msg, 255, stdin);
printf("child%d: erhalten: %s\n", (i + 1), msg);
modify(msg);
printf("child%d: weiter: %s\n", (i + 1), msg);
fprintf(stdout, msg);
}
else
{
// printf("in else i= %d \n",i);
if (dup2(fd[1], 0) == -1)
{
error("Can't dup2 (B)");
}
close(fd[0])
exit(0);
}
}
/* Wait for children to exit. */
int status;
pid_t pid;
while (nrch > 0)
{
pid = wait(&status);
if (pid > -1)
printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
--nrch;
}
return 0;
}