Zdravim,
mal som spravit taky program pre linux, len akosi mi nejde a ja neviem prist preco...
Konkretne problem je u funkcii execve a sice pri predavani parametrov potomkovi, nevedeli by ste mi niekto pomoct?
tu su zdrojaky...
potomok.c kompilovany ako potomok :
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *buffer;
int file_pipe,
velkost;
file_pipe = atoi(argv[1]);
velkost = atoi(argv[2]);
buffer = (char *) malloc(velkost * sizeof(char));
read(file_pipe, buffer, velkost);
printf("Vypis pipe: %s\n", buffer);
return 0;
}
no a rodic.c kompilovany ako rodic (aj ked na tom nezalezi...)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int pid;
int file_pipes[2];
char *pole[2];
char text[] = "hello world!";
pole[0] = (char *) malloc(10 * sizeof(char));
pole[1] = (char *) malloc(10 * sizeof(char));
pipe(file_pipes);
sprintf(pole[0], "%d", file_pipes[0]);
printf("%s", pole[0]);
sprintf(pole[1], "%d", strlen(text));
pid=fork();
switch(pid)
{
case -1:
perror("Nepodarilo sa vytvorit potomka.");
break;
case 0:
execve("potomok", pole, NULL);
break;
case 1:
write(file_pipes[1], text, strlen(text));
printf("Ja som rodic...");
wait(NULL);
break;
}
free((void *) pole[0]);
free((void *) pole[1]);
return 0;
}