C - Named pipe between top level process and bottom level (leaf) process - c

I'm trying to write to a named pipe from a bottom level leaf process and read from the pipe on the top level process.
To do this, I'm first creating the FIFO in the top level process, then using a for loop to fork more processes. In the for loop I'm checking for leaf processes and if it's a leaf, I'm writing to the FIFO and breaking from the loop. Then, after the loop, I'm trying to read from the FIFO in the top level process. This isn't working, my program just gets stuck and stalls after the leaf process is created.
How can I send a message from a leaf back up to the top parent process through a FIFO?
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#define MAX_BUF 1024
int main(int argc, char **argv){
int numprocs = atoi(argv[1]);
int lev = numprocs;
fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", lev, getpid(), getppid());
//create shared memory
const int SIZE = numprocs * sizeof(int);
const char *name = "dleggio1OS";
int shm_fd;
int *ptr;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, SIZE);
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
*ptr = getpid();
//create fifo
int fd;
char *myfifo = "/tmp/dleggio1fifo";
mkfifo(myfifo, 0666);
//spawn procs
int i;
for(i = 1; i < numprocs; i++){
lev--;
int pfds[2];
char buf[30];
if(pipe(pfds) == -1){
perror("pipe");
exit(1);
}
pid_t pid;
if((pid = fork()) < 0){
perror("fork");
exit(1);
}
if(pid == 0){ //child
const int SIZE = numprocs * sizeof(int);
const char *name = "dleggio1OS";
int shm_fd;
int *ptr;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, SIZE);
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
ptr[i] = getpid();
close(pfds[1]);
if(read(pfds[0], buf, 3) <= 0){
perror("child");
exit(1);
}
int check = atoi(buf);
fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", check, getpid(), getppid());
if(check == 1){ //leaf
fd = open(myfifo, O_WRONLY);
write(fd,"leaf",sizeof("leaf"));
close(fd);
break;
}
}
else{ //parent
close(pfds[0]);
char hold[3];
sprintf(hold,"%d",lev);
if(write(pfds[1], hold, 3) <= 0){
perror("parent");
exit(1);
}
wait(NULL);
return 0;
}
}
//read fifo
char buff[MAX_BUF];
fd = open(myfifo,O_RDONLY);
read(fd,buff,MAX_BUF);
close(fd);
shm_unlink(name);
unlink(myfifo);
return 0;
}
OUTPUT:
ALIVE: Level 3 process with pid=554, child of ppid=451.
ALIVE: Level 2 process with pid=555, child of ppid=554.
ALIVE: Level 1 process with pid=556, child of ppid=555.
_ // <---- stalls here

All processes are hung on a "wait()" call (i.e. waiting for one of its children to exit), except for the last child forked... which is hung on "open(myfifo, O_WRONLY)"....
The last child will continue to hang until a process opens the fifo for reading...

Related

Processes with group of 2 semaphores and shared memory

I write a program containing two processes: the first one contains a group of two semaphores and creates the child process that reads all data in the shared memory segment and prints them.
In the second one, the child process computes the data using a compute function that returns 0 when all data are computed. It transmits them to the parent through the shared memory segment.
To write data:
On the 1st semaphore the child makes P and the parent make V.
On the 2nd semaphore the child makes V and the parent make P.
But as I'm new in this topic and still getting hardness to understand, it seems like I'm doing something wrong because it's not working as it has to be.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/wait.h>
int sum =0;
int compute(int data){
sum += data;
return sum;
}
int main(){
int i;
int shm_id;
int data;
pid_t pid;
key_t shm_key;
sem_t *sem;
// unsigned int sem_value =2;
shm_key = ftok("/dev/null", 65);
shm_id = shmget(shm_id, sizeof(int), 0644 | IPC_CREAT);
if (shm_id < 0){
perror("shmgget");
exit(EXIT_FAILURE);
}
// data = shmat(shm_id, NULL, 0);
sem = sem_open("semaphore", O_CREAT | O_EXCL, 0644, 2);
for (i = 0; i < 2; i++){
pid = fork();
if (pid < 0)
{
perror("fork");
sem_unlink("semaphore");
sem_close(sem);
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
break;
}
}
if (pid == 0)
{
puts("Enter the data:");
scanf("%d", &data);
//child process
sem_wait(sem);
printf("Child - %d is in critical section\n", i);
sleep(1);
puts("Enter the data:");
scanf("%d", &data);
// *shrd_value += data;
printf("Child - %d: new value of data = %d\n", i, data);
printf("Child - %d: sum of whole data by far = %d\n", i, compute(data));
sem_post(sem);
exit(EXIT_SUCCESS);
}
else if (pid > 0)
{
//parent process
while (pid = waitpid(-1, NULL, 0))
{
if (errno == ECHILD)
{
break;
}
}
puts("All children exited");
shmdt(&data);
shmctl(shm_id, IPC_RMID, 0);
sem_unlink("semaphore");
sem_close(sem);
exit(0);
}
}
Output:
Enter the data:
Enter the data:
2
Child - 0 is in critical section
1Enter the data:
Child - 1 is in critical section
Enter the data:
3
Child - 0: new value of data = 3
Child - 0: sum of whole data by far = 3
2
Child - 1: new value of data = 2
Child - 1: sum of whole data by far = 2
All children exited
I have also modified the way they write to shared memory: they write directly at the address given by shmat call that is missing in your code.
I have fixed some bugs and simplifed the code (removed the array - added detailed logging especially before and after entering the critial section):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
int i;
int shm_id;
pid_t pid;
int *addr;
int data;
pid_t current_pid;
key_t shm_key;
sem_t *sem;
shm_key = ftok("/dev/null", 65);
shm_id = shmget(shm_key, sizeof(int), 0644 | IPC_CREAT);
if (shm_id < 0){
perror("shmget");
exit(EXIT_FAILURE);
}
sem_unlink("semaphore");
sem = sem_open("semaphore", O_CREAT, 0644, 1);
if (sem == SEM_FAILED) {
perror("sem_open");
exit(EXIT_FAILURE);
}
addr = (int *) shmat(shm_id, (void *) 0, 0);
if (addr == (void *) -1) {
perror("shmat");
exit(EXIT_FAILURE);
}
*addr = 0;
for (i = 0; i < 2; i++){
pid = fork();
if (pid < 0)
{
perror("fork");
sem_close(sem);
sem_unlink("semaphore");
exit(EXIT_FAILURE);
}
}
if (pid == 0)
{
current_pid = getpid();
printf("Child %d: waiting for critical section \n", current_pid);
sem_wait(sem);
printf("Child %d: enters in critical section \n", current_pid);
printf("child %d: Enter the data:\n", current_pid);
scanf("%d", &data);
printf("Child %d: new value of data = %d\n", current_pid, data);
printf("Child %d: sum of whole data so far = %d\n", current_pid, *addr += data);
sem_post(sem);
printf("Child %d exits from critical section\n", current_pid);
exit(EXIT_SUCCESS);
}
else if (pid > 0)
{
//parent process
while (pid = waitpid(-1, NULL, 0))
{
if (errno == ECHILD)
{
break;
}
}
puts("All children exited");
shmdt(addr);
shmctl(shm_id, IPC_RMID, 0);
sem_close(sem);
sem_unlink("semaphore");
exit(0);
}
exit(0);
}
Note that semaphore initial value must be 1 to have a true critical section for 2 processes.
I have also removed the sleep calls and we can see that one of the process is waiting:
Child 22514: waiting for critical section
Child 22514: enters in critical section
child 22514: Enter the data:
Child 22515: waiting for critical section
333
Child 22514: new value of data = 333
Child 22514: sum of whole data so far = 333
Child 22514 exits from critical section
Child 22515: enters in critical section
child 22515: Enter the data:
666
Child 22515: new value of data = 666
Child 22515: sum of whole data so far = 999
Child 22515 exits from critical section
All children exited
All children exited
Here's the code with producer and consumer process
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> // O_CREAT, O_EXEC
#include <errno.h> // errno, ECHILD
#include <unistd.h>
#include <sys/shm.h> // shmat(), IPC_RMID
#include <sys/wait.h>
#include <semaphore.h> // sem_open(), sem_destroy(), sem_wait()...
#include <sys/types.h> // key_t, sem_t, pid_t
#include <pthread.h>
#define BUFF 10
typedef struct data{
int buff[BUFF];
int size;
int index;
}DATA;
int main(int argc, char const *argv[])
{
sem_t *full, *empty, *access;
key_t shm_key;
int shm_id;
full = sem_open ("fullname", O_CREAT , 0644, 15);
empty = sem_open ("empty", O_CREAT , 0644, 0);
access = sem_open ("access", O_CREAT , 0644, 1);
if (argc!=2)
{
exit(1);
}
int value=atoi(argv[1]);
//initialize a shared variable in shared memory
shm_key = ftok("/dev/null", 60);
shm_id = shmget(shm_key, sizeof(DATA), 0);
// shared memory error check
if (shm_id < 0){
shm_id = shmget(shm_key, sizeof(DATA), 0644 | IPC_CREAT);
DATA *data = (DATA*) shmat (shm_id, NULL, 0);
data->size=0;
data->index=0; //index
}
printf("Shared memory id: %d\n",shm_id );
printf("Checking buffer...,\n");
//If in the buffer have free space then will wait for consumer to consume the data\n"
sem_wait(empty);
printf("\nLocking buffer to produce data\n");
sem_wait(access);
//initialize a shared variable in shared memory
shm_key = ftok("/dev/null", 60);
shm_id = shmget(shm_id, sizeof(DATA), 0644 | IPC_CREAT);
// shared memory error check
if (shm_id < 0){
perror("semaphore");
exit(EXIT_FAILURE);
}
//Shared variable
DATA *data = (DATA*) shmat (shm_id, NULL, 0);
int index=(data->size + data->index) % 15;
data->buff[index]=value;
data->size++;
printf("%d is located in %d on the buffer\n",value,index );
//consusming
// attach data to shared memory
index=data->index;
value=data->buff[index];
printf("%d now consumed\n",value );
data->size--;
data->index++;
sem_post(access);
sem_post(full);
return 0;
}

C Programming: Segmentation Fault (Core Dumped)

The program i am trying to write is trying to demonstrate how IPC works on Linux, but i keep getting a core dump error. It compiles fine and will run up until the last output statement in the parent process.
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <err.h>
#include <sysexits.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#define SHM_SIZE 15
int main (int argc, char ** argv[]) {
pid_t pid; //pid variable of type pid
int shmid; //shared memory id
int key = 1776; //randomly chosen key
char *shm; //shared memory name
int pipefd[2];
char buff;
pid = fork(); //creating child process
pipe(pipefd); //creating pipe
if (pid < 0) {
fprintf(stderr, "Fork Failed");
return -1;
} else if (pid == 0) {
shmid = shmget(key, SHM_SIZE, 0);
shm = shmat(shmid, 0, 0);
char *n = (char *) shm;
printf("hello i am the child process. my pid is %d. what is your name?: ", getpid());
scanf("%s", n);
printf("\n");
///////////////////////////////////////////////////////////////////////////////////////
close(pipefd[1]);
printf("pipe opened on child end");
printf("\n");
while(read(pipefd[0], &buff, 1) > 0) {
write(1, &buff, 1);
}
write(1, "\n", 1);
close(pipefd[0]);
printf("pipe successfully closed");
printf("\n");
exit(EXIT_SUCCESS);
} else {
shmid = shmget(key, SHM_SIZE, 0777 | IPC_CREAT);
shm = shmat(shmid, 0, 0);
wait(NULL);
printf("\nThis is Child's Parent. My pid is %d. Nice to me you %s.\n", getpid(), shm);
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////
close(pipefd[0]);
printf("pipe open on parent end");
printf("\n");
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]);
printf("pipe successfully closed");
wait(NULL);
exit(EXIT_SUCCESS);
}
return 0;
}
Does it have something to do with my args[]? Such as could i be accessing memory out of reach? Or am trying to access some invalid pointer?
Many Thanks!
You have several problems in your code
Create the pipe before the fork. You create the pipe twice, once for
the parent process and one for the child process. That makes no sense, the pipe
that the child created cannot be used by the parent. The pipe must already
exists so that the child inherits the file descriptors when the child is
created.
Usually the parent creates the shared memory and the child gets the shmid
from the parent when it does the fork. Otherwise you will have to synchronize
the child and parent. So I would put the creation of the shared memory before
the fork, so that the child inherits the shmid from the parent.
In the line char *n = (char *) shm; the cast is not needed, shm is
already a char*.
In the parent block after the fork, you do wait(NULL); and then proceed to
write into the pipe. That makes no sense and you block both parent and child.
The child blocks on read because the parent hasn't send anything through the
pipe, yet. And the parent blocks on wait, because the child never exits and thus
cannot send anything through the pipe. The parent must first send data
through the pipe, then wait for the child to exit.
In the child block you do scanf("%s", n);, you are not protecting you
against buffer overflows. scanf("%14s", n) would be better. Also you are not
checking if scanf read anything at all. If the user presses
CtrlD then stdin is closed, scanf fails. In that case
n might not be '\0'-terminated and this would lead to undefined behaviour
when the parent tries to print it. So it would be better:
if(scanf("%14s", n) != 1) // avoid buffer overflow
{
fprintf(stderr, "Child: cannot read from stdin\n");
n[0] = 0; // 0-terminating
}
In the parent block after the fork, you do wait twice, why?
Your main is wrong, it should be
int main(int argc, char **argv);
The parent sends the contents of argv[1] to the child through the pipe, but
you fail to check if argv[1] is not NULL. Use this at the start of the
program:
if(argc != 2)
{
fprintf(stderr, "usage: %s string\n", argv[0]);
return 1;
}
So the correct version would be:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <err.h>
#include <sysexits.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#define SHM_SIZE 15
int main (int argc, char **argv) {
pid_t pid; //pid variable of type pid
int shmid; //shared memory id
char *shm; //shared memory name
if(argc != 2)
{
fprintf(stderr, "usage: %s string\n", argv[0]);
return 1;
}
int pipefd[2];
char buff;
// create shared memory before the fork,
// otherwise you will need to syncronize parent
// and child
pipe(pipefd); //creating pipe before the fork
// parent creates shared memory, child inherits shmid
// after fork
shmid = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);
pid = fork(); //creating child process
if (pid < 0) {
fprintf(stderr, "Fork Failed");
return 1; // return -1 would be the same as return 255
} else if (pid == 0) {
shm = shmat(shmid, 0, 0);
char *n = shm; // shm is already a char*
printf("hello i am the child process. my pid is %d. what is your name?: ", getpid());
if(scanf("%14s", n) != 1) // avoid buffer overflow
{
fprintf(stderr, "Child: cannot read from stdin\n");
n[0] = 0; // 0-terminating
}
printf("\n");
///////////////////////////////////////////////////////////////////////////////////////
close(pipefd[1]);
printf("pipe opened on child end");
printf("\n");
printf("Parent sends: ");
fflush(stdout);
while(read(pipefd[0], &buff, 1) > 0) {
write(1, &buff, 1);
}
write(1, "\n", 1);
close(pipefd[0]);
printf("pipe successfully closed");
printf("\n");
exit(EXIT_SUCCESS);
} else {
shm = shmat(shmid, 0, 0);
close(pipefd[0]);
printf("pipe open on parent end");
printf("\n");
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]);
printf("pipe successfully closed");
// not we wait for child to exit
wait(NULL);
printf("\nThis is Child's Parent. My pid is %d. Nice to me you %s.\n", getpid(), shm);
printf("\n");
//////////////////////////////////////////////////////////////////////////////////////
exit(EXIT_SUCCESS);
}
return 0;
}
And the output is:
$ ./b "message to child: stop playing video games!"
pipe open on parent end
hello i am the child process. my pid is 10969. what is your name?: Pablo
pipe opened on child end
Parent sends: message to child: stop playing video games!
pipe successfully closed
pipe successfully closed
This is Child's Parent. My pid is 10968. Nice to me you Pablo.
You are reading and writing from the same end of the pipe you create. Common practice is to read from end [1] and write to end [0]. Tell me if that helps. Additionally, it is also common practice to not have too much going on between the child and parent processes. Attempting to execute code in between segments (parent and child) usually ends up with a segmentation fault, even if your code compiles.

C - Last child never reached signal

My code forks n processes. When the last process is reached, it sends a message to the top level process via a FIFO to kill all processes. My code works great, except the the last process never reaches my signal and never gets killed by the top process.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#define MAX_BUF 1024
int *ptr;
int numprocs;
void signal_handler(int sig){
int currpid = getpid();
int lev;
int i;
for(i = 1; i < numprocs; i++){
if(ptr[i] == currpid){
lev = numprocs - i;
break;
}
}
fprintf(stdout,"EXITING: Level %d process with pid=%d, child of ppid=%d.\n", lev, ptr[i], ptr[i-1]);
exit(0);
}
int main(int argc, char **argv){
numprocs = atoi(argv[1]);
int lev = numprocs;
bool x = true;
//create shared memory
const int SIZE = numprocs * sizeof(int);
const char *name = "dleggio1OS";
int shm_fd;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, SIZE);
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
*ptr = getpid();
//create unnamed pipe
int pfds[2];
char buf[30];
if(pipe(pfds) == -1){
perror("pipe");
exit(1);
}
//create fifo
int fd;
char *myfifo = "/tmp/dleggio1fifo";
mkfifo(myfifo, 0666);
pid_t pid = fork();
if(pid == 0){ //first child ..... keep forking
//read unnamed pipe
close(pfds[1]);
if(read(pfds[0], buf, 3) <= 0){
perror("child");
exit(1);
}
int check = atoi(buf);
fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", check, getpid(), getppid());
//write to shared memory
ptr[numprocs - check] = getpid();
signal(SIGUSR1,signal_handler);
int i;
lev--;
for(i = 2; i < numprocs; i++){
//create named pipe
int pfds[2];
char buf[30];
if(pipe(pfds) == -1){
perror("pipe");
exit(1);
}
lev--;
pid_t pid;
if((pid = fork()) < 0){
perror("fork");
exit(1);
}
else if(pid == 0){
//update shared memory
ptr[i] = getpid();
//read from named pipe
close(pfds[1]);
if(read(pfds[0], buf, 3) <= 0){
perror("child");
exit(1);
}
int check = atoi(buf);
fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", check, getpid(), getppid());
if(check == 1){
//write to fifo
fd = open(myfifo, O_WRONLY);
write(fd,"leaf",sizeof("leaf"));
close(fd);
unlink(myfifo);
signal(SIGUSR1,signal_handler);
}
signal(SIGUSR1,signal_handler);
}
else{
//write to named pipe
close(pfds[0]);
char hold[3];
sprintf(hold,"%d",lev);
if(write(pfds[1], hold, 3) <= 0){
perror("parent");
exit(1);
}
wait(NULL);
}
}
}
else{ //top level
fprintf(stdout,"ALIVE: Level %d process with pid=%d, child of ppid=%d.\n", lev, getpid(), getppid());
//write to unnamed pipe
lev--;
close(pfds[0]);
char hold[3];
sprintf(hold,"%d",lev);
if(write(pfds[1], hold, 3) <= 0){
perror("parent");
exit(1);
}
//read from fifo
char buff[MAX_BUF];
fd = open(myfifo,O_RDONLY);
read(fd,buff,MAX_BUF);
close(fd);
unlink(myfifo);
char *comp = "leaf";
if(strcmp(buff,comp) == 0){
int j;
for(j = 0; j < numprocs; j++) printf("%d\n",ptr[j]);
for(j = 1; j < numprocs; j++) kill(ptr[j],SIGUSR1);
shm_unlink(name);
fprintf(stdout,"EXITING: Level %d process with pid=%d, child of ppid=%d.\n", numprocs, getpid(), getppid());
exit(0);
}
}
}
Output:
./prog1 4
ALIVE: Level 4 process with pid=5129, child of ppid=4692.
ALIVE: Level 3 process with pid=5130, child of ppid=5129.
ALIVE: Level 2 process with pid=5131, child of ppid=5130.
ALIVE: Level 1 process with pid=5132, child of ppid=5131.
5129
5130
5131
5132
EXITING: Level 4 process with pid=5129, child of ppid=4692.
EXITING: Level 3 process with pid=5130, child of ppid=5129.
EXITING: Level 2 process with pid=5131, child of ppid=5130.

Can not wait on shared semaphore

I have to program Readers-Writers Problem in C without using threads. I have used this to initialize shared semaphores:
/* initialize semaphores for shared processes */
resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/rSem");
mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/mSem");
but while running child process
sem_wait(resource);
terminates the program.
here is my complete code:
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
#include <sys/mman.h>
#include <fcntl.h> /* O_CREAT, O_EXEC */
/* prototype for thread routine */
void writer ( int , int *);
void reader ( int , int *, int *);
void multipleFork ( int );
/* global variables */
sem_t *mutex;
sem_t *resource;
int main(int argc, char **argv)
{
int * Buffer = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
int * readcount = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
/* initialize semaphores for shared processes */
resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/rSem");
mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/mSem");
pid_t pid = fork();
if (pid == 0)
writer(1, Buffer);
else
reader(3, Buffer, readcount);
sem_destroy(resource);
sem_destroy(mutex);
exit(0);
} /* main() */
void writer ( int n , int * Buffer)
{
multipleFork(n);
sleep(1);
while(1){
usleep(rand()%3000);
sem_wait(resource);
//<CRITICAL Section>
printf("The writer (%d) ‫‪acquires‬‬ ‫‪the‬‬ ‫‪lock.\n‬‬", getpid());
(*Buffer)++;
printf("The writer (%d) writes ‫‪the‬‬ ‫‪value %d.\n‬‬", getpid(), *Buffer);
sleep(1);
printf("The writer (%d) releases ‫‪the‬‬ ‫‪lock.‬‬\n", getpid());
//<CRITICAL Section>
sem_post(resource);
sleep(1);
}
}
void reader( int n , int * Buffer, int * readcount) {
multipleFork(n);
while(1){
usleep(rand()%3000);
sem_wait(mutex);
usleep(rand()%3000);
if ((*readcount) == 0)
{
sem_wait(resource);
printf("The first reader (%d) acquires the lock.%d\n", getpid(), *readcount);
(*readcount)++;
}
else if ((*readcount) > 0)
{
printf("The reader (%d) acquires the lock.%d\n", getpid(), *readcount);
(*readcount)++;
}
sem_post(mutex);
//<CRITICAL Section>
printf("Reader (%d) reads the value %d.%d\n", getpid(), *Buffer, *readcount);
sleep(1);
//<CRITICAL Section>
sem_wait(mutex);
(*readcount)--;
if ((*readcount) == 0)
{
printf("The last reader (%d) releases the lock.\n", getpid());
sem_post(resource);
}
else
printf("The reader (%d) releases the lock.\n", getpid());
sem_post(mutex);
sleep(1);
}
}
void multipleFork (int n)
{
while(n-1 > 0){
pid_t pid = fork();
if (pid == 0){
return;
}
else
n--;
}
return;
}
I have used
resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
instead of
resource = sem_open ("/rSem", O_CREAT | O_EXCL, 0644, 1);

FIFO between multiple processes - Only printing from a single process

I'm trying to make a FIFO between two programs (one being a child process of the other) so that the child can write data back to the parent. Here's what I have so far:
(Parent)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_BUF 1024
int main(int argc, char *argv[]) {
//number of seperate processes to create
int num_processes = 4;
int i = 0;
//FIFO accross processes
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
/* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
for (i; i < num_processes; i++) {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
}
else if (pid == 0) {
//child now exec's
char* args[] = {"./child", "args", NULL};
execv("./child", args);
}
}
printf("Parent doing stuff\n");
//Parent wait for child
printf("Parent waiting on child\n");
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
if (fcntl(fd, F_GETFD) == -1) {
perror("fd failed");
exit(1);
}
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
//Wait for child processes to finish
int j = 0;
for (j; j < num_processes; j++) {
wait(NULL);
}
//Close FIFO
close(fd);
return 0;
}
(Child, created 4 times)
void main() {
printf("Completed\n");
//Create FIFO
int fd;
char * myfifo = "/tmp/myfifo";
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
if (fcntl(fd, F_GETFD) == -1) {
perror("open failed");
exit(1);
}
write(fd, "Hi", sizeof("Hi"));
//close(fd);
/* remove the FIFO */
//unlink(myfifo);
}
Right now, "Completed" is being printed 4 times, showing that there are 4 seperate processes running as there should be. However, only one "Received: Hi" is printed in the terminal. How come I am not getting a FIFO response from the other processes?
Any help would be greatly appreciated!
You need to check fd and make sure the open succeeded. And note that it can only succeed once, because the first child will unlink(myfifo).
The parent should also wait for all of the children to finish before reading from the fifo. And the parent should read the fifo in a loop until the fifo is empty.
The problem in your code is that there are multiple child writing to the same FIFO.
As pointed out also by user3386109 you have to wait each child and read the FIFO.
here is a sample code:
//Wait for child processes to finish
int child_status = 0;
while (wait(&child_status) != -1) {
if (WIFEXITED (child_status)) {
fprintf (stdout, "the child process exited normally, with exit code %d\n", WEXITSTATUS (child_status));
// Read The buffer
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
}
else fprintf (stderr, "the child process exited abnormally\n");
}
I also suggest to pass to the child an id (this is just a sample add checks if needed):
else if (pid == 0) {
//child now exec's
char mypid[10];
snprintf(mypid, 10, "%d", i);
char* args[] = {"./child", mypid, NULL};
execv("./child", args);
sleep(1);
That each child read in argv[1]
int mypid = atoi(argv[1]);
Please, see also this post: C Named pipe (fifo). Parent process gets stuck
Solved by putting my read statements into the loop waiting for the child processes to finish:
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
if (fcntl(fd, F_GETFD) == -1) {
perror("fd failed");
exit(1);
}
//Wait for child processes to finish
int j = 0;
for (j; j < num_processes; j++) {
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
wait(NULL);
}
//Close
close(fd);
return 0;

Resources