yeaa i have been trying to fix it for the past hour or so
Code:
int myfork(int i);
int main(void)
{
int numofpro, i;
FILE *fifo;
printf("How many child processes? (max5) >");
scanf("%d", &numofpro);
printf("\n%d Child Process Being Created...please wait...\n\n",numofpro);
mkfifo("pipefile", S_IRUSR | S_IWUSR);
fifo=fopen("pipefile", "r+"); /* read to filehandle */
for(i=0;i<numofpro;i++)
{
myfork(i);
}
printf("Process 1 is sucsessfully ended\n\n ");
}
int mfork(int i)
{
int random;
int pipepid[10], pipeprior[10];
pid_t pid;
FILE *fifo;
random = rand()%90;
pid = fork(); // fork
if (pid == 0) /* child*/
{
pipepid[i] = getpid();
pipeprior[i] = random;
fprintf(fifo, "Prio : %d PID : %d \n",pipepid[i],pipeprior[i]); fflush(fifo); /* notice the newline */
printf("Message sent ..\n");
}
else
{
/*PARENT*/
}
fclose(fifo);
unlink("pipefile"); /* erase it */
}
i dont know weather to have the forking in diff functions is this a good way will it work out
i want the user to input how many children for i i will then be passed into the fork function the fork function will then make sooo many children , put the pid and priority in order put them in the fifo then i will have a read function in 2.c to read it put the pid and prior back into arrays the other side and print them out soo i can then kill process n stuff . any ideas how i could achieve this ??
EDIT:
i have found the problem the myfork function had a typo although now when i run 2.c i get "Segmentation Fail" sooo now i have broken my fifo again.
This was my orignal code before i made the myfork function:
Code:
int main(void)
{
int numofpro, i=0,random;
int pipepid[10], pipeprior[10];
FILE *fifo;
pid_t pid;
random = rand()%90;
printf("How many child processes? (max5) > ");
scanf("%d", &numofpro);
printf("\n%d Child Process Being Created...please wait...\n\n",numofpro);
mkfifo("pipefile", S_IRUSR | S_IWUSR);
fifo=fopen("pipefile", "r+"); /* read to filehandle */
for(i=0;i<numofpro;i++)
{
pid = fork(); // fork
if (pid == 0) /* child*/
{
pipepid[i] = getpid();
pipeprior[i] = random;
fprintf(fifo, "Prio : %d PID : %d \n",pipepid[i],pipeprior[i]); fflush(fifo); /* notice the newline */
printf("Message sent ..\n");
}
else
{
/*PARENT*/
}
fclose(fifo);
return 0;
}
fclose(fifo);
unlink("pipefile"); /* erase it */
return 0;
}