Semget and fork(): no such file or directory - c

I have a problem with accessing semaphore in child process. I can't get already created semaphore through semget() when in child process.
Here is make_semaphores() function;
int make_semaphores(key_t key)
{
int semid;
semid = semget(key, 3, IPC_CREAT|/*IPC_EXCL|*/0666);
if(semid == -1)
{
perror("Creating an array of semaphores");
exit(1);
}
if(semctl(semid,0,SETVAL, (int)MAX)==-1)
{
perror("Initializing 'empty' semaphore");
exit(1);
}
if(semctl(semid,1,SETVAL, (int)0)==-1)
{
perror("Initializing 'full' semaphore");
exit(1);
}
if(semctl(semid,2,SETVAL, (int)1)==-1)
{
perror("Initializing mutex");
exit(1);
}
return semid;
}
Here is allocate_memory function:
int* allocate_memory(int *buf, key_t key)
{
int shmid;
shmid=shmget(key,(MAX+1)*sizeof(int),IPC_CREAT|/*IPC_EXCL|*/0666);
if(shmid==-1)
{
perror("Creating shared memory segment");
exit(1);
}
buf=(int*)shmat(shmid,NULL,0);
if(buf==NULL)
{
perror("Including shared memory segment");
exit(1);
}
buf[0]=0;
return buf;
}
I searched and didn't find a satisfying answer, so sorry if I'm needlessly spamming. Thanks in advance for any kind of help.
Here is full main:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define S1KEY 467221
#define S2KEY 379231
#define S3KEY 217411
#define MAX 10
int main(int argc, char* argv[])
{
srand(time(NULL));
int fork_id;
int *buf1, *buf2, *buf3;
int semid1, semid2, semid3, i, j, r, smd1;
int prod_info[3]={0,0,0}, cons_info[3]={0,0,0};
printf("-------------------\n");
buf1=allocate_memory(buf1,S1KEY);
buf2=allocate_memory(buf2,S2KEY);
buf3=allocate_memory(buf3,S3KEY);
printf("-------------------\n");
semid1=make_semaphores(S1KEY);
semid2=make_semaphores(S2KEY);
semid3=make_semaphores(S3KEY);
smd1=semget(S1KEY,3,0666);//here is okay
if(semid1!=smd1)
{
printf("semid1: %d smd1: %d\n",semid1,smd1);
perror("S1KEY does not exist");
}
printf("----------------\n");
for(i=0;i<4;++i)
{
fork_id=fork();
if(fork_id<0)
{
perror("fork()");
exit(1);
}
else if(fork_id==0)// one of four children
{
smd1=semget(S1KEY,3,0666);//here is NOT okay, smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
if(i==3) //producer for 3 buffers
{
for(j=0;j<100;j++)
{
r=(rand()%3)+1;
printf("-------------------------\n");
if(r==1)
{
printf("Prod:1||%d||%d\n",prod_info[0],getval_semaphores(semid1,0));
printf(" %d\n", buf1[0]);
produce(buf1,semid1,i);
prod_info[0]++;
}
else if(r==2)
{
printf("Prod:2||%d||%d\n",prod_info[1],getval_semaphores(semid2,0));
printf(" %d\n", buf2[0]);
produce(buf2,semid2,i);
prod_info[1]++;
}
else
{
printf("Prod:3||%d||%d\n",prod_info[2],getval_semaphores(semid3,0));
printf(" %d\n", buf3[0]);
produce(buf3,semid3,i);
prod_info[2]++;
}
}
printf("Produced: 1:%d 2:%d 3:%d\n", prod_info[0],prod_info[1],prod_info[2]);
exit(1);
}
else if(i==2) //3 consumers for 3 buffers (1,2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:3\n");
printf("%d\n", buf1[0]);
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[0]=consume(buf1,semid1);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf1: %d\n",cons_info[0]);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==1) //2 consumers for 2 buffers (2,3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:2\n");
printf("%d\n", buf2[0]);
printf("%d\n", buf3[0]);
cons_info[1]=consume(buf2,semid2);
cons_info[2]=consume(buf3,semid3);
printf("Buf2: %d\n",cons_info[1]);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
else if(i==0) //1 consumer for 1 buffer (3)
{
for(j=0;j<100;j++)
{
//usleep(1);
printf("Cons:1\n");
printf("%d\n", buf3[0]);
cons_info[2]=consume(buf3,semid3);
printf("Buf3: %d\n",cons_info[2]);
}
exit(1);
}
}
else
{
if(i==4) //Parent, waits after making children
{
smd1=semget(S1KEY,3,0666);
if(semid1!=smd1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
printf("Parent\n");
wait(NULL);
}
}
}
break_semaphores(semid1);
break_semaphores(semid2);
break_semaphores(semid3);
return 0;
}

I am afraid you misunderstand how the creation of processes works.
Parent/Child process model
The process creation model is generally as follows
pid_t p1 = fork();
if (p1 < 0) {
perror("problem forking");
}
else if (p1 == 0) {
// parent process
printf("parent process\n");
// do_parent_process_stuff()
}
else {
// child process
printf("child process: %d\n", p1);
// do_child_process_stuff()
}
Correction to your code
As for your code, in your main function you will want to replace
for(i=0;i<4;++i)//parent creates four children
{
fork_id=fork();
smd1=semget(S1KEY,3,0666);//here is NOT okay (when in child process), smd returns -1
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
//the rest
with something like this
/* Start children. */
for (i = 0; i < n; ++i) {
if ((pids[i] = fork()) < 0) {
perror("error forking");
}
// parent
else if (pids[i] == 0) {
exit(0);
}
// child process
else {
smd1=semget(S1KEY,3,0666);
if(smd1==-1)
{
printf("[%d]semid1: %d smd1: %d\n",i,semid1,smd1);
perror("S1KEY does not exist");
}
else {
// do stuff with semaphores
}
}
}

Related

How to make multiple child proccess pause until a signal is sent

I have a program with three child processes all doing the exact same thing with a fourth child process told send signals while the main function waits until the three child processes are complete. I am using an alarm signal as well as two user signals to make the child process send output to a different terminal. I'm using the pause function to wait for a signal but the second I start the program it completes, not waiting at all. Am I using pause correctly?
int alarmR =0;
int SNMPR =0;
int reconR=0;
int child_pid[4];
char strD[100];
FILE *fpt[4];
void Alarmhandler(int sig);
void Reconfigure(int sig);
void SNMPhandler(int sig);
int which(int wait_ret, int child_proc[], int p);
void IT1(void);
void IT2(void);
void IT3(void);
void command(void);
void command(void)
{
signal(SIGALRM,Alarmhandler);
signal(SIGUSR1,Reconfigure );
signal(SIGUSR2,SNMPhandler );
char text;
int n;
printf("please enter a command from the following list\n");
printf("\tsn:Send a SNMP request\n\trn:Send a reconfiguration");
printf("script\n\tkn: shutdown process\n");
scanf("%c%d",&text,&n);
if(text == 'k')
{
printf("Terminated IT service %d\n",n);
n = n-1;
kill(child_pid[n],1);
}
else if(text=='s')
{
n = n-1;
kill(child_pid[n],SIGUSR2);
}
else if(text=='r')
{
n = n-1;
kill(child_pid[n],SIGUSR1);
}
}
void Alarmhandler(int sig)
{
alarmR =1;
}
void Reconfigure(int sig)
{
reconR =1;
}
void SNMPhandler(int sig)
{
SNMPR =1;
}
int which(int wait_ret, int child_proc[], int p)
{
int i;
for (i = 0; i < p; i++)
if (child_proc[i] == wait_ret)
return i;
return -1;
}
int main(int argc, char *argv[])
{
int ttyindex;
// int Terminal[4];
int term_cnt = 0;
int wait_r,x;
int process = 0;
int child_proc[4];
int es[3];
if(argc != 5) {
printf("Usage: ./lab7 1 2 3 4\n");
exit(1);
}
for(term_cnt =0; term_cnt <4;term_cnt ++)
{
ttyindex = -1;
ttyindex = atoi(argv[term_cnt+1]);
if (ttyindex < 1) {
printf("invalid terminal number %s\n", argv[term_cnt+1]);
exit(1);
}
sprintf(strD, "/dev/pts/%d", ttyindex);
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if (child_proc[process] != 0)
{
// parent process
process++;
child_proc[process] = fork();
if(child_proc[process] !=0)
{
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
wait_r = wait(NULL);
x =which(wait_r, child_proc, process);
printf("Waited for %d (child %d) to finish.\n", wait_r,x);
if(WIFEXITED(wait_r))
{
es[x] = WEXITSTATUS(wait_r);
}
for(x = 0; x <3;x++)
{
if(es[x] == 0)
{
printf("\nJob well done IT specialist %d",(x+1));
printf(" Prepare for new attacks!\n");
}
else if(es[x] == 1)
{
printf("\nIT service %d compromised",(x+1));
printf(" , we are going out of business!\n");
}
else
{
printf("\nCall HR, we need a new");
printf(" cybersecurity expert for service");
printf(" %d\n",(x+1));
}
}
}
else
{
child_pid[3] = getpid();
command();
}
}
else
{
child_pid[2]=getpid();
IT3();
}
}
else
{
child_pid[1] = getpid();
IT2();
}
}
else
{
child_pid[0] = getpid();
IT1();
}
}
void IT1(void)
{
printf("i live");
signal(SIGALRM,Alarmhandler);
signal(SIGUSR1,Reconfigure );
signal(SIGUSR2,SNMPhandler );
clock_t start=0, end=0;
static int check = 0;
static int recona =0;
double t= 0;
int threat= 1;
srand48(time(NULL));
fprintf(fpt[1],"This is IT service 1\n");
while(1)
{
printf("%d %d %d",reconR,alarmR,SNMPR);
pause();
// alarm(1);
if(reconR == 1)
{
reconR =0;
if(recona >0)
{
fprintf(fpt[1],"Cannot reconfigure more than once.you are fired!");
exit(1);
}
if(threat < 16)
{
fprintf(fpt[1],"Threat level is not critical.you are fired");
exit(1);
}
fprintf(fpt[1],"Reconfiguring system to thwart attack-this may take a few seconds\n");
recona++;
}
if(alarmR == 1 )
{
if(check >0)
{
t =((double)(end - start))/CLOCKS_PER_SEC;
}
alarmR = 0;
if(recona >0)
{
threat --;
}
else
{
if (drand48() < 0.5)
{
threat++;
}
else if (threat > 1 && drand48() < 0.6)
{
threat--;
}
}
if(t <5)
{
fprintf(fpt[1],"Next report available in %f seconds\n",(5-t));
}
if(threat >15)
{
fprintf(fpt[1],"Intruder! Data stolen...");
exit(1);
}
else if(threat < 10 && recona >0)
{
fprintf(fpt[1],"Attack averted. Mission Complete");
exit(0);
}
}
if(SNMPR == 1)
{
if(check == 2)
{
end = clock();
t = ((double)(end - start))/CLOCKS_PER_SEC;
}
SNMPR = 0;
check = 1;
if(t <5)
{
fprintf(fpt[1],"Load to high. Threat is increased");
threat++;
t = 0;
}
else{
if(threat >=10)
fprintf(fpt[1],"Threat level is red\n");
else if(threat <10 && threat >=5)
fprintf(fpt[1],"Threat level is orange\n");
else
fprintf(fpt[1],"Threat level is green\n");
}
if(check ==1)
{
start = clock();
check = 2;
}
}
}
}
you can use the sigsuspend() system call to make a process wait for a signal, and kill() for sending signal to a process or process group.I hope by using both of these system calls, you can code your desired task.
you are putting a wait() on a process which has no child here
wait_r = wait(NULL);
This causes that wait() return immediately with error ECHILD and terminates the fourth child process. It's termination raises SIGCHLD causes pause() to return.

C - child process of a child process

I have to reproduce a process family like this: father --> child --> grandson.
I don't understand why the grandson code is never executed.
My code scheme is like this:
int main() {
int fatherProcess, p1, p2;
p1 = fork();
if(p1 <0) {
perorr("Failed to create P1\n");
} else if(p1 == 0) {
//child code
p2 = fork();
if(p2 < 0) {
perorr("Failed to create P2\n");
} else if(p2 == 0) {
//grandson code
pritnf("Hello I'm the GRANDSON\n");
} else {
//child code
pritnf("Hello I'm the CHILD\n");
}
} else {
//father code
pritnf("Hello I'm the father\n");
}
return 0;
}
The stamp that I get is:
- Hello I'm the GRANDSON
- Hello I'm the father
You have made two spelling error. I have fixed it and you can try the following code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main() {
int fatherProcess, p1, p2;
p1 = fork();
if(p1 <0) {
perror("Failed to create P1\n");
} else if(p1 == 0) {
//child code
p2 = fork();
if(p2 < 0) {
perror("Failed to create P2\n");
} else if(p2 == 0) {
//grandson code
printf("Hello I'm the GRANDSON\n");
} else {
//child code
printf("Hello I'm the CHILD\n");
}
} else {
//father code
printf("Hello I'm the father\n");
}
return 0;
}
Your code:
perorr --> perror
pritnf --> printf

Creating shared memory in C

I created following method in C to create a shared memory segment to store counter value. But I can't store data in this segment.When I try to print the value of the counter it gives me a garbage value.Whats wrong with this code?
CreateCounter()
{
key = ftok(".",'B');
shmCntid = shmget(key,COUNTER_SIZE,IPC_CREAT|0666);
if(shmCntid == -1 )
{
perror("shmget");
exit(1);
}
else
{
printf("Creating new Sahred memory sement\n");
cntPtr = shmat(shmCntid,0,0);
if(cntPtr == -1 )
{
perror("shmat");
exit(1);
}
}
}
This method is called inside the main method as follows.
int *cntPtr;
int rowCnt;
sem_t s;
sem_t c;
sem_t r;
int main(int argc, int *argv[])
{
int pid, pid2, pid3, i;
CreateBuf1();
CreateBuf2();
CreateCounter();
GetInput(argv[1],*buf1Ptr);
sem_init(&c, 0, 1);
sem_init(&r, 0, 1);
sem_init(&s, 0, 1);
for( i = 0 ; i < 9; i++)
{
pid = fork();
if(pid < 0)
{
printf("Fork error !\n");
}
else if (pid == 0)
break;
}
if(pid < 0)
{
printf("Fork error !\n");
}
else if (pid == 0)
{
sem_wait(&r);
Grp1 (i,i);
cntPtr+=rowCnt;
sleep(1);
sem_post(&r);
sem_post(&c);
exit(0);
}
else
{
wait(NULL);
}
pid2 = fork();
if(pid2 < 0)
{
printf("Fork error !\n");
}
else if (pid2 == 0)
{
sem_wait(&c);
Grp2(9);
cntPtr+=colCnt;
sleep(1);
sem_post(&c);
exit(0);
}
else
{
wait(NULL);
}
// This space is to print the values..............
shmctl(shmBuf1id,IPC_RMID,0);
shmctl(shmBuf2id,IPC_RMID,0);
shmctl(shmCntid,IPC_RMID,0);
return 0;
}

Close pipes get Bad file descriptor

Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 2;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
if (close(p1[i][0]) != 0) {
perror("close1"); // <----error
}
if (close(p1[i][1]) != 0) {
perror("close");
}
if (close(p2[i][0]) != 0) {
perror("close");
}
if (close(p2[i][1]) != 0) {
perror("close2"); // <----error
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
When I run this, it gives me this output
close1: Bad file descriptor
close2: Bad file descriptor
close1: Bad file descriptor
close2: Bad file descriptor
8798
8799
What I'm trying to do is to create two 2D array of pipes and fork num times.
The creating and running are working well, but some pipes can't close.
It seems that p1[i][0] and p2[i][1] are never closer properly.
The reason you're getting the EBADFD is you're attempting to close the same file descriptor twice.
I added a print statement in this first snippet of code to show/track what file descriptor is being closed. If you compile and run this you'll see that the error message appears right after you try to close the descriptor for a second time.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 1;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
printf("Child closing: Pipe1 %d End %d\n", i, 1);
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
printf("Child closing: Pipe2 %d End %d\n", i, 1);
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
printf("Parent closing: Pipe1 %d End %d\n", i, 0);
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
printf("Parent closing: Pipe1 %d End %d\n", i, 1);
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
printf("Closing: Pipe1: %d End: %d\n", i, 0);
if (close(p1[i][0]) != 0) {
perror("close1"); // <----error
}
printf("Closing: Pipe1: %d End: %d\n", i, 1);
if (close(p1[i][1]) != 0) {
perror("close");
}
printf("Closing: Pipe2: %d End: %d\n", i, 0);
if (close(p2[i][0]) != 0) {
perror("close");
}
printf("Closing: Pipe2: %d End: %d\n", i, 1);
if (close(p2[i][1]) != 0) {
perror("close2"); // <----error
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}
Check for the pid and close the ends that weren't closed inside your first loop. This code assumes you are reading and writing on a specific pipe depending on the child's/parent's need. You may need to adjust to however your use case dictates:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int main(int argc, char **argv) {
int num = 1;
pid_t pid;
int i;
int p1[num][2], p2[num][2];
for (i = 0; i < num; i++) {
if (pipe(p1[i]) == -1) {
perror("pipe");
exit(1);
}
if (pipe(p2[i]) == -1) {
perror("pipe");
exit(1);
}
}
for (i = 0; i < num; i++) {
if ((pid = fork()) == 0) {
if (close(p1[i][1]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][0]) != 0) {
perror("close");
exit(1);
}
printf("%d\n", getpid());
exit(0);
} else if (pid > 0) {
if (close(p1[i][0]) != 0) {
perror("close");
exit(1);
}
if (close(p2[i][1]) != 0) {
perror("close");
exit(1);
}
continue;
} else {
perror("fork");
exit(1);
}
}
for (i = 0; i < num; i++) {
if (pid == 0) {
if (close(p1[i][0]) != 0) {
perror("close1");
}
if (close(p2[i][1]) != 0) {
perror("close");
}
} else {
if (close(p1[i][1]) != 0) {
perror("close");
}
if (close(p2[i][0]) != 0) {
perror("close2");
}
}
}
for (i = 0; i < num; i++) {
if (wait(NULL) == -1) {
perror("wait");
exit(1);
}
}
return 0;
}

Error creating the Fifo

I have the following code and I'm using the function mkfifo to create a Fifo, but the problem is that when I run the program, I get the printf saying "Error creating the fifo". What could it be? Here's the code:
//gcc Exam.c -o p
//./p 5 /home/directoryFile.c
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int studentId, children = 0, j, i, childNumber[15], fdFile, fdread, fdFifo, fdOpenFifo;
float bytesToRead;
char directory[50];
char *buffer = malloc(256), *textToSend = malloc(256), *buffer2 = malloc(256);
char childFifo[16];
system("clear");
if(argc-1 < 1)
{
printf("\nSome arguments are missing\n");
return EXIT_FAILURE;
}
studentId = atoi(argv[1]);
strcpy(directory,argv[2]);
if((studentId%2) == 0)
{
children = 15;
}
else
{
if((studentId%3) == 0)
{
children = 10;
}
else
{
if((studentId%5) == 0)
{
children = 7;
}
else
{
printf("\nThe studentId is invalid \n");
return EXIT_FAILURE;
}
}
}
struct stat fileInfo;
stat(argv[2],&fileInfo);
bytesToRead = fileInfo.st_size / children;
printf("Children: %d\n",children);
printf("File size: %lld\n",(long long int) fileInfo.st_size);
printf("Bytes: %.2f\n",bytesToRead);
fdFile = open(directory,O_RDONLY);
if(fdFile == -1)
{
printf("\nError opening file\n");
return EXIT_FAILURE;
}
for(i=0;i<children;i++)
{
sprintf(childFifo,"Child_%d",i);
fdFifo = mkfifo(childFifo,0777);
if(fdFifo == -1)
{
printf("\nError creating the fifo\n");
return EXIT_FAILURE;
}
childNumber[i] = fork();
if(childNumber[i] == -1)
{
printf("\nError creating the child\n");
return EXIT_FAILURE;
}
if(childNumber[i] == 0)
{
fdOpenFifo = open(childFifo,O_WRONLY);
if(fdOpenFifo == -1)
{
printf("\nError opening the fifo\n");
return EXIT_FAILURE;
}
fdread = read(fdFile,buffer,bytesToRead);
if(fdread == -1)
{
printf("\nError reading the file\n");
return EXIT_FAILURE;
}
//printf("%s",buffer);
//printf("\n\n------------------------\n\n");
write(fdOpenFifo,&buffer,sizeof(char));
//sprintf(textToSend,"%s%s",textToSend,buffer);
return EXIT_SUCCESS;
}
else
{
sleep(1);
read(fdOpenFifo,&buffer2,sizeof(char));
printf("\nI show the content of the file: %s\n",buffer2);
//waitpid(childNumber[i],NULL,WNOHANG);
close(fdOpenFifo);
}
}
//printf("\nI show the content of the file: %s\n",textToSend);
close(fdFile);
for(j=0;j<children;j++)
{
wait(NULL);
}
return EXIT_SUCCESS;
}
Here's the output for: ./p 5 /home/directoryFile.c
Children: 7
File size: 267
Bytes: 38.00
Error creating the fifo

Resources