i've written a small client-server demo program that should implement a simple comunication between two process using a FIFO and two semaphores.
The problem is that even if i've put a sem_wait before the client reads the fifo, he dosen't wait for the server to write on the fifo so the client reads the old value ("2" instead of "pippo"). I really can't find where the mistake is. Following the code of Server and Client.
I hope someone could help me.
Client:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
#define FIFO_FILE "MIA_FIFO"
FILE * fiforw ;
int fifo_write ( char * buf );
int fifo_read ( char * buf );
sem_t *pizz;
sem_t *cli;
int main(void){
fiforw=fopen ( FIFO_FILE, "rw");
pizz=sem_open("pizz", O_EXCL, S_IRUSR | S_IWUSR, 0);
cli=sem_open("cli", O_EXCL, S_IRUSR | S_IWUSR, 0);
char buff[20];
int pieces=10;;
sprintf(buff,"%d",pieces);
fifo_write(buff);
sem_post(cli);
sem_wait(pizz);
fifo_read(buff);
printf("I've read %s \n",buff); //here he should read "pippo" instead of "2"
fclose ( fiforw );
}
int fifo_write ( char * buf ) {
fputs ( buf, fiforw );
return 1;
}
int fifo_read ( char * buf ) {
fgets( buf, sizeof(buf)+1, fiforw);
return 1;
}
Server:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <stdarg.h>
#include <fcntl.h>
#define FIFO_FILE "MIA_FIFO"
int fifo_write ( char * buf );
int fifo_read ( char * buf );
sem_t *pizz;
sem_t *cli;
FILE * fiforw;
int main(void){
mkfifo(FIFO_FILE,0666);
fiforw = fopen ( FIFO_FILE, "rw");
char buff[20];
char temp[]="pippo";
pizz = sem_open("pizz", O_CREAT, S_IRUSR | S_IWUSR, 0);
cli= sem_open("cli", O_CREAT, S_IRUSR | S_IWUSR, 0);
//while(1) {
sem_wait(cli);
fifo_read(buff);
printf("the value is %s \n",buff);
sprintf(buff,"%s",temp);
printf("i will write pippo on the fifo \n");
fifo_write(buff);
sem_post(pizz);
fclose ( fiforw );
//}
}
int fifo_write ( char * buf ) {
fputs ( buf, fiforw );
return 1;
}
int fifo_read ( char * buf ) {
fgets( buf, sizeof(buf)+1, fiforw);
return 1;
}
Running server first then client is required. If that is the case, it can be helpful to check return codes from system calls.
Related
I am trying to create client server communication through named pipe. From my client I want to send current time. I am trying to use time() function but the time won't appear on my server side terminal. I just see provided text. What am I doing wrong?
server
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int main() {
char *pathname = "/tmp/myfifo";
int make_fifo = mkfifo(pathname, 0666);
char str[80];
for(;;){
int opn = open(pathname, O_RDONLY);
read(opn, str, sizeof(str));
close(opn);
}
return 0;
}
client
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
int main() {
char *pathname = "/tmp/myfifo";
time_t current_time = time(0);
char str[80];
int fd = open(pathname, O_WRONLY);
fgets(str, 80, stdin);
write(fd, str, sizeof(str));
write(fd, (void*) current_time, sizeof(current_time));
close(fd);
return 0;
}
What am I doing wrong?
client: Not passing the address of current_time.*1
// write(fd, (void*) current_time, sizeof(current_time));
write(fd, ¤t_time, sizeof(current_time)); // Add &
server: not reading the time.
Ignoring return values of read()/write().
*1 Avoid casting like (void*) current_time. Casting tends to hide errors.
I am trying to create a simple fifo client/server. Whenever I compile the program and try to type some data in client, in server I am getting weird outputs such as:
How can I fix this?
server:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
mkfifo("/tmp/my_fifo", 0666);
for(;;){
fd = open("/tmp/my_fifo", O_RDONLY);
int d;
char buf[64];
read(fd, buf, sizeof(buf));
sscanf(buf, "%d", &d);
close(fd);
}
return 0;
}
client:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
int fd;
fd = open("/tmp/my_fifo", O_WRONLY);
int d;
scanf("%d", &d);
char buf[32];
sprintf(buf, "%d", d);
write(fd, buf,strlen(buf));
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define BUFF_SIZE 1024
typedef struct {
long data_type;
int data_num;
char data_buff[BUFF_SIZE];
} t_data;
int main(){
key_t msqid;
int ndx = 0;
t_data data;
msqid = msgget( (key_t)1234, IPC_CREAT | 0666);
if ( -1 == msqid)
{
perror( "msgget() fail");
exit( 1);
}
return 0;
}
and next, i do
gcc -o parent parent.c
and next,
./parent
but the result is
msgget() fail: Function not implemented
I don't know why the msgget function is not implemented even though this is just simple code.
How can i fix my code??
I'm trying to write and read from a named fifo with some methods... Apparently when I run it gets stuck on write_fifo(0) for no reason... So my question is am I using named fifo in the right way? Or does the unlink method mess with my program? When and where can I unlink a named fifo.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
void write_fifo(int s)
{
printf("write_fifo");
int writing, n;
writing = open("myFIFO",O_WRONLY);
n = write(writing, &s, sizeof(s));
printf("write: %i byte.\n", n);
close(writing);
unlink("myFIFO");
}
int read_fifo()
{
printf("read_fifo");
int reading, n, s;
reading = open("myFIFO", O_RDONLY);
n = read(reading , &s, sizeof(s));
printf("read: %i byte: %d\n", n, s);
close(reading);
unlink("myFIFO");
return s;
}
int main()
{
printf("beginning");
printf("removing myFIFO");
if (mkfifo("myFIFO", FILE_MODE) == -1)
{
perror("myFIFO");
exit(1);
}
write_fifo(0);
printf("\n\n\n\n\n\n\nReading %d",read_fifo());
return 0;
}
I am trying to send a string to another program
but i am having problem using O_WRONLY | O_NONBLOCK,
if i replace that with O_RDWR the program works fine
but i wanted to know if there is a way to send/read the
string without using O_RDWR. Right now it returns a
empty string for some reason.
Writer:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_LINE 1024
int main(int argc, char **argv)
{
char line[MAX_LINE];
int pipe;
printf("Enter line: \n");
fgets(line, MAX_LINE, stdin);
pipe = open("link1", O_WRONLY | O_NONBLOCK);
write(pipe, line, strlen(line));
system("./run"); //executing the reader
close(pipe);
return 0;
}
reader:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_BUF 1024
int main(int argc, char **argv)
{
int fd;
char * link1 = "link1";
char buf[MAX_BUF];
fd = open(link1, O_RDONLY | O_NONBLOCK);
read(fd, buf, MAX_BUF);
printf("%s\n", buf);
close(fd);
return 0;
}
Are you running the reader first? If no process has the FIFO open for reading when the writer attempts to open it write only, then the open will fail.
From the Open Group man page:
When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is set:
An open() for reading only will return without delay. An open() for writing only will return an error if no process currently has the file open for reading.